Sunteți pe pagina 1din 2

1 /*

2 * Project name: Automatic Temperature Control


3 * Copyright:
4 * (c) www.studentcompanion.co.za, 2014.
5 * Test configuration:
6 * MCU: PIC18F45K22
7 * http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf
8 * Oscillator: HS-PLL, 32.00000 MHz
9 * Compiler mikroC Pro for PIC v6.5.0
10 */
11
12 // Keypad module connections
13 char keypadPort at PORTB;
14 // End Keypad module connections
15
16 // LCD module connections
17 sbit LCD_RS at LATC4_bit;
18 sbit LCD_EN at LATC5_bit;
19 sbit LCD_D4 at LATC0_bit;
20 sbit LCD_D5 at LATC1_bit;
21 sbit LCD_D6 at LATC2_bit;
22 sbit LCD_D7 at LATC3_bit;
23
24 sbit LCD_RS_Direction at TRISC4_bit;
25 sbit LCD_EN_Direction at TRISC5_bit;
26 sbit LCD_D4_Direction at TRISC0_bit;
27 sbit LCD_D5_Direction at TRISC1_bit;
28 sbit LCD_D6_Direction at TRISC2_bit;
29 sbit LCD_D7_Direction at TRISC3_bit;
30 // End LCD module connections
31
32 #define HEATER PORTD.RD0
33 #define FAN PORTD.RD1
34 #define ENTER 15
35 #define CLEAR 13
36 #define ON 1
37 #define OFF 0
38
39 void main() {
40 unsigned short kp,Txt[14];
41 unsigned short Temp_Ref ; // Reference Temperature
42 unsigned char inTemp;
43 unsigned int temp;
44 float mV, ActualTemp;
45
46 Keypad_Init(); // Initialize Keypad
47 ANSELC = 0; // Configure PORTC as digital I/O
48 ANSELB = 0; // Configure PORTB as digital I/O
49 ANSELD = 0; // Configure PORTD as digital I/O
50 TRISA0_bit = 1; //Configure AN0 (RA0) as input
51 TRISC = 0; //PORTC are outputs (LCD)
52 TRISD0_bit=0; //RD0 is output (Heater)
53 TRISD1_bit=0; //RD1 is output (Fan)
54
55 Lcd_Init(); // Initialize LCD
56 Lcd_Cmd(_LCD_CLEAR); // Clear display
57 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
58 Lcd_Out(1, 4, "Automatic");
59 Lcd_Out(2, 2, "Temp Control");
60 delay_ms(2000); //2s delay
61
62 HEATER = OFF;
63 FAN = OFF;
64
65 //ON startup, read the Referance Temperature from the Keypad
66 START:
67 Lcd_Cmd(_LCD_CLEAR); // Clear display
68 Lcd_Out(1, 1, "Enter Temp Ref");
69 Temp_Ref=0;
70 Lcd_Out(2, 1, "Temp Ref: ");
71 while(1)
72 {
73 do
74 kp = Keypad_Key_Click(); // Store key code in kp variable
75 while (!kp);
76 if ( kp == ENTER )break;
77 if (kp > 3 && kp < 8) kp = kp-1;
78 if (kp > 8 && kp < 12) kp = kp-2;
79 if (kp ==14)kp = 0;
80 if ( kp == CLEAR )goto START;
81 Lcd_Chr_Cp(kp + '0');
82 Temp_Ref =(10*Temp_Ref) + kp;
83 }
84 Lcd_Cmd(_LCD_CLEAR); // Clear display
85 Lcd_Out(1, 1, "Temp Ref: ");
86 intToStr( Temp_Ref,Txt); //Convert to String
87 inTemp=Ltrim(Txt);
88 Lcd_Out_CP(inTemp); //Display Ref Temp
89 Lcd_Out(2, 1, "Press # to Cont.");
90 //Wait until # is pressed
91 kp =0;
92 while(kp!=ENTER)
93 {
94 do
95 kp = Keypad_Key_Click(); // Store key code in kp variable
96 while(!kp);
97 }
98 Lcd_Cmd(_LCD_CLEAR); // Clear display
99
100 Lcd_Out(1, 1, "Temp Ref: ");
101 Lcd_Chr(1,15,223); // Different LCD displays have different
102 // char code for degree
103 Lcd_Chr(1,16,'C'); // Display "C" for Celsius
104 //Program loop
105 while(1) {
106 //Display Referance Temperature and Actual Temperature
107 temp = ADC_Read(0); //Read temperature from AN0
108 mV = temp * 5000.0/1024.0; //Convert to mV
109 ActualTemp = mV/10.0 ; // Convert to degrees Celcius
110 intToStr( Temp_Ref,Txt); //Convert to String
111 inTemp=Ltrim(Txt);
112 //Lcd_Out(1, 1, "Temp Ref: ");
113 Lcd_Out(1, 11, inTemp); //Display Ref Temp
114 Lcd_Out(2, 1, "Temp= ");
115 FloatToStr(ActualTemp,Txt); //Convert to string
116 Txt[4] = 0;
117 Lcd_Out(2,7,Txt);
118 Lcd_Out(2,12," ");
119
120 //Compare ref temp with actual emp
121 if (Temp_Ref > ActualTemp) //If Temp Ref is less than actual Temp, Switch ON Heater
122 {
123 HEATER = ON,
124 FAN = OFF;
125 }
126 if (Temp_Ref < ActualTemp) //If Temp Ref is greater than actual Temp, Switch ON Fan
127 {
128 HEATER = OFF,
129 FAN = ON;
130 }
131 if (Temp_Ref == ActualTemp) //If Temp Ref is equal to actual Temp, Switch OFF Fan and Heater
132 {
133 HEATER = OFF,
134 FAN = OFF;
135 }
136 Delay_ms(10000); //Wait 10 s then repeat
137 }
138 }

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