-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_code.c
304 lines (275 loc) · 6.79 KB
/
main_code.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// During day-time the intensity of street light is varied using LDR
//At night during peak hours(23hr to 07hr) the light is controlled using PIR sensor.When the PIR sensor detects any motion it switches on the light for a specific period of time
//Here just for demonstration purpose the led will light up for 2 seconds only. LED represents the street light.
//The LCD will display time only during peak hours(23hr to 07hr) at night.
//To check this circuit during day-time, set hr value of function rtc_set_t(hr,mm,ss) between 06 to 23 [means 7am to 11pm] (24 hrs format), which is there in the main function.
//To check this circuit during night, set hr value of function rtc_set_t(hr,mm,ss) between 22 to 07 [means 11pm to 7am] (24 hrs format) which is there in the main function.
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#define lcd PORTB
void serial_ini()
{
UCSRB=(1<<TXEN);
UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL=0X33;
}
void serial_tr(unsigned char x)
{
while(!(UCSRA & (1<<UDRE)));
UDR= x;
while(TXC ==0);
}
void serial_tr_bcd(unsigned char x)
{
serial_tr('0'+(x>>4));
serial_tr('0'+(x & 0x0f));
}
void i2c_ini()
{
TWSR=0X00;
TWBR=0X47;
TWCR=0X04;
}
void i2c_start()
{
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while((TWCR &(1<<TWINT))==0);
}
void i2c_wr(unsigned char x)
{
TWDR=x;
TWCR=(1<<TWINT)|(1<<TWEN);
while((TWCR & (1<<TWINT))==0);
}
unsigned char i2c_re(unsigned char x)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(x<<TWEA);
while((TWCR &(1<<TWINT))==0);
return TWDR;
}
void i2c_stop()
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
int i;
for(i=0;i<200;i++);
}
void rtc_ini()
{
i2c_ini();
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(0x07); //set register pointer to 7
i2c_wr(0x00); //set value of location 7 to 0
i2c_stop(); // transmit stop condition
}
void rtc_set_t(unsigned char h,unsigned char m,unsigned char s)
{
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(0); //set register pointer to 0
i2c_wr(s);
i2c_wr(m);
i2c_wr(h);
i2c_stop();
}
void rtc_get_t(unsigned char *h,unsigned char *m,unsigned char *s)
{
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(0); //set register pointer to 0
i2c_stop();
i2c_start();
i2c_wr(0xd1); // address DS1307 for read
*s=i2c_re(1); //read sec ,read ack
*m=i2c_re(1); //read min ,read ack
*h=i2c_re(0); //read hour ,read nack
i2c_stop();
}
void cmd(unsigned char x)
{
lcd=x;
PORTD=(0<<3);
PORTD=(0<<4);
PORTD=(1<<5);
_delay_ms(10);
PORTD=(0<<5);
}
void lcd_display(unsigned char x)
{
lcd=x;
PORTD=(0<<4)|(1<<3);
PORTD=(1<<5)|(0<<4)|(1<<3);
_delay_ms(20);
PORTD=(0<<5)|(0<<4)|(1<<3);
}
void lcd_ini()
{
cmd(0x38);
cmd(0x0e);
cmd(0x01);
cmd(0x06);
cmd(0x80);
}
void lcd_str(unsigned char *str)
{
while(*str!='\0')
{
lcd_display(*str);
str++;
}
}
void lcd_pos(int line,int pos)
{
if(line==1)
cmd(0x80+pos);
else if(line==2)
cmd(0xc0+pos);
}
void ADC_Init()
{
DDRA |=0x00; /* Make ADC port as input */
ADCSRA = 0x87; /* Enable ADC, fr/128 */
ADMUX = 0x40; /* Vref: Avcc, ADC channel: 0 */
}
int ADC_Read(char channel)
{
int Ain,AinLow;
ADMUX=ADMUX|(channel & 0x0f); /* Set input channel to read */
ADCSRA |= (1<<ADSC); /* Start conversion */
while((ADCSRA&(1<<ADIF))==0); /* Monitor end of conversion interrupt */
_delay_us(10);
AinLow = (int)ADCL; /* Read lower byte*/
Ain = (int)ADCH*256; /* Read higher 2 bits and
Multiply with weight */
Ain = Ain + AinLow;
return(Ain); /* Return digital value*/
}
int main()
{
DDRD|=(1<<3) | (1<<4) | (1<<5) | (1<<7);
DDRD |= (1<<2);
DDRB=0XFF;
ADC_Init();
MCUCR = 0x03;
PORTD |= (0<<2);
unsigned char i,j,k;
int temp;
rtc_ini();
rtc_set_t(0x07,0x59,0x55);
lcd_ini();
serial_ini();
while (1)
{
rtc_get_t(&i,&j,&k);
while((i==0x23) || (i<=6 && i>=0))
{
TCCR2 = 0x00;
sei();
GICR |= (1<<INT0);
/*if((PIND&(1<<2)))
{
cmd(0x0E);
//cmd(0x80);
TCCR2 = 0x69;
OCR2 = 255;
//GICR |= (1<<INT0);
lcd_str("TIME ");
lcd_pos(1,6);
lcd_display('0'+(i>>4));
lcd_display('0'+(i & 0x0f));
lcd_display(':');
lcd_display('0'+(j>>4));
lcd_display('0'+(j & 0x0f));
lcd_display(':');
lcd_display('0'+(k>>4));
lcd_display('0'+(k & 0x0f));
lcd_pos(2,0);
lcd_str("Lights On");
_delay_ms(2000);
TCCR2 = 0x00;
cmd(0x01);
cmd(0x08);
}
*/
rtc_get_t(&i,&j,&k);
}
//rtc_get_t(&i,&j,&k);
cli();
cmd(0x08);
temp = ADC_Read(0);
if(temp< 100)
{
TCCR2 = 0x00;
}
else if(temp>=100 && temp<=230)
{
TCCR2 = 0x69;
OCR2 = 35;
}
else if(temp>=231 && temp<=360)
{
TCCR2 = 0x69;
OCR2 = 70;
}
else if(temp>=361 && temp<=490)
{
TCCR2 = 0x69;
OCR2 = 105;
}
else if(temp>=491 && temp<=620)
{
TCCR2 = 0x69;
OCR2 = 140;
}
else if(temp>=621 && temp<=750)
{
TCCR2 = 0x69;
OCR2 = 175;
}
else if(temp>=751 && temp<=880)
{
TCCR2 = 0x69;
OCR2 = 210;
}
else if(temp>=881 && temp<=1024)
{
TCCR2 = 0x69;
OCR2 = 255;
}
if((i==0x23) || (i<=6 && i>=0))
TCCR2 = 0x00;
}
return 0;
}
ISR(INT0_vect)
{
unsigned char i =0,j = 0,k = 0,l,m,n;
rtc_get_t(&i,&j,&k);
l = i;
m = j;
n = k;
cmd(0x0E);
//cmd(0x80);
TCCR2 = 0x69;
OCR2 = 255;
//GICR |= (1<<INT0);
lcd_str("TIME ");
lcd_pos(1,6);
lcd_display('0'+(l>>4));
lcd_display('0'+(l & 0x0f));
lcd_display(':');
lcd_display('0'+(m>>4));
lcd_display('0'+(m& 0x0f));
lcd_display(':');
lcd_display('0'+(n>>4));
lcd_display('0'+(n & 0x0f));
lcd_pos(2,0);
lcd_str("Lights On");
_delay_ms(2000);
TCCR2 = 0x00;
cmd(0x01);
cmd(0x08);
}