firmware/main.c

changeset 0
bdcf2c76d86e
child 1
d224ff5c155b
equal deleted inserted replaced
-1:000000000000 0:bdcf2c76d86e
1 #include <avr/io.h>
2 #include <util/delay.h>
3 #include <stdlib.h>
4
5 uint8_t mode;
6
7 void entprellung( volatile uint8_t *port, uint8_t maske ) {
8 uint8_t port_puffer;
9 uint8_t entprellungs_puffer;
10
11 for( entprellungs_puffer=0 ; entprellungs_puffer!=0xff ; ) {
12 entprellungs_puffer<<=1;
13 port_puffer = *port;
14 _delay_us(150);
15 if( (*port & maske) == (port_puffer & maske) )
16 entprellungs_puffer |= 0x01;
17 }
18 }
19
20 void led(uint8_t n) {
21 // first clear all leds
22 PORTB &= 0b00011101;
23
24 // enable selected led
25 switch (n) {
26 case 1:
27 PORTB |= _BV(1);
28 break;
29 case 2:
30 PORTB |= _BV(5);
31 break;
32 case 3:
33 PORTB |= _BV(6);
34 break;
35 case 4:
36 PORTB |= _BV(7);
37 break;
38 }
39
40 }
41
42
43 void init(void) {
44 // initialize LED and FET pins
45 DDRB = 0b11101011;
46
47 // DEVELOP: ENABLE PULL-UP ON AVR
48 //PORTD |= ( 1 << PD2 );
49
50
51 TCCR1A = (1<<WGM10)|(1<<COM1A1) // Set up the two Control registers of Timer1.
52 |(1<<COM1B1); // Wave Form Generation is Fast PWM 8 Bit,
53 TCCR1B = (1<<WGM12) // OC1A and OC1B are cleared on compare match
54 |(1<<CS10); // and set at BOTTOM. Clock Prescaler is 1024.
55
56
57 uint8_t i;
58 for(i = 0; i<5; i++) {
59 led(i);
60 _delay_ms(50);
61 }
62
63 for(i = 4; i>mode; i--) {
64 led(i);
65 _delay_ms(50);
66 }
67
68 led(mode);
69 pwm_update();
70 }
71
72 void pwm_update(void) {
73 PORTB |= _BV(0); // switch on led driver
74
75 switch (mode) {
76 case 1:
77 OCR1A = 63; // Dutycycle of OC1A = 25%
78 break;
79 case 2:
80 OCR1A = 127; // Dutycycle of OC1A = 50%
81 break;
82 case 3:
83 OCR1A = 189; // Dutycycle of OC1A = 75%
84 break;
85 case 4:
86 OCR1A = 255; // Dutycycle of OC1A = 100%
87 break;
88 default:
89 // switch off pwm and driver
90 OCR1A = 0;
91 PORTB &= _BV(0);
92 }
93 }
94
95
96 void main(void) {
97
98 mode = 1;
99 uint8_t key_last = _BV(2);
100 uint8_t tmp;
101
102 init();
103
104 while (1) { /* main event loop */
105
106 entprellung( &PIND, _BV(2) );
107 tmp = PIND & _BV(2);
108 if (tmp != key_last) {
109 key_last = tmp;
110 if (tmp) {
111 if (mode < 4) {
112 mode++;
113 } else {
114 mode = 1;
115 }
116 led(mode);
117 pwm_update();
118 _delay_ms(50);
119 _delay_ms(50);
120 }
121 }
122
123 }
124
125 }

mercurial