------------------------------------------------------------------------------------------------
本程序有BUG,在电压测量模式与时间查询模式下,按键使用不灵敏;在电压测量模式下数码管会闪。
使用的iic协议、ds1302协议在前文中有
------------------------------------------------------------------------------------------------
0、很多很多的定义
#include
#include "init.h"
#include "iic1.h"
#include "ds13021.h"
#define uchar unsigned char
#define VH_ADDR1 0x00
#define VH_ADDR2 0x01
#define VL_ADDR1 0x02
#define VL_ADDR2 0x03
#define H_ADDRH 0x04
#define M_ADDRH 0x05
#define S_ADDRH 0x06
#define H_ADDRL 0x07
#define M_ADDRL 0x08
#define S_ADDRL 0x09
void Timer0Init(void);
void Timer1Init(void); //1微秒@11.0592MHz
void pro_time();
void pro_set_time();
void pro_vo();
void pro_set_vo();
void pro_fr();
void pro_sea();
uchar vlh,vlm,vls,vhh,vhm,vhs;
uchar returnflag = 0;
uchar mode = 0;
uchar key = 0,key1 = 0;
uchar isrcount = 0;
uchar flag1s = 0;
int vh,vl;
uchar vh1,vh2,vl1,vl2;
uchar time1flag,flagS;
uchar flagss;
1、主程序
void main()
{
close_buzz();
Timer0Init();
init_1302();
init_dis();
Timer1Init(); //1微秒@11.0592MHz
vhh = read_eeprom(H_ADDRH);
vhm = read_eeprom(M_ADDRH);
vhs = read_eeprom(S_ADDRH);
vlh = read_eeprom(H_ADDRL);
vlm = read_eeprom(M_ADDRL);
vls = read_eeprom(S_ADDRL);
while(1)
{
display();
key = keyscan();
if(key != 0)
{
switch(key)
{
case 7: mode = 1; break; //time
case 6: mode = 2; break; //vo
case 5: mode = 3; break; //fr
case 9: mode = 4; break; //sea
}
}
if(mode == 1)
{
pro_time();
}
if(mode == 2)
{
pro_vo();
}
if(mode == 3)
{
pro_fr();
}
if(mode == 4)
{
pro_sea();
}
}
}
void pro_time()
{
dis[2] = 10;
dis[5] = 10;
get_time(); //每一秒读一次时间
dis[0] = cur_time[0]/10;
dis[1] = cur_time[0]%10;
dis[3] = cur_time[1]/10;
dis[4] = cur_time[1]%10;
dis[6] = cur_time[2]/10;
dis[7] = cur_time[2]%10;
display();
key = keyscan();
if(key != 0)
{
switch(key)
{
case 4:pro_set_time();break;//时钟调整
}
}
}
void pro_set_time()
{
uchar time_mode = 0;
while(1)
{
if(time_mode == 0)
{
if(flag1s == 1)
{
dis[0] = init_time[0]/10;
dis[1] = init_time[0]%10;
}
else
{
dis[0] = 11;
dis[1] = 11;
}
dis[3] = init_time[1]/10;
dis[4] = init_time[1]%10;
dis[6] = init_time[2]/10;
dis[7] = init_time[2]%10;
}
if(time_mode == 1)
{
if(flag1s == 1)
{
dis[3] = init_time[1]/10;
dis[4] = init_time[1]%10;
}
else
{
dis[3] = 11;
dis[4] = 11;
}
dis[0] = init_time[0]/10;
dis[1] = init_time[0]%10;
dis[6] = init_time[2]/10;
dis[7] = init_time[2]%10;
}
if(time_mode == 2)
{
if(flag1s == 1)
{
dis[6] = init_time[2]/10;
dis[7] = init_time[2]%10;
}
else
{
dis[6] = 11;
dis[7] = 11;
}
dis[0] = init_time[0]/10;
dis[1] = init_time[0]%10;
dis[3] = init_time[1]/10;
dis[4] = init_time[1]%10;
}
display();
key = keyscan();
if(key != 0)
{
switch(key)
{
case 4: {if(time_mode == 0){time_mode = 1;}
else if(time_mode == 1){time_mode = 2;}
else if(time_mode == 2){time_mode = 0;}
break;
}//选择
case 11:{if(time_mode == 0){init_time[0]++;if(init_time[0]>23){init_time[0]=0;}}
if(time_mode == 1){init_time[1]++;if(init_time[0]>59){init_time[0]=0;}}
if(time_mode == 2){init_time[2]++;if(init_time[0]>59){init_time[0]=0;}}
break;//+
}
case 10:{if(time_mode == 0){init_time[0]--;if(init_time[0]<0){init_time[0]=23;}}
if(time_mode == 1){init_time[1]--;if(init_time[0]<0){init_time[0]=59;}}
if(time_mode == 2){init_time[2]--;if(init_time[0]<0){init_time[0]=59;}}
break;//-
}
case 7: returnflag = 1; break;
default: key = 0; break;
}
}
if(returnflag == 1)
{
returnflag = 0;
init_1302();
break;
}
}
}
uchar vo111;
int temp111;
void pro_vo()
{
dis[0] = 10;
dis[1] = 1;
dis[2] = 10;
dis[3] = 11;
display();
if(flagss == 1)
{
flagss = 0;
init_ad();
vo111 = read_ad();
temp111 = vo111/255.0*5000;
}
if(temp111 >= vh)
{
get_time();
write_eeprom(H_ADDRH,cur_time[0]);
write_eeprom(M_ADDRH,cur_time[1]);
write_eeprom(S_ADDRH,cur_time[2]);
}
if(temp111 <= vl)
{
get_time();
write_eeprom(H_ADDRL,cur_time[0]);
write_eeprom(M_ADDRL,cur_time[1]);
write_eeprom(S_ADDRL,cur_time[2]);
}
dis[4] = temp111/1000;
dis[5] = temp111%1000/100;
dis[6] = temp111%100/10;
dis[7] = temp111%10;
display();
key = keyscan();
if(key != 0)
{
switch(key)
{
case 4:pro_set_vo();break;//VO调整
}
}
}
void pro_set_vo()
{
uchar vo_mode = 0;
//du vh,vl
vh1 = read_eeprom(VH_ADDR1);
vh2 = read_eeprom(VH_ADDR2);
vl1 = read_eeprom(VL_ADDR1);
vl2 = read_eeprom(VL_ADDR2);
vh = vh1*256+vh2;
vl = vl1*256+vl2;
while(1)
{
if(vo_mode == 0)
{
if(flag1s == 1)
{
dis[0] = vh/1000;
dis[1] = vh%1000/100;
dis[2] = vh%100/10;
dis[3] = vh%10;
}
else
{
dis[0] = 11;
dis[1] = 11;
dis[2] = 11;
dis[3] = 11;
}
dis[4] = vl/1000;
dis[5] = vl%1000/100;
dis[6] = vl%100/10;
dis[7] = vl%10;
}
if(vo_mode == 1)
{
if(flag1s == 1)
{
dis[4] = vl/1000;
dis[5] = vl%1000/100;
dis[6] = vl%100/10;
dis[7] = vl%10;
}
else
{
dis[4] = 11;
dis[5] = 11;
dis[6] = 11;
dis[7] = 11;
}
dis[0] = vh/1000;
dis[1] = vh%1000/100;
dis[2] = vh%100/10;
dis[3] = vh%10;
}
display();
key = keyscan();
if(key != 0)
{
switch(key)
{
case 4: {if(vo_mode == 0){vo_mode = 1;}
else if(vo_mode == 1){vo_mode = 0;}
break;
}//选择
case 11:{if(vo_mode == 0){vh=vh+500;if(vh>5000){vh=0;}}
if(vo_mode == 1){vl=vl+500;if(vl>5000){vl=0;}}
break;//+
}
case 10:{if(vo_mode == 0){vh=vh-500;if(vh<0){vh=5000;}}
if(vo_mode == 1){vl=vl-500;if(vl<0){vl=5000;}}
break;//-
}
case 6: returnflag = 1; break;
default: key = 0; break;
}
}
if(returnflag == 1)
{
returnflag = 0;
write_eeprom(VH_ADDR1,vh/256);
write_eeprom(VH_ADDR2,vh%256);
write_eeprom(VL_ADDR1,vl/256);
write_eeprom(VL_ADDR2,vl%256);
break;
}
}
}
4、测量频率模式
uchar ft_mode = 0; //0 fr,1 time
void pro_fr()
{
float fr,time;
dis[0] = 10;
// dis[1] = 2;
dis[2] = 10;
display();
TR0 = 1; //start t1;
key = keyscan();
if(key != 0)
{
switch(key)
{
case 4:
{
if(ft_mode == 0)
{ft_mode = 1;}
else
{
if(ft_mode == 1){ft_mode = 0;}
}
break;
}//time fr调整
case 7: mode = 1; break; //time
case 6: mode = 2; break; //vo
case 5: mode = 3; break; //fr
case 9: mode = 4; break; //sea
}
}
if(ft_mode == 0)
{
dis[1] = 2; //test
//fr
if(flagS == 1)
{
TR0 = 0;
flagS = 0;
fr = time1flag*65536+TH0*256+TL0;
TH0 = 0;
TL0 = 0;
time1flag = 0;
dis[3]=((int)fr)/10000;
dis[4]=((int)fr)%10000/1000;
dis[5]=((int)fr)%1000/100;
dis[6]=((int)fr)%100/10;
dis[7]=((int)fr)%10;
}
}
if(ft_mode == 1)
{
dis[1] = 3; //test
//TIME
if(flagS == 1)
{
TR0 = 0;
flagS = 0;
fr = time1flag*65536+TH0*256+TL0;
TH0 = 0;
TL0 = 0;
time1flag = 0;
time = 1.0/fr * 1000000.0;
dis[3]=((int)time)/10000;
dis[4]=((int)time)%10000/1000;
dis[5]=((int)time)%1000/100;
dis[6]=((int)time)%100/10;
dis[7]=((int)time)%10;
}
}
}
uchar sea_mode = 0; //0 fr,1 time
void pro_sea()
{
TR0 = 0;
display();
key = keyscan();
if(key != 0)
{
switch(key)
{
case 4:{
if(sea_mode == 0){sea_mode = 1;}
else {if(sea_mode == 1){sea_mode = 2;}
else {if(sea_mode == 2){sea_mode = 3;}
else {if(sea_mode == 3){sea_mode = 0;}
}
}
}
break;}//time fr调整
case 7: mode = 1; break; //time
case 6: mode = 2; break; //vo
case 5: mode = 3; break; //fr
case 9: mode = 4; break; //sea
}
}
if(sea_mode == 0)
{
//VL
dis[0] = 11;
dis[1] = 11;
dis[2] = 11;
dis[3] = 11;
dis[4] = 11;
dis[5] = 11;
dis[6] = 0;
dis[7] = 0;
}
if(sea_mode == 1)
{
//VL
dis[0] = vlh/10;
dis[1] = vlh%10;
dis[2] = 10;
dis[3] = vlm/10;
dis[4] = vlm%10;
dis[5] = 10;
dis[6] = vls/10;
dis[7] = vls%10;
}
if(sea_mode == 2)
{
dis[0] = 11;
dis[1] = 11;
dis[2] = 11;
dis[3] = 11;
dis[4] = 11;
dis[5] = 11;
dis[6] = 0;
dis[7] = 1;
}
if(sea_mode == 3)
{
dis[0] = vhh/10;
dis[1] = vhh%10;
dis[2] = 10;
dis[3] = vhm/10;
dis[4] = vhm%10;
dis[5] = 10;
dis[6] = vhs/10;
dis[7] = vhs%10;
}
}
void Timer1Init(void) //20毫秒@11.0592MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TMOD |= 0x10; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0xB8; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
void Timer0Init(void)
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xf0;
TMOD |= 0x05; //方式一,count模式
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF1标志
TR0 = 0;
ET0 = 1;
}
void t1_isr() interrupt 3
{
TL1 = 0x00; //设置定时初值
TH1 = 0xB8; //设置定时初值
isrcount++;
if(isrcount >= 25)
{
flagss = 1;
}
if(isrcount >= 50)
{
isrcount = 0;
flagS = 1;
if(flag1s == 1)
{
flag1s = 0;
}
else
{
flag1s = 1;
}
}
}
void t0_isr() interrupt 1
{
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
time1flag++;
}