Temperature controller using PIC16F877A microcontroller Software ...

12 downloads 29466 Views 73KB Size Report
Temperature controller using PIC16F877A microcontroller. Software is written in C language and compiled using HI-TECH ANSI C Compiler.
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.5ºC LED switches OFF //above 37.5ºC 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

//LED port pin

#define max_count 15 #define low_temp 30

//define lower temperature range

#define high_temp 40

//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++; 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;

//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; 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 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

//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;ihigh_temp)

//check for temperature range

{high_temp_flag=0xFF;

//if temperature > 37.5ºC set high_temp_flag

low_temp_flag=0;}

//clear low_temp_flag

else if(sensor_data2 {low_temp_flag=0xFF;

//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.5ºC and 37.5ºC blink LED LED=~LED; //toggle LED //PORTB.F1= ~PORTB.F1; for(i=0;i