功能:手机充电,1.0V到4.5V电池通吃
可以电池过放保护,也可以取消保护榨干干电池。
还可以测量电池电压并显示。
不计成本,效果绝非地摊上能买到的可比。
(原文件名:sch.jpg) 引用图片源码,读以下就可以知道操作方法了
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
?Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2009-4-3
Author : user
Company : Dell
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 4.800000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 24
REF=1.07V
*****************************************************/
#include <tiny13.h>
#include <delay.h>
#define DEBUG 0
#define OUT_ENABLE PORTB.0
#define KEY_CHECK PINB.2
#define BOOST_ENABLE PORTB.3
#define OUTPUT_VOLTAGE_SELECT PORTB.1
#if !DEBUG
#define LED_OUT PORTB.5
#define LED_ON 0
#define LED_OFF 1
#endif
#define WDCE 4
#define WDE 3
#define ADEN ADCSRA.7
#define ADSC ADCSRA.6
volatile unsigned char temp_v[16];
volatile unsigned char Count;
volatile unsigned char Real_voltage;
volatile unsigned char System_states;
unsigned char Shut_up_voltage;
unsigned int Worked_timer_count;
bit adc_busy;
bit enable_lower_voltage_protect;
unsigned char Lower_voltage_checked_times;
interrupt [PCINT0] void pin_change_isr(void)
{
unsigned char i=0;
while(KEY_CHECK==0)
{
i++;
delay_ms(10);
if(i==250)//if(KEY_CHECK==1||i==250)
{
break;
}
}
if(i>=200) //按钮按下时间大于2秒 ,启动或关闭输出
{
if(System_states!=2)
{
System_states=2;
Worked_timer_count=0;
}
else
{
System_states=0;
}
}
else //按钮按下时间小于2秒 ,启动或关闭放电低电压保护,并显示空载电压值及低电压保护值
{
if(i>=5)
{
if(System_states!=2)
{
System_states=1;
enable_lower_voltage_protect=~enable_lower_voltage_protect;
}
}
}
}
// Watchdog timeout interrupt service routine
//interrupt [WDT] void wdt_timeout_isr(void)
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
if(adc_busy==0)
{
ADSC=1;
adc_busy=1;
}
}
// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
unsigned char i;
unsigned int temp;
temp_v[Count]=ADCH;
Count++;
if(Count>15)
{
Worked_timer_count++;
Count=0;
temp=0;
for(i=0;i<16;i++)
{
temp+=(unsigned int)temp_v
;
}
Real_voltage=(unsigned char)(((unsigned long )temp*1531)/100000);
if(Real_voltage<Shut_up_voltage||Worked_timer_count>=22500) //电池电压低或约一小时后关闭输出
{
Lower_voltage_checked_times++;
if(Lower_voltage_checked_times>3) //连续三次采样都低于保护值
{
System_states=0;
BOOST_ENABLE =0;
OUT_ENABLE=1;
#if !DEBUG
LED_OUT=LED_OFF;
#endif
}
}
else
{
Lower_voltage_checked_times=0;
}
}
adc_busy=0;
}
//************************************************
//显示数值,长闪次数为十位数值,短闪次数为个位数值
//************************************************
void display_value(unsigned char value)
{
#if !DEBUG
unsigned char i;
unsigned char temp;
temp=value/10;
for(i=0;i<temp;i++)
{
LED_OUT=LED_OFF;
delay_ms(200);
LED_OUT=LED_ON;
delay_ms(500);
}
temp=value%10;
for(i=0;i<temp;i++)
{
LED_OUT=LED_OFF;
delay_ms(500);
LED_OUT=LED_ON;
delay_ms(100);
}
LED_OUT=LED_OFF;
#endif
}
void main(void)
{
// Input/Output Ports initialization
// Port B initialization
// Func5=Out Func4=In Func3=Out Func2=In Func1=Out Func0=Out
// State5=1 State4=T State3=0 State2=P State1=0 State0=1
PORTB=0x25;
DDRB=0x2B;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
// actual value: 10.027mSec (-0.3%)
OCR0A = 0xBB;
TCCR0A = 0x02;
TCCR0B = 0x04; //start timer
TIMSK0 = 0x04; //timer interrupt sources
// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: On
GIMSK=0x20;
MCUCR=0x00;
PCMSK=0x04;
GIFR=0x20;
DIDR0|=0x10;
ADMUX=0x62;
//ADMUX=0x22;
ADCSRA=0x8D;
// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
#asm("sei")
System_states=1;
enable_lower_voltage_protect=0;
while (1)
{
switch(System_states)
{
case 1:
{
Count=0;
OUTPUT_VOLTAGE_SELECT=0;
BOOST_ENABLE = 1;
OUT_ENABLE=1;
#if !DEBUG
LED_OUT=LED_ON;
#endif
delay_ms(1000);
display_value(Real_voltage); //显示电池空载电压(实际带载约10mA)
if(!enable_lower_voltage_protect)
{
if(Real_voltage>=31) //大于3.1V,低电压保护值设为2.8V(带载时),用于锂电池过放保护
{
Shut_up_voltage=28;
}
else
{
if(Real_voltage>=20) //大于2.0V,低电压保护值设为1.0V(带载时), 用于2x镍电池过放保护
{
Shut_up_voltage=10;
}
else
{
Shut_up_voltage=4; //小于2.0V,低电压保护值设为0.4V(带载时), 用于1x镍电池过放保护
}
}
}
else
{
Shut_up_voltage=0; //如果不启用过放保护,低电压保护值设为0.0V,用于非可充电池
}
delay_ms(1000);
display_value(Shut_up_voltage); //显示低电压保护值
System_states=0;
}break;
case 2:
{
OUTPUT_VOLTAGE_SELECT=1;
BOOST_ENABLE =1;
OUT_ENABLE=0;
display_value(Real_voltage); //显示带载电池电压
delay_ms(3000);
}break;
default: //未启动输出,进入休眠
{
Worked_timer_count=0;
BOOST_ENABLE =0;
OUT_ENABLE=1;
#if !DEBUG
LED_OUT=LED_OFF;
#endif
#pragma optsize-
MCUCR=0x20;
#asm("sleep");
#asm("NOP");
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
}break;
}
};
}
文章评论(0条评论)
登录后参与讨论