📄 e_display.c
字号:
//ICC-AVR application builder : 2006-11-16 18:54:41
// Target : M48
// Crystal: 1.0000Mhz
#include <iom88v.h>
#include <macros.h>
#include <eeprom.h>
#define Vref 1101
//设定充电设定时间3600秒
#define charge_settime 3600
//battery_adc_count 电池电压采样数组
#define battery_adc_count 60
#define V_1000 593
//10.05V
#define V_10 596
//10.12V
#define V_20 600
//10.28
#define V_40 609
//10.44
#define V_60 619
//10.60
#define V_80 628
//11.20
#define V_100 664
//#define Vin_960 (int)680*9600*1024/10680/Vref
//#define Vin_1123 (int)680*11230*1024/10680/Vref
unsigned long d1,d2,d3,d4,d5,d6,d7;
unsigned char battery_stat; //1:停 2:充电 3:放电
unsigned int battery_adc[battery_adc_count],capacity,battery_adc_0s,battery_adc_1s,battery_adc_2s; //电容量
unsigned char watchdog_flag,led,clocks100ms,clocks1s,delay_time;
unsigned int clocks2s,clocks10s,clocks600s,charge_time,res_time;
unsigned int battery_rel,Vin_960,Vin_1123,Vin_h_l,V_discharge,V_charge;
unsigned char battery_adc_ip=0;
unsigned int battery_adc_now,battery_discharge_before,battery_charge_before;
unsigned int battery_discharge_V1,battery_charge_V1;
unsigned char battery_discharge_time,battery_discharge_ip,detect_charge_time;
unsigned char charge_end;
unsigned int battery_temperature;
unsigned char V_up,V_down,battery_dis_flag,j;
unsigned int battery_adc_q,i;
unsigned int battery_stat_6;
unsigned int V_eeprom,V_1120,V_1104,V_1088,V_1072,V_1040,V_1008;
unsigned int calculate_capacity(unsigned int cc) //计算电量
{
unsigned int aa;
if (cc>V_1120)
{
aa=100;
}
else
{
if (cc>V_1104)
{
aa=100;
}
else
{
if (cc>V_1088)
{
aa=(cc-V_1088)*20/(V_1104-V_1088);
aa+=60;
}
else
{
if (cc>V_1072)
{
aa=(cc-V_1072)*20/(V_1088-V_1072);
aa+=40;
}
else
{
if (cc>V_1040)
{
aa=(cc-V_1040)*20/(V_1072-V_1040);
aa+=20;
}
else
{
if (cc>V_1008)
{
aa=(cc-V_1008)*10/(V_1040-V_1008);
aa+=10;
}
else
{
aa=0;
}
}
}
}
}
}
//aa=45;
//WDR(); //this prevents a timout on enabling
//EEPROM_WRITE(0xb0,aa);
return(aa);
}
void battery_temperature_display(void)//超温显示
{
if (clocks2s%400==0)
{
PORTD=0x28;
}
if (clocks2s%400==40)
{
PORTD=0x20;
}
}
void over_loading_display(void) //过载显示
{
if (clocks1s%200==0)
{
PORTD=0x1f;
}
if (clocks1s%200==40)
{
PORTD=0x00;
}
}
void led_display(void)
{
if (battery_dis_flag==1) //停止使用显示
{
if (capacity<10 )
{
if (clocks1s==0) PORTD=0x01;
if (clocks1s==20) PORTD=0x00;
}
else
{
if (clocks2s==0)
{
if (capacity<20)
PORTD=0x01;
else
if (capacity<40 )
PORTD=0x02;
else
if (capacity<60)
PORTD=0x04;
else
if (capacity<80)
PORTD=0x08;
else
PORTD=0x10;//电量大于80%
}
if (clocks2s==40)
PORTD=0x00;
}
// PORTD|=0x20;
}
if (battery_dis_flag==2)
{
if (charge_time<=charge_settime/10 ) //充电总时间小于10%
{
if (clocks1s==0) PORTD=0x01;
if (clocks1s==20) PORTD=0x00;
}
else
if (charge_time<=(charge_settime*2/10)) //充电总时间小于20%
PORTD=0x01;
else
if (charge_time<=(charge_settime*4/10)) //充电总时间小于40%
PORTD=0x03;
else
if (charge_time<=(charge_settime*6/10)) //充电总时间小于60%
PORTD=0x07;
else
if (charge_time<=(charge_settime*8/10)) //充电总时间小于80%
PORTD=0x0f;
else
if (charge_time<=charge_settime) //充电总时间小于100%
PORTD=0x1f;
else
{
PORTD=0x1f;
/*
if (clocks2s==0)
PORTD=0x10;
if (clocks2s==40)
PORTD=0x00;
*/
}
//PORTD|=0x20;
}
if (battery_dis_flag==3)
{
if (capacity<10 )
{
if (clocks1s==0) PORTD=0x01;
if (clocks1s==20) PORTD=0x00;
}
else
if (capacity<20)
PORTD=0x01;
else
if (capacity<40 )
PORTD=0x03;
else
if (capacity<60)
PORTD=0x07;
else
if (capacity<80)
PORTD=0x0f;
else
PORTD=0x1f;//电量大于80%
//PORTD|=0x20;
}
}
//TIMER2 initialize - prescale:32
// WGM: Normal
// desired value: 200Hz
// actual value: 200.321Hz (0.2%)
void timer2_init(void)
{
TCCR2B = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x64; //setup
OCR2A = 0x9C;
OCR2B = 0x00;
TCCR2A = 0x00;
TCCR2B = 0x03; //start
}
#pragma interrupt_handler timer2_ovf_isr:10
void timer2_ovf_isr(void)
{
TCNT2 = 0x64; //reload counter value
watchdog_flag=1;
PORTB^=0x80;//输出校准频率方波
clocks1s++;
clocks2s++;
if (clocks1s>=200) //1秒钟到
{
clocks1s=0;
res_time++;
if (res_time>5400)
{
battery_stat=1; //过时回到停止状态
}
battery_adc[battery_adc_ip]=battery_adc_0s;
battery_adc_ip++;
if (battery_adc_ip>=battery_adc_count)
battery_adc_ip=0;
battery_adc_2s=battery_adc_1s;
battery_adc_1s=battery_adc_0s;
battery_adc_0s=0;
if (battery_dis_flag==2 && charge_time<10000) //充电计时
{
charge_time++;
}
}
if (clocks2s>=400) //2秒钟到
{
clocks2s=0;
}
if (clocks1s%200==0) //采样电压值,每1000ms采样1次
{
battery_rel=ADC&0x3ff; //读ADC
ADMUX = 0xC0; //select adc input 1
ADCSRA|=(1<<ADSC); //启动AD转换
battery_adc_0s=battery_rel;
//***************开机时状态*********************************
if (battery_stat==0) //开机时状态
{
battery_dis_flag=2;
charge_time=0;
battery_stat=3;
//V_up=0;
}
/*
if (battery_stat==20)
{
if (battery_adc_0s>battery_adc_2s)
{
V_up++;
}
else
{
V_up=0;
}
charge_time++;
if (charge_time>=10)
battery_stat=21;
}
if (battery_stat==21)
{
if (V_up>=4) //持续10秒钟上升
{
battery_stat=3; //转入充电状态
}
else
{
battery_stat=1; //转入停止状态
}
}
*/
//*********************************************************
if (battery_stat==1) //停止状态
{
battery_dis_flag=1;
res_time=0;
capacity=calculate_capacity( battery_adc_0s );//计算电量
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -