kapalterbang
Newbie level 3
I Need Your Ideas...
hye and good day...
currently i am doing my project which is detecting the surrounding temperature and then send it to the receiver...
the main components that i used is PIC16F877A LM35 temperature sensor and two XBee RF module...
I have already done my circuitry and i have test it before...so now i am working on the programming part...here is my code...
unsigned int cntr ;
long temp ; // Temperature in Celcius * 10
/*
* offset reference of the sensor : 0°C is 500 mV => 102.4
* since the sensor is factory calibrated, there is no need for adjustment
*/
int ref = 1024 ; // offset is multiplied by 10 to get tenth of degree
/*
* interrupt routine, called on each timer0 overflow
*/
void interrupt(void)
{
if(INTCON.T0IF) // timer 0 overflow ?
{
cntr++ ; // increment counter
INTCON.T0IF = 0 ; // done
}
}
/*
* program entry
*/
void main()
{
USART_Init(9600);
ADCON1 = 0x00 ; // set PORTA as analog input
TRISA = 0xff ; // set PORTA as inputs
TRISD = 0 ; // PORTD is output
OPTION_REG = 0x80 ; // start timer 0, no prescaler
INTCON = 0xA0 ; // allow timer 0 overflow interrupt
for(;; ) // forever
{
if(cntr >= 4000) // if enough time since last sample
{
/*
* read the sensor
*/
temp = Adc_Read(7) * 10 - ref ; // read RE2 ADC, adjust to 0°C
/*
* get the result in celcius * 10
* sensor temperature coefficient is +10mV/°C
* ADC resolution is 5000/1024 = 4.88 mV so one ADC point is 0.488°C
*/
temp *= 488 ;
temp /= 1000 ;
cntr = 0 ; // clear counter
USART_Write(temp);
}
}
}
so my question is do you think that the code is correct?
p/s: my circuit is working at 20MHz crystal
Thanx....
hye and good day...
currently i am doing my project which is detecting the surrounding temperature and then send it to the receiver...
the main components that i used is PIC16F877A LM35 temperature sensor and two XBee RF module...
I have already done my circuitry and i have test it before...so now i am working on the programming part...here is my code...
unsigned int cntr ;
long temp ; // Temperature in Celcius * 10
/*
* offset reference of the sensor : 0°C is 500 mV => 102.4
* since the sensor is factory calibrated, there is no need for adjustment
*/
int ref = 1024 ; // offset is multiplied by 10 to get tenth of degree
/*
* interrupt routine, called on each timer0 overflow
*/
void interrupt(void)
{
if(INTCON.T0IF) // timer 0 overflow ?
{
cntr++ ; // increment counter
INTCON.T0IF = 0 ; // done
}
}
/*
* program entry
*/
void main()
{
USART_Init(9600);
ADCON1 = 0x00 ; // set PORTA as analog input
TRISA = 0xff ; // set PORTA as inputs
TRISD = 0 ; // PORTD is output
OPTION_REG = 0x80 ; // start timer 0, no prescaler
INTCON = 0xA0 ; // allow timer 0 overflow interrupt
for(;; ) // forever
{
if(cntr >= 4000) // if enough time since last sample
{
/*
* read the sensor
*/
temp = Adc_Read(7) * 10 - ref ; // read RE2 ADC, adjust to 0°C
/*
* get the result in celcius * 10
* sensor temperature coefficient is +10mV/°C
* ADC resolution is 5000/1024 = 4.88 mV so one ADC point is 0.488°C
*/
temp *= 488 ;
temp /= 1000 ;
cntr = 0 ; // clear counter
USART_Write(temp);
}
}
}
so my question is do you think that the code is correct?
p/s: my circuit is working at 20MHz crystal
Thanx....