1 | //ICC-AVR application builder : 13.04.2010 16:15:44
|
2 | // Target : m48
|
3 | // Crystal: 8.0000Mhz
|
4 |
|
5 | #include <iom48v.h>
|
6 | #include <macros.h>
|
7 |
|
8 | unsigned char i = 0;
|
9 | unsigned int result;
|
10 |
|
11 | void port_init(void)
|
12 | {
|
13 | PORTB = 0xF1;
|
14 | DDRB = 0x0E;
|
15 | PORTC = 0x43;
|
16 | DDRC = 0xBC;
|
17 | PORTD = 0x00;
|
18 | DDRD = 0xFF;
|
19 | }
|
20 |
|
21 | void timer0_init(void)
|
22 | {
|
23 | OCR0A = 0x80;
|
24 | OCR0B = 0x80;
|
25 | TCCR0A |= (1 << WGM01) | (1 << WGM00); // Fast PWM
|
26 | TCCR0A |= (1 << COM0A1); // Fast PWM, non-inverting mode
|
27 | TCCR0A &= ~(1 << COM0A0);
|
28 | TCCR0A |= (1 << CS02); // Prescaler: 1/256
|
29 |
|
30 | // TCCR0B |= (1 << WGM01) | (1 << WGM00); // Fast PWM
|
31 | // TCCR0B |= (1 << COM0B1); // Fast PWM, non-inverting mode
|
32 | // TCCR0B &= ~(1 << COM0B0);
|
33 | // TCCR0B |= (1 << CS02); // Prescaler: 1/256
|
34 |
|
35 |
|
36 | }
|
37 |
|
38 | //TIMER1 initialize - prescale:64
|
39 | // WGM: 0) Normal, TOP=0xFFFF
|
40 | // desired value: 100mSec
|
41 | // actual value: 100,000mSec (0,0%)
|
42 | void timer1_init(void)
|
43 | {
|
44 | TCCR1B = 0x00; //stop
|
45 | TCNT1H = 0xCF; //setup
|
46 | TCNT1L = 0x2C;
|
47 | OCR1AH = 0x30;
|
48 | OCR1AL = 0xD4;
|
49 | OCR1BH = 0x30;
|
50 | OCR1BL = 0xD4;
|
51 | ICR1H = 0x30;
|
52 | ICR1L = 0xD4;
|
53 | TCCR1A = 0x00;
|
54 | TCCR1B = 0x03; //start Timer
|
55 | }
|
56 |
|
57 | //ADC initialize
|
58 | // Conversion time: 6uS
|
59 | void adc_init(void)
|
60 | {
|
61 | ADCSRA = 0x00; //disable adc
|
62 | ADMUX = 0x00; //select adc input 0
|
63 | ACSR = 0x80;
|
64 | ADCSRB = 0x00;
|
65 | ADCSRA = 0x01;
|
66 |
|
67 | }
|
68 |
|
69 | #pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
|
70 | void timer1_ovf_isr(void)
|
71 | {
|
72 | //TIMER1 has overflowed
|
73 | TCNT1H = 0xCF; //reload counter high value
|
74 | TCNT1L = 0x2C; //reload counter low value
|
75 |
|
76 |
|
77 |
|
78 | //----------------------------------------------------------------------------//
|
79 | // AD-Wandlung
|
80 | //----------------------------------------------------------------------------//
|
81 | // Dummy-Readout
|
82 | ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1); // ADC aktivieren, Frequenzvorteiler: setzen auf 64 (8 MHz / 64 = 125 kHz) und ADC aktivieren
|
83 | ADMUX = 0x00; // Kanal waehlen (ADC0)
|
84 | ADMUX |= (1<<REFS0) | (1<<REFS1); // interne Referenzspannung nutzen
|
85 | ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
|
86 | while(!(ADCSRA & (1<<ADIF))); // auf Abschluss der Konvertierung warten (ADIF-bit)
|
87 | result = ADC; // ADC muss einmal gelesen werden,
|
88 | // sonst wird Ergebnis der nächsten Wandlung
|
89 | // nicht übernommen.
|
90 |
|
91 | //ADC-Messung mit arithmetischen Mittel aus 4 Messungen
|
92 | result = 0;
|
93 | for (i=0;i<4;i++)
|
94 | {
|
95 | ADCSRA |= (1<<ADSC); // Messung ausfuehren
|
96 | while ( ADCSRA & (1<<ADSC) ); // Konvertierung abwarten
|
97 | result += ADC; // aufaddieren zur Mittelwertbildung (ADC auslesen der zwei Bits
|
98 | }
|
99 | ADCSRA &= ~(1<<ADEN); // ADC ausschalten
|
100 |
|
101 | result /= 16; // Mittelwert aus 4 und 8Bit für PWM (1024 / 4 = 256)
|
102 |
|
103 | OCR0A = result; // OC0A (PD6) = Poti-wert
|
104 | OCR0B = result; // OC0B (PD5) = Poti-wert
|
105 |
|
106 |
|
107 | //----------------------------------------------------------------------------//
|
108 | // AD-Abfrage zur Prüfung auf Funktion - Komentare sind für ein anders Projekt
|
109 | //----------------------------------------------------------------------------//
|
110 | if (result < 63) //410 = 5,4V
|
111 | {
|
112 | PORTD &= ~0x15; // PD4, PD2 &PD0 low
|
113 | PORTD |= 0x08; // PD3 high => LED an
|
114 | }
|
115 | if ((result < 127) & (result >= 63)) //430 = 5,7V
|
116 | {
|
117 | PORTD &= ~0x0D; // PD3, PD2 & PD0 low
|
118 | PORTD |= 0x10; // PD4 high => LED an
|
119 | }
|
120 | if ((result < 191) & (result >= 127)) //915 = 10,8V, 975 = 11,5V bei 100K und 330R
|
121 | {
|
122 | PORTD &= ~0x1c; // PD4, PD3 & PD2 low
|
123 | PORTD |= 0x01; // PD0 high => LED an
|
124 | }
|
125 | if (result >= 191)
|
126 | {
|
127 | PORTD &= ~0x19; // PD4, PD3 & PD0 low
|
128 | PORTD |= 0x04; // PD2 high => LED an
|
129 | }
|
130 |
|
131 | }
|
132 | ////////////////////////////////////////////////////////////////////////////////
|
133 | // Hauptprogramm
|
134 | ////////////////////////////////////////////////////////////////////////////////
|
135 | void main(void)
|
136 | {
|
137 | //stop errant interrupts until set up
|
138 | CLI(); //disable all interrupts
|
139 | port_init();
|
140 | timer0_init();
|
141 | timer1_init();
|
142 | adc_init();
|
143 |
|
144 |
|
145 | MCUCR = 0x00;
|
146 | EICRA = 0x00; //extended ext ints
|
147 | EIMSK = 0x00;
|
148 |
|
149 | TIMSK0 = 0x01; //timer 0 interrupt sources
|
150 | TIMSK1 = 0x01; //timer 1 interrupt sources
|
151 | TIMSK2 = 0x00; //timer 2 interrupt sources
|
152 |
|
153 | PCMSK0 = 0x00; //pin change mask 0
|
154 | PCMSK1 = 0x00; //pin change mask 1
|
155 | PCMSK2 = 0x00; //pin change mask 2
|
156 | PCICR = 0x00; //pin change enable
|
157 | PRR = 0x00; //power controller
|
158 | SEI(); //re-enable interrupts
|
159 | //all peripherals are now initialized
|
160 |
|
161 | while(1);
|
162 | // Endlosschleife, blinken läuft im 100ms Interrupt
|
163 |
|
164 |
|
165 | }
|