Sunteți pe pagina 1din 6

Temperature controller using PIC16F877A microcontroller Software is written in C language and compiled using HI-TECH ANSI C Compiler ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//program for temperature monitoring system written for PIC16F877A // //below 36.5C LED switches OFF //above 37.5C LED switches ON // //between 36.5C and 37.5 C LED Toggles ON/OFF// //timer delay~0.5sec // //10/30/09// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #define LED PORTB.F1 #define max_count 15 #define low_temp 30 #define high_temp 40 //define lower temperature range //define higher temperature range //LED port pin

//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++; TMR0 = 96; //increment counter //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; TRISB=0; PORTA=0; PORTB=0; Usart_Init(9600); //configure PORTA as input //configure PORTB as output //clear PORTA //clear PORTB //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; //TXSTA=0x22; //RCSTA=0x90; //PIE1.RCIE=1; //PIE1.TXIE=1; //Configure A/D ADCON0=0x80; ADCON1=0x80; ADCON0.ADON=1; //Fosc/32, AN0, A/D disabled //right justified, Fosc/32, analog inputs //enable ADC module //for 9600 baud rate (with 4MHz) //8 bit transmission, asynchronous, low baud rate, no parity //8 bit receive, enable serial port, no parity //enable receive interrupt //enable transmission interrupt

OPTION_REG=0b00000111; //disable portb pullup, -ve edge, internal clock, increment //on +ve edge, prescalar-0:256 INTCON.TMR0IE=1; INTCON.PEIE=1; //enable timer0 interrupt //enable peripheral interrupts

INTCON.GIE=1;

//enable global interrupt

for(;;) { INTCON.TMR0IE=1; //enable timer 0 interrupt

while(timer_counter!=max_count) //timer0 delay; wait till delay elapsed {asm nop;} timer_counter=0; //Delay_ms(450); sensor_data=Adc_Read(0); //delay over; reinitialize counter //without timer delay we can use built in delay function // read A/D using built in function

//ADCON0.GO=1; //start A/D conversion //while(ADCON0.GO){} //sensor_data=ADRESL; for(i=0;i<=5;i++) {Usart_Write(mes_sensor[i]);} //send_data(&mes_sensor,sizeof(mes_temp)); //no built in function-use this function sensor_data1=sensor_data/2; sensor_data2=sensor_data1; //calibrate sensor data //temporarily store calibrated temperature //wait till A/D conversion is over (1.6S ) //get ADC data; lower byte is sufficient //send message to serial port

sensor_data1=hex_to_ascii((sensor_data/10)); //do hex to ascii conversion of MSB digit Usart_Write(sensor_data1); //send to serial port

//dont want built in function-use bellow two codes //while(!TXIF){} //TXREG=sensor_data1; //if transmit buffer is empty put data else wait //send to serial port

sensor_data1=hex_to_ascii((sensor_data%10));//hex to ascii conversion of LSB digit Usart_Write(sensor_data1); //while(!TXIF){} //TXREG=sensor_data1; // send to serial port //if transmit buffer is empty put data else wait //send to serial port

if(sensor_data2>high_temp) {high_temp_flag=0xFF; low_temp_flag=0;} else if(sensor_data2 {low_temp_flag=0xFF;

//check for temperature range //if temperature > 37.5C set high_temp_flag //clear low_temp_flag

//if temperature

high_temp_flag=0;} //clear high_temp_flag else {low_temp_flag=0; high_temp_flag=0;} //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 36.5C and 37.5C 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)); //dont 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 //

unsigned char hex_to_ascii(unsigned char number) { //if hex number is 0 to 9 add 0x30 if(number<10) 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 { while(!TXIF) {} //if transmission buffer is full wait TXREG=*(p+i); //send character to serial port } } */

/////////////////////////////////////////////////////////////////

S-ar putea să vă placă și