上键流水灯
左键数码管倒计时
右键点阵
下键全部显示
上键控制流水灯完成
左键控制数码管完成
右键点阵----------------待完成—按键切换已经完成,后续也就不想当缝合怪了,开启通信模块的学习
下键全部显示----------------待完成—按键切换已经完成,后续也就不想当缝合怪了,开启通信模块的学习
突然想到定义4个全局变量四种状态
#include
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
unsigned char code image[] = { //图片的字模表
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x99,0x99,0x99,0x99,0x99,0x81,0xC3,0xFF,
0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,0xFF,
0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = { //数码管+点阵+流水灯显示缓冲区
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF
};
unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
{ 0x31, 0x32, 0x33, 0x26 }, //数字键1、数字键2、数字键3、向上键
{ 0x34, 0x35, 0x36, 0x25 }, //数字键4、数字键5、数字键6、向左键
{ 0x37, 0x38, 0x39, 0x28 }, //数字键7、数字键8、数字键9、向下键
{ 0x30, 0x1B, 0x0D, 0x27 } //数字键0、ESC键、 回车键、 向右键
};
unsigned char KeySta[4][4] = { //全部矩阵按键的当前状态
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
unsigned char i = 0; //动态扫描的索引
unsigned int cnt = 0; //记录T0中断次数
unsigned char flag1s = 0; //1秒定时标志
void WaterLamp();
void KeyDriver();
void DigitalTube();
void main()
{
EA = 1; //使能总中断
//ENLED = 0; //选择数码管进行显示
//ADDR3 = 1;
ENLED = 0; //使能U3,选择独立LED
ADDR3 = 1;
//流水灯
//ADDR2 = 1;
//ADDR1 = 1;
//ADDR0 = 0;
TMOD = 0x01; //设置T0为模式1
TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms
TL0 = 0x67;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
//LedBuff[0] = LedChar[0]; //上电显示0
while (1)
{
KeyDriver(); //调用按键驱动函数
}
}
/* 按键动作函数,根据键码执行相应的操作,keycode-按键键码 */
void KeyAction(unsigned char keycode)
{
//static unsigned long result = 0; //用于保存运算结果
//static unsigned long addend = 0; //用于保存输入的加数
if ((keycode>=0x30) && (keycode<=0x39)) //输入0-9的数字
{
//addend = (addend*10)+(keycode-0x30); //整体十进制左移,新数字进入个位
//ShowNumber(addend); //运算结果显示到数码管
}
else if (keycode == 0x26) //向上键用作加号,执行加法或连加运算
{
WaterLamp();
//result += addend; //进行加法运算
//addend = 0;
//ShowNumber(result); //运算结果显示到数码管
}
else if (keycode == 0x25) //向上键用作加号,执行加法或连加运算
{
DigitalTube();
//result += addend; //进行加法运算
//addend = 0;
//ShowNumber(result); //运算结果显示到数码管
}
else if (keycode == 0x0D) //回车键,执行加法运算(实际效果与加号相同)
{
//result += addend; //进行加法运算
//addend = 0;
//ShowNumber(result); //运算结果显示到数码管
}
else if (keycode == 0x1B) //Esc键,清零结果
{
//addend = 0;
//result = 0;
//ShowNumber(addend); //清零后的加数显示到数码管
}
}
void KeyDriver()
{
unsigned char i, j;
static unsigned char backup[4][4] = { //按键值备份,保存前一次的值
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
for (i=0; i<4; i++) //循环检测4*4的矩阵按键
{
for (j=0; j<4; j++)
{
if (backup[i][j] != KeySta[i][j]) //检测按键动作
{
if (backup[i][j] != 0) //按键按下时执行动作
{
KeyAction(KeyCodeMap[i][j]); //调用按键动作函数
}
backup[i][j] = KeySta[i][j]; //刷新前一次的备份值
}
}
}
}
void DigitalTube()
{
char j; //循环变量
unsigned long sec; //记录经过的秒数
unsigned char buf[6]; //中间转换缓冲区
unsigned char dir = 0;
if (dir == 0) {
sec = 0;
} else {
sec = 1000000;
}
while (1)
{
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
if (dir == 0) {
sec++; //秒计数自加1
if (sec >= 1000000)
{
dir = 1;
}
} else {
sec--;
if (sec < 0)
{
dir = 1;
}
}
//将sec按十进制位从低到高依次提取到buf数组中
buf[0] = sec%10;
buf[1] = sec/10%10;
buf[2] = sec/100%10;
buf[3] = sec/1000%10;
buf[4] = sec/10000%10;
buf[5] = sec/100000%10;
//从最高为开始,遇到0不显示,遇到非0退出循环
for (j=5; j>=0; j--)
{
if (buf[j] == 0)
LedBuff[j] = 0xFF;
else
break;
}
//将剩余的有效数字位如实转换
for ( ; j>=0; j--) //for()起始未对j操作,j即保持上个循环结束时的值
{
LedBuff[j] = LedChar[buf[j]];
}
}
}
}
void WaterLamp()
{
static unsigned int cnt = 0; //定义计数变量cnt,记录T0溢出次数
static unsigned char dir = 0; //定义移位方向变量dir,用于控制移位的方向
static unsigned char shift = 0x01; //定义循环移位变量shift,并赋初值0x01
//static unsigned int i = 0;
ENLED = 0; //使能U3,选择独立LED
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
while(1)
{
P0 = ~shift; //P0等于循环移位变量取反,控制8个LED
if (TF0 == 1) {
TF0 = 0; //T0溢出后,清零中断标志
TH0 = 0xFC; //并重新赋初值
TL0 = 0x67;
cnt++; //计数值自加1
if (cnt >= 1000) { //T0溢出达到10次后,控制移位
cnt = 0;
if (dir == 0) { //移位方向变量为0时,左移
shift = shift << 1; //循环移位变量左移1位
if (shift == 0x80) { //左移到最左端后,改变移位方向
dir = 1;
}
} else { //移位方向变量不为0时,右移
shift = shift >> 1; //循环移位变量右移1位
if (shift == 0x01) { //右移到最右端后,改变移位方向
dir = 0;
}
}
}
}
}
}
void KeyScan()
{
unsigned char i;
static unsigned char keyout = 0; //矩阵按键扫描输出索引
static unsigned char keybuf[4][4] = { //矩阵按键扫描缓冲区
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
//将一行的4个按键值移入缓冲区
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
//消抖后更新按键状态
for (i=0; i<4; i++) //每行4个按键,所以循环4次
{
if ((keybuf[keyout][i] & 0x0F) == 0x00)
{ //连续4次扫描值为0,即4*4ms内都是按下状态时,可认为按键已稳定的按下
KeySta[keyout][i] = 0;
}
else if ((keybuf[keyout][i] & 0x0F) == 0x0F)
{ //连续4次扫描值为1,即4*4ms内都是弹起状态时,可认为按键已稳定的弹起
KeySta[keyout][i] = 1;
}
}
//执行下一次的扫描输出
keyout++; //输出索引递增
keyout = keyout & 0x03; //索引值加到4即归零
switch (keyout) //根据索引,释放当前输出引脚,拉低下次的输出引脚
{
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
/* T0中断服务函数,用于数码管显示扫描与按键扫描 */
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
cnt++; //中断次数计数值加1
if (cnt >= 1000) //中断1000次即1秒
{
cnt = 0; //清零计数值以重新开始下1秒计时
flag1s = 1; //设置1秒定时标志为1
}
//以下代码完成数码管动态扫描刷新
P0 = 0xFF; //显示消隐
switch (i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5]; break;
default: break;
}
//LedScan(); //调用数码管显示扫描函数
KeyScan(); //调用按键扫描函数
}
/* 定时器0中断服务函数 */
/*
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0; //动态扫描的索引
static unsigned char tmr = 0; //250ms软件定时器
static unsigned char index = 32; //图片刷新索引
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
//以下代码完成LED点阵动态扫描刷新
P0 = 0xFF; //显示消隐
switch (i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[index+0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[index+1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[index+2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[index+3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[index+4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[index+5]; break;
case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[index+6]; break;
case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[index+7]; break;
default: break;
}
//以下代码完成每250ms改变一帧图像
tmr++;
if (tmr >= 250) //达到250ms时改变一次图片索引
{
tmr = 0;
index--;
if (index <= 0) //图片索引达到32后归零
{
index = 32;
}
}
}
*/
上面那个版本没法实现数码管和流水灯的切换
下面这个版本实现了数码管和流水灯的切换但是流水灯和数码管走不起来,主要是while(1),死循环的问题,下个版本再改进
#include
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
unsigned char code image[] = { //图片的字模表
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x99,0x99,0x99,0x99,0x99,0x81,0xC3,0xFF,
0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,0xFF,
0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = { //数码管+点阵+流水灯显示缓冲区
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF
};
unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
{ 0x31, 0x32, 0x33, 0x26 }, //数字键1、数字键2、数字键3、向上键
{ 0x34, 0x35, 0x36, 0x25 }, //数字键4、数字键5、数字键6、向左键
{ 0x37, 0x38, 0x39, 0x28 }, //数字键7、数字键8、数字键9、向下键
{ 0x30, 0x1B, 0x0D, 0x27 } //数字键0、ESC键、 回车键、 向右键
};
unsigned char KeySta[4][4] = { //全部矩阵按键的当前状态
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
unsigned char i = 0; //动态扫描的索引
unsigned int cnt = 0; //记录T0中断次数
unsigned char flag1s = 0; //1秒定时标志
unsigned char model_1; //流水灯模式
unsigned char model_2; //数码管模式
void WaterLamp();
void KeyDriver();
void DigitalTube();
void main()
{
EA = 1; //使能总中断
ENLED = 0; //使能U3,选择独立LED
ADDR3 = 1;
TMOD = 0x01; //设置T0为模式1
TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms
TL0 = 0x67;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
while (1)
{
KeyDriver(); //调用按键驱动函数
}
}
/* 按键动作函数,根据键码执行相应的操作,keycode-按键键码 */
void KeyAction(unsigned char keycode)
{
switch (keycode)
{
case 0x26: model_1 = 1; model_2 = 0; WaterLamp(); break;
case 0x25: model_2 = 1; model_1 = 0; DigitalTube(); break;
}
/*
if ((keycode>=0x30) && (keycode<=0x39)) //输入0-9的数字
{
}
else if (keycode == 0x26) //向上键用作加号,执行加法或连加运算
{
model_1 = 1;
model_2 = 0;
if (model_1) {
while(1) {
WaterLamp();
}
}
}
else if (keycode == 0x25) //向上键用作加号,执行加法或连加运算
{
model_2 = 1;
model_1 = 0;
if (model_2) {
DigitalTube();
}
}
else if (keycode == 0x0D) //回车键,执行加法运算(实际效果与加号相同)
{
}
else if (keycode == 0x1B) //Esc键,清零结果
{
}
*/
}
void KeyDriver()
{
unsigned char i, j;
static unsigned char backup[4][4] = { //按键值备份,保存前一次的值
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
for (i=0; i<4; i++) //循环检测4*4的矩阵按键
{
for (j=0; j<4; j++)
{
if (backup[i][j] != KeySta[i][j]) //检测按键动作
{
if (backup[i][j] != 0) //按键按下时执行动作
{
KeyAction(KeyCodeMap[i][j]); //调用按键动作函数
}
backup[i][j] = KeySta[i][j]; //刷新前一次的备份值
}
}
}
}
void DigitalTube()
{
char j; //循环变量
unsigned long sec; //记录经过的秒数
unsigned char buf[6]; //中间转换缓冲区
unsigned char dir = 0;
if (dir == 0) {
sec = 0;
} else {
sec = 1000000;
}
if(model_2)
{
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
if (dir == 0) {
sec++; //秒计数自加1
if (sec >= 1000000)
{
dir = 1;
}
} else {
sec--;
if (sec < 0)
{
dir = 1;
}
}
//将sec按十进制位从低到高依次提取到buf数组中
buf[0] = sec%10;
buf[1] = sec/10%10;
buf[2] = sec/100%10;
buf[3] = sec/1000%10;
buf[4] = sec/10000%10;
buf[5] = sec/100000%10;
//从最高为开始,遇到0不显示,遇到非0退出循环
for (j=5; j>=0; j--)
{
if (buf[j] == 0)
LedBuff[j] = 0xFF;
else
break;
}
//将剩余的有效数字位如实转换
for ( ; j>=0; j--) //for()起始未对j操作,j即保持上个循环结束时的值
{
LedBuff[j] = LedChar[buf[j]];
}
}
}
}
void WaterLamp()
{
if(model_1) {
static unsigned char dir = 0; //定义移位方向变量dir,用于控制移位的方向
static unsigned char shift = 0x01; //定义循环移位变量shift,并赋初值0x01
P0 = ~shift; //P0等于循环移位变量取反,控制8个LED
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
if (dir == 0) { //移位方向变量为0时,左移
shift = shift << 1; //循环移位变量左移1位
if (shift == 0x80) { //左移到最左端后,改变移位方向
dir = 1;
}
} else { //移位方向变量不为0时,右移
shift = shift >> 1; //循环移位变量右移1位
if (shift == 0x01) { //右移到最右端后,改变移位方向
dir = 0;
}
}
}
}
}
void KeyScan()
{
unsigned char i;
static unsigned char keyout = 0; //矩阵按键扫描输出索引
static unsigned char keybuf[4][4] = { //矩阵按键扫描缓冲区
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
//将一行的4个按键值移入缓冲区
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
//消抖后更新按键状态
for (i=0; i<4; i++) //每行4个按键,所以循环4次
{
if ((keybuf[keyout][i] & 0x0F) == 0x00)
{ //连续4次扫描值为0,即4*4ms内都是按下状态时,可认为按键已稳定的按下
KeySta[keyout][i] = 0;
}
else if ((keybuf[keyout][i] & 0x0F) == 0x0F)
{ //连续4次扫描值为1,即4*4ms内都是弹起状态时,可认为按键已稳定的弹起
KeySta[keyout][i] = 1;
}
}
//执行下一次的扫描输出
keyout++; //输出索引递增
keyout = keyout & 0x03; //索引值加到4即归零
switch (keyout) //根据索引,释放当前输出引脚,拉低下次的输出引脚
{
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
/* T0中断服务函数,用于数码管显示扫描与按键扫描 */
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
cnt++; //中断次数计数值加1
if (cnt >= 1000) //中断1000次即1秒
{
cnt = 0; //清零计数值以重新开始下1秒计时
flag1s = 1; //设置1秒定时标志为1
}
//以下代码完成数码管动态扫描刷新
if (model_1) {
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
}
if (model_2) {
P0 = 0xFF; //显示消隐
switch (i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5]; break;
default: break;
}
}
//LedScan(); //调用数码管显示扫描函数
KeyScan(); //调用按键扫描函数
}
突然想到了return 和break;
应该用return当前函数---------------------失败
解决方法放进主函数里进行循环
这个版本解决了流水灯和数码管的切换效果
#include
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
unsigned char code image[] = { //图片的字模表
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x99,0x99,0x99,0x99,0x99,0x81,0xC3,0xFF,
0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,0xFF,
0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = { //数码管+点阵+流水灯显示缓冲区
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF
};
unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
{ 0x31, 0x32, 0x33, 0x26 }, //数字键1、数字键2、数字键3、向上键
{ 0x34, 0x35, 0x36, 0x25 }, //数字键4、数字键5、数字键6、向左键
{ 0x37, 0x38, 0x39, 0x28 }, //数字键7、数字键8、数字键9、向下键
{ 0x30, 0x1B, 0x0D, 0x27 } //数字键0、ESC键、 回车键、 向右键
};
unsigned char KeySta[4][4] = { //全部矩阵按键的当前状态
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
unsigned char i = 0; //动态扫描的索引
unsigned int cnt = 0; //记录T0中断次数
unsigned char flag1s = 0; //1秒定时标志
unsigned char model_1; //流水灯模式
unsigned char model_2; //数码管模式
unsigned char model_3; //数码管模式
void WaterLamp();
void KeyDriver();
void DigitalTube();
//static char j; //循环变量
static unsigned long sec; //记录经过的秒数
//static unsigned char buf[6]; //中间转换缓冲区
static unsigned char dir = 0;
void main()
{
EA = 1; //使能总中断
ENLED = 0; //使能U3,选择独立LED
ADDR3 = 1;
TMOD = 0x01; //设置T0为模式1
TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms
TL0 = 0x67;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
if (dir == 0) {
sec = 0;
} else {
sec = 1000000;
}
while (1)
{
KeyDriver(); //调用按键驱动函数
if (model_1)
{
WaterLamp();
} else {
/*
if (dir == 0) {
sec++; //秒计数自加1
if (sec >= 1000000)
{
dir = 1;
}
} else {
sec--;
if (sec < 0)
{
dir = 1;
}
}
*/
DigitalTube();
}
}
}
/* 按键动作函数,根据键码执行相应的操作,keycode-按键键码 */
void KeyAction(unsigned char keycode)
{
switch (keycode)
{
case 0x26: model_1 = 1;
model_2 = 0;
break;
case 0x25: model_2 = 1;
model_1 = 0;
break;
}
/*
if ((keycode>=0x30) && (keycode<=0x39)) //输入0-9的数字
{
}
else if (keycode == 0x26) //向上键用作加号,执行加法或连加运算
{
model_1 = 1;
model_2 = 0;
if (model_1) {
while(1) {
WaterLamp();
}
}
}
else if (keycode == 0x25) //向上键用作加号,执行加法或连加运算
{
model_2 = 1;
model_1 = 0;
if (model_2) {
DigitalTube();
}
}
else if (keycode == 0x0D) //回车键,执行加法运算(实际效果与加号相同)
{
}
else if (keycode == 0x1B) //Esc键,清零结果
{
}
*/
}
void KeyDriver()
{
unsigned char i, j;
static unsigned char backup[4][4] = { //按键值备份,保存前一次的值
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
for (i=0; i<4; i++) //循环检测4*4的矩阵按键
{
for (j=0; j<4; j++)
{
if (backup[i][j] != KeySta[i][j]) //检测按键动作
{
if (backup[i][j] != 0) //按键按下时执行动作
{
KeyAction(KeyCodeMap[i][j]); //调用按键动作函数
}
backup[i][j] = KeySta[i][j]; //刷新前一次的备份值
}
}
}
}
void DigitalTube()
{
static char j; //循环变量
//unsigned long sec; //记录经过的秒数
static unsigned char buf[6]; //中间转换缓冲区
//unsigned char dir = 0;
/*
if (dir == 0) {
sec = 0;
} else {
sec = 1000000;
}
*/
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
if (dir == 0) {
sec++; //秒计数自加1
if (sec >= 1000000)
{
dir = 1;
}
} else {
sec--;
if (sec < 0)
{
dir = 1;
}
}
//将sec按十进制位从低到高依次提取到buf数组中
buf[0] = sec%10;
buf[1] = sec/10%10;
buf[2] = sec/100%10;
buf[3] = sec/1000%10;
buf[4] = sec/10000%10;
buf[5] = sec/100000%10;
//从最高为开始,遇到0不显示,遇到非0退出循环
for (j=5; j>=0; j--)
{
if (buf[j] == 0)
LedBuff[j] = 0xFF;
else
break;
}
//将剩余的有效数字位如实转换
for ( ; j>=0; j--) //for()起始未对j操作,j即保持上个循环结束时的值
{
LedBuff[j] = LedChar[buf[j]];
}
}
}
void WaterLamp()
{
static unsigned char dir = 0; //定义移位方向变量dir,用于控制移位的方向
static unsigned char shift = 0x01; //定义循环移位变量shift,并赋初值0x01
P0 = ~shift; //P0等于循环移位变量取反,控制8个LED
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
if (dir == 0) { //移位方向变量为0时,左移
shift = shift << 1; //循环移位变量左移1位
if (shift == 0x80) { //左移到最左端后,改变移位方向
dir = 1;
}
} else { //移位方向变量不为0时,右移
shift = shift >> 1; //循环移位变量右移1位
if (shift == 0x01) { //右移到最右端后,改变移位方向
dir = 0;
}
}
}
}
void KeyScan()
{
unsigned char i;
static unsigned char keyout = 0; //矩阵按键扫描输出索引
static unsigned char keybuf[4][4] = { //矩阵按键扫描缓冲区
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
//将一行的4个按键值移入缓冲区
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
//消抖后更新按键状态
for (i=0; i<4; i++) //每行4个按键,所以循环4次
{
if ((keybuf[keyout][i] & 0x0F) == 0x00)
{ //连续4次扫描值为0,即4*4ms内都是按下状态时,可认为按键已稳定的按下
KeySta[keyout][i] = 0;
}
else if ((keybuf[keyout][i] & 0x0F) == 0x0F)
{ //连续4次扫描值为1,即4*4ms内都是弹起状态时,可认为按键已稳定的弹起
KeySta[keyout][i] = 1;
}
}
//执行下一次的扫描输出
keyout++; //输出索引递增
keyout = keyout & 0x03; //索引值加到4即归零
switch (keyout) //根据索引,释放当前输出引脚,拉低下次的输出引脚
{
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
/* T0中断服务函数,用于数码管显示扫描与按键扫描 */
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
cnt++; //中断次数计数值加1
if (cnt >= 1000) //中断1000次即1秒
{
cnt = 0; //清零计数值以重新开始下1秒计时
flag1s = 1; //设置1秒定时标志为1
}
//以下代码完成数码管动态扫描刷新
if (model_1) {
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
}
if (model_2) {
P0 = 0xFF; //显示消隐
switch (i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5]; break;
default: break;
}
}
//LedScan(); //调用数码管显示扫描函数
KeyScan(); //调用按键扫描函数
}
/* 定时器0中断服务函数 */
/*
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0; //动态扫描的索引
static unsigned char tmr = 0; //250ms软件定时器
static unsigned char index = 32; //图片刷新索引
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
//以下代码完成LED点阵动态扫描刷新
P0 = 0xFF; //显示消隐
switch (i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[index+0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[index+1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[index+2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[index+3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[index+4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[index+5]; break;
case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[index+6]; break;
case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[index+7]; break;
default: break;
}
//以下代码完成每250ms改变一帧图像
tmr++;
if (tmr >= 250) //达到250ms时改变一次图片索引
{
tmr = 0;
index--;
if (index <= 0) //图片索引达到32后归零
{
index = 32;
}
}
}
*/