From 18cb2f29c76a3efb5b418b9ee0e36ec3b228f6ad Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Wed, 3 Oct 2007 01:07:05 +0200 Subject: [PATCH] initial commit --- .gitignore | 6 + Makefile | 51 +++++++ lipo-charger.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 411 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 lipo-charger.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d36148 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.o +*.elf +*.bin +*.hex +*.lst +*.map diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d594a6d --- /dev/null +++ b/Makefile @@ -0,0 +1,51 @@ +PRG = lipo-charger +OBJ = lipo-charger.o +MCU_TARGET = atmega8 +OPTIMIZE = -Os + +DEFS = +LIBS = + +# You should not have to change anything below here. + +CC = avr-gcc + +# Override is only needed by avr-lib build system. + +override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) +override LDFLAGS = -Wl,-Map,$(PRG).map + +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size + +all: $(PRG).elf lst text + $(SIZE) -x -A $(PRG).elf + +$(PRG).elf: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +clean: + rm -rf *.o *.lst *.map $(PRG).elf *.hex *.bin + +lst: $(PRG).lst + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +# Rules for building the .text rom images + +text: hex bin + +hex: $(PRG).hex +bin: $(PRG).bin + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +%.bin: %.elf + $(OBJCOPY) -j .text -j .data -O binary $< $@ + +install: text + uisp -dprog=avr910 -dserial=/dev/ttyS0 -dspeed=115200 -dpart=M8 --erase --upload if=$(PRG).hex +# avrdude -p m8 -c butterfly -b 19200 -P /dev/ttyUSB0 -u -e -U flash:w:$(PRG).hex diff --git a/lipo-charger.c b/lipo-charger.c new file mode 100644 index 0000000..17bec29 --- /dev/null +++ b/lipo-charger.c @@ -0,0 +1,354 @@ +/*************************************************************************** + * Copyright (C) 09/2007 by Olaf Rempel * + * razzor@kopf-tisch.de * + * * + * 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; 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 +#include + +#define F_CPU 8000000 +#include + +#include + +#define BAUDRATE 19200 +#define UART_CALC_BAUDRATE(baudRate) ((uint32_t)(F_CPU) / ((uint32_t)(baudRate)*16) -1) + +/* + * LCD: + * - CS Chip Select + * - RS Register Select + * - RW Read/Write + */ +#define RS PORTB6 +#define CS PORTB7 +#define RW PORTD3 +#define LCD_D4 PORTD4 +#define LCD_D5 PORTD5 +#define LCD_D6 PORTD6 +#define LCD_D7 PORTD7 +#define LCD_DATA_MASK ((1< 1mA + * - PWM - high-active output to buckconverter + */ +#define CH0 PORTC0 +#define CH1 PORTC1 +#define PWM PORTB1 + +#define NOP asm volatile ("nop") + +#define MOD_WAITING 0x00 +#define MOD_CHARGING 0x01 +#define MOD_READY 0x02 + +static void lcd_wait_busy(void) +{ + uint8_t status; + + DDRD &= ~(LCD_DATA_MASK); + PORTD |= (1<> 4); + PORTB &= ~(1< 12450 || current > 16000) + if (pwm > 0x00) + pwm--; + } else { + pwm = 0; + } + + if (pwm > 0) { + OCR1A = pwm; + TCCR1A |= (1< 9V is detected (lipo connected) + */ + if (voltage > 9000) + mode = MOD_CHARGING; + break; + + case MOD_CHARGING: + /* + * end charging if voltage > 12.42V and current < 200mA + */ + if (voltage > 12425 && current < 2000) + mode = MOD_READY; + break; + + case MOD_READY: + /* wait for lipo disconnect */ + if (voltage < 1000) + mode = MOD_WAITING; + break; + } +} + +int main(void) +{ + DDRB = (1<>8) & 0xFF; + UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF); + + /* USART: rx/tx enable, 19200, 8n1 */ + UCSRB = (1<