Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1734 - 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.h"
46
#include "lipo.h"
47
#include "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)  // MartinR: wird in HAL_HW3_9 aufgerufen
74
{
75
        ADMUX = (0<<REFS1) | (1<<REFS0);      // externe 5V Referenzspannung nutzen
76
        ADMUX = (ADMUX & ~(0x1F)) | (1 & 0x1F); // ADC1 verwenden
77
        ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // ADC Enable, Interrupt Enable, ADC Start, Auto Trigger, Prescaler 128
78
}
79
 
80
 
81
/*! Error compensation, Scaling 16-bit result, Rounding up
82
    , Calculate 16-bit result, Resets variables
83
 
84
Quelle AVR121: Enhancing ADC resolution by versampling
85
 
86
*/
87
void oversampled(void) // MartinR: wird vom Interrupt aufgerufen
88
{
89
  cli();
90
  accumulator += Lipo_UOffset;                 //5150 Offset error compensation
91
 
92
//  accumulator *= 0.9993;               // Gain error compensation
93
  accumulator *= 0.9600;               //0.9800 Gain error compensation
94
  temp=(int)accumulator%64;
95
  accumulator/=64;                     // Scaling the answer
96
  if(temp>=32)
97
    {
98
      accumulator += 1;                // Round up
99
    }
100
 
101
//  Vin = (accumulator/65536)*4.910;     // Calculating 16-bit result
102
 
103
    Vin =accumulator/7.5;
104
    volt_avg = Vin;
105
//    write_ndigit_number_u(0, 3, Vin, 5, 0);
106
//    write_ndigit_number_u(0, 4, volt_avg, 5, 0);
107
  samples     = 0;
108
  accumulator = 0;
109
 
110
  sei();
111
}
112
 
113
 
114
void show_Lipo(void)  // MartinR: wird an verschiedenen Stellen aufgerufen
115
{
116
 
117
uint16_t Balken = 0;
118
 
119
 
120
  lcd_rect(103,2,1,3,1);
121
  if (volt_avg < 320)
122
    {
123
      Balken = 0;
124
      lcd_frect(106 + Balken-1, 2, 19-Balken, 3, 0);  // löschen
125
    }
126
 
127
 
128
  if (PKT_Accutyp == true)               //LiPO Akku
129
    {
130
      lcd_rect(104, 0, 23, 7, 1);  // Rahmen
131
    if (volt_avg >= 420) Balken = 19;
132
    if ((volt_avg > 320) && (volt_avg < 420)) Balken = (volt_avg-320)/5;
133
    lcd_frect(106 + Balken+1, 2, 19-Balken, 3, 0);  // löschen
134
    }
135
 
136
  if (PKT_Accutyp == false)              // LiON Akku
137
    {
138
      lcd_rect(104, 0, 22, 7, 1);  // Rahmen
139
    if (volt_avg >= 410) Balken = 18;
140
    if ((volt_avg > 320) && (volt_avg < 410)) Balken = ((volt_avg-320)/5);
141
    lcd_frect(106 + Balken+1, 2, 18-Balken, 3, 0);  // löschen
142
    }
143
 
144
 
145
  if (Balken > 0) lcd_frect(106, 2, Balken, 3, 1);  // Füllung
146
 
147
}
148
 
149
#endif
150