Rev Author Line No. Line
1384 mija 1 /* mija 2009
2  
3 SW pro ATmega8 mereni + regulace teploty
4 Fosc = 8192000Hz externi krystal
5  
6 ver.: 0.0 - start
7 */
8  
9 #include <avr/io.h>
10 #include <avr/interrupt.h>
11 #include <util/delay.h>
12 #include <avr/eeprom.h>
13 #include "reflow.h"
14 #include "lcd.h" // musi se definovat PINy LCD
15 #include "key.h" // musi se definovat PINy Tlacitek
16 #include "clock.h" // typ promenne Clockt_t
17 #include "process.h" // musi se definovat PIN topeni
18  
19 //*************************************************************
20 // delay + interrupt
21  
22 void delay_ms(uint16_t time)
23 {
24 while (time --) _delay_ms(1);
25 }
26  
27 volatile static uint8_t ClockTik8ms=125;
28  
29 ISR(TIMER0_OVF_vect) //tik 8ms
30 {
31 if (ClockTik8ms) ClockTik8ms --;
32 }
33  
34 //*************************************************************
35 // gemeral cpu set
36  
37 void GeneralCpuInit(void)
38 {
39 //*** IO PIN *** init,setup
40  
41 //*** TIMER0 ***
42 // 256 prescaler
43 // overflow 8ms
44 // enable interrupt overflow
45 TCCR0 = _BV(CS02);
46 TIMSK |= _BV(TOIE0);
47  
48 //*** TIMER1 ***
49  
50 //*** TIMER2 ***
51  
52 //*** EXTERNAL INTERRUPT
53  
54 //*** USART ***
55  
56 //*** ADC ***
57 // input ADC2 (pin25 C2)
58 // external reference 2.5V
59 // clock adc 128k (div 64)
60 ADMUX = _BV(MUX1);
61 ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADPS1) | _BV(ADPS2);
62 }
63  
64 //*************************************************************
65 // analog read
66  
67 int16_t ReadAdc(void)
68 {
69 int16_t analog;
70 uint8_t a;
71  
72 analog = 0;
73 for (a=0;a<32;a++)
74 {
75 ADCSR &= ~(_BV(ADIF));
76 ADCSR |= _BV(ADSC);
77 while (!(ADCSR & _BV(ADIF)));
78 analog += ADC;
79 }
80 return (analog >> 5 ); // prumer = analog/32
81 }
82  
83 //*************************************************************
84 // main
85  
86 int main(void)
87 {
88 uint8_t proces = START;
89 char DisplBuffer[MAXDISPLBUFFER];
90 Clockt_t clock;
91 uint16_t temp = 0;
92 uint8_t key = NOKEY;
93  
94 GeneralCpuInit();
95 HeatInit();
96 HeatOff();
97 delay_ms(100);
98 LCD_init();
99 KeyInit();
100 ClockInit(0,0,0,&clock);
101 sei();
102  
103 for (;;)
104 {
105 switch(proces)
106 {
107 case START: proces = ProcesStart(DisplBuffer,key,temp);break;
108 case SOLDER: proces = ProcesSolder(DisplBuffer,key,temp,&clock);break;
109 case POSITIV: proces = ProcesPositiv(DisplBuffer,key,temp,&clock);break;
110 case DRY: proces = ProcesDry(DisplBuffer,key,temp,&clock); break;
111 default: proces = START;
112 }
113  
114 if (DisplBuffer[0]) ProcesDispl(DisplBuffer);
115  
116 ProcesKey(&key);
117 ProcesClock((uint8_t *)&ClockTik8ms,&clock);
118 ProcesTemp(ReadAdc(),&temp);
119 }
120 return 0;
121 }