微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机数码管显示数字为流水灯亮的个数

单片机数码管显示数字为流水灯亮的个数

时间:12-02 来源:互联网 点击:
本程序所用的原理图下载:点这里,单片机芯片使用的stc89c52;

/**
*Function:数码管显示数字为流水灯亮的个数
*Author: 徐冉
*Time: 2013-07-28
**/
/*************基于AT89C52-RC MCU************/
/************基于HL-1开发板****************/

#include
typedef unsigned int uint;
typedef unsigned char uchar;

/*数码管锁存器位声明*/
sbit wela = P2^7;
sbit dula = P2^6;

/*数码管编码表*/
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};

/*全局变量定义*/
uchar num, counter;

/*延时子程序*/
void delay(uint xms)
{
uint x, y;
for(x = xms; x > 0; x--)
for(y = 125; y > 0; y--);
}

/*定时器T0初始化程序*/
void Time0_init()
{
P1 = 0xff;//关闭led
TMOD = 0x01;//设置定时器T0工作方式0
TH0 = 0xB8;//设定T0定时20ms
TL0 = 0x00;
TR0 = 1;//打开定时器T0
EA = 1;//开总中断
ET0 = 1;//开定时器T0中断
}

/*数码管显示子程序*/
void Display(uint num)
{
uchar shi, ge;

shi = num / 10 % 10;
ge = num % 10;

dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(5);

dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(5);
}

/*主程序main()*/
void main(void)
{
Time0_init();
while(1)
{
if(num == 0)
{
P1 = 0xff;//1111 1111
}
else if(num == 1)
{
P1 = 0xfe;//1111 1110
}
else if(num == 2)
{
P1 = 0xfc; //1111 1100
}
else if(num == 3)
{
P1 = 0xf8;//1111 1000
}
else if(num == 4)
{
P1 = 0xf0;//1111 0000
}
else if(num == 5)
{
P1 = 0xe0;//1110 0000
}
else if(num == 6)
{
P1 = 0xc0;//1100 0000
}
else if(num == 7)
{
P1 = 0x80;//1000 0000
}
else if(num == 8)
{
P1 = 0x00;//0000 0000
}
Display(num);
}
}

/*定时器T0中断服务子程序*/
void Time0_int() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(counter == 50)
{
counter = 0;
num++;
if(num > 8)
{
num = 0;
}
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top