1  | #ifndef MINIPEAK_H
  | 
2  | #define MINIPEAK_H
  | 
3  | 
  | 
4  | #define CLK 1200000 // internal CLOCK
  | 
5  | 
  | 
6  | #define IST 1
  | 
7  | #define SOLL 0
  | 
8  | #define TIME 2
  | 
9  | 
  | 
10  | 
  | 
11  | #include <avr/io.h>
  | 
12  | #include <avr/interrupt.h>
  | 
13  | #include <stdint.h>
  | 
14  | #include <inttypes.h>
  | 
15  | 
  | 
16  | // IRQ-Vars
  | 
17  | volatile uint8_t timertick_s;
  | 
18  | volatile uint8_t ADCWert[3];//PB2|PB4|PB3
  | 
19  | 
  | 
20  | /*
  | 
21  | ADC 3V 8bit => 0,012 V pro Bit => Timer 4 Minuten bei vollem Pegel = 256
  | 
22  | */
  | 
23  |   
  | 
24  | 
  | 
25  | 
  | 
26  | //prototypes
  | 
27  | 
  | 
28  | void init(void);
  | 
29  | 
  | 
30  | 
  | 
31  | #endif
  | 
32  | #include "minipeak.h"
  | 
33  | #include <avr/io.h>
  | 
34  | #include <avr/interrupt.h>
  | 
35  | #include <stdint.h>
  | 
36  | #include <inttypes.h>
  | 
37  | 
  | 
38  | 
  | 
39  | //Timer-IRQ
  | 
40  | ISR(TIM0_OVF_vect)
  | 
41  | {
 | 
42  |   static uint8_t tick;
  | 
43  |   // 1,2 MHz / 256 = 4,687 => 5000 Durchgänge/s Prescaler 1024 => 5 Durchgänge/s
  | 
44  |   // 1,2 MHz / 256 + Prescaler 256 => 20
  | 
45  |   // 1,2 MHz / 256 + Prescaler 64 => 80
  | 
46  |   // 1,2 MHz / 256 + Prescaler 8 => 640
  | 
47  |   // 1,2 MHz / 256 + Prescaler 0 => 4700
  | 
48  |   // 9,6 MHz => 36,6 ca. 37
  | 
49  |   if(tick++ > 80)
  | 
50  |   {
 | 
51  |     timertick_s++;
  | 
52  |     tick=0;
  | 
53  |     //PORTB = PINB ^ ( 1 << PB1 );
  | 
54  |   }
  | 
55  | }
  | 
56  | 
  | 
57  | ISR(ADC_vect)
  | 
58  | {
 | 
59  |   /*
  | 
60  |   Pro Kanal zwanzig mal wandeln und Wert zuweisen
  | 
61  |   */
  | 
62  |   static uint8_t debounce=0;
  | 
63  |   static uint8_t enumerate=0;
  | 
64  | //  static uint8_t test=0;
  | 
65  |   
  | 
66  |   if(debounce++ > 200)
  | 
67  |   {
 | 
68  |   
  | 
69  |     ADCWert[enumerate]=ADCH;
  | 
70  |   
  | 
71  |     ADMUX=0;
  | 
72  |     //ADMUX =  (1<<ADLAR)|(1<<MUX0);
  | 
73  |   
  | 
74  |   
  | 
75  |     switch(enumerate)
  | 
76  |     {
 | 
77  |       case 0: ADMUX =  (1<<ADLAR)|(1<<MUX1);// PB4 abfragen
  | 
78  |               enumerate = 1;
  | 
79  |               //PORTB = PINB ^ ( 1 << PB1 );
  | 
80  |       break;
  | 
81  |       case 1: ADMUX =  (1<<ADLAR)|(1<<MUX1)|(1<<MUX0);// PB3 abfragen
  | 
82  |               enumerate = 2;
  | 
83  |               //PORTB = PINB ^ ( 1 << PB1 );
  | 
84  |       break;
  | 
85  |       case 2: ADMUX =  (1<<ADLAR)|(1<<MUX0);// PB2 abfragen 
  | 
86  |               enumerate = 0;
  | 
87  |               //PORTB = PINB ^ ( 1 << PB1 );
  | 
88  |       break;
  | 
89  |       default: ADMUX =  (1<<ADLAR)|(1<<MUX0);// PB2 default 
  | 
90  |               enumerate = 0;
  | 
91  |       break;
  | 
92  |     }
  | 
93  |   
  | 
94  |     debounce = 0;
  | 
95  |   }
  | 
96  | }
  | 
97  | 
  | 
98  | void init(void)
  | 
