一,数组
从概念上讲,数组是具有相同数据类型的有序数据的组合,一般来讲,数组定义后满足
以下三个条件。
1、具有相同的数据类型;
2、具有相同的名字;
3、在存储器中是被连续存放的。
一维数组的声明格式如下:
数据类型 数组名 [数组长度];
例如: unsigned char LedChar[16] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
二,数码管动态显示
999999的倒计时
- #include <reg52.h>
- sbit P20=P2^0;
- sbit P21=P2^1;
- sbit P22=P2^2;
- sbit ENLED=P1^1;
- unsigned char code LedChar[]={
- 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
- 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
- };
- unsigned char LedBuff[6]={
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
- };
- unsigned char i = 0;
- unsigned int cnt = 0;
- unsigned char flagls = 0;
- signed long sec = 999999;
- void main()
- {
-
-
- ENLED = 0;
- EA = 1; //打开中断总开关
- TMOD = 0x01;
- TH0 = 0xF5;
- TL0 = 0x56;
- ET0 = 1; //设置T0中断
- TR0 = 1; //打开T0
-
- while(1)
- {
- if(flagls==1)
- {
- flagls=0;
-
- LedBuff[0] = LedChar[sec%10];
- LedBuff[1] = LedChar[sec/10%10];
- LedBuff[2] = LedChar[sec/100%10];
- LedBuff[3] = LedChar[sec/1000%10];
- LedBuff[4] = LedChar[sec/10000%10];
- LedBuff[5] = LedChar[sec/100000%10];
- sec--;
- }
- }
- }
- void InterruptTimer0() interrupt 1 //定时器溢出自动进入中断
- {
- TH0=0xF5;
- TL0=0x56; //硬件清零无需TF0=0
- cnt++;
- if(cnt>=1000)
- {
- cnt = 0;
- flagls=1;
- }
- P0=0xFF; //显示消隐
- switch(i) //加入if else语句使数码管只显示有效位
- {
- case 0:P22=1;P21=0;P20=1;i++;P0=LedBuff[0];break;
- case 1:P22=1;P21=0;P20=0;i++;P0=LedBuff[1];break;
- case 2:P22=0;P21=1;P20=1;i++;P0=LedBuff[2];break;
- case 3:P22=0;P21=1;P20=0;i++;P0=LedBuff[3];break;
- case 4:P22=0;P21=0;P20=1;i++;P0=LedBuff[4];break;
- case 5:P22=0;P21=0;P20=0;i=0;P0=LedBuff[5];break;
- default:break;
- }
- }
复制代码 高位不显示的计时(定时器T1和中断)
- #include <reg52.h>
- sbit P20=P2^0;
- sbit P21=P2^1;
- sbit P22=P2^2;
- sbit ENLED=P1^1;
- unsigned char code LedChar[]={
- 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
- 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
- };
- unsigned char LedBuff[6]={
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
- };
- unsigned char i = 0;
- unsigned int cnt = 0;
- unsigned char flagls = 0;
- unsigned long sec = 0;
- void main()
- {
-
-
- ENLED = 0;
- EA = 1; //打开中断总开关
- TMOD = 0x10; //定时器T1
- TH1 = 0xF5;
- TL1 = 0x56;
- ET1 = 1; //设置T1中断
- TR1 = 1; //打开T1
-
- while(1)
- {
- if(flagls==1)
- {
- flagls=0;
- sec++;
- LedBuff[0] = LedChar[sec%10];
- LedBuff[1] = LedChar[sec/10%10];
- LedBuff[2] = LedChar[sec/100%10];
- LedBuff[3] = LedChar[sec/1000%10];
- LedBuff[4] = LedChar[sec/10000%10];
- LedBuff[5] = LedChar[sec/100000%10];
- }
- }
- }
- void InterruptTimer0() interrupt 3 //定时器溢出自动进入中断
- {
- TH1=0xF5;
- TL1=0x56; //硬件清零无需TF1=0
- cnt++;
- if(cnt>=1000)
- {
- cnt = 0;
- flagls=1;
- }
- P0=0xFF;
- switch(i) //加入if else语句使数码管只显示有效位
- {
- case 0:P22=1;P21=0;P20=1;i++;P0=LedBuff[0];break;
- case 1:P22=1;P21=0;P20=0;i++;if(sec>9){P0=LedBuff[1];}else{P0=0xFF;}break;
- case 2:P22=0;P21=1;P20=1;i++;if(sec>99){P0=LedBuff[2];}else{P0=0xFF;}break;
- case 3:P22=0;P21=1;P20=0;i++;if(sec>999){P0=LedBuff[3];}else{P0=0xFF;}break;
- case 4:P22=0;P21=0;P20=1;i++;if(sec>99999){P0=LedBuff[4];}else{P0=0xFF;}break;
- case 5:P22=0;P21=0;P20=0;i=0;if(sec>999999){P0=LedBuff[5];}else{P0=0xFF;}break;
- default:break;
- }
- }
复制代码
|