📄 main.c
字号:
/*
标题:范例 5-1
版本:1.0
Target:89S51
程序描述:这个范例说明如何使用8051 的Port 1连接到四颗七段显示器,PORT 0的低4位分别控制这四颗共阳七段显示器,让四颗共阳七段显示器使用扫描的方式轮流显示数字。这四颗七段显示器将显示出自数字时钟PORT 2的P2.0和P2.1分别连接到2个弹跳按钮。*/
/* ***************************************************** */
#include <REGX51.H>
#define TIMER0_COUNT -2000//0xEE18
/*10000h-(12,000,000/(12*200)) */
#define ON 0
#define OFF 1
#define HighChoose P0_3
#define LowChoose P0_7
#define NorthCount P1
#define NorthRed P0_0
#define NorthYellow P0_1
#define NorthGreen P0_2
#define WestCount P2
#define WestRed P0_4
#define WestYellow P0_5
#define WestGreen P0_6
#define Speed 21 //动态刷新速度
const number[10] = {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
const scan[4] = {0X0E,0X0D,0X0B,0X07};
static unsigned char timecount = 0;
static unsigned int timetrick = 0;
static unsigned char northnum = 0;
static unsigned char westnum = 0;
static void timer0_isr(void) interrupt 1 using 1
{
TR0=0;
TL0=(TIMER0_COUNT & 0x00FF);
TH0=(TIMER0_COUNT >> 8);
TR0=1;
timetrick++;
if(northnum < 10&&westnum < 10)
{
LowChoose = OFF;
HighChoose = ON;
NorthCount = number[northnum%10];
WestCount = number[westnum%10];
}
else
{
if(timetrick%Speed==0)
{
if((timetrick/Speed)%3!=0)
{
LowChoose = OFF;
HighChoose = ON;
NorthCount = number[northnum%10];
WestCount = number[westnum%10];
}
else
{
LowChoose = ON;
HighChoose = OFF;
if(northnum >= 10)
NorthCount = number[northnum/10];
else
NorthCount = 0xff;
if(westnum >= 10)
WestCount = number[westnum/10];
else
WestCount = 0xff;
}
}
}
if(timetrick==500)
{
timecount++;
northnum--;
westnum--;
switch(timecount)
{
case 13:
NorthRed = OFF;
NorthYellow = ON;
NorthGreen = OFF;
WestRed = ON;
WestYellow = OFF;
WestGreen = OFF;
northnum = 3;
break;
case 16:
NorthRed = ON;
NorthYellow = OFF;
NorthGreen = OFF;
WestRed = OFF;
WestYellow = OFF;
WestGreen = ON;
northnum = 16;
westnum = 13;
break;
case 29:
NorthRed = ON;
NorthYellow = OFF;
NorthGreen = OFF;
WestRed = OFF;
WestYellow = ON;
WestGreen = OFF;
westnum = 3;
break;
case 32:
NorthRed = OFF;
NorthYellow = OFF;
NorthGreen = ON;
WestRed = ON;
WestYellow = OFF;
WestGreen = OFF;
northnum = 13;
westnum = 16;
break;
}
timecount %= 32;
timetrick = 0;
}
}
static void timer0_initialize(void)
{
EA = 0;
TR0 = 0;
TMOD = 0x01;
TL0 = TIMER0_COUNT % 256;
TH0 = TIMER0_COUNT / 256;
PT0 = 0;
ET0 = 1;
TR0 = 1;
EA = 1;
}
void traffic_initialize(void)
{
NorthRed = OFF;
NorthYellow = OFF;
NorthGreen = ON;
WestRed = ON;
WestYellow = OFF;
WestGreen = OFF;
northnum = 13;
westnum = 16;
}
void main (void)
{
traffic_initialize();
timer0_initialize();
while (1); /*无穷循环 */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -