Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2136 | - | 1 | /***************************************************************************** |
2 | * Copyright (C) 2008 Thomas Kaiser, thomas@ft-fanpage.de * |
||
3 | * Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net * |
||
4 | * based on the key handling by Peter Dannegger * |
||
5 | * see www.mikrocontroller.net * |
||
6 | * Copyright (C) 2011 Christian "Cebra" Brandtner, brandtner@brandtner.net * |
||
7 | * Copyright (C) 2011 Harald Bongartz * |
||
8 | * * |
||
9 | * This program is free software; you can redistribute it and/or modify * |
||
10 | * it under the terms of the GNU General Public License as published by * |
||
11 | * the Free Software Foundation; either version 2 of the License. * |
||
12 | * * |
||
13 | * This program is distributed in the hope that it will be useful, * |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
16 | * GNU General Public License for more details. * |
||
17 | * * |
||
18 | * You should have received a copy of the GNU General Public License * |
||
19 | * along with this program; if not, write to the * |
||
20 | * Free Software Foundation, Inc., * |
||
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
22 | * * |
||
23 | * * |
||
24 | * Credits to: * |
||
25 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN * |
||
26 | * http://www.mikrokopter.de * |
||
27 | * Gregor "killagreg" Stobrawa for his version of the MK code * |
||
28 | * Thomas Kaiser "thkais" for the original project. See * |
||
29 | * http://www.ft-fanpage.de/mikrokopter/ * |
||
30 | * http://forum.mikrokopter.de/topic-4061-1.html * |
||
31 | * Claas Anders "CaScAdE" Rathje for providing the font and his C-OSD code * |
||
32 | * http://www.mylifesucks.de/oss/c-osd/ * |
||
33 | * Harald Bongartz "HaraldB" for providing his Ideas and Code for usibility* |
||
34 | *****************************************************************************/ |
||
35 | |||
36 | //############################################################################ |
||
37 | //# HISTORY lipo.c |
||
38 | //# |
||
39 | //# 15.06.2014 OG |
||
40 | //# - fix; show_Lipo() - wenn die Spannung unter 3.20 Volt ging, wurde wieder |
||
41 | //# ein voller Akkubalken angezeigt aufgrund einer Subtraktion - fixed |
||
42 | //# |
||
43 | //# 21.02.2014 OG |
||
44 | //# - chg: show_Lipo() neu geschrieben - etwas smarter, ein paar Bytes kleiner |
||
45 | //# |
||
46 | //# 20.02.2014 OG |
||
47 | //# - chg: Codeformattierungen |
||
48 | //############################################################################ |
||
49 | |||
50 | |||
51 | #include "../cpu.h" |
||
52 | #include <avr/io.h> |
||
53 | #include <util/delay.h> |
||
54 | #include <avr/interrupt.h> |
||
55 | #include <inttypes.h> |
||
56 | #include <stdlib.h> |
||
57 | #include <avr/pgmspace.h> |
||
58 | #include "../main.h" |
||
59 | #include "../lcd/lcd.h" |
||
60 | #include "lipo.h" |
||
61 | #include "../eeprom/eeprom.h" |
||
62 | |||
63 | |||
64 | // Global variables |
||
65 | double accumulator = 0; //!< Accumulated 10-bit samples |
||
66 | double Vin = 0; //!< 16-bit float number result |
||
67 | short temp = 0; //!< Temporary variable |
||
68 | short samples = 0; //!< Number of conversions |
||
69 | uint16_t volt_avg = 0; |
||
70 | |||
71 | |||
72 | |||
73 | //-------------------------------------------------------------- |
||
74 | //! ADC interrupt routine |
||
75 | //-------------------------------------------------------------- |
||
76 | ISR (ADC_vect) |
||
77 | { |
||
78 | accumulator += ADCW; |
||
79 | samples++; |
||
80 | |||
81 | if( samples>4095 ) |
||
82 | { |
||
83 | oversampled(); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | |||
88 | |||
89 | //-------------------------------------------------------------- |
||
90 | //-------------------------------------------------------------- |
||
91 | void ADC_Init (void) |
||
92 | { |
||
93 | DDRA &= ~(1<<DDA1); // MartinR |
||
94 | PORTA &= ~(1<<PORTA1); // MartinR |
||
95 | ADMUX = (0<<REFS1) | (1<<REFS0); // externe 5V Referenzspannung nutzen |
||
96 | ADMUX = (ADMUX & ~(0x1F)) | (1 & 0x1F); // ADC1 verwenden |
||
97 | ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // Prescaler 128, Freilaufend, Interrupte frei |
||
98 | } |
||
99 | |||
100 | |||
101 | |||
102 | |||
103 | //-------------------------------------------------------------- |
||
104 | // Error compensation, Scaling 16-bit result, Rounding up, |
||
105 | // Calculate 16-bit result, Resets variables |
||
106 | // |
||
107 | // Quelle AVR121: Enhancing ADC resolution by versampling |
||
108 | //-------------------------------------------------------------- |
||
109 | void oversampled(void) |
||
110 | { |
||
111 | cli(); |
||
112 | accumulator += Config.Lipo_UOffset; //5150 Offset error compensation |
||
113 | |||
114 | // accumulator *= 0.9993; // Gain error compensation |
||
115 | accumulator *= 0.9600; //0.9800 Gain error compensation |
||
116 | temp=(int)accumulator%64; |
||
117 | accumulator/=64; // Scaling the answer |
||
118 | |||
119 | if(temp>=32) |
||
120 | { |
||
121 | accumulator += 1; // Round up |
||
122 | } |
||
123 | |||
124 | // Vin = (accumulator/65536)*4.910; // Calculating 16-bit result |
||
125 | |||
126 | Vin =accumulator/7.5; |
||
127 | volt_avg = Vin; |
||
128 | |||
129 | // write_ndigit_number_u(0, 3, Vin, 5, 0,0); |
||
130 | // write_ndigit_number_u(0, 4, volt_avg, 5, 0,0); |
||
131 | |||
132 | samples = 0; |
||
133 | accumulator = 0; |
||
134 | |||
135 | sei(); |
||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | //-------------------------------------------------------------- |
||
141 | //-------------------------------------------------------------- |
||
142 | void show_Lipo( void ) |
||
143 | { |
||
144 | int16_t w = 0; |
||
145 | |||
146 | //-------------------------------------------- |
||
147 | // Batterie Symbol zeichnen |
||
148 | //-------------------------------------------- |
||
149 | lcd_rect(104, 0, 23, 7, 1); // Rahmen aussen |
||
150 | lcd_rect(105, 1, 21, 5, 0); // Rahmen innen |
||
151 | lcd_frect(102, 0, 1, 7, 0); // ganz links etwas loeschen |
||
152 | lcd_rect(103,2,1,3,1); // Batterie "Kappe" |
||
153 | |||
154 | |||
155 | //-------------------------------------------- |
||
156 | // Batterie Balken berechnen / zeichnen |
||
157 | //-------------------------------------------- |
||
158 | // |
||
159 | // Config.PKT_Accutyp |
||
160 | // true = LiPO Akku mit max. 4.2 Volt (420) |
||
161 | // false = LiON Akku mit max. 4.1 Volt (410) |
||
162 | |||
163 | if( volt_avg > 320 ) |
||
164 | { |
||
165 | w = (volt_avg-320)/(Config.PKT_Accutyp ? 4.8 : 4.5); // die Faktoren 4.8 (kann bis 5.0) und 4.5 fuer LiPo / LiON wurden per Test ermittelt |
||
166 | //lcdx_printf_at_P( 12, 1, MNORMAL, 0,0, PSTR("v=%3d"), w ); // Debug Anzeige um Faktoren zu ermitteln |
||
167 | } |
||
168 | |||
169 | if(w<0) w = 0; |
||
170 | if(w>20) w = 20; // nicht mehr als 20 Pixel Breite |
||
171 | |||
172 | if( w>0 ) |
||
173 | lcd_frect( 106+(20-w), 2, w-1, 3, 1); // Batterie Balken: fill |
||
174 | |||
175 | if( w<20 ) |
||
176 | lcd_frect( 106, 2, 19-w, 3, 0); // Batterie Balken: clear |
||
177 | } |
||
178 |