1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| /*
PIC16F877A and LM35 Based Temperature Monitor
MACHI M.C
STD.no 20403715
*/
#include <16f877a.h>
#device adc=10 // Set ADC resolution to 10Bit
#fuses XT,NOLVP,NOWDT,NOPROTECT
#use delay(crystal=4000000)
#use rs232(baud=960,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
#include <lcd.c>
#define LOAD PIN_B7
#define THRES 70.0 // load switching threshold in Celsius
#define OFF 50.0 // fan stops when temparature reaches value in degrees
#define ADC_OFF 0 // ADC off
#define RELAY PIN_B4
#define R2 PIN_B2
#define R1 PIN_B1
#include <stdlib.h>
int16 digital_reading; // ADC resolution is 10Bit, an 8Bit integer is not enough to hold the reading
float temp;
void main()
{
/* ADC Initialization */
setup_adc(ADC_CLOCK_INTERNAL); // initialize ADC with a sampling rate of Crystal/4 MHz
setup_adc_ports(RA0_ANALOG); // set PIN_A0 as analog input channel
set_adc_channel(0); // point ADC to channel 0 for ADC reading
delay_ms(1); // ADC module is slow, needs some time to adjust.
disable_interrupts(GLOBAL); // DISABE ALL INTERRUPTS.
SET_TRIS_B(0X0F); //set RB0,RB1,RB2 & RB3 as inputs, RB4,RB5,RB6 & RB7 as output.
output_low(RELAY); // relay initialy off
if (input(PIN_B2)==0)
{
output_high(PIN_B4);
}
else if (input(PIN_B2)==1)
{
output_low(RELAY);
}
/* Peripherals Configurations */
lcd_init(); // Turn LCD ON, along with other initialization commands
output_low(LOAD); // the load is initially OFF
lcd_gotoxy(1,1); // point LCD cursor to col1 row1
lcd_putc("Temperature is:"); // print on LCD
{
while(1) // infinite loop
{
digital_reading = read_adc(); // capture current temperature reading
delay_us(100); // 0.1ms delay for ADC stabilization
temp = digital_reading * 0.4883; // convert reading to Celsius
lcd_gotoxy(1,2); // point LCD cursor to col1 row2
printf(lcd_putc,"%2.1f C",temp); // print value on LCD
if(temp>=THRES)
Do
{
output_high(LOAD); // Control Load
}
while (temp>=OFF);
else output_low(LOAD);
delay_ms(1000); // 1 second delay between readings
}
}
} |