-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
229 lines (190 loc) · 4.96 KB
/
main.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <msp430.h>
#include "LCD_I2C.H"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void initTimerA0(void); //Timer for counting 10 seconds
void initTimerA1(void); //Timer for PWM
void delayMS(int msecs); //Creating delay with TimerA0
unsigned int controlHeater = 0;
unsigned int Count_1ms;
unsigned int Set_Temp;
int adc_value;
unsigned int current_temperature;
char str[20];
unsigned int adc_read(unsigned int channel);//Function to read analog value from sensor
int scan_keypad(void);
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P2DIR |= BIT2; //P2.2 Port have been made output for pwm
P2SEL |= BIT2; // Changed from GPIO to PWM
P1DIR = 0x00;
P1DIR |= BIT0 + BIT1 + BIT2 + BIT3; //P1.0,P1.1,P1.2,P1.3 are rows
P1OUT = 0x00;
P1REN |= BIT4; //Pull down for column1 P1.4
P1OUT &= ~(BIT4);//Make sure that column bits are zero
P2REN |= BIT0 + BIT1; //pull down for P2.0,P2.1 2,3.column
P2OUT &= ~(BIT0 + BIT1);
__enable_interrupt();
initTimerA0(); //Configure TimerA0
initTimerA1(); //Configure TimerA1
lcd_init(0x27);
set_backlight();
lcd_setcursor(0,0);
lcd_print("Set Temp:");
Set_Temp = scan_keypad();
sprintf(str, "%d", Set_Temp);
lcd_print(str);
TA1CCR0 = 1000;
while(1)
{
adc_value = adc_read(INCH_5); //read from channel5
current_temperature = (3.6*(float)adc_value*100)/1023.0;
lcd_setcursor(1, 0);
sprintf(str, "%d", current_temperature);
lcd_print(str);
if(current_temperature < Set_Temp)
{
TA1CCR1 = 800; //Set PWM with %95 duty cycle
}
else if (current_temperature >= Set_Temp)
{
controlHeater = 1;
delayMS(10000); //10 saniye bekle;
}
}
}
void initTimerA0(void)
{
TA0CCR0 = 0; //initially stop the timer
TA0CCTL0 |= CCIE; //Enable interrupt for TA0CCR0
TA0CTL = TASSEL_2 + ID_0 + MC_1; //SMCLK, no division, up mode
}
void initTimerA1(void)
{
TA1CCR0 = 0;
TA1CCTL1 = OUTMOD_7; //Set Reset Mode
TA1CTL = TASSEL_2 + ID_0 + MC_1;
}
void delayMS(int msecs)
{
Count_1ms = 0; //Reset
TA0CCR0 = 999; //1ms
while(Count_1ms <= msecs);
TA0CCR0 = 0; //Stop timer
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A_CCR0_ISR(void)
{
Count_1ms++;
if(controlHeater)
{
TA1CCR1 = 0;
}
}
int scan_keypad(void)
{
unsigned int i = 0;
char scanned_value[3];
unsigned int temperature_setted;
while(scanned_value[2] != '#')
{
P1OUT |= BIT0;
delayMS(1);
if((P1IN & BIT4))
{
while((P1IN & BIT4));
scanned_value[i] = '1';
i++;
}
else if((P2IN & BIT1))
{
while((P2IN & BIT1));
scanned_value[i] = '2';
i++;
}
else if((P2IN & BIT0))
{
while((P2IN & BIT0));
scanned_value[i] = '3';
i++;
}
P1OUT &= ~BIT0;
P1OUT |= BIT1;
delayMS(1);
if((P1IN & BIT4))
{
while((P1IN & BIT4));
scanned_value[i] = '4';
i++;
}
else if((P2IN & BIT1))
{
while((P2IN & BIT1));
scanned_value[i] = '5';
i++;
}
else if((P2IN & BIT0))
{
while((P2IN & BIT0));
scanned_value[i] = '6';
i++;
}
P1OUT &= ~BIT1;
P1OUT |= BIT2;
delayMS(1);
if((P1IN & BIT4))
{
while((P1IN & BIT4));
scanned_value[i] = '7';
i++;
}
else if((P2IN & BIT1))
{
while((P2IN & BIT1));
scanned_value[i] = '8';
i++;
}
else if((P2IN & BIT0))
{
while((P2IN & BIT0));
scanned_value[i] = '9';
i++;
}
P1OUT &= ~BIT2;
P1OUT |= BIT3;
delayMS(1);
if((P1IN & BIT4))
{
continue;
}
else if((P2IN & BIT1))
{
while((P2IN & BIT1));
scanned_value[i] = '0';
i++;
}
else if((P2IN & BIT0))
{
while((P2IN & BIT0));
scanned_value[i] = '#';
i++;
}
P1OUT &= ~BIT3;
}
temperature_setted = ((scanned_value[0] - '0')*10) + (scanned_value[1] - '0');
return temperature_setted;
}
unsigned int adc_read(unsigned int channel)
{
ADC10CTL0 &= ~ENC; // Disabled ADC
ADC10CTL0 = ADC10SHT_2 + ADC10ON; // 8 clock ticks, ADC on
ADC10CTL1 = ADC10SSEL_3 + channel + CONSEQ_2 + SHS_0 + MSC; //Set 'chan', SMCLK
ADC10CTL0 |= ENC + ADC10SC;
ADC10AE0 = BIT5;
while(!(ADC10CTL0 & ADC10IFG));
return ADC10MEM;
}