Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
231 | killagreg | 1 | #include <avr/boot.h> |
2 | |||
3 | #include <avr/io.h> |
||
4 | #include <avr/interrupt.h> |
||
5 | |||
6 | #include "main.h" |
||
7 | #include "timer0.h" |
||
8 | #include "uart0.h" |
||
9 | #include "uart1.h" |
||
10 | #include "fat16.h" |
||
11 | #include "led.h" |
||
12 | #include "menu.h" |
||
13 | #include "printf_P.h" |
||
273 | killagreg | 14 | #include "analog.h" |
274 | killagreg | 15 | #include "ubx.h" |
276 | killagreg | 16 | #include "button.h" |
231 | killagreg | 17 | |
273 | killagreg | 18 | #ifdef USE_FOLLOWME |
19 | int16_t UBat = 120; |
||
303 | joko | 20 | int16_t Zellenzahl = 0; |
21 | int16_t PowerOn = 0; |
||
22 | int16_t i = 0; |
||
23 | int16_t delay = 0; |
||
273 | killagreg | 24 | #endif |
25 | |||
274 | killagreg | 26 | #define GPS_RX_TIMEOUT 0x0001 |
27 | |||
28 | |||
231 | killagreg | 29 | int main (void) |
30 | { |
||
274 | killagreg | 31 | static uint16_t GPS_Timer = 0; |
32 | static uint16_t Error = 0; |
||
33 | |||
231 | killagreg | 34 | // disable interrupts global |
35 | cli(); |
||
36 | |||
37 | // disable watchdog |
||
38 | MCUSR &=~(1<<WDRF); |
||
39 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
||
40 | WDTCSR = 0; |
||
41 | |||
42 | // initalize modules |
||
43 | LED_Init(); |
||
271 | killagreg | 44 | LEDRED_ON; |
231 | killagreg | 45 | TIMER0_Init(); |
46 | USART0_Init(); |
||
274 | killagreg | 47 | UBX_Init(); |
231 | killagreg | 48 | USART1_Init(); |
273 | killagreg | 49 | ADC_Init(); |
276 | killagreg | 50 | Button_Init(); |
231 | killagreg | 51 | // enable interrupts global |
52 | sei(); |
||
297 | killagreg | 53 | |
271 | killagreg | 54 | LEDRED_OFF; |
55 | #ifdef USE_FOLLOWME |
||
56 | LEDGRN_ON; |
||
57 | #endif |
||
231 | killagreg | 58 | |
297 | killagreg | 59 | // try to initialize the FAT 16 filesystem on the SD-Card |
60 | Fat16_Init(); |
||
61 | |||
231 | killagreg | 62 | #ifdef USE_SDLOGGER |
297 | killagreg | 63 | printf("\r\n\r\nHW: SD-Logger"); |
231 | killagreg | 64 | #endif |
65 | #ifdef USE_FOLLOWME |
||
297 | killagreg | 66 | printf("\r\n\r\nHW: Follow-Me"); |
231 | killagreg | 67 | #endif |
297 | killagreg | 68 | printf("\r\nFollow Me\n\rSoftware:V%d.%d%c ",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
69 | printf("\r\n------------------------------"); |
||
70 | printf("\r\n"); |
||
231 | killagreg | 71 | |
72 | |||
271 | killagreg | 73 | #ifdef USE_FOLLOWME |
303 | joko | 74 | //BeepTime = 2000; |
271 | killagreg | 75 | #endif |
76 | |||
231 | killagreg | 77 | LCD_Clear(); |
78 | |||
274 | killagreg | 79 | GPS_Timer = SetDelay(1000); |
231 | killagreg | 80 | while (1) |
81 | { |
||
276 | killagreg | 82 | // check for button action |
294 | killagreg | 83 | if(GetButton()) |
84 | { |
||
303 | joko | 85 | BeepTime = 200; |
297 | killagreg | 86 | Fat16_Init(); |
87 | |||
294 | killagreg | 88 | } |
276 | killagreg | 89 | |
273 | killagreg | 90 | // restart ADConversion if ready |
91 | if(ADReady) |
||
92 | { |
||
93 | DebugOut.Analog[0] = Adc0; |
||
94 | DebugOut.Analog[1] = Adc1; |
||
95 | DebugOut.Analog[2] = Adc2; |
||
96 | DebugOut.Analog[3] = Adc3; |
||
97 | DebugOut.Analog[4] = Adc4; |
||
98 | DebugOut.Analog[5] = Adc5; |
||
99 | DebugOut.Analog[6] = Adc6; |
||
100 | DebugOut.Analog[7] = Adc7; |
||
101 | |||
102 | #ifdef USE_FOLLOWME |
||
103 | // AVcc = 5V --> 5V = 1024 counts |
||
104 | // the voltage at the voltage divider reference point is 0.8V less that the UBat |
||
105 | // because of the silicon diode inbetween. |
||
284 | killagreg | 106 | // voltage divider R2=10K, R3=3K9 |
107 | // UAdc4 = R3/(R3+R2)*UBat= 3.9/(3.9+10)*UBat = UBat/3.564 |
||
108 | UBat = (3 * UBat + (64 * Adc4) / 368) / 4; |
||
273 | killagreg | 109 | DebugOut.Analog[8] = UBat; |
303 | joko | 110 | |
111 | // check for zellenzahl |
||
112 | if(PowerOn < 100) |
||
113 | { |
||
114 | if(UBat<=84) Zellenzahl = 2; |
||
115 | else Zellenzahl = 3; |
||
116 | PowerOn++; |
||
117 | } |
||
118 | DebugOut.Analog[16] = Zellenzahl; |
||
119 | DebugOut.Analog[17] = PowerOn; |
||
120 | |||
121 | //show recognised Zellenzahl to user |
||
122 | if(i < Zellenzahl && PowerOn >= 100 && BeepTime == 0 && delay > 1000) |
||
123 | { |
||
124 | BeepTime = 100; |
||
125 | i++; |
||
126 | delay = 0; |
||
127 | } |
||
128 | if(delay < 1500) delay++; |
||
129 | |||
130 | // monitor battery undervoltage |
||
131 | if(UBat < Zellenzahl * 31 && PowerOn >= 100) BeepTime = 200; |
||
273 | killagreg | 132 | #endif |
133 | ADReady = 0; |
||
274 | killagreg | 134 | ADC_Enable(); // restart ad conversion sequence |
273 | killagreg | 135 | } |
274 | killagreg | 136 | |
137 | |||
138 | if(GPSData.Status == NEWDATA) |
||
139 | { |
||
140 | Error &= ~GPS_RX_TIMEOUT; // clear possible error |
||
141 | GPS_Timer = SetDelay(1000); // reset timeout |
||
142 | if(CheckGPSOkay >= 5) |
||
143 | { |
||
144 | // trigger transmission of FollowMe message here. |
||
145 | |||
146 | CheckGPSOkay = 0; |
||
147 | } |
||
148 | GPSData.Status = PROCESSED; |
||
149 | } |
||
150 | else // invalid or already processed |
||
151 | { |
||
152 | if(CheckDelay(GPS_Timer)) |
||
153 | { |
||
154 | Error |= GPS_RX_TIMEOUT; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | USART0_ProcessRxData(); |
||
159 | USART0_TransmitTxData(); |
||
231 | killagreg | 160 | } |
161 | return (1); |
||
162 | } |
||
163 |