Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
730 | woggle | 1 | /***************************************************************************** |
2 | * Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net * |
||
3 | * * |
||
4 | * This program is free software; you can redistribute it and/or modify * |
||
5 | * it under the terms of the GNU General Public License as published by * |
||
6 | * the Free Software Foundation; either version 2 of the License. * |
||
7 | * * |
||
8 | * This program is distributed in the hope that it will be useful, * |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
11 | * GNU General Public License for more details. * |
||
12 | * * |
||
13 | * You should have received a copy of the GNU General Public License * |
||
14 | * along with this program; if not, write to the * |
||
15 | * Free Software Foundation, Inc., * |
||
16 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
17 | * * |
||
18 | *****************************************************************************/ |
||
19 | |||
20 | #include <avr/io.h> |
||
21 | #include <avr/interrupt.h> |
||
22 | #include <avr/pgmspace.h> |
||
23 | #include <util/delay.h> |
||
24 | |||
25 | #include "main.h" // LEDs |
||
26 | #include "timer.h" // Keys |
||
27 | #include "lcd.h" |
||
28 | #include "lipo.h" |
||
29 | |||
30 | |||
31 | volatile uint8_t AD_Ready = 0; |
||
32 | volatile uint8_t AD_Channel = 0; |
||
33 | volatile uint16_t AD_Val[4]; |
||
34 | |||
35 | volatile uint8_t AD_WarnLevel; |
||
36 | volatile uint8_t AD_Warn = 0; |
||
37 | |||
38 | |||
39 | //***************************************************************************** |
||
40 | // |
||
41 | ISR(ADC_vect) |
||
42 | { |
||
43 | // LED1_TOGGLE; |
||
44 | AD_Val[AD_Channel] = ADC; |
||
45 | if (AD_Val[AD_Channel] <= (AD_WarnLevel * 20)) |
||
46 | { |
||
47 | AD_Warn |= (1 << AD_Channel); |
||
48 | } |
||
49 | AD_Channel++; |
||
50 | AD_Channel &= 0x03; |
||
51 | ADMUX = (ADMUX & 0xe0) | AD_Channel; |
||
52 | if (AD_Channel) |
||
53 | { |
||
54 | ADCSRA |= (1 << ADSC); |
||
55 | } |
||
56 | else |
||
57 | { |
||
58 | AD_Ready = 1; |
||
59 | } |
||
60 | |||
61 | } |
||
62 | |||
63 | //***************************************************************************** |
||
64 | // |
||
65 | void ADC_Init(void) |
||
66 | { |
||
67 | DIDR0 = 0x0f; // disable digital inputs on PA0-3 |
||
68 | ADMUX = (1 << REFS1) | (1 << REFS0); // internal 2.56 V ref. |
||
69 | ADCSRB = 0; // free running mode |
||
70 | // enable ADC, start conversion, auto trigger, enable interrupt, prescaler = 128 |
||
71 | ADCSRA = (1 << ADEN) | (1 << ADSC) | (0 << ADATE) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); |
||
72 | } |
||
73 | |||
74 | //***************************************************************************** |
||
75 | // |
||
76 | void lipo (void) |
||
77 | { |
||
78 | uint8_t i; |
||
79 | |||
80 | lcd_cls (); |
||
81 | lcd_printp_at (0, 0, PSTR("LiPo Display"), 0); |
||
82 | lcd_printpns_at (0, 7, PSTR(" Exit"), 0); |
||
83 | |||
84 | lcd_printp_at (0, 2, PSTR("Cell 1:"), 0); |
||
85 | lcd_printp_at (0, 3, PSTR("Cell 2:"), 0); |
||
86 | lcd_printp_at (0, 4, PSTR("Cell 3:"), 0); |
||
87 | lcd_printp_at (0, 5, PSTR("Cell 4:"), 0); |
||
88 | |||
89 | do |
||
90 | { |
||
91 | if (AD_Ready) |
||
92 | { |
||
93 | for (i = 0; i < 4; i++) |
||
94 | { |
||
95 | //write_ndigit_number_u (0, 2 + i, AD_Val[i], 4, 0); |
||
96 | //write_ndigit_number_u_10th (6, 2 + i, AD_Val[i] >> 1, 3, 0); |
||
97 | write_ndigit_number_u_100th (10, 2 + i, AD_Val[i] >> 1, 10, 0); |
||
98 | } |
||
99 | AD_Ready = 0; |
||
100 | ADCSRA |= (1 << ADSC); |
||
101 | } |
||
102 | } |
||
103 | while (!get_key_press (1 << KEY_ESC)); |
||
104 | get_key_press(KEY_ALL); |
||
105 | } |