Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

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