Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1920 - 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
#include "../cpu.h"
38
#include <avr/io.h>
39
#include <util/delay.h>
40
#include <avr/interrupt.h>
41
#include <inttypes.h>
42
#include <stdlib.h>
43
#include <avr/pgmspace.h>
44
#include "../main.h"
45
#include "../lcd/lcd.h"
46
#include "lipo.h"
47
#include "../eeprom/eeprom.h"
48
#if defined HWVERSION1_3W || defined HWVERSION3_9 || defined HWVERSION1_2W
49
// Global variables
50
double        accumulator       = 0;  //!< Accumulated 10-bit samples
51
double        Vin               = 0;  //!< 16-bit float number result
52
short         temp              = 0;  //!< Temporary variable
53
short         samples           = 0;  //!< Number of conversions
54
uint16_t        volt_avg        = 0;
55
 
56
 
57
//! ADC interrupt routine
58
 
59
ISR (ADC_vect)
60
{
61
  accumulator += ADCW;
62
  samples++;
63
  if(samples>4095)
64
       {
65
         oversampled();
66
      }
67
}
68
 
69
 
70
 
71
//--------------------------------------------------------------
72
//
73
void ADC_Init (void)
74
{
75
        DDRA &= ~(1<<DDA1);       // MartinR
76
        PORTA &= ~(1<<PORTA1);  // MartinR
77
        ADMUX = (0<<REFS1) | (1<<REFS0);      // externe 5V Referenzspannung nutzen
78
        ADMUX = (ADMUX & ~(0x1F)) | (1 & 0x1F); // ADC1 verwenden
79
        ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // Prescaler 128, Freilaufend, Interrupte frei
80
}
81
 
82
 
83
/*! Error compensation, Scaling 16-bit result, Rounding up
84
    , Calculate 16-bit result, Resets variables
85
 
86
Quelle AVR121: Enhancing ADC resolution by versampling
87
 
88
*/
89
void oversampled(void)
90
{
91
  cli();
92
  accumulator += Config.Lipo_UOffset;                 //5150 Offset error compensation
93
 
94
//  accumulator *= 0.9993;               // Gain error compensation
95
  accumulator *= 0.9600;               //0.9800 Gain error compensation
96
  temp=(int)accumulator%64;
97
  accumulator/=64;                     // Scaling the answer
98
  if(temp>=32)
99
    {
100
      accumulator += 1;                // Round up
101
    }
102
 
103
//  Vin = (accumulator/65536)*4.910;     // Calculating 16-bit result
104
 
105
    Vin =accumulator/7.5;
106
    volt_avg = Vin;
107
//    write_ndigit_number_u(0, 3, Vin, 5, 0,0);
108
//    write_ndigit_number_u(0, 4, volt_avg, 5, 0,0);
109
  samples     = 0;
110
  accumulator = 0;
111
 
112
  sei();
113
}
114
 
115
 
116
void show_Lipo(void)
117
{
118
 
119
uint16_t Balken = 0;
120
 
121
 
122
  lcd_rect(103,2,1,3,1);
123
  if (volt_avg < 320)
124
    {
125
      Balken = 0;
126
      lcd_frect(106 + Balken-1, 2, 19-Balken, 3, 0);  // löschen
127
    }
128
 
129
 
130
  if (Config.PKT_Accutyp == true)               //LiPO Akku
131
    {
132
      lcd_rect(104, 0, 23, 7, 1);  // Rahmen
133
    if (volt_avg >= 420) Balken = 19;
134
    if ((volt_avg > 320) && (volt_avg < 420)) Balken = (volt_avg-320)/5;
135
    lcd_frect(106 + Balken+1, 2, 19-Balken, 3, 0);  // löschen
136
    }
137
 
138
  if (Config.PKT_Accutyp == false)              // LiON Akku
139
    {
140
      lcd_rect(104, 0, 22, 7, 1);  // Rahmen
141
    if (volt_avg >= 410) Balken = 18;
142
    if ((volt_avg > 320) && (volt_avg < 410)) Balken = ((volt_avg-320)/5);
143
    lcd_frect(106 + Balken+1, 2, 18-Balken, 3, 0);  // löschen
144
    }
145
 
146
 
147
  if (Balken > 0) lcd_frect(106, 2, Balken, 3, 1);  // Füllung
148
 
149
}
150
 
151
#endif
152