Subversion Repositories Projects

Rev

Blame | Last modification | View Log | RSS feed

/*****************************************************************************
 *   Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net                  *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License.          *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the                           *
 *   Free Software Foundation, Inc.,                                         *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
 *                                                                           *
 *****************************************************************************/


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

#include "main.h"               // LEDs
#include "timer.h"      // Keys
#include "lcd.h"
#include "lipo.h"


volatile uint8_t AD_Ready = 0;
volatile uint8_t AD_Channel = 0;
volatile uint16_t AD_Val[4];

volatile uint8_t AD_WarnLevel;
volatile uint8_t AD_Warn = 0;


//*****************************************************************************
//
ISR(ADC_vect)
{
        //      LED1_TOGGLE;
        AD_Val[AD_Channel] = ADC;
        if (AD_Val[AD_Channel] <= (AD_WarnLevel * 20))
        {
                AD_Warn |= (1 << AD_Channel);
        }
        AD_Channel++;
        AD_Channel &= 0x03;
        ADMUX = (ADMUX & 0xe0) | AD_Channel;
        if (AD_Channel)
        {
                ADCSRA |= (1 << ADSC);
        }
        else
        {
                AD_Ready = 1;
        }

}

//*****************************************************************************
//
void ADC_Init(void)
{
        DIDR0 = 0x0f; // disable digital inputs on PA0-3
        ADMUX = (1 << REFS1) | (1 << REFS0);    // internal 2.56 V ref.
        ADCSRB = 0;     // free running mode
        // enable ADC, start conversion, auto trigger, enable interrupt, prescaler = 128
        ADCSRA = (1 << ADEN) | (1 << ADSC) | (0 << ADATE) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}

//*****************************************************************************
//
void lipo (void)
{
        uint8_t i;
       
        lcd_cls ();
        lcd_printp_at (0, 0, PSTR("LiPo Display"), 0);
        lcd_printpns_at (0, 7, PSTR("            Exit"), 0);

        lcd_printp_at (0, 2, PSTR("Cell 1:"), 0);
        lcd_printp_at (0, 3, PSTR("Cell 2:"), 0);
        lcd_printp_at (0, 4, PSTR("Cell 3:"), 0);
        lcd_printp_at (0, 5, PSTR("Cell 4:"), 0);

        do
        {
                if (AD_Ready)
                {
                        for (i = 0; i < 4; i++)
                        {
                                //write_ndigit_number_u (0, 2 + i, AD_Val[i], 4, 0);
                                //write_ndigit_number_u_10th (6, 2 + i, AD_Val[i] >> 1, 3, 0);
                                write_ndigit_number_u_100th (10, 2 + i, AD_Val[i] >> 1, 10, 0);
                        }
                        AD_Ready = 0;
                        ADCSRA |= (1 << ADSC);
                }
        }
        while (!get_key_press (1 << KEY_ESC));
        get_key_press(KEY_ALL);
}