现在电子式电度表很便宜,买了一个测量电动车的充电量,以此估算电池性能。
为方便观察充电时间和充电功率,本人用STC15W408AS做了一个计时计数器。利用光敏二极管将电度表的闪灯信号转化为电脉冲信号,经单片机计算处理后,用一个4位数码管轮流显示充电度数、每闪功率和充电时长。程序见附件。
单片机源程序如下:
- #define MAIN_Fosc 12000000 //定义主时钟
- //#include<reg52.h>
- #include "STC15Fxxxx.H"
- #define uchar unsigned char
- #define uint unsigned int
- sbit COUNT_IN=P3^2;
- unsigned code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- #define DataPort P1;
- sbit led1=P3^4;
- sbit led2=P3^5;
- sbit led3=P3^6;
- sbit led4=P3^7;
- unsigned long count; //max 4294967295*.000625Kwh=2684354Kws
- unsigned long kwh; //max 4294967295*.000625Kwh=2684354Kws
- unsigned int t0,t1,w0,w,s10; //t0,t1 max 65535*.05=3276s, w max 45000w, s10 max 65535*10s/3600s=182h,7.58day
- unsigned char h; //max 65535*10s/3600s=182h,7.58day
- bit flag;
- void init()
- {
- IT0=1; //INT0负跳变触发
- TMOD=0x01;//定时器工作于方式1
- TH0=0x3c; //50ms 12Mhz
- TL0=0xb0;
- EA=1; //CPU开中断总允许
- ET0=1;//开定时中断
- EX0=1;//开外部INTO中断
- TR0=1;//启动定时
- }
- //========================================================================
- // 函数: void Delayms(unsigned int ms)
- // 描述: STC15延时函数。
- // 参数: ms,要延时的ms数, 这里只支持1~65535ms. 自动适应主时钟.
- // 返回: none.
- // 版本: VER1.0
- // 日期: 2013-4-1
- // 备注:
- //========================================================================
- void Delayms(unsigned int ms)
- {
- unsigned int i;
- do{
- i = MAIN_Fosc / 13000;
- while(--i) ; //14T per loop
- }while(--ms);
- }
- //**********************************************
- void EXINT0() interrupt 0
- {
- // kwh=kwh+.000625;
- count++; //累计电度表输出脉冲个数
- w0=45000/t1; //每闪功率瓦数 w=.625*3600*20/t1(50毫秒数)
- t1=0; //每闪时长清0
- }
- void time0() interrupt 1
- {
- TH0=0x3c;
- TL0=0xb0; //50ms,STC15W408AS内部时钟,12Mhz
- t0++; //累计时长
- t1++; //每闪时长
- if(t0>199) //10s 200*.05s=10s
- {
- t0=0;
- s10=s10+1; //max 65535*10s/3600s=182h,7.58day
- }
- if(t1>65534) //max 65534*.05=3276s
- {
- t1=65534;
- }
- }
- void display_SMG_kwh(void)
- {
- P1=table[kwh/1000];
- led1= 0;
- Delayms(5);
- led1 = 1;
- P1=table[kwh%1000/100]&0x7f;
- led2 = 0;
- Delayms(5);
- led2 = 1;
- P1=table[kwh%100/10];
- led3 = 0;
- Delayms(5);
- led3 = 1;
- P1=table[kwh%10];
- led4 = 0;
- Delayms(5);
- led4 = 1;
- }
- void display_SMG_w(void)
- {
- P1=table[w%10000/1000];
- led1= 0;
- Delayms(5);
- led1 = 1;
- P1=(table[w%1000/100]);
- led2 = 0;
- Delayms(5);
- led2 = 1;
- P1=table[w%100/10];
- led3 = 0;
- Delayms(5);
- led3 = 1;
- P1=table[w%10];
- led4 = 0;
- Delayms(5);
- led4 = 1;
- }
- void display_SMG_h(void)
- {
- P1=0x8b;
- // P1=table[h/1000];
- led1= 0;
- Delayms(5);
- led1 = 1;
- P1=table[h%1000/100];
- led2 = 0;
- Delayms(5);
- led2 = 1;
- P1=table[h%100/10]&0x7f;
- led3 = 0;
- Delayms(5);
- led3 = 1;
- P1=table[h%10];
- led4 = 0;
- Delayms(5);
- led4 = 1;
- }
- void main()
- {
- unsigned char a;
- init();
- while(1)
- {
- for(a=0;a<=50;a++)
- {
- kwh=count*.0625; //用电度数=电度表闪灯次数*.000625kwh*100,(2位小数)
- display_SMG_kwh();
- }
- for(a=0;a<=50;a++)
- {
- // w=kwh*1000*3600/s10; //用电瓦数=累计千瓦时*1000*3600/累计秒数
- w=w0;
- display_SMG_w();
- }
- for(a=0;a<=50;a++)
- {
- h=s10/360; //用电小时数=累计秒数/3600*10,(1位小数)
- display_SMG_h();
- }
- }
- }
复制代码
|