|
現(xiàn)在電子式電度表很便宜,買了一個(gè)測量電動(dòng)車的充電量,以此估算電池性能。
為方便觀察充電時(shí)間和充電功率,本人用STC15W408AS做了一個(gè)計(jì)時(shí)計(jì)數(shù)器。利用光敏二極管將電度表的閃燈信號(hào)轉(zhuǎn)化為電脈沖信號(hào),經(jīng)單片機(jī)計(jì)算處理后,用一個(gè)4位數(shù)碼管輪流顯示充電度數(shù)、每閃功率和充電時(shí)長。程序見附件。
單片機(jī)源程序如下:
- #define MAIN_Fosc 12000000 //定義主時(shí)鐘
- //#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負(fù)跳變觸發(fā)
- TMOD=0x01;//定時(shí)器工作于方式1
- TH0=0x3c; //50ms 12Mhz
- TL0=0xb0;
- EA=1; //CPU開中斷總允許
- ET0=1;//開定時(shí)中斷
- EX0=1;//開外部INTO中斷
- TR0=1;//啟動(dòng)定時(shí)
- }
- //========================================================================
- // 函數(shù): void Delayms(unsigned int ms)
- // 描述: STC15延時(shí)函數(shù)。
- // 參數(shù): ms,要延時(shí)的ms數(shù), 這里只支持1~65535ms. 自動(dòng)適應(yīng)主時(shí)鐘.
- // 返回: 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++; //累計(jì)電度表輸出脈沖個(gè)數(shù)
- w0=45000/t1; //每閃功率瓦數(shù) w=.625*3600*20/t1(50毫秒數(shù))
- t1=0; //每閃時(shí)長清0
- }
- void time0() interrupt 1
- {
- TH0=0x3c;
- TL0=0xb0; //50ms,STC15W408AS內(nèi)部時(shí)鐘,12Mhz
- t0++; //累計(jì)時(shí)長
- t1++; //每閃時(shí)長
- 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; //用電度數(shù)=電度表閃燈次數(shù)*.000625kwh*100,(2位小數(shù))
- display_SMG_kwh();
- }
- for(a=0;a<=50;a++)
- {
- // w=kwh*1000*3600/s10; //用電瓦數(shù)=累計(jì)千瓦時(shí)*1000*3600/累計(jì)秒數(shù)
- w=w0;
- display_SMG_w();
- }
- for(a=0;a<=50;a++)
- {
- h=s10/360; //用電小時(shí)數(shù)=累計(jì)秒數(shù)/3600*10,(1位小數(shù))
- display_SMG_h();
- }
- }
- }
復(fù)制代碼
|
|