1、基于单片机的电能表设计#includeLcd12864.h#includeDs1302.h#includeEeprom.h#includeRs485.h#includeAtt7026.hsfr T2MOD = 0x0c9; /定时器控制字定义sfr WDT_CONTR = 0xE1; /看门狗控制字定义/Ds1302模块变量idata uchar time7=0;/保存从Ds1302寄存器读出的数据idata uchar time172;/保存经过转换的数据,形式为十位、个位/Ds1302模块变量 uchar tao = 0;/复费率套数/*rs485 数据定义uchar rs485_stat
2、us = 0;uchar rs485_addr = 0;uchar rs485_data = 0xff;uchar i = 0; /本模块的循环变量uchar time27=0 ;/时间数组uchar charge3=0;/充值数据存储空间uchar control = 99; /初始化控制字uchar mima3=0; /密码是六个0uchar addr6=0x99,0x99,0x99,0x99,0x99,0x99 ; /地址初始化/*rs485 数据定义uchar err_num = 0;uchar p_key = 0;uchar crc_key = 0; /循环控制/定时器的参数uint
3、 timer2_i=0;uint timer0_i=0;uchar month;uchar day;sbit Relay = P12;/报警位sbit B_alarm = P17; sbit led=P16;uchar hour;long power=0;long power1 = 0;float price =0;float yu_e=0 ; void Delay3s(void) /延时3s unsigned char a,b,c; for(c=167;c0;c-) for(b=171;b0;b-) for(a=48;a0;a-); /显示错误信息Disp_Err(uchar dat)/da
4、t 为错误代码 Clear_Lcd(); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIdat);/Rs485 部分数据转换函数, uchar Bcd_To_Int(uchar c) uchar temp ; temp = c/16; temp = temp*10+c%16; return temp;/从DS1302中的寄存器中读取数值 ;私有void Get_Ds1302() uchar i; uchar addr = 0x81;
5、for(i=0;i7;i+) timei = Read_Ds1302(addr); addr = addr +2; /获得转换后的ds1302时间信息 ;私有void Get_time1() uchar i; uchar temp; for(i=0;i7;i+) temp = timei ; time1i1 = temp%16; time1i0 = (timei&0xf0)/16; /*Lcd和Ds1302之间的联合函数*/参数为0,则为实时数据,否则就是存储数据void Disp_Date(uchar i) if(i=0) Get_Ds1302(); Get_time1(); Disp_Zn
6、(0,0,RI_zh); Disp_Zn(0,16,QI_zh); Disp_Ch(0,32,MAOHAO_ch); /日期信息 Disp_Ch(0,40,SHUZI2); Disp_Ch(0,48,SHUZI0); Disp_Ch(0,56,SHUZItime160); Disp_Ch(0,64,SHUZItime161); Disp_Ch(0,72,FENGEFU_ch); Disp_Ch(0,80,SHUZItime140); Disp_Ch(0,88,SHUZItime141); Disp_Ch(0,96,FENGEFU_ch); Disp_Ch(0,104,SHUZItime130)
7、; Disp_Ch(0,112,SHUZItime131);/参数为0,则为实时数据,否则就是存储数据void Disp_Time(i) if(i=0) Get_Ds1302(); Get_time1(); Disp_Zn(2,0,SHI_zh); Disp_Zn(2,16,JIAN_zh); Disp_Ch(2,32,MAOHAO_ch); /时间信息 Disp_Ch(2,40,SHUZItime120); Disp_Ch(2,48,SHUZItime121); Disp_Ch(2,56,FENGEFU_ch); Disp_Ch(2,64,FENGEFU_ch); Disp_Ch(2,72,
8、SHUZItime110); Disp_Ch(2,80,SHUZItime111); Disp_Ch(2,88,FENGEFU_ch); Disp_Ch(2,96,FENGEFU_ch); Disp_Ch(2,104,SHUZItime100); Disp_Ch(2,112,SHUZItime101);/*Lcd和Ds1302之间的联合函数*/*Eeprom和Ds1302之间的联合函数*/保存故障时间Save_Err_Time() uchar i,j; EEPROM_WP = 0; j=E_Read_Byte(0x01);/获得以前的故障次数 j=j%8; Get_Ds1302(); for(
9、i=0;i7;i+) E_Write_Byte(0x31+8*j+i,timei); E_Write_Byte(0x01,j+1); EEPROM_WP= 1 ; /*Eeprom和Ds1302之间的联合函数*/*Eeprom和Lcd12864之间的联合函数*/显示故障日期及时间,i从0-7Disp_Err_Time(uchar i) uchar j; EEPROM_WP=0; for(j=0;j7;j+) timej=E_Read_Byte(0x31+8*i+j); Disp_Date(1);Disp_Time(1); EEPROM_WP=1; /显示余额不足函数Disp_Yu_E_Err(
10、) Clear_Lcd(); Disp_Zn(0,0,YU_zh); Disp_Zn(0,16,E_zh); Disp_Ch(0,32,MAOHAO_ch); Disp_Ch(0,40,SHUZIE_Read_Byte(0x02)/16); Disp_Ch(0,48,SHUZIE_Read_Byte(0x02)%16); Disp_Ch(0,56,SHUZIE_Read_Byte(0x03)/16); Disp_Ch(0,64,SHUZIE_Read_Byte(0x03)%16); Disp_Ch(0,72,POINT_ch); Disp_Ch(0,80,SHUZIE_Read_Byte(0x
11、04)/16); Disp_Ch(0,88,SHUZIE_Read_Byte(0x04)%16); Disp_Zn(0,96,YUAN_zh) ; Disp_Zn(2,0,YU_zh); Disp_Zn(2,16,E_zh); Disp_Zn(2,32,BU_zh); Disp_Zn(2,48,ZU_zh);/*Eeprom和Lcd12864之间的联合函数*/ void Disp_Crc() long temp=220; crc_key=crc_key % 13; switch (crc_key) case 0 : /输出实时信息 Clear_Lcd(); Disp_Date(0); Disp
12、_Time(0); crc_key+; break; case 1: /输出电压信号 Clear_Lcd(); Disp_Zn(0,0,DIAN_zh); Disp_Zn(0,16,YA_zh); /Disp_Ch(0,32,U_ch); / Disp_Ch(0,40,MAOHAO_ch); Disp_Ch(2,0,U_ch); Disp_Ch(2,8,SHUZI1); Disp_Ch(2,16,MAOHAO_ch); / temp=Get_Uirms(r_URmsa); Disp_Ch(2,24,SHUZItemp/100); Disp_Ch(2,32,SHUZI(temp%100)/10)
13、; Disp_Ch(2,40,SHUZItemp%10); Disp_Ch(4,0,U_ch); Disp_Ch(4,8,SHUZI2); Disp_Ch(4,16,MAOHAO_ch); / temp=Get_Uirms(r_URmsb); Disp_Ch(4,24,SHUZItemp/100); Disp_Ch(4,32,SHUZI(temp%100)/10); Disp_Ch(4,40,SHUZItemp%10); Disp_Ch(6,0,U_ch); Disp_Ch(6,8,SHUZI3); Disp_Ch(6,16,MAOHAO_ch); / temp=Get_Uirms(r_URm
14、sc); Disp_Ch(6,24,SHUZItemp/100); Disp_Ch(6,32,SHUZI(temp%100)/10); Disp_Ch(6,40,SHUZItemp%10); crc_key+; break; case 2:/输出电流信息 Clear_Lcd(); Disp_Zn(0,0,DIAN_zh); Disp_Zn(0,16,LIU_zh); Disp_Ch(0,32,I_ch); Disp_Ch(0,40,MAOHAO_ch); /temp=Get_Uirms(0x2b); Disp_Ch(0,48,SHUZItemp/100); Disp_Ch(0,56,SHUZI
15、(temp%100)/10); Disp_Ch(0,64,SHUZItemp%10); Disp_Ch(2,0,I_ch); Disp_Ch(2,8,SHUZI1); Disp_Ch(2,16,MAOHAO_ch); / temp=Get_Uirms(r_IRmsa); Disp_Ch(2,24,SHUZItemp/100); Disp_Ch(2,32,SHUZI(temp%100)/10); Disp_Ch(2,40,SHUZItemp%10); Disp_Ch(4,0,I_ch); Disp_Ch(4,8,SHUZI2); Disp_Ch(4,16,MAOHAO_ch); / temp=G
16、et_Uirms(r_IRmsb); Disp_Ch(4,24,SHUZItemp/100); Disp_Ch(4,32,SHUZI(temp%100)/10); Disp_Ch(4,40,SHUZItemp%10); Disp_Ch(6,0,I_ch); Disp_Ch(6,8,SHUZI3); Disp_Ch(6,16,MAOHAO_ch); / temp=Get_Uirms(r_IRmsc); Disp_Ch(6,24,SHUZItemp/100); Disp_Ch(6,32,SHUZI(temp%100)/10); Disp_Ch(6,40,SHUZItemp%10); crc_key
17、+; break; case 3 : /输出功率信息 Clear_Lcd(); Disp_Zn(0,0,GONG_zh); Disp_Zn(0,16,LV_zh); Disp_Ch(0,32,MAOHAO_ch); / temp=Get_PQS(r_Pt); Disp_Ch(0,40,SHUZItemp/100); Disp_Ch(0,48,SHUZI(temp%100)/10); Disp_Ch(0,56,SHUZItemp%10); crc_key+; break; case 4 : /输出余额信息格式为xxxx.xx元 Clear_Lcd(); Disp_Zn(0,0,YU_zh); D
18、isp_Zn(0,16,E_zh); Disp_Ch(0,32,MAOHAO_ch); Disp_Ch(0,40,SHUZIE_Read_Byte(0x02)/16); Disp_Ch(0,48,SHUZIE_Read_Byte(0x02)%16); Disp_Ch(0,56,SHUZIE_Read_Byte(0x03)/16); Disp_Ch(0,64,SHUZIE_Read_Byte(0x03)%16); Disp_Ch(0,72,POINT_ch); Disp_Ch(0,80,SHUZIE_Read_Byte(0x04)/16); Disp_Ch(0,88,SHUZIE_Read_By
19、te(0x04)%16); Disp_Zn(0,96,YUAN_zh); crc_key+; break; case 5: /输出故障信息1 Clear_Lcd(); Disp_Ch(5,0,SHUZI1); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIE_Read_Byte(0x30); Disp_Err_Time(0); crc_key+; break; case 6: Clear_Lcd(); Disp_Ch(5,0,SHUZ
20、I2); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIE_Read_Byte(0x38); Disp_Err_Time(1); crc_key+; break; case 7: Clear_Lcd(); Disp_Ch(5,0,SHUZI3); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5
21、,72,SHUZIE_Read_Byte(0x40); Disp_Err_Time(2); crc_key+; break; case 8: Clear_Lcd(); Disp_Ch(5,0,SHUZI4); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIE_Read_Byte(0x50); Disp_Err_Time(3); crc_key+; break; case 9: Clear_Lcd(); Disp_Ch(5,0,SHUZ
22、I5); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIE_Read_Byte(0x58); Disp_Err_Time(4); crc_key+; break; case 10: Clear_Lcd(); Disp_Ch(5,0,SHUZI6); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(
23、5,72,SHUZIE_Read_Byte(0x60); Disp_Err_Time(5); crc_key+; break; case 11: Clear_Lcd(); Disp_Ch(5,0,SHUZI7); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIE_Read_Byte(0x68); Disp_Err_Time(6); crc_key+; break; case 12: Clear_Lcd(); Disp_Ch(5,0,S
24、HUZI8); Disp_Ch(5,40,E_ch); Disp_Ch(5,48,R_ch); Disp_Ch(5,56,R_ch); Disp_Ch(5,64,FENGEFU_ch); Disp_Ch(5,72,SHUZIE_Read_Byte(0x70); Disp_Err_Time(7); crc_key+; break; Delay3s(); if(TR2=0) TR2=1;/*外部终中断1*/void C_Key(void) interrupt 2 TR2=0; Disp_Crc(); WDT_CONTR =0x3f; /*外部终中断0*/void Program_Key(void)
25、 interrupt 0 if(p_key +) Clear_Lcd(); Disp_Zn( 5,0, BIAN_zh); Disp_Zn(5,16,CHENG_zh); if(p_key % 2) Rs485_Init(); Disp_Ch(5,32,U_ch); Rs485_Init(); else /Ir_Init(); Disp_Ch(5,32,I_ch); /*/定时器0处理函数,void timer0() interrupt 1 TH0 = 0x3C; TL0 = 0xB0; if(timer0_i+ =60) /3s long temp=0x70; timer0_i=0; /te
26、mp = Read_Spi(0x2c);/读取标志状态寄存器 if(temp &0x00000008) Disp_Err(0); B_alarm = 0; /报警 err_num=err_num%8; E_Write_Byte(30+8*err_num,0); Save_Err_Time(); B_alarm =0; Relay=0;/切闸 TR2=0; ET2 =0; else if(temp&0x000000007)!=0) err_num=err_num%8; Disp_Err(1); E_Write_Byte(30+8*err_num,1); Save_Err_Time();/保存故障 B_alarm =0;/报警 Relay=0;/切闸 TR2=0; /关闭循显 ET2 =0; else if(E_Read_Byte(2)=0&E_Read_Byte(0x03)16)/低于十元只报警不切闸 err_num=err_num%8; Disp_Yu_E_Err(); E_Write_Byte(30+8*err_num,1);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2