数码管模块:
1.电压采集界面 显示当前输入电压数据 - - - - 熄灭 输入电压数据显示(mV)
TPIS:输入电压为 4 位数据,单位为 mV,当前输入数码管以 0.5S/次进行闪烁提示。 例如:输入电压 2588mV,则实际电压为 2.58V(结果保留两位有效数字)
2.数据显示界面 显示当前实际电压数据 U 2. 5 8 提示符 熄灭 实际电压数据显示(V)
3.参数设置界面 显示当前设置电压参数 P 3. 0 0 提示符 熄灭 电压参数数据显示(V)
4.计数统计界面 显示当前计数值数据 N 1 2 提示符 高位未启用数码管熄灭
每当前一次实际电压(VAIN3)大于电压设置参数(Vp),重新输入后,实际电 压(VAIN3)小于电压设置参数(Vp)时,计数值加一。
按键模块:
按键定义
1.按键 :0-9 键盘输入如上图所示
2.按键 S5:电压采集按键,按下 S11 后进入电压采集界面,可重新采集一次电 压值
3.按键 S6:界面切换按键,按下 S12 后可在数据显示-参数设置-计数统计三个 界面中循环切换
4、按键 S7:清零参数按键,在电压采集界面按下 S14 可清除当前输入数据,在 计数统计界面可清除当前计数值
5.按键 S11:参数+按键,按下 S15 后,电压参数+0.5V,达到上限 6V 后回到下 限 1V
6.按键 S15:参数-按键,按下 S16 后,电压参数-0.5V,达到下限 1V 后回到上 限 6V
按键说明
1.按键 :键盘按键 只在电压采集界面有效
2.按键 S6 只在非电压采集界面有效
3.在电压采集界面按下 S6 后保存当前输入电压值,并返回电压显示界面(若输 入数据不符合规范性,则清空数据重新输入)
4.按键 S11、S15 只在电压参数设置界面有效
LED 模块:
指示灯 L1:当实际电压小于电压设置参数状态持续超过五秒后,L1 点亮,否则 熄灭 指示灯
L2:当前计数值为奇数时,L2 点亮,否则熄灭 指示灯
L3:连续三次以上无效按键操作时,L3 点亮,触发有效按键操作时熄灭
初始状态: 默认处于电压采集界面
默认电压设置参数为 3.0V
电压采集范围为 0-10V
默认计数值为0
代码范例如下,仅供参考
#include <STC15F2K60S2.H>
sbit h1 = P3^0;
sbit h2 = P3^1;
sbit h3 = P3^2;
sbit h4 = P3^3;
sbit s1 = P4^4;
sbit s2 = P4^2;
sbit s3 = P3^5;
sbit s4 = P3^4;
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned char code SMG_Dot[10]={0x40,0x79,
0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char UI = 0; //0-电压采集界面 1-数据显示界面 2-参数设置界面 3- 计数统计界面
unsigned int input_data = 0;
unsigned char input_Array[4] = {0}; //存储按键输入的电压数据
unsigned char i = 0;
unsigned char num_07s = 0;
unsigned char shan = 0;
unsigned char volt_param = 30;
unsigned char volt_param2 = 0;
unsigned char count_UI = 0;
unsigned int old_data = 0;
unsigned char stat_data = 0;
unsigned char stat_1 = 0;
unsigned char stat_led = 0xff;
unsigned char stat_L1 = 0;
unsigned int num_5s = 0;
unsigned char error_key = 0;
void SelectHC573(unsigned char channel,unsigned char dat);
void Init_timer0()
{
TMOD = 0x01;
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void Sevice_timer0() interrupt 1
{
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
if(UI == 0)
{
num_07s++;
if(num_07s == 70)
{
if(shan == 0)
{
shan = 1;
}
else
{
shan = 0;
}
num_07s = 0;
}
}
if(stat_L1 == 1)
{
num_5s++;
if(num_5s == 500)
{
stat_led = stat_led & 0xfe;
SelectHC573(4,stat_led);
num_5s = 0;
}
}
}
void SelectHC573(unsigned char channel,unsigned char dat)
{
P2 = (P2 & 0x1f) | 0x00;
P0 = dat;
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
case 0:
P2 = (P2 & 0x1f) | 0x00;
break;
}
P2 = (P2 & 0x1f) | 0x00;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char value)
{
SelectHC573(6,0x01 << pos);
SelectHC573(7,value);
DelaySMG(500);
SelectHC573(6,0x01 << pos);
SelectHC573(7,0xff);
}
void DisplaySMG_ALL()
{
SelectHC573(6,0xff);
SelectHC573(7,0xff);
}
void DisplaySMG_Info()
{
switch(UI)
{
case 0:
if(i == 0)
{
if(shan == 0)
{
DisplaySMG_Bit(4,SMG_NoDot[input_Array[0]]);
}
}
else if(i == 1)
{
if(shan == 0)
{
DisplaySMG_Bit(5,SMG_NoDot[input_Array[1]]);
}
DisplaySMG_Bit(4,SMG_NoDot[input_Array[0]]);
}
else if(i == 2)
{
if(shan == 0)
{
DisplaySMG_Bit(6,SMG_NoDot[input_Array[2]]);
}
DisplaySMG_Bit(4,SMG_NoDot[input_Array[0]]);
DisplaySMG_Bit(5,SMG_NoDot[input_Array[1]]);
}
else if(i == 3)
{
if(shan == 0)
{
DisplaySMG_Bit(7,SMG_NoDot[input_Array[3]]);
}
DisplaySMG_Bit(4,SMG_NoDot[input_Array[0]]);
DisplaySMG_Bit(6,SMG_NoDot[input_Array[2]]);
DisplaySMG_Bit(5,SMG_NoDot[input_Array[1]]);
}
else if(i == 4)
{
DisplaySMG_Bit(7,SMG_NoDot[input_Array[3]]);
DisplaySMG_Bit(4,SMG_NoDot[input_Array[0]]);
DisplaySMG_Bit(6,SMG_NoDot[input_Array[2]]);
DisplaySMG_Bit(5,SMG_NoDot[input_Array[1]]);
}
break;
case 1:
DisplaySMG_Bit(0,0xc1);
DisplaySMG_Bit(5,SMG_Dot[input_data / 1000]);
DisplaySMG_Bit(6,SMG_NoDot[input_data / 100 % 10]);
DisplaySMG_Bit(7,SMG_NoDot[input_data / 10 % 10]);
break;
case 2:
DisplaySMG_Bit(0,0x8c);
DisplaySMG_Bit(5,SMG_Dot[volt_param /10]);
DisplaySMG_Bit(6,SMG_NoDot[volt_param % 10]);
DisplaySMG_Bit(7,SMG_NoDot[0]);
break;
case 3:
DisplaySMG_Bit(0,0xc8);
if(count_UI > 9)
{
DisplaySMG_Bit(6,SMG_NoDot[count_UI / 10]);
}
else
{
DisplaySMG_Bit(7,SMG_NoDot[count_UI]);
}
break;
}
}
void scan_key()
{
s1 = 0;
s2 = s3 = s4 = h1 = h2 = h3 = h4 = 1;
if(h1 == 0)//s7
{
Delay10ms();
if(h1 == 0)
{
if(UI == 2)
{
volt_param = 0;
error_key = 0;
}
if(UI == 0)
{
i = 0;
input_Array[0] = 0;
input_Array[1] = 0;
input_Array[2] = 0;
input_Array[3] = 0;
error_key = 0;
}
if(UI == 3)
{
count_UI = 0;
error_key = 0;
}
if(UI == 1)
{
error_key++;
}
while(h1 == 0)
{
DisplaySMG_Info();
}
}
}
if(h2 == 0)//s6
{
Delay10ms();
if(h2 == 0)
{
error_key = 0;
if(UI == 0)
{
UI = 1;
input_data = input_Array[0] * 1000 + input_Array[1] * 100 + input_Array[2] * 10 + input_Array[3];
if(stat_data == 0)
{
old_data = input_data;
stat_data = 1;
}
old_data = input_data;
if(old_data > (volt_param * 100))
{
stat_1 = 1;
}
else if(stat_1 == 1)
{
if(old_data < (volt_param * 100))
{
count_UI++;
stat_1 = 0;
}
}
}
else if(UI == 1)
{
UI = 2;
}
else if(UI == 2)
{
UI = 3;
}
else if(UI == 3)
{
UI = 1;
}
while(h2 == 0)
{
DisplaySMG_Info();
}
}
}
if(h3 == 0)//s5
{
Delay10ms();
if(h3 == 0)
{
error_key = 0;
UI = 0;
i = 0;
input_Array[0] = 0;
input_Array[1] = 0;
input_Array[2] = 0;
input_Array[3] = 0;
while(h3 == 0)
{
DisplaySMG_Info();
}
}
}
if(h4 == 0)//s4
{
Delay10ms();
if(h4 == 0)
{
if(UI == 0)
{
if(i < 4)
{
input_Array[i] = 0;
i = i + 1;
}
error_key = 0;
}
else
{
error_key++;
}
while(h4 == 0)
{
DisplaySMG_Info();
}
}
}
s2 = 0;
s1 = s3 = s4 = h1 = h2 = h3 = h4 = 1;
if(h1 == 0)//s11
{
Delay10ms();
if(h1 == 0)
{
if(UI == 2)
{
error_key = 0;
if(volt_param == 60)
{
volt_param = 10;
}
else
{
volt_param = volt_param + 5;
}
}
else
{
error_key++;
}
while(h1 == 0)
{
DisplaySMG_Info();
}
}
}
if(h2 == 0)//s10
{
Delay10ms();
if(h2 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 7;
i = i + 1;
}
}
else
{
error_key++;
}
while(h2 == 0)
{
DisplaySMG_Info();
}
}
}
if(h3 == 0)//s9
{
Delay10ms();
if(h3 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 4;
i = i + 1;
}
}
else
{
error_key++;
}
while(h3 == 0)
{
DisplaySMG_Info();
}
}
}
if(h4 == 0)//s8
{
Delay10ms();
if(h4 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 1;
i = i + 1;
}
}
else
{
error_key++;
}
while(h4 == 0)
{
DisplaySMG_Info();
}
}
}
s3 = 0;
s2 = s1 = s4 = h1 = h2 = h3 = h4 = 1;
if(h1 == 0)//s15
{
Delay10ms();
if(h1 == 0)
{
if(UI == 2)
{
error_key = 0;
if(volt_param == 10)
{
volt_param = 60;
}
else
{
volt_param = volt_param - 5;
}
}
else
{
error_key++;
}
while(h1 == 0)
{
DisplaySMG_Info();
}
}
}
if(h2 == 0)//s14
{
Delay10ms();
if(h2 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 8;
i = i + 1;
}
}
else
{
error_key++;
}
while(h2 == 0)
{
DisplaySMG_Info();
}
}
}
if(h3 == 0)//s13
{
Delay10ms();
if(h3 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 5;
i = i + 1;
}
}
else
{
error_key++;
}
while(h3 == 0)
{
DisplaySMG_Info();
}
}
}
if(h4 == 0)//s12
{
Delay10ms();
if(h4 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 2;
i = i + 1;
}
}
else
{
error_key++;
}
while(h4 == 0)
{
DisplaySMG_Info();
}
}
}
s4 = 0;
s2 = s3 = s1 = h1 = h2 = h3 = h4 = 1;
if(h1 == 0)//s19
{
Delay10ms();
if(h1 == 0)
{
while(h1 == 0)
{
DisplaySMG_Info();
}
}
}
if(h2 == 0)//s18
{
Delay10ms();
if(h2 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 9;
i = i + 1;
}
}
else
{
error_key++;
}
while(h2 == 0)
{
DisplaySMG_Info();
}
}
}
if(h3 == 0)//s17
{
Delay10ms();
if(h3 == 0)
{
if(UI == 0)
{
error_key = 0;
if(i < 4)
{
input_Array[i] = 6;
i = i + 1;
}
}
else
{
error_key++;
}
while(h3 == 0)
{
DisplaySMG_Info();
}
}
}
if(h4 == 0)//s16
{
Delay10ms();
if(h4 == 0)
{
if(UI == 0)
{
error_key= 0;
if(i < 4)
{
input_Array[i] = 3;
i = i + 1;
}
}
else
{
error_key++;
}
while(h4 == 0)
{
DisplaySMG_Info();
}
}
}
}
void led_deal()
{
if(input_data < (volt_param * 100))
{
stat_L1 = 1;
}
else
{
stat_led = stat_led | 0x01;
SelectHC573(4,stat_led);
}
if((count_UI % 2) != 0)
{
stat_led = stat_led & 0xfd;
SelectHC573(4,stat_led);
}
else
{
stat_led = stat_led | 0x02;
SelectHC573(4,stat_led);
}
if(error_key > 3)
{
stat_led = stat_led & 0xfb;
SelectHC573(4,stat_led);
}
else
{
stat_led = stat_led | 0x04;
SelectHC573(4,stat_led);
}
}
void main()
{
SelectHC573(4,0xff);
SelectHC573(5,0x00);
DisplaySMG_ALL();
led_deal();
Init_timer0();
while(1)
{
led_deal();
scan_key();
DisplaySMG_Info();
}
}