单片机,修改以下代码#include<reg52.h> unsigned char cols[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE7,0xE3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; unsigned char p_i =0;//p_i=0表示显示第一幅图像 void main(){ //定时器1和中断 TMOD = 0x10; TR1=1; TH1=0xfc; TL1=0x67; EA=1; ET1=1; while(1); } void interrupt_T1() interrupt 3{ static unsigned char row_i=0;//row_i=0表示选中点阵第一行 static unsigned int cnt=0;//cnt=1表示进过1ms,用于点阵行的切换 static unsigned int p_cnt=0;//p_cnt=1表示进过1ms,用于图像的切换 TH1=0xfc; TL1=0x67; P0 = 0xff;//消隐 cnt++; p_cnt++; if(cnt==1){ cnt=0; row_i++; if(row_i==8){ row_i =0; } } if(p_cnt==10){ p_cnt=0; p_i++; if(p_i==17){ p_i =0; } } switch(row_i){ case 0:P2 =0x01;P0 = cols[0+p_i];break; case 1:P2=0x02;P0 = cols[1+p_i];break; case 2:P2 =0x04;P0 = cols[2+p_i];break; case 3:P2=0x08;P0 = cols[3+p_i];break; case 4:P2 =0x10;P0 = cols[4+p_i];break; case 5:P2=0x20;P0 = cols[5+p_i];break; case 6:P2 =0x40;P0 = cols[6+p_i];break; case 7:P2=0x80;P0 = cols[7+p_i];break; default:break; plaintext Copy code } }实现8*8点阵9到0倒序秒表
时间: 2023-06-25 07:04:08 浏览: 156
以下是修改后的代码:
#include<reg52.h>
unsigned char cols[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE7,0xE3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
unsigned char p_i =0;//p_i=0表示显示第一幅图像
unsigned char sec_cnt = 0; // 秒计数器
unsigned char sec_num = 9; // 当前显示的秒数
void main(){
//定时器1和中断
TMOD = 0x10;
TR1=1;
TH1=0xfc;
TL1=0x67;
EA=1;
ET1=1;
while(1);
}
void interrupt_T1() interrupt 3{
static unsigned char row_i=0;//row_i=0表示选中点阵第一行
static unsigned int cnt=0;//cnt=1表示进过1ms,用于点阵行的切换
static unsigned int p_cnt=0;//p_cnt=1表示进过1ms,用于图像的切换
TH1=0xfc;
TL1=0x67;
P0 = 0xff;//消隐
cnt++;
p_cnt++;
if(cnt==1){
cnt=0;
row_i++;
if(row_i==8){
row_i =0;
}
}
if(p_cnt==10){
p_cnt=0;
p_i++;
if(p_i==17){
p_i =0;
}
}
switch(row_i){
case 0:P2 =0x01;P0 = cols[0+p_i];break;
case 1:P2=0x02;P0 = cols[1+p_i];break;
case 2:P2 =0x04;P0 = cols[2+p_i];break;
case 3:P2=0x08;P0 = cols[3+p_i];break;
case 4:P2 =0x10;P0 = cols[4+p_i];break;
case 5:P2=0x20;P0 = cols[5+p_i];break;
case 6:P2 =0x40;P0 = cols[6+p_i];break;
case 7:P2=0x80;P0 = cols[7+p_i];break;
default:break;
}
// 每隔1秒更新秒数
if (sec_cnt == 100) {
sec_cnt = 0;
if (sec_num == 0) {
sec_num = 9;
} else {
sec_num--;
}
// 更新cols数组,显示倒序秒数
for (int i = 16; i < 24; i++) {
cols[i] = cols[i] & ~(1 << sec_num);
}
}
sec_cnt++;
}
在原有的代码基础上,我们添加了两个变量sec_cnt和sec_num,分别用于计数和记录当前秒数。在T1中断中,我们每隔1秒更新一次秒数,并更新cols数组,将倒序秒数显示在点阵上。
阅读全文