求助QAQ,一直是这样查了好久看不出来问题。数组没办法初始化。。
#include <reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
sbit LSA = P2^2; //38译码器引脚编号,选择数码管
sbit LSB = P2^3;
sbit LSC = P2^4;
sbit number = P0; //发送转换表(段码)
sbit key1 = P3^1; //开始/暂停
sbit key2 = P3^0; //复位
sbit key3 = P3^2; //
sbit key4 = P3^3; //
u8 code smgduan[17] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};//数码管转换表
u8 LedBuff[8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
} ;//数码管缓冲区
u8 KeySt[4] = {
1, 1, 1, 1
};//按键电平状态储存区
bit StopwatchRunning = 1; //秒表是否运行(确定是否暂停)
bit StopwatchRefresh = 1; //秒表是否要刷新
u8 DecimalPart = 0; //计数小数部分
u16 IntegerPart = 0; //计数整数部分
u8 T0RH = 0; //T0重装高位
u8 T0RL = 0; //T0重装低位
void ConfigTimer0(u16 ms); //配置定时器
void StopwatchDisplay(); //显示函数(数字分割显示)
void KeyDriver(); //检测按键,执行指令(主函数中调用)
void main()
{
EA = 1; //打开总中断
ConfigTimer0(2); //定时器2ms
while(1)
{
if(StopwatchRefresh) //是否更新数码管显示
{
StopwatchRefresh = 0;
StopwatchDisplay();
}
KeyDriver(); //调用按键驱动
}
}
/*函数部分*/
void ConfigTimer0(u16 ms)//配置定时器
{
u32 tmp;
tmp = 12000000 / 12;
tmp = (tmp * ms) / 1000;
tmp = 65536 - tmp;
tmp += 18;
T0RH = (u8)(tmp >> 8);
T0RL = (u8)tmp;
TMOD &= 0XF0; //仅改变后四位(置零)
TMOD |= 0X01;
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
TR0 = 1;
}
void StopwatchDisplay()//显示函数(数字分割显示)
{
signed char i;
u8 buf[4]; //
LedBuff[0] = smgduan[DecimalPart % 10]; //小数转换
LedBuff[1] = smgduan[DecimalPart / 10];
buf[0] = IntegerPart % 10;
buf[1] = (IntegerPart / 10) % 10;
buf[2] = (IntegerPart / 100) % 10;
buf[3] = (IntegerPart / 1000) % 10;
for(i = 3; i >= 1; i --) //整数高位0转换为空
{
if(buf[i] == 0)
{
LedBuff[i + 2] = 0XFF;
}
else break;
}
for( ; i >= 0; i --) //整数转换
{
LedBuff[i + 2] = smgduan[buf[i]];
}
LedBuff[2] &= 0X80; //小数点
}
void StopwatchAction()//秒表的启动与暂停
{
if (StopwatchRunning) //已启动则停止
StopwatchRunning = 0;
else //未启动则启动
StopwatchRunning = 1;
}
void StopwatchReset()//秒表复位
{
StopwatchRunning = 0;
DecimalPart = 0;
IntegerPart = 0;
StopwatchRefresh = 1;
}
void KeyDriver()//检测按键,执行指令(主函数中调用)
{
u8 i;
static u8 backup[4] = {1, 1, 1 , 1};//键值对比样本
for(i = 0; i < 4; i ++)
{
if(backup[i] != KeySt[i])
{
if(backup[i] != 0)
{
if(i == 1)
{
StopwatchReset();
}
else if(i == 2)
{
StopwatchAction();
}
}
backup[i] = KeySt[i]; //刷新备份,作为下次判定样本
}
}
}
void KeyScan()//按键扫描,定时中断调用
{
u8 i;
static u8 keybuf[4] = {
0xFF, 0xFF, 0xFF, 0xFF
};
keybuf[0] = (keybuf[0] << 1) | key1; //键值输入
keybuf[1] = (keybuf[1] << 1) | key2;
keybuf[2] = (keybuf[2] << 1) | key3;
keybuf[3] = (keybuf[3] << 1) | key4;
for(i = 0; i < 4; i ++)
{
if(keybuf[i] == 0X00)
{
KeySt[i] = 0;
}
else if(keybuf == 0XFF)
{
KeySt[i] = 1;
}
}
}
void LedScan()//数码管扫描,定时中断调用
{
static u8 i = 0;
number = 0X00;
//LS = (LS & 0XE3) | (i << 2);
for(i = 0;i < 5;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//显示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//显示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//显示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//显示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//显示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//显示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//显示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//显示第7位
}
}
P0 = LedBuff[i];
//if(i < 5) i ++;
//else i = 0;
}
void StopwatchCount()//秒数统计
{
if(StopwatchRunning)
{
DecimalPart ++;
if(DecimalPart >= 100)
{
DecimalPart = 0;
IntegerPart ++;
if(IntegerPart > 10000)
{
IntegerPart = 0;
}
}
StopwatchRefresh = 1; //提醒刷新
}
}
void InterruptTimer0() interrupt 1 //中断函数
{
static u8 tmr10ms = 0;
TH0 = T0RH;
TL0 = T0RL;
LedScan();
KeyScan();
tmr10ms ++;
if(tmr10ms >= 5) //10ms记一次数
{
tmr10ms = 0;
StopwatchCount();
}
}