Rev Author Line No. Line
1118 mija 1 /* mija 2008
2  
3 nanotern02A
4 f osc 1Mhz
5 timer1 PWM
6  
7 ver.: 1.0
8 /n .. goto 1,1
9 /r .. goto 1,2
10 /f .. clear lcd
11  
12 */
13  
14 //*************************************************************
15  
16 #include <avr/io.h>
17 #include <util/delay.h>
18 #include <avr/interrupt.h>
19 #include <avr/eeprom.h>
20 //#include <avr/sleep.h>
21 #include "lcd.h"
22  
23  
24 //*************************************************************
25  
26 void line_feed(void);
27 void PWM_init(void);
28 void COMP_init(void);
29 ISR(ANA_COMP_vect);
30 int main(void);
31  
32 //*************************************************************
33  
34 #define TEXT1 "setup"
35  
36 #define MAX_BUF 16
37  
38 #define FIRST 40 // *16ms time next key
39 #define REPEAT 10 // *16ms time repeat key
40  
41 #define LIGHT PB5 // tlacitko A
42 #define PORT_LIGHT PORTB // nutne definovat PORT,DDR,PIN
43 #define DDR_LIGHT DDRB
44  
45 #define TL_A PD6 // tlacitko A
46 #define PORT_A PORTD // nutne definovat PORT,DDR,PIN
47 #define DDR_A DDRD
48 #define PIN_A PIND
49  
50 #define TL_B PB0 // tlacitko B
51 #define PORT_B PORTB // nutne definovat PORT,DDR,PIN
52 #define DDR_B DDRB
53 #define PIN_B PINB
54  
55  
56 #define TL_C PB2 // tlacitko C
57 #define PORT_C PORTB // nutne definovat PORT,DDR,PIN
58 #define DDR_C DDRB
59 #define PIN_C PINB
60  
61 #define TL_D PD3 // tlacitko D
62 #define PORT_D PORTB // nutne definovat PORT,DDR,PIN
63 #define DDR_D DDRB
64 #define PIN_D PINB
65  
66 // eeprom
67 #define EE_SETUP 0x03
68 #define EE_VOLTAGE_LO 0x00
69 #define EE_VOLTAGE_HI 0x01
70  
71 // key
72 #define KEY_0 0x8
73 #define KEY_1 0xC
74 #define KEY_2 0xE
75 #define KEY_3 0x4
76 #define KEY_4 0x6
77 #define KEY_5 0x7
78 #define KEY_6 0xA
79 #define KEY_7 0x2
80 #define KEY_8 0x3
81 #define KEY_9 0xB
82 #define KEY_cr 0x1
83 #define KEY_esc 0x9
84  
85 //interni
86 #define A_INPUT (!(PIN_A & _BV(TL_A)))
87 #define A_PULLUP (PORT_A |= _BV(TL_A))
88 #define A_INIT (DDR_A &= ~(_BV(TL_A)))
89  
90 #define B_INPUT (!(PIN_B & _BV(TL_B)))
91 #define B_PULLUP (PORT_B |= _BV(TL_B))
92 #define B_INIT (DDR_B &= ~(_BV(TL_B)))
93  
94 #define C_INPUT (!(PIN_C & _BV(TL_C)))
95 #define C_PULLUP (PORT_C |= _BV(TL_C))
96 #define C_INIT (DDR_C &= ~(_BV(TL_C)))
97  
98 #define D_INPUT (!(PIN_D & _BV(TL_D)))
99 #define D_PULLUP (PORT_D |= _BV(TL_D))
100 #define D_INIT (DDR_D &= ~(_BV(TL_D)))
101  
102 #define LIGHT_ON (PORT_LIGHT |= _BV(LIGHT))
103 #define LIGHT_OFF (PORT_LIGHT &= ~(_BV(LIGHT)))
104 #define LIGHT_INIT (DDR_LIGHT |= _BV(LIGHT))
105  
106 //flag
107 volatile uint8_t flag;
108  
109 #define KEY_A 0
110 #define KEY_B 1
111 #define KEY_C 2
112 #define KEY_D 3
113 #define VOLTAGE 4
114 #define LIGHT_SET 5
115 #define LIGHT_T0 6
116 #define LIGHT_T1 7
117  
118 #define FLAG_A_SET (flag |= _BV(KEY_A))
119 #define FLAG_A_CLR (flag &= ~(_BV(KEY_A)))
120 #define FLAG_A (flag & _BV(KEY_A))
121  
122 #define FLAG_B_SET (flag |= _BV(KEY_B))
123 #define FLAG_B_CLR (flag &= ~(_BV(KEY_B)))
124 #define FLAG_B (flag & _BV(KEY_B))
125  
126 #define FLAG_C_SET (flag |= _BV(KEY_C))
127 #define FLAG_C_CLR (flag &= ~(_BV(KEY_C)))
128 #define FLAG_C (flag & _BV(KEY_C))
129  
130 #define FLAG_D_SET (flag |= _BV(KEY_D))
131 #define FLAG_D_CLR (flag &= ~(_BV(KEY_D)))
132 #define FLAG_D (flag & _BV(KEY_D))
133  
134 #define FLAG_VOLTAGE_HI (flag |= _BV(VOLTAGE))
135 #define FLAG_VOLTAGE_LO (flag &= ~(_BV(VOLTAGE)))
136 #define FLAG_VOLTAGE (flag & (_BV(VOLTAGE)))
137  
138 #define FLAG_LIGHT_ON (flag |= _BV(LIGHT_SET))
139 #define FLAG_LIGHT_OFF (flag &= ~(_BV(LIGHT_SET)))
140 #define FLAG_LIGHT (flag & _BV(LIGHT_SET))
141  
142 #define FLAG_T0_SET (flag |= _BV(LIGHT_T0))
143 #define FLAG_T0_CLR (flag &= ~(_BV(LIGHT_T0)))
144 #define FLAG_T0 (flag & _BV(LIGHT_T0))
145  
146 #define FLAG_T1_SET (flag |= _BV(LIGHT_T1))
147 #define FLAG_T1_CLR (flag &= ~(_BV(LIGHT_T1)))
148 #define FLAG_T1 (flag & _BV(LIGHT_T1))
149  
150 //*************************************************************
151  
152 uint8_t a,pos;
153 //uint8_t buf[8] = TEXT1;
154 uint8_t buf_rx[MAX_BUF];
155 volatile uint8_t rx_shift;
156 volatile uint8_t tik16ms;
157 uint8_t lcd_shift;
158 enum {LED_OFF,LED_5s,LED_10s,LED_15s,LED_20s,LED_25s,LED_30s,LED_ON};
159  
160 //*************************************************************
161  
162 void IO_init(void)
163 {
164 LIGHT_INIT;
165 A_PULLUP;
166 B_PULLUP;
167 C_PULLUP;
168 D_PULLUP;
169 }
170  
171 void rs232_init(void)
172 {
173 //set baud rate fixed 9600 8N1 for fosc 1Mhz
174 UCSRA = _BV(U2X) ;
175 UBRRL = 12;
176 UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
177 }
178  
179 void PWM_init(void)
180 {
181 TCNT1 = 0;
182 OCR1B = 0x1;
183 DDRB |= _BV(PB4);
184 TCCR1A |= _BV(COM1B1) | _BV(WGM10);
185 TCCR1B |= _BV(WGM12) | _BV(CS10);
186 }
187  
188 void PWM_set(void)
189 {
190 if (ACSR & _BV(ACO))
191 {
192 //OCR1BL = 0x5f;
193 OCR1BL = eeprom_read_byte(EE_VOLTAGE_LO);
194 FLAG_VOLTAGE_LO;
195 }
196 else
197 {
198 //OCR1BL = 0x17;
199 OCR1BL = eeprom_read_byte(EE_VOLTAGE_HI);
200 FLAG_VOLTAGE_HI;
201 }
202 }
203  
204 void COMP_init(void)
205 {
206 ACSR = _BV(ACBG) | _BV(ACIE);
207 DIDR = _BV(AIN1D);
208 }
209  
210 void timer0_init(void)
211 {
212 TCCR0B = _BV(CS01) | _BV(CS00);
213 TIMSK |= _BV(TOIE0);
214 }
215  
216 ISR(TIMER0_OVF_vect) //cca 16ms tik
217 {
218 if (A_INPUT) FLAG_A_SET;else FLAG_A_CLR;
219 if (B_INPUT) FLAG_B_SET;else FLAG_B_CLR;
220 if (C_INPUT) FLAG_C_SET;else FLAG_C_CLR;
221 if (D_INPUT) FLAG_D_SET;else FLAG_D_CLR;
222  
223 if (FLAG_LIGHT) LIGHT_ON;
224 else LIGHT_OFF;
225 if (tik16ms) tik16ms--;
226 }
227  
228 ISR(USART_RX_vect)
229 {
230 //while ( !(UCSRA & (_BV(RXC))) );
231 if (++rx_shift >= MAX_BUF) rx_shift = 0;
232 buf_rx[rx_shift]=UDR;
233 }
234  
235 ISR(ANA_COMP_vect)
236 {
237 //if (ACSR & _BV(ACO)) OCR1BL = 0x5f;
238 //else OCR1BL = 0x17;
239 PWM_set();
240 }
241  
242 void put(char data)
243 {
244 while (!( UCSRA & _BV(UDRE)));
245 UDR = data;
246 }
247  
248 void send(uint8_t data)
249 {
250 switch (data)
251 {
252 case (KEY_0): put('0');break;
253 case (KEY_1): put('1');break;
254 case (KEY_2): put('2');break;
255 case (KEY_3): put('3');break;
256 case (KEY_4): put('4');break;
257 case (KEY_5): put('5');break;
258 case (KEY_6): put('6');break;
259 case (KEY_7): put('7');break;
260 case (KEY_8): put('8');break;
261 case (KEY_9): put('9');break;
262 case (KEY_cr): put('\n');break;
263 case (KEY_esc): put('\f');break;
264 }
265 }
266  
267 void delay_ms(uint8_t time)
268 {
269 while (time--) _delay_ms(1);
270 }
271  
272 void print_lcd(char *data)
273 {
274 while (*data) LCD_putc(*data++);
275 }
276  
277 void set(void)
278 {
279 LCD_gotoxy(1,1);
280 print_lcd("con. 0/1");
281 LCD_gotoxy(1,2);
282 print_lcd(" led 2-9");
283 while (A_INPUT);
284 for(;;)
285 {
286 switch (flag & 0x0f)
287 {
288 case KEY_0: OCR1BL--;break;
289 case KEY_1: OCR1BL++;break;
290 case KEY_2: LIGHT_OFF;FLAG_LIGHT_OFF;break;
291 case KEY_3: LIGHT_ON;FLAG_LIGHT_ON;break;
292 case KEY_4: break;
293 case KEY_esc:
294 {
295 eeprom_write_byte(EE_SETUP,flag & 0xF0);
296 if (FLAG_VOLTAGE) eeprom_write_byte(EE_VOLTAGE_HI,OCR1BL);
297 else eeprom_write_byte(EE_VOLTAGE_LO,OCR1BL);
298  
299 return;
300 }
301 }
302 delay_ms(50);
303 }
304 }
305  
306 int main(void)
307 {
308 uint8_t save_flag;
309  
310 IO_init();
311 LCD_init();
312 rs232_init();
313 PWM_init();
314 COMP_init();
315 timer0_init();
316 LIGHT_OFF;
317  
318 print_lcd(TEXT1);
319 LCD_gotoxy(1,2);
320 print_lcd("cr+reset");
321 LCD_gotoxy(1,1);
322  
323 flag = 0xfE0 & eeprom_read_byte(EE_SETUP);
324  
325 sei();
326 if (A_INPUT) set();
327 pos=0;
328 rx_shift=0;
329 lcd_shift=0;
330 save_flag=0;
331 for(;;)
332 {
333 if (rx_shift != lcd_shift )
334 {
335 if (++lcd_shift >= MAX_BUF) lcd_shift = 0;
336 a=buf_rx[lcd_shift];
337 switch (a)
338 {
339 case ('\n'): LCD_gotoxy(1,1);pos=0;break;
340 case ('\r'): pos=8;break;
341 case ('\f'): LCD_clear();pos=0;break;
342 default :if(pos == 8)
343 {
344 LCD_gotoxy(1,2);
345 //delay_ms(10);
346 }
347 LCD_putc(a);
348 if (++pos > 15)
349 {
350 pos = 0;
351 LCD_gotoxy(1,1);
352 //delay_ms(10);
353 }
354 }
355 }
356 if (flag & 0x0F)
357 {
358 if ((flag & 0x0F) != save_flag)
359 {
360 save_flag = flag & 0x0F;
361 send(save_flag);
362 tik16ms=FIRST;
363 }
364 else
365 {
366 if (!tik16ms)
367 {
368 send(save_flag);
369 tik16ms = REPEAT;
370 }
371 }
372 }
373 else save_flag = 0;
374 }
375 return 0;
376 }