99  | {
 | 
100  |   // Internal RC
  | 
101  |   
  | 
102  |   //PIN-Settings
  | 
103  |   DDRB = 0; // Alles Eingänge
  | 
104  |   DDRB |= (1<<PB0); // PWm Ausgang
  | 
105  |   DDRB |= (1<<PB1); // Testausgang
  | 
106  |   PORTB |= ( 1 << PB1 );
  | 
107  |   PORTB |= ( 1 << PB0 );
  | 
108  |   
  | 
109  |   //Timer0 Fast-PWM 50:50 Tast, Prescaler 64
  | 
110  | /*  TCCR0A = 0;
  | 
111  |   TCCR0A = (1<<WGM00)|(1<<WGM01);
  | 
112  |   TCCR0B = 0;
  | 
113  |   TCCR0B = (1<<CS00)|(1<<CS01);
  | 
114  |   OCR0A = 127;
  | 
115  |   TIMSK0 = 0;
  | 
116  |   */
  | 
117  |   /*
  | 
118  |   //Timer0 Fast-PWM 50:50 Tast, Prescaler 1024
  | 
119  |   TCCR0A = 0;
  | 
120  |   TCCR0A = (1<<WGM00)|(1<<WGM01);//|(1<<COM0B1);//|(1<<COM0A0);
  | 
121  |   TCCR0B = 0;
  | 
122  |   TCCR0B = (1<<WGM02)|(1<<CS00)|(1<<CS02);
  | 
123  |   OCR0A = 32;
  | 
124  |   OCR0B = 16;
  | 
125  |   TIMSK0 = 0;
  | 
126  |   //TIMSK0 = (1<<TOIE0);
  | 
127  |   */
  | 
128  |   
  | 
129  |   //ADC Free running, ADLAR=1 Mux=0 1,2MHz/8=150kHz
  | 
130  |   ADCSRA = 0;
  | 
131  |   ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADATE)|(1<<ADSC);
  | 
132  |   ADCSRB = 0;
  | 
133  |   ADMUX =  (1<<ADLAR)|(1<<MUX0);//|(1<<MUX1);//PB2 Mux0~Mux1//PB4 ~Mux0 Mux1//PB3 Mux0Mux1
  | 
134  |   DIDR0 = (1<<ADC0D)|(1<<ADC1D)|(1<<ADC2D)|(1<<ADC3D); //PB2,PB4,PB3
  | 
135  |   
  | 
136  |   //IRQs
  | 
137  |   sei();
  | 
138  | }
  | 
139  | 
  | 
140  | int main(void)
  | 
141  | {
 | 
142  |   //local Vars
  | 
143  |   uint8_t timer = 0;
  | 
144  |   
  | 
145  |   init();
  | 
146  |   
  | 
147  |   while(1)
  | 
148  |   {
 | 
149  |   
  | 
150  |   
  | 
151  |   if(ADCWert[SOLL]<128)
  | 
152  |   {
 | 
153  |   //PORTB = PINB ^ ( 1 << PB1 );
  | 
154  |     PORTB &= ~( 1 << PB0 );
  | 
155  |   }
  | 
156  |   else
  | 
157  |     PORTB |= ( 1 << PB0 );
  | 
158  | 
  | 
159  |   
  | 
160  |   if(ADCWert[IST]<128)
  | 
161  |   {
 | 
162  |   //PORTB = PINB ^ ( 1 << PB0 );
  | 
163  |     PORTB &= ~( 1 << PB1 );
  | 
164  |   }
  | 
165  |   else
  | 
166  |     PORTB |= ( 1 << PB1 );
  | 
167  |   
  | 
168  |   if(ADCWert[TIME]<128)
  | 
169  |   {
 | 
170  |   //PORTB = PINB ^ ( 1 << PB1 );
  | 
171  |     PORTB &= ~( 1 << PB1 );
  | 
172  |   }
  | 
173  |   else
  | 
174  |     PORTB |= ( 1 << PB1 );
  | 
175  |   
  | 
176  |   /*
  | 
177  |     Wenn ADC-Soll < ADC-Ist  einschalten
  | 
178  |     Wenn ADC-Time < 20 Timer-Aus
  | 
179  |     Wenn ADC-Time > 20 Timer-Ein
  | 
180  |     Wenn Timer-Ein für eingestellte Zeit einschalten
  | 
181  |     /
  | 
182  | 
  | 
183  |     if(ADCWert[SOLL]<ADCWert[IST])
  | 
184  |     {
 | 
185  |       // wenn Timer aktiv Zeitmessung starten
  | 
186  |       if(ADCWert[TIME]>20)
  | 
187  |       {
 | 
188  |         timertick_s = 0;
  | 
189  |         TIMSK0 |= (1<<TOIE0);
  | 
190  |         timer=1;
  | 
191  |       }
  | 
192  |       // anschalten
  | 
193  |       TCCR0A |= (1<<COM0A1);
  | 
194  |     }
  | 
195  |     
  | 
196  |     if(ADCWert[SOLL]>ADCWert[IST])
  | 
197  |     {
 | 
198  |       if(timer&&ADCWert[TIME]>timertick_s)
  | 
199  |       {
 | 
200  |         // solange Zeitvorgabe nicht erfüllt anschalten
  | 
201  |         TCCR0A |= (1<<COM0A1);
  | 
202  |       }
  | 
203  |       else
  | 
204  |       {
 | 
205  |         // sonst alles aus
  | 
206  |         timer = 0;
  | 
207  |         TIMSK0 &= ~(1<<TOIE0);
  | 
208  |         TCCR0A &= ~(1<<COM0A1);
  | 
209  |       }
  | 
210  |     }*/
  | 
211  |   }
  | 
212  | 
  | 
213  | }
  |