15.11.2012 Views

Temperature controller using PIC16F877A microcontroller Software is

Temperature controller using PIC16F877A microcontroller Software is

Temperature controller using PIC16F877A microcontroller Software is

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Temperature</strong> <strong>controller</strong> <strong>using</strong> <strong>PIC16F877A</strong> micro<strong>controller</strong><br />

<strong>Software</strong> <strong>is</strong> written in C language and compiled <strong>using</strong> HI-TECH ANSI C Compiler<br />

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br />

//program for temperature monitoring system written for <strong>PIC16F877A</strong> //<br />

//below 36.5ºC LED switches OFF //above 37.5ºC LED switches ON //<br />

//between 36.5C and 37.5 C LED Toggles ON/OFF//<br />

//timer delay~0.5sec //<br />

//10/30/09//<br />

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br />

#define LED PORTB.F1 //LED port pin<br />

#define max_count 15<br />

#define low_temp 30 //define lower temperature range<br />

#define high_temp 40 //define higher temperature range<br />

//Function declarations<br />

void send_data(unsigned char *p,unsigned char no_of_bytes);<br />

unsigned char hex_to_ascii(unsigned char number);<br />

//global variable declarations<br />

unsigned short int timer_counter=0;<br />

unsigned char mes_temp[]={"temp:"},mes_sensor[]={"sdata: "};<br />

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br />

//timer 0 ISR; increments timer_counter ; reinitializes TMR0 //<br />

//input: none output: none affected: timer_counter, TMR0,INTCON //<br />

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br />

void interrupt() //timer0 interrupt<br />

{<br />

timer_counter++; //increment counter<br />

TMR0 = 96; //reinitialize TMR0 reg<strong>is</strong>ter


INTCON.TMR0IF=0; //clear timer0 overflow interrupt flag bit<br />

}<br />

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br />

////main program starts here; reads ADC channel 0; calibrates the sensor data; //<br />

////sends to serial port //<br />

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br />

void main(void)<br />

{<br />

unsigned char sensor_data,sensor_data1,sensor_data2,high_temp_flag,low_temp_flag,med_temp_flag,i;<br />

TRISA=0xFF; //configure PORTA as input<br />

TRISB=0; //configure PORTB as output<br />

PORTA=0; //clear PORTA<br />

PORTB=0; //clear PORTB<br />

Usart_Init(9600); //initialize serial port <strong>using</strong> built in function<br />

//if built in function <strong>is</strong> not used then use bellow commented //codes to initialize serial port<br />

//SPBRG=103; //for 9600 baud rate (with 4MHz)<br />

//TXSTA=0x22; //8 bit transm<strong>is</strong>sion, asynchronous, low baud rate, no parity<br />

//RCSTA=0x90; //8 bit receive, enable serial port, no parity<br />

//PIE1.RCIE=1; //enable receive interrupt<br />

//PIE1.TXIE=1; //enable transm<strong>is</strong>sion interrupt<br />

//Configure A/D<br />

ADCON0=0x80; //Fosc/32, AN0, A/D d<strong>is</strong>abled<br />

ADCON1=0x80; //right justified, Fosc/32, analog inputs<br />

ADCON0.ADON=1; //enable ADC module<br />

OPTION_REG=0b00000111; //d<strong>is</strong>able portb pullup, -ve edge, internal clock, increment<br />

INTCON.TMR0IE=1; //enable timer0 interrupt<br />

INTCON.PEIE=1; //enable peripheral interrupts<br />

//on +ve edge, prescalar-0:256


INTCON.GIE=1; //enable global interrupt<br />

for(;;)<br />

{<br />

INTCON.TMR0IE=1; //enable timer 0 interrupt<br />

while(timer_counter!=max_count) //timer0 delay; wait till delay elapsed<br />

{asm nop;}<br />

timer_counter=0; //delay over; reinitialize counter<br />

//Delay_ms(450); //without timer delay we can use built in delay function<br />

sensor_data=Adc_Read(0); // read A/D <strong>using</strong> built in function<br />

//ADCON0.GO=1; //start A/D conversion<br />

//while(ADCON0.GO){} //wait till A/D conversion <strong>is</strong> over (1.6µS )<br />

//sensor_data=ADRESL; //get ADC data; lower byte <strong>is</strong> sufficient<br />

for(i=0;i


if(sensor_data2>high_temp) //check for temperature range<br />

{high_temp_flag=0xFF; //if temperature > 37.5ºC set high_temp_flag<br />

low_temp_flag=0;} //clear low_temp_flag<br />

else if(sensor_data2<br />

{low_temp_flag=0xFF; //if temperature<br />

high_temp_flag=0;} //clear high_temp_flag<br />

else<br />

{low_temp_flag=0;<br />

high_temp_flag=0;} //to avoid false triggering of LED clear both temp flags<br />

if(high_temp_flag==0xFF) //check flag conditions;<br />

LED=1; //if high_temp_flag <strong>is</strong> set switch ON LED<br />

//PORTB.F1=1;<br />

else if(low_temp_flag==0xFF) //if low_temp_flag <strong>is</strong> set switch OFF LED<br />

LED=0;<br />

//PORTB.F1=0;<br />

else //if temperature <strong>is</strong> in between 36.5ºC and 37.5ºC blink LED<br />

LED=~LED; //toggle LED<br />

//PORTB.F1= ~PORTB.F1;<br />

for(i=0;i


TXREG=sensor_data1; //send to serial port<br />

}<br />

}<br />

/////////////////////////////////////////////////////////////<br />

//function to convert hex number to ASCII format //<br />

unsigned char hex_to_ascii(unsigned char number)<br />

{ //if hex number <strong>is</strong> 0 to 9 add 0x30<br />

if(number

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!