动态数码管
动态数码管使用所需原理图
锁存器
高电平通路,低电平锁存不执行通路
0XC0第一个573(位锁存器)
0XFF第二个573(段锁存器)
注:位锁存器与段锁存器是相互干扰的,当位锁存器打开时段锁存器将关闭,依次类推。
//1、将第1、2、3为数码管显示260->0的倒计时
#include<stc15.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
uint sum = 260; //定义三位数
uint num = 0; //给定计时
uint one;
uint two;
uint three;
void delay_ms(uchar tm);
void main()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X01;P2=0XFF;P0=0XFF;
one = sum /100; //百位数
two = sum /10%10; //十位数
three = sum%100; //个位数
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[one];
delay_ms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[two];
delay_ms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[three];
delay_ms(1);
while(1)
{
if(num == 50)
{
one = sum /100;
two = sum /10%10;
three = sum%10;
num = 0;
sum--;
}
num++;
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[one];
delay_ms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[two];
delay_ms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[three];
delay_ms(1);
}
}
void delay_ms(uchar tm)
{
uint i ,j;
for(i = 853; i > 1; i--)
{
for(j = tm; j > 1; j--);
}
}
优化版本:
#include<stc15.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
uint sum = 260;
uint num = 0;
uint one;
uint two;
uint three;
void delay_ms(uchar tm);
void main()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X01;P2=0XFF;P0=0XFF;
one = sum /100;
two = sum /10%10;
three = sum%100;
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[one];
delay_ms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[two];
delay_ms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[three];
delay_ms(1);
while(1)
{
if(num == 50)
{
one = sum /100;
two = sum /10%10;
three = sum%10;
num = 0;
sum--;
}
num++;
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[one];
delay_ms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[two];
delay_ms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[three];
delay_ms(1);
if(sum == 0)
sum = 260; //此处加一个判断可以避免出现程序死循环
}
}
void delay_ms(uchar tm)
{
uint i ,j;
for(i = 853; i > 1; i--)
{
for(j = tm; j > 1; j--);
}
}
//2、LED数码管综合显示:将第1、2、3位数码管从260->0进行倒计时,当倒计时为250时L1点亮
#include<stc15.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
uint sum = 260;
uint num = 0;
uint one;
uint two;
uint three;
void delay_ms(uchar tm);
void main()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X01;P2=0XFF;P0=0XFF;
one = sum /100;
two = sum /10%10;
three = sum%100;
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[one];
delay_ms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[two];
delay_ms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[three];
delay_ms(1);
while(1)
{
if(num == 100)
{
one = sum /100;
two = sum /10%10;
three = sum%10;
num = 0;
--sum;
}
num++;
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[one];
delay_ms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[two];
delay_ms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[three];
delay_ms(1);
if(sum == 250) //当sum == 250时打开第一个LED灯
{
P2 = 0X80; P0 = 0XFE;
}
if(sum == 0)
sum = 260;
}
}
void delay_ms(uchar tm)
{
uint i ,j;
for(i = 853; i > 1; i--)
{
for(j = tm; j > 1; j--);
}
}
//3、上电后所以有数码管显示“-”,按下S7键第一个数码管显示数字“1”,按下S6按键,第二个数码管显示数字“2”,按下S5键第一个数码管显示数字“3”,按下S4键第一个数码管显示数字“4”
#include<stc15.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit S7 = P3^0 ;
sbit S6 = P3^1 ;
sbit S5 = P3^2 ;
sbit S4 = P3^3 ;
uchar S7_flag = 0;
uchar S6_flag = 0;
uchar S5_flag = 0;
uchar S4_flag = 0;
uchar tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
void keypad();
void delay_tm(uint ms);
void main()
{
P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
P2 = 0XC0; P0 = 0XFF; P2 = 0XFF; P0 = 0XFF;
while(1)
{
keypad();
if(S7_flag == 1){
S7_flag = 0;
P2 = 0XC0; P0 = 0X01; P2 = 0XFF; P0 = 0XF9;
}
if(S6_flag == 1){
S6_flag = 0;
P2 = 0XC0; P0 = 0X02; P2 = 0XFF; P0 = 0XA4;
}
if(S5_flag == 1){
S5_flag = 0;
P2 = 0XC0; P0 = 0X04; P2 = 0XFF; P0 = 0XB0;
}
if(S4_flag == 1){
S4_flag = 0;
P2 = 0XC0; P0 = 0X08; P2 = 0XFF; P0 = 0X99;
}
}
}
void keypad()
{
if(S7 == 0) //当键盘被按下时,数字电路为0
{
delay_tm(10);
if(S7 == 0)
{
S7_flag = 1;
}
while(!S7);
}
else if(S6 == 0)
{
delay_tm(10);
if(S6 == 0)
{
S6_flag = 1;
}
while(!S6);
}
else if(S5 == 0)
{
delay_tm(10);
if(S5 == 0)
{
S5_flag = 1;
}
while(!S5);
}
else if(S4 == 0)
{
delay_tm(10);
if(S4 == 0)
{
S4_flag = 1;
}
while(!S4);
}
}
void delay_tm(uint ms)
{
uint i , j;
for(i = 0; i < 853; i++)
for(j = 0; j < ms; j++);
}
//4、将所有的数码管显示“ - ”,按下S7键第一个数码管显示数字“1”,按下S6按键,第二个数码管显示数字“2”,按下S5键后点亮L1灯,按下S4键后第5、6、7位数码管从260->0进行倒计时,当倒计时为240时L8灯点亮
#include<stc15.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit S7 = P3^0 ;
sbit S6 = P3^1 ;
sbit S5 = P3^2 ;
sbit S4 = P3^3 ; //对按键的定义
uint sum = 260;
uint num = 0;
uchar S7_flag = 0; //设置标志位
uchar S6_flag = 0;
uchar S5_flag = 0;
uchar S4_flag = 0;
uchar SEG_Run = 0;
uchar one,two,three,four,five,six,seven,eight;
uchar SEG1,SEG2,SEG3,SEG4,SEG5,SEG6,SEG7,SEG8;
uchar tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};//数码管数字显示的数组(数据库)
void keypad();
void delay_tm(uint ms);
void SEG(one,two,three,four,five,six,seven,eight);
void main()
{
P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
P2 = 0XC0; P0 = 0XFF; P2 = 0XFF; P0 = 0XFF;
SEG1=10,SEG2=10,SEG3=10,SEG4=10,SEG5=10;SEG6=10,SEG7=10,SEG8=10;//初始化,将所有数码管编写成“ - ”。
while(1)
{
keypad(); //调用按键是否按下函数
if(S7_flag == 1){
S7_flag = 0;
SEG1 = 1; //将SEG1赋为1即第一个数码管的数字为1
}
if(S6_flag == 1){
S6_flag = 0;
SEG2 = 2;
}
if(S5_flag == 1){
S5_flag = 0;
P2 = 0X80 ; P0 = 0XFE;
}
if(S4_flag == 1){
S4_flag = 0;
if(SEG_Run == 0)
SEG_Run = 1;
}
if(SEG_Run == 1){ //倒计时函数(使用SEG_Run标志位进行标记)
if(++num == 10){
num = 0;
sum--;
SEG5 = sum/100; //百位
SEG6 = sum/10%10; //十位
SEG7 = sum%10; //个位
if(sum == 240){ //当数码管显示的数值为240时,打开第八个LED灯
P2 = 0X80 ; P0 = 0X7F;
}
}
}
SEG(SEG1,SEG2,SEG3,SEG4,SEG5,SEG6,SEG7,SEG8);
}
}
void SEG(one,two,three,four,five,six,seven,eight) //将数码管所需显示的数字封装为函数,方便后续使用。
{
P2 = 0XC0; P0 = 0X01; P2 = 0XFF; P0 = tab[one];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X02; P2 = 0XFF; P0 = tab[two];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X04; P2 = 0XFF; P0 = tab[three];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X08; P2 = 0XFF; P0 = tab[four];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X10; P2 = 0XFF; P0 = tab[five];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X20; P2 = 0XFF; P0 = tab[six];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X40; P2 = 0XFF; P0 = tab[seven];
delay_tm(1);
P0=0XFF;
P2 = 0XC0; P0 = 0X80; P2 = 0XFF; P0 = tab[eight];
delay_tm(1);
P0=0XFF;
}
void keypad() //判断按键是否按下
{
if(S7 == 0){
delay_tm(10);
if(S7 == 0){
S7_flag = 1;
}
while(!S7);
}
if(S6 == 0){
delay_tm(10);
if(S6 == 0){
S6_flag = 1;
}
while(!S6);
}
if(S5 == 0){
delay_tm(10);
if(S5 == 0){
S5_flag = 1;
}
while(!S5);
}
if(S4 == 0){
delay_tm(10);
if(S4 == 0){
S4_flag = 1;
}
while(!S7);
}
}
void delay_tm(uint ms) //时间函数
{
uint i , j;
for(i = 0; i < 853; i++)
for(j = 0; j < ms; j++);
}