Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2216 | - | 1 | /*********************************************************************************************************************************** |
2 | * File: main.c |
||
3 | * |
||
4 | * Purpose: This program has been developed for the microcontroller test board AVR-CTRL |
||
5 | * It is reading a poti connected to analog input = ADC and passes that value to BL CTRL via I2C |
||
6 | * to set the turn rate of the connected electrical motor |
||
7 | * |
||
8 | * Functions: int main(void) |
||
9 | * ISR(ADC_vect) |
||
10 | * void init_ADC(void) |
||
11 | * |
||
12 | * hardware: AVR-Ctrl experimental board without LCD display as I2C bus is used |
||
13 | * |
||
14 | * Created: Jan 2013 |
||
15 | * |
||
16 | * Version: 1.00 experimental version |
||
17 | * |
||
18 | * Copyright: (c)2013 dk9nw -at- darc.de |
||
19 | * All rights reserved. This software is available only for non-commercial amateur radio or educational applications. |
||
20 | * Other uses are prohibited. This software may be modified only if |
||
21 | * the resulting code be made available publicly and the original author(s) given credit. |
||
22 | * |
||
23 | ***********************************************************************************************************************************/ |
||
24 | #include "main.h" |
||
25 | #include "timer.h" |
||
26 | #include "twimaster.h" |
||
27 | #include "analog.h" |
||
28 | |||
29 | |||
30 | // ************************************************************************************************************************** |
||
31 | // main function starting here |
||
32 | // |
||
33 | // INPUT: None |
||
34 | // OUTPUT: None |
||
35 | // RETURN: 1 |
||
36 | // -------------------------------------------------------------------------------------------------------------------------- |
||
37 | int main (void) |
||
38 | { |
||
39 | // DDRA = Port A Data Direction Register –> DDA7 DDA6 DDA5 DDA4 DDA3 DDA2 DDA1 DDA0 |
||
40 | DDRA &= ~((1<<DDA7)|(1<<DDA6)|(1<<DDA5)|(1<<DDA4)|(1<<DDA3)); // those 5 keys are configured as input |
||
41 | |||
42 | DDRB=0xFF; // all ports set as output as LEDs are connected here |
||
43 | PORTB = 0x00; // switch off all LEDs |
||
44 | |||
45 | init_ADC(); // initialize analog digial converter |
||
46 | init_Timer0(); // initialize timer |
||
47 | i2c_init(); // initialize I2C Bus (TWI) |
||
48 | |||
49 | |||
50 | // ************************************************************************************************************************** |
||
51 | void loop2ms(void) |
||
52 | { |
||
53 | // ADCSRA = Analog Digital Conversion Status Register A -> ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 |
||
54 | // ADSC = start conversion |
||
55 | ADCSRA |= (1<<ADSC); |
||
56 | |||
57 | while(!AdReady); // wait that ISR in analog.c is passed |
||
58 | AdReady = 0; |
||
59 | |||
60 | AdWertNick = AdWertNick / 2; // AdWertNick 0...511 |
||
61 | Motor[0] = (AdWertNick); // desired turn rate is passed over to the motor front side |
||
62 | Motor[1] = (AdWertNick); // desired turn rate is passed over to the motor rear side |
||
63 | |||
64 | if (AdWertNick<32) PORTB = 0x01; // switch on LED 1 |
||
65 | if (AdWertNick>31 && AdWertNick<64) PORTB = 0x02; // ... LED 2 |
||
66 | if (AdWertNick>63 && AdWertNick<96) PORTB = 0x04; // ... LED 3 |
||
67 | if (AdWertNick>95 && AdWertNick<128) PORTB = 0x08; // ... LED 4 |
||
68 | if (AdWertNick>127 && AdWertNick<160) PORTB = 0x10; // ... LED 5 |
||
69 | if (AdWertNick>159 && AdWertNick<192) PORTB = 0x20; // ... LED 6 |
||
70 | if (AdWertNick>191 && AdWertNick<224) PORTB = 0x40; // ... LED 7 |
||
71 | if (AdWertNick>223 ) PORTB = 0x80; // ... LED 8 |
||
72 | } |
||
73 | // ************************************************************************************************************************** |
||
74 | |||
75 | |||
76 | |||
77 | // ************************************************************************************************************************** |
||
78 | while (1) |
||
79 | { |
||
80 | if ( ((PINA & 0xF8)>> 3) == 0x01) break; // red button to finish program if necessary |
||
81 | |||
82 | |||
83 | if(Flag2ms()) // toggle that loop every 2 ms |
||
84 | { |
||
85 | loop2ms(); |
||
86 | } |
||
87 | } |
||
88 | // ************************************************************************************************************************** |
||
89 | |||
90 | } |
||
91 | // *** EOF : main() ************************************************************************************************************* |
||
92 | |||
93 |