baileychic
Advanced Member level 3
Regarding post #60 do I need an external Crystal at T1OSO/OSI pins so that I can make PIC wakeup from sleep using Timer1 ? By enabling T1OSCEN_bit ?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
That's how SPI bus works. Review the interface description in datasheet, SO has internal tri-state driver enabled by CS.Are you telling that I can connect SCK and SO lines of the TC amplifiers to 2 pins of PIC and just select the 74HC138 to select only the CS lines of 8 TC amplifiers?
And I need 8x pullup resistors on the 8x CSx lines when 74HC138 is used ?
No problem. It´s just useless.Any problem if I put pullup resistors on 8x CSx lines when 74HC138 is used ?
sbit On_Off_1 at LATC0_bit;
sbit On_Off_1_Direction at TRISC0_bit;
sbit On_Off_2 at LATC1_bit;
sbit On_Off_2_Direction at TRISC1_bit;
sbit Fan at LATC2_bit;
sbit Fan_Direction at TRISC2_bit;
sbit SCLK at LATE0_bit;
sbit SCLK_Direction at TRISE0_bit;
sbit SO at RE1_bit;
sbit SO_Direction at TRISE1_bit;
sbit MUX_A at LATD0_bit;
sbit MUX_A_Direction at TRISD0_bit;
sbit MUX_B at LATD1_bit;
sbit MUX_B_Direction at TRISD1_bit;
sbit MUX_C at LATD2_bit;
sbit MUX_C_Direction at TRISD2_bit;
sbit MUX_E1 at LATD3_bit;
sbit MUX_E1_Direction at TRISD3_bit;
#define Peltier_Voltage 0
#define Battery_Voltage 1
#define Device_Temperature_Sensor 2
#define Dummy_Adc_Value 1024
#define g_isDeviceConfigured_Flag_Address 0x10
#define True 1
#define False 0
#define On 1
#define Off 0
#define Low 0
#define High 1
#define Input 1
#define Output 0
#define Max_Device_Temperature 150.0
#define Adc_Resolution 1023
#define Regulator_Circuit_Diode_Drop 0.6
#define Minimum_Peltier_Voltage 10.0
#define Minimum_Battery_Voltage 6.6
#define Minimum_Input_Voltage 8.4
#define Peltier_Voltage_Hysterisis 0.5
#define Battery_Voltage_Hysterisis 0.5
typedef struct {
double g_maxPeltierVoltage;
double g_maxBatteryVoltage;
}DEVICE_CONFIGURATION_TYPE;
typedef struct {
unsigned int g_rawAdcValue[3];
unsigned int g_oldRawAdcValue[3];
double g_peltierVoltage;
double g_regulatorInputVoltage;
double g_batteryVoltage;
double g_deviceTemperature;
}ADC_TYPE;
typedef struct {
char g_uartRead;
char g_uartBuffer[100];
}UART1_TYPE;
typedef struct {
char g_uartRead;
char g_uartBuffer[100];
}UART2_TYPE;
typedef struct {
DEVICE_CONFIGURATION_TYPE deviceConfig;
ADC_TYPE adc;
UART1_TYPE uart1;
UART2_TYPE uart2;
char i;
char msg[23];
unsigned long temp[8];
double temperature[8];
}TMS_TYPE;
TMS_TYPE tms;
unsigned char flagRegister1 = 0;
unsigned char flagRegister2 = 0;
sbit g_isDeviceConfigured at flagRegister1.B0;
sbit g_deviceSleeping at flagRegister1.B1;
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
//Place/Copy this part in declaration section
void InitTimer1() {
T1CON = 0x31;
TMR1IF_bit = 0;
TMR1H = 0x0B;
TMR1L = 0xDC;
TMR1IE_bit = 1;
}
void interrupt() {
if((RC1IE_bit) && (RC1IF_bit)) {
if(g_deviceSleeping == True) {
tms.uart1.g_uartRead = UART1_Read();
g_deviceSleeping = False;
}
else {
}
}
if((RC2IE_bit) && (RC2IF_bit)) {
if(g_deviceSleeping == True) {
tms.uart2.g_uartRead = UART2_Read();
g_deviceSleeping = False;
}
else {
}
}
if((TMR1IE_bit) && (TMR1IF_bit)) {
TMR1IF_bit = 0;
TMR1H = 0x0B;
TMR1L = 0xDC;
if(g_deviceSleeping == True) {
g_deviceSleeping = False;
}
else {
}
}
if((INT0IE_bit) && (INT0IF_bit)) {
INT0IF_bit = 0;
if(g_deviceSleeping == True) {
g_deviceSleeping = False;
}
else {
}
}
}
void initializePorts() {
On_Off_1_Direction = Output;
On_Off_2_Direction = Output;
Fan_Direction = Output;
SCLK_Direction = Output;
SO_Direction = Input;
MUX_A_Direction = Output;
MUX_B_Direction = Output;
MUX_C_Direction = Output;
MUX_E1_Direction = Output;
On_Off_1 = Off;
On_Off_2 = Off;
Fan = On;
SCLK = Low;
MUX_A = Low;
MUX_B = Low;
MUX_C = Low;
MUX_E1 = Low;
}
void initializeVariables() {
tms.adc.g_rawAdcValue[0] = 0;
tms.adc.g_oldRawAdcValue[0] = Dummy_Adc_Value;
tms.adc.g_rawAdcValue[1] = 0;
tms.adc.g_oldRawAdcValue[1] = Dummy_Adc_Value;
tms.adc.g_rawAdcValue[2] = 0;
tms.adc.g_oldRawAdcValue[2] = Dummy_Adc_Value;
tms.adc.g_peltierVoltage = 0.0;
tms.adc.g_batteryVoltage = 0.0;
tms.adc.g_deviceTemperature = 0.0;
}
double readMax31855(char channel) {
char i = 0;
unsigned long result = 0;
double centigrade = 0.0;
MUX_E1 = Low;
if((channel >= 1) && (channel <= 8)) {
channel = channel - 1;
MUX_A = channel.B0;
MUX_B = channel.B1;
MUX_C = channel.B2;
MUX_E1 = High;
Delay_us(50);
}
for(i = 0; i < 32; i++) {
SCLK = High;
Delay_us(20);
if(SO)result = result | 1;
SCLK = Low;
Delay_us(20);
result <<= 1;
}
MUX_E1 = Low;
if(result & 0x80000000) {
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
result = 0xFFFFC000 | ((result >> 18) & 0x00003FFFF);
}
else {
// Positive value, just drop the lower 18 bits.
result >>= 18;
}
centigrade = (double)result;
return centigrade;
}
void main() {
asm clrwdt
OSCCON = 0x57;
OSCCON2 = 0x83;
OSCTUNE = 0x00;
CM1CON0 = 0x00;
CM2CON0 = 0x00;
SLRCON = 0x00;
ANSELA = 0x07;
ANSELB = 0x00;
ANSELC = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
TRISA = 0x07;
TRISB = 0x01;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
UART1_Init(9600);
UART2_Init(9600);
Delay_ms(200);
//g_isDeviceConfigured = EEPROM_Read(g_isDeviceConfigured_Flag_Address);
Delay_ms(20);
initializePorts();
g_isDeviceConfigured = True;
if(g_isDeviceConfigured == True) {
INTEDG0_bit = 0;
INT0IF_bit = 0;
INT0IE_bit = 1;
InitTimer1();
INTCON |= 0xC0;
}
while(1) {
asm clrwdt
if(g_isDeviceConfigured == True) {
tms.adc.g_rawAdcValue[0] = (unsigned int)ADC_Read(Peltier_Voltage);
Delay_us(20);
tms.adc.g_rawAdcValue[1] = (unsigned int)ADC_Read(Battery_Voltage);
Delay_us(20);
tms.adc.g_rawAdcValue[2] = (unsigned int)ADC_Read(Device_Temperature_Sensor);
Delay_us(20);
if(tms.adc.g_rawAdcValue[0] != tms.adc.g_oldRawAdcValue[0]) {
tms.adc.g_peltierVoltage = (double)tms.adc.g_rawAdcValue[0] * tms.deviceConfig.g_maxPeltierVoltage / Adc_Resolution;
tms.adc.g_regulatorInputVoltage = tms.adc.g_peltierVoltage - Regulator_Circuit_Diode_Drop;
tms.adc.g_rawAdcValue[0] = tms.adc.g_oldRawAdcValue[0];
}
if(tms.adc.g_rawAdcValue[1] != tms.adc.g_oldRawAdcValue[1]) {
tms.adc.g_batteryVoltage = (double)tms.adc.g_rawAdcValue[1] * tms.deviceConfig.g_maxBatteryVoltage / Adc_Resolution;
tms.adc.g_rawAdcValue[1] = tms.adc.g_oldRawAdcValue[1];
}
if(tms.adc.g_rawAdcValue[2] != tms.adc.g_oldRawAdcValue[2]) {
tms.adc.g_peltierVoltage = (double)tms.adc.g_rawAdcValue[2] * Max_Device_Temperature / Adc_Resolution;
tms.adc.g_rawAdcValue[2] = tms.adc.g_oldRawAdcValue[2];
}
if(tms.adc.g_peltierVoltage <= Minimum_Peltier_Voltage) {
}
for(tms.i = 0; tms.i < 8; tms.i++) {
sprintf(tms.msg,"TC%u = %5.2f\r\n",tms.i, readMax31855(tms.i + 1));
UART2_Write_Text(tms.msg);
}
}
else {
}
if(g_deviceSleeping == 0) {
//BAUDCON1.WUE = 1;
//BAUDCON2.WUE = 1;
g_deviceSleeping = 1;
//asm sleep
}
}
}
sbit On_Off_1 at LATC0_bit;
sbit On_Off_1_Direction at TRISC0_bit;
sbit On_Off_2 at LATC1_bit;
sbit On_Off_2_Direction at TRISC1_bit;
sbit Fan at LATC2_bit;
sbit Fan_Direction at TRISC2_bit;
sbit SCLK at LATE0_bit;
sbit SCLK_Direction at TRISE0_bit;
sbit SO at RE1_bit;
sbit SO_Direction at TRISE1_bit;
sbit MUX_A at LATD0_bit;
sbit MUX_A_Direction at TRISD0_bit;
sbit MUX_B at LATD1_bit;
sbit MUX_B_Direction at TRISD1_bit;
sbit MUX_C at LATD2_bit;
sbit MUX_C_Direction at TRISD2_bit;
sbit MUX_E1 at LATD3_bit;
sbit MUX_E1_Direction at TRISD3_bit;
#define Peltier_Voltage 0
#define Battery_Voltage 1
#define Device_Temperature_Sensor 2
#define Dummy_Adc_Value 1024
#define g_isDeviceConfigured_Flag_Address 0x10
#define True 1
#define False 0
#define On 1
#define Off 0
#define Low 0
#define High 1
#define Input 1
#define Output 0
#define Max_Device_Temperature 330.0
#define Adc_Resolution 1023
#define Regulator_Circuit_Diode_Drop 0.6
#define Minimum_Peltier_Voltage 10.0
#define Minimum_Battery_Voltage 6.6
#define Minimum_Input_Voltage 10.0
#define Max_Peltier_Voltage 20.0
#define Max_Battery_Voltage 8.4
#define Peltier_Voltage_Hysterisis 0.5
#define Battery_Voltage_Hysterisis 0.5
typedef struct {
double g_maxPeltierVoltage;
double g_maxBatteryVoltage;
}DEVICE_CONFIGURATION_TYPE;
typedef struct {
unsigned int g_rawAdcValue[3];
unsigned int g_oldRawAdcValue[3];
double g_peltierVoltage;
double g_regulatorInputVoltage;
double g_batteryVoltage;
double g_deviceTemperature;
}ADC_TYPE;
typedef struct {
char g_uartRead;
char g_uartBuffer[100];
}UART1_TYPE;
typedef struct {
char g_uartRead;
char g_uartBuffer[100];
}UART2_TYPE;
typedef struct {
DEVICE_CONFIGURATION_TYPE deviceConfig;
ADC_TYPE adc;
UART1_TYPE uart1;
UART2_TYPE uart2;
char i;
char msg[23];
unsigned long temp[8];
double temperature[8];
}TMS_TYPE;
TMS_TYPE tms;
unsigned char flagRegister1 = 0;
unsigned char flagRegister2 = 0;
sbit g_isDeviceConfigured at flagRegister1.B0;
sbit g_deviceSleeping at flagRegister1.B1;
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
//Place/Copy this part in declaration section
void InitTimer1() {
T1CON = 0x31;
TMR1IF_bit = 0;
TMR1H = 0x0B;
TMR1L = 0xDC;
TMR1IE_bit = 1;
}
void interrupt() {
if((RC1IE_bit) && (RC1IF_bit)) {
if(g_deviceSleeping == True) {
tms.uart1.g_uartRead = UART1_Read();
g_deviceSleeping = False;
}
else {
}
}
if((RC2IE_bit) && (RC2IF_bit)) {
if(g_deviceSleeping == True) {
tms.uart2.g_uartRead = UART2_Read();
g_deviceSleeping = False;
}
else {
}
}
if((TMR1IE_bit) && (TMR1IF_bit)) {
TMR1IF_bit = 0;
TMR1H = 0x0B;
TMR1L = 0xDC;
if(g_deviceSleeping == True) {
g_deviceSleeping = False;
}
else {
}
}
if((INT0IE_bit) && (INT0IF_bit)) {
INT0IF_bit = 0;
if(g_deviceSleeping == True) {
g_deviceSleeping = False;
}
else {
}
}
}
void initializePorts() {
On_Off_1_Direction = Output;
On_Off_2_Direction = Output;
Fan_Direction = Output;
SCLK_Direction = Output;
SO_Direction = Input;
MUX_A_Direction = Output;
MUX_B_Direction = Output;
MUX_C_Direction = Output;
MUX_E1_Direction = Output;
On_Off_1 = Off;
On_Off_2 = Off;
Fan = On;
SCLK = Low;
MUX_A = Low;
MUX_B = Low;
MUX_C = Low;
MUX_E1 = Low;
}
void initializeVariables() {
tms.adc.g_rawAdcValue[0] = 0;
tms.adc.g_oldRawAdcValue[0] = Dummy_Adc_Value;
tms.adc.g_rawAdcValue[1] = 0;
tms.adc.g_oldRawAdcValue[1] = Dummy_Adc_Value;
tms.adc.g_rawAdcValue[2] = 0;
tms.adc.g_oldRawAdcValue[2] = Dummy_Adc_Value;
tms.adc.g_peltierVoltage = 0.0;
tms.adc.g_batteryVoltage = 0.0;
tms.adc.g_deviceTemperature = 0.0;
}
double readMax31855(char channel) {
char i = 0;
unsigned long result = 0;
double centigrade = 0.0;
MUX_E1 = Low;
if((channel >= 1) && (channel <= 8)) {
channel = channel - 1;
MUX_A = channel.B0;
MUX_B = channel.B1;
MUX_C = channel.B2;
MUX_E1 = High;
Delay_us(5);
}
SCLK = Low;
Delay_ms(1);
for(i = 0; i < 32; i++) {
SCLK = Low;
Delay_us(20);
result <<= 1;
if(SO)result |= 1;
SCLK = High;
Delay_us(20);
}
MUX_E1 = Low;
if(result & 0x80000000) {
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
result = 0xFFFFC000 | ((result >> 18) & 0x00003FFFF);
}
else {
// Positive value, just drop the lower 18 bits.
result >>= 18;
}
centigrade = (double)result;
return centigrade;
}
void main() {
asm clrwdt
OSCCON = 0x57;
OSCCON2 = 0x83;
OSCTUNE = 0x00;
CM1CON0 = 0x00;
CM2CON0 = 0x00;
SLRCON = 0x00;
ADCON1 = 0x80;
ADCON2 = 0b10110101;
ANSELA = 0x07;
ANSELB = 0x00;
ANSELC = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
TRISA = 0x07;
TRISB = 0x01;
TRISC = 0xC0;
TRISD = 0xC0;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
initializePorts();
UART1_Init(9600);
UART2_Init(9600);
Delay_ms(200);
//g_isDeviceConfigured = EEPROM_Read(g_isDeviceConfigured_Flag_Address);
Delay_ms(20);
g_isDeviceConfigured = True;
if(g_isDeviceConfigured == True) {
INTEDG0_bit = 0;
INT0IF_bit = 0;
INT0IE_bit = 1;
RC1IE_bit = 1;
RC2IE_bit = 1;
//InitTimer1();
//INTCON |= 0xC0;
}
while(1) {
asm clrwdt
if(g_isDeviceConfigured == True) {
tms.adc.g_rawAdcValue[0] = (unsigned int)ADC_Read(Peltier_Voltage);
Delay_us(20);
tms.adc.g_rawAdcValue[1] = (unsigned int)ADC_Read(Battery_Voltage);
Delay_us(20);
tms.adc.g_rawAdcValue[2] = (unsigned int)ADC_Read(Device_Temperature_Sensor);
Delay_us(20);
if(tms.adc.g_rawAdcValue[0] != tms.adc.g_oldRawAdcValue[0]) {
tms.adc.g_peltierVoltage = (double)tms.adc.g_rawAdcValue[0] * Max_Peltier_Voltage / Adc_Resolution;
tms.adc.g_regulatorInputVoltage = tms.adc.g_peltierVoltage - Regulator_Circuit_Diode_Drop;
sprintf(tms.msg,"PV = %4.2f\r\n", tms.adc.g_peltierVoltage);
UART2_Write_Text(tms.msg);
tms.adc.g_rawAdcValue[0] = tms.adc.g_oldRawAdcValue[0];
}
if(tms.adc.g_rawAdcValue[1] != tms.adc.g_oldRawAdcValue[1]) {
tms.adc.g_batteryVoltage = (double)tms.adc.g_rawAdcValue[1] * Max_Battery_Voltage / Adc_Resolution;
sprintf(tms.msg,"DT = %4.2f\r\n", tms.adc.g_batteryVoltage);
UART2_Write_Text(tms.msg);
tms.adc.g_rawAdcValue[1] = tms.adc.g_oldRawAdcValue[1];
}
if(tms.adc.g_rawAdcValue[2] != tms.adc.g_oldRawAdcValue[2]) {
tms.adc.g_deviceTemperature = (double)tms.adc.g_rawAdcValue[2] * Max_Device_Temperature / Adc_Resolution;
sprintf(tms.msg,"DT = %5.2f\r\n", tms.adc.g_deviceTemperature);
UART2_Write_Text(tms.msg);
tms.adc.g_rawAdcValue[2] = tms.adc.g_oldRawAdcValue[2];
}
if(tms.adc.g_peltierVoltage <= Minimum_Peltier_Voltage) {
}
for(tms.i = 0; tms.i < 8; tms.i++) {
sprintf(tms.msg,"TC%u = %5.2f\r\n",tms.i, readMax31855(tms.i + 1));
UART2_Write_Text(tms.msg);
}
}
else {
}
if(g_deviceSleeping == 0) {
//BAUDCON1.WUE = 1;
//BAUDCON2.WUE = 1;
g_deviceSleeping = 1;
//asm sleep
}
}
}
typedef struct {
DEVICE_CONFIGURATION_TYPE deviceConfig;
ADC_TYPE adc;
UART1_TYPE uart1;
UART2_TYPE uart2;
int i;
char msg[23];
unsigned long temp[8];
double temperature[8];
}TMS_TYPE;
double readMax31855(char channel) {
char i = 0;
signed long result = 0;
double centigrade = 0.0;
MUX_E1 = Low;
if((channel >= 1) && (channel <= 8)) {
channel = channel - 1;
MUX_A = channel.B0;
MUX_B = channel.B1;
MUX_C = channel.B2;
MUX_E1 = High;
Delay_us(10);
}
for(i = 0; i < 32; i++) {
SCLK = High;
Delay_us(10);
result <<= 1;
if(SO)result |= 1;
SCLK = Low;
Delay_us(10);
}
MUX_E1 = Low;
if(result & 0x80000000) {
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
result = 0xFFFFC000 | ((result >> 18) & 0x00003FFFF);
}
else {
// Positive value, just drop the lower 18 bits.
result >>= 18;
}
centigrade = (double)result;
return (centigrade * 0.25) - 25.0;
}
SCK = 0;
Delay_us(1); // only 100nS is actually needed.
result = 0;
for (i = 0; i < 32; i++)
{
SCK = 1;
Delay_us(1);
result << 1;
if (SO == 1) result |= 1;
else result &= 0xFFFE;
SCK = 0;
Delay_us(1);
}