数码管真值表
- #include <reg52.h>
- sbit ADDR0 = P1 ^ 0;
- sbit ADDR1 = P1 ^ 1;
- sbit ADDR2 = P1 ^ 2;
- sbit ADDR3 = P1 ^ 3;
- sbit ENLED = P1 ^ 4;
- void main(void)
- {
- ENLED = 0;
- ADDR3 = 1; // U3 74HC138芯片使能
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = 0xf9; //数字1
- while (1);
- }
复制代码 单个数码管倒计时
- #include <reg52.h>
- sbit ADDR0 = P1 ^ 0;
- sbit ADDR1 = P1 ^ 1;
- sbit ADDR2 = P1 ^ 2;
- sbit ADDR3 = P1 ^ 3;
- sbit ENLED = P1 ^ 4;
- unsigned char code LedChar[] =
- {
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
- 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
- };//真值表
- void main(void)
- {
- char i = 15; // 真值表下标
- char cnt = 0; // 定时器溢出次数
- ENLED = 0;
- ADDR3 = 1; // U3 74HC138芯片使能
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- TMOD = 1; // 设置定时器0为模式1
- TH0 = 0xB8;
- TL0 = 0x00; // 设置定时器0的初始值
- TR0 = 1; // 定时器0开始工作
- P0 = LedChar[i];
- while (1) {
- if (1 == TF0) // 定时器溢出
- {
- TF0 = 0;//将溢出标志恢复初值,再次定时
- TH0 = 0xB8;
- TL0 = 0x00; // 设置定时器0的初始值
- cnt++;
- if (cnt >= 50)
- {
- cnt = 0;
- if (i < 0)
- i = 15;
- P0 = LedChar[i--];
- }
- }
- }
- }
复制代码 6段数码管动态显示
- #include <reg52.h>
- sbit ADDR0 = P1 ^ 0;
- sbit ADDR1 = P1 ^ 1;
- sbit ADDR2 = P1 ^ 2;
- sbit ADDR3 = P1 ^ 3;
- sbit ENLED = P1 ^ 4;
- unsigned char code LedChar[] =
- {
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99,
- 0x92, 0x82, 0xF8, 0x80, 0x90
- };
- unsigned char LedBuff[] =
- {
- 0xFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF
- };
- void main(void)
- {
- unsigned char i = 0;//当前工作的数码管下标
- unsigned int cnt = 0;//溢出次数
- unsigned long sec = 0; // 秒
- ENLED = 0;
- ADDR3 = 1; // U3 74HC138芯片使能
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- TMOD = 1; // 设置定时器0为模式1
- TH0 = 0xFC;
- TL0 = 0x67; // 设置定时器0的初始值
- TR0 = 1; // 定时器0开始工作
- while (1)
- {
- if (1 == TF0) // 定时器溢出
- {
- TF0 = 0; // 将溢出标志恢复初值,再次定时
- TH0 = 0xFC;
- TL0 = 0x67; // 设置定时器0的初始值
- cnt++;
- if (cnt >= 1000) //1s
- {
- cnt = 0;
- sec++;
- if (sec >= 999999)
- sec = 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];
- }
- switch (i)
- {
- case 0:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[0];
- i++;
- break;
- case 1:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[1];
- i++;
- break;
- case 2:
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[2];
- i++;
- break;
- case 3:
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[3];
- i++;
- break;
- case 4:
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[4];
- i++;
- break;
- case 5:
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[5];
- i = 0;
- break;
- }
- }
- }
- }
复制代码 动态显示的数码管显示消隐
- #include <reg52.h>
- sbit ADDR0 = P1 ^ 0;
- sbit ADDR1 = P1 ^ 1;
- sbit ADDR2 = P1 ^ 2;
- sbit ADDR3 = P1 ^ 3;
- sbit ENLED = P1 ^ 4;
- unsigned char code LedChar[] =
- {
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99,
- 0x92, 0x82, 0xF8, 0x80, 0x90
- };
- unsigned char LedBuff[] =
- {
- 0xFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF
- };
- void main(void)
- {
- unsigned char i = 0;//当前工作的数码管下标
- unsigned int cnt = 0;//溢出次数
- unsigned long sec = 0; // 秒
- ENLED = 0;
- ADDR3 = 1; // U3 74HC138芯片使能
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- TMOD = 1; // 设置定时器0为模式1
- TH0 = 0xFC;
- TL0 = 0x67; // 设置定时器0的初始值
- TR0 = 1; // 定时器0开始工作
- while (1)
- {
- if (1 == TF0) // 定时器溢出
- {
- TF0 = 0; // 将溢出标志恢复初值,再次定时
- TH0 = 0xFC;
- TL0 = 0x67; // 设置定时器0的初始值
- cnt++;
- if (cnt >= 1000) //1s
- {
- cnt = 0;
- sec++;
- if (sec >= 999999)
- sec = 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];
- }
-
- P0 = 0xFF;//显示消隐处理
- switch (i)
- {
- case 0:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[0];
- i++;
- break;
- case 1:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[1];
- i++;
- break;
- case 2:
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[2];
- i++;
- break;
- case 3:
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[3];
- i++;
- break;
- case 4:
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[4];
- i++;
- break;
- case 5:
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[5];
- i = 0;
- break;
- }
- }
- }
- }
复制代码 采用中断的动态显示
- #include <reg52.h>
- sbit ADDR0 = P1 ^ 0;
- sbit ADDR1 = P1 ^ 1;
- sbit ADDR2 = P1 ^ 2;
- sbit ADDR3 = P1 ^ 3;
- sbit ENLED = P1 ^ 4;
- unsigned char code LedChar[] =
- {
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99,
- 0x92, 0x82, 0xF8, 0x80, 0x90
- };//数码管真值表
- unsigned char LedBuff[] =
- {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
- };//数码管显示缓冲区
- unsigned char i = 0;//动态扫描的索引
- unsigned int cnt = 0;//记录T0中断次数
- unsigned char flag1s = 0;//1s定时标志
- void main(void)
- {
- unsigned long sec = 0;//记录经过的秒数
- EA = 1;//使能总中断
- ENLED = 0;
- ADDR3 = 1;//U3 74HC138芯片使能
- TMOD = 0x01;//设置定时器T0模式1
- TH0 = 0xFC;
- TL0 = 0x67;//设置定时器T0的初值
- ET0 = 1;//使能定时器T0中断
- TR0 = 1;//启动定时器T0
- while (1)
- {
- if (1 == flag1s)//判断1s定时标志
- {
- flag1s = 0;//1s定时标志清零
- sec++;//秒数加1
- //设置数码管的值
- 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 1
- {
- TH0 = 0xFC;
- TL0 = 0x67;//设置定时器T0的初值
- cnt++;//中断次数+1
- if (cnt >= 1000)//中断1000次即1秒
- {
- cnt = 0;//中断次数清零
- flag1s = 1;//设置1s定时标志为1
- }
- P0 = 0xff;//显示消隐
- switch (i)
- {
- case 0:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[0];
- i++;
- break;
- case 1:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[1];
- i++;
- break;
- case 2:
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[2];
- i++;
- break;
- case 3:
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[3];
- i++;
- break;
- case 4:
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[4];
- i++;
- break;
- case 5:
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[5];
- i = 0;
- break;
- default:
- break;
- }
- }
复制代码 动态显示时不显示高位0-采用中断
- #include <reg52.h>
- sbit ADDR0 = P1 ^ 0;
- sbit ADDR1 = P1 ^ 1;
- sbit ADDR2 = P1 ^ 2;
- sbit ADDR3 = P1 ^ 3;
- sbit ENLED = P1 ^ 4;
- unsigned char code LedChar[] =
- {
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99,
- 0x92, 0x82, 0xF8, 0x80, 0x90
- };//数码管真值表
- unsigned char LedBuff[] =
- {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
- };//数码管显示缓冲区
- unsigned char i = 0;//动态扫描的索引
- unsigned int cnt = 0;//记录T0中断次数
- unsigned char flag1s = 0;//1s定时标志
- unsigned long sec = 0;//记录经过的秒数
- void main(void)
- {
- //unsigned long sec = 0;//记录经过的秒数
- EA = 1;//使能总中断
- ENLED = 0;
- ADDR3 = 1;//U3 74HC138芯片使能
- TMOD = 0x01;//设置定时器T0模式1
- TH0 = 0xFC;
- TL0 = 0x67;//设置定时器T0的初值
- ET0 = 1;//使能定时器T0中断
- TR0 = 1;//启动定时器T0
- while (1)
- {
- if (1 == flag1s)//判断1s定时标志
- {
- flag1s = 0;//1s定时标志清零
- sec++;//秒数加1
- //设置数码管的值
- 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 1
- {
- TH0 = 0xFC;
- TL0 = 0x67;//设置定时器T0的初值
- cnt++;//中断次数+1
- if (cnt >= 1000)//中断1000次即1秒
- {
- cnt = 0;//中断次数清零
- flag1s = 1;//设置1s定时标志为1
- }
- P0 = 0xff;//显示消隐
- switch (i)
- {
- case 0:
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[0];
- i++;
- break;
- case 1:
- i++;
- if (sec < 10)
- break;
- ADDR2 = 0;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[1];
- break;
- case 2:
- i++;
- if (sec < 100)
- break;
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[2];
- break;
- case 3:
- i++;
- if (sec < 1000)
- break;
- ADDR2 = 0;
- ADDR1 = 1;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[3];
- break;
- case 4:
- i++;
- if (sec < 10000)
- break;
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 0; // 三极管导通
- P0 = LedBuff[4];
- break;
- case 5:
- i = 0;
- if (sec < 100000)
- break;
- ADDR2 = 1;
- ADDR1 = 0;
- ADDR0 = 1; // 三极管导通
- P0 = LedBuff[5];
- break;
- default:
- break;
- }
- }
复制代码 来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除 |