firmware/main.c

Fri, 12 May 2017 15:46:46 +0200

author
Malte Di Donato <mdd@neo-soft.org>
date
Fri, 12 May 2017 15:46:46 +0200
changeset 2
114e1283d03a
parent 1
d224ff5c155b
permissions
-rw-r--r--

no software without a bug :)

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

mercurial