Minky2k9
Newbie level 1
lm35 temperature sensor
Hello dear edaboard members !
im stuck with some output issues off this C-code, tryin to build a rather simple temp-sensor and want it to have an output on the serial interface in plain asciicode... but i cant figure out whats wrong, the code is origin from this site:
**broken link removed**
i´ve edited it abit, made a few changes... (i´ve wired the electronics like the schematics on the picture on the site)
my code is posted below:
This is 3 "typical" outputs in Termhex (9600 baud transfer rate, no parity and 1 stop bit):
xü†üfxéfx
xü†üfxéfx
xü†üx€€éf
would love if someone actually could see whats wrong cause im really stuck atm :S
// Minky
Hello dear edaboard members !
im stuck with some output issues off this C-code, tryin to build a rather simple temp-sensor and want it to have an output on the serial interface in plain asciicode... but i cant figure out whats wrong, the code is origin from this site:
**broken link removed**
i´ve edited it abit, made a few changes... (i´ve wired the electronics like the schematics on the picture on the site)
my code is posted below:
Code:
//program for temperature monitoring system written for PIC16F877A //
//author: keshava murali //
//version1 //
//bellow 30ºC LED switches OFF //above 40ºC LED switches ON //
//between 30ºC and 40ºC LED toggles //
//timer delay~0.5sec //
//9/11/07 //
// Compiler: MikroC for PIC - V. 5.0.0.3 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define LED PORTB.F1 //LED port pin
#define max_count 15
#define low_temp 25 //define lower temperature range
#define high_temp 31 //define higher temperature range
//Function declarations
void send_data(unsigned char *p,unsigned char no_of_bytes);
unsigned char hex_to_ascii(unsigned char number);
//global variable declarations
unsigned short int timer_counter=0;
unsigned char mes_temp[]={"temp:"},mes_sensor[]={"sdata: "};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//timer 0 ISR; increments timer_counter ; reinitializes TMR0 //
//input: none output: none affected: timer_counter, TMR0,INTCON //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void interrupt() //timer0 interrupt
{
timer_counter++; //increment counter
TMR0 = 96; //reinitialize TMR0 register
INTCON.TMR0IF=0; //clear timer0 overflow interrupt flag bit
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////main program starts here; reads ADC channel 0; calibrates the sensor data; //
////sends to serial port //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
unsigned char sensor_data,sensor_data1,sensor_data2,high_temp_flag,low_temp_flag,med_temp_flag,i;
TRISA=0xFF; //configure PORTA as input
TRISB=0; //configure PORTB as output
PORTA=0; //clear PORTA
PORTB=0; //clear PORTB
Usart_Init(9600); //initialize serial port using built in function
//if built in function is not used then use bellow commented //codes to initialize serial port
//SPBRG=103; //for 9600 baud rate (with 4MHz)
//TXSTA=0x22; //8 bit transmission, asynchronous, low baud rate, no parity
//RCSTA=0x90; //8 bit receive, enable serial port, no parity
//PIE1.RCIE=1; //enable receive interrupt
//PIE1.TXIE=1; //enable transmission interrupt
//Configure A/D
ADCON0=0x80; //Fosc/32, AN0, A/D disabled
ADCON1=0x80; //right justified, Fosc/32, analog inputs
ADCON0.ADON=1; //enable ADC module
OPTION_REG=0b00000111; //disable portb pullup, -ve edge, internal clock, increment
//on +ve edge, prescalar-0:256
INTCON.TMR0IE=1; //enable timer0 interrupt
INTCON.PEIE=1; //enable peripheral interrupts
INTCON.GIE=1; //enable global interrupt
for (;;) //infinite loop
{
INTCON.TMR0IE=1; //enable timer 0 interrupt
while (timer_counter!=max_count) //timer0 delay; wait till delay elapsed
{
asm nop;
}
timer_counter=0; //delay over; reinitialize counter
//Delay_ms(450); //without timer delay we can use built in delay function
sensor_data=Adc_Read(0); // read A/D using built in function
// if you don’t want to use built in function then use //bellow commented code
//ADCON0.GO=1; //start A/D conversion
//while(ADCON0.GO){} //wait till A/D conversion is over (1.6µS )
//sensor_data=ADRESL; //get ADC data; lower byte is sufficient
for (i=0; i<=5; i++) //send message to serial port
{
Usart_Write(mes_sensor[i]);
}
//send_data(&mes_sensor,sizeof(mes_temp)); //no built in function-use this function
sensor_data1=sensor_data/2; //calibrate sensor data
sensor_data2=sensor_data1; //temporarily store calibrated temperature
sensor_data1=hex_to_ascii((sensor_data/10)); //do hex to ascii conversion of MSB digit
Usart_Write(sensor_data1); //send to serial port
//don’t want built in function-use bellow two codes
//while(!TXIF){} //if transmit buffer is empty put data else wait
//TXREG=sensor_data1; //send to serial port
sensor_data1=hex_to_ascii((sensor_data%10));//hex to ascii conversion of LSB digit
Usart_Write(sensor_data1); // send to serial port
//while(!TXIF){} //if transmit buffer is empty put data else wait
//TXREG=sensor_data1; //send to serial port
if (sensor_data2>high_temp) //check for temperature range
{
high_temp_flag=0xFF; //if temperature > 40ºC set high_temp_flag
low_temp_flag=0x00;
} //clear low_temp_flag
else if (sensor_data2<high_temp && sensor_data2>low_temp)
{
low_temp_flag=0x00; //if temperature <>
high_temp_flag=0x00;
} //clear high_temp_flag
else
{
low_temp_flag=0xFF;
high_temp_flag=0x00;
} //to avoid false triggering of LED clear both temp flags
if (high_temp_flag==0xFF) //check flag conditions;
LED=1; //if high_temp_flag is set switch ON LED
//PORTB.F1=1;
else if (low_temp_flag==0xFF) //if low_temp_flag is set switch OFF LED
LED=0;
//PORTB.F1=0;
else //if temperature is in between 30ºC and 40ºC blink LED
LED=~LED; //toggle LED
//PORTB.F1= ~PORTB.F1;
for (i=0; i<=4; i++) //send temperature to serial port
{
Usart_Write(mes_temp[i]); //send message to serial port
}
//send_data(&mes_temp,sizeof(mes_temp)); //don’t want built in?-use this function
sensor_data1=hex_to_ascii((sensor_data2/10)); //hex to ASCII convesion of MSB digit
Usart_Write(sensor_data1); //send to serial port;
//while(!TXIF){} //if transmit buffer is empty put data else wait
//TXREG=sensor_data1; //send to serial port
sensor_data1=hex_to_ascii((sensor_data2%10));//hex to ASCII convesion of LSB digit
Usart_Write(sensor_data1); //send to serial port
//while(!TXIF){} //if transmit buffer is empty put data else wait
//TXREG=sensor_data1; //send to serial port
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//function to convert hex number to ASCII format //
//input: unsigned char hex number return: unsigned char ASCII equivalent //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char hex_to_ascii(unsigned char number)
{
if (number<10) //if hex number is 0 to 9 add 0x30
number=number+0x30;
else //if hex number is A to F add 0x37
number=number+0x37;
return(number); //return ASCII equivalent
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//function to send a character to serial port //
//use this function if USART built in function is not used //
//input: starting address of the string, length of the string return: none //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void send_data(unsigned char *p,unsigned char no_of_bytes)
{
unsigned char i;
for (i=0; i<=7; i++)
{
while (!TXIF) {} //if transmission buffer is full wait
TXREG=*(p+i); //send character to serial port
}
}
This is 3 "typical" outputs in Termhex (9600 baud transfer rate, no parity and 1 stop bit):
xü†üfxéfx
xü†üfxéfx
xü†üx€€éf
would love if someone actually could see whats wrong cause im really stuck atm :S
// Minky