【单片机】数码管应用

一、操作目的

(1)简单的数码管应用。
(2)60进制操作练习。

二、代码详解(简单应用)

#include <reg51.h>
#include <absacc.h>
#define uchar unsigned char
#define com8255 XBYTE[0xff23]//宏定义口地址
#define pa8255 XBYTE[0xff20]
#define pb8255 XBYTE[0xff21]
//unsigned char code mon51[3] _at_ 0x3b;//保留0X3B开始三个程序空间作全速中断用
uchar code table[20] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0xFF, 0x0C, 0x89, 0xDE};
uchar code wei[] = {0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

void delay(unsigned int i)
{
unsigned int j, k;
for (k = 0; k < i; k++)
for (j = 0; j < 100; j++)
;
}

void main(void)
{
uchar m = 0;
com8255 = 0x80; //控制口赋值
pa8255 = 0x00; //PA初始化
pb8255 = 0x00; //PB口初始化
delay(2);

while (1)
{
pb8255 = table[m];
pa8255 = ~wei[m];
delay(2);
m++;
if (m == 6)
{
m = 0;
}
}
}

三、代码详解(60进制)

#include <reg51.h>
#include <absacc.h>

#define uchar unsigned char
#define com8255 XBYTE[0xff23]//宏定义口地址
#define pa8255 XBYTE[0xff20]
#define pb8255 XBYTE[0xff21]
//unsigned char code mon51[3] _at_ 0x3b;//保留0X3B开始三个程序空间作全速中断用
uchar code table[20] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0xFF, 0x0C, 0x89, 0xDE}; //0x0C p.
uchar code wei[] = {0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
uchar code myTable[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; //0123456789

uchar code mywei[] = {0x02, 0x01};
void delay(unsigned int i)
{
unsigned int j, k;
for (k = 0; k < i; k++)
for (j = 0; j < 100; j++)
;
}

void main(void)
{
uchar m = 0;
uchar midM = 0;
com8255 = 0x80; //控制口赋值
pa8255 = 0x00; //PA初始化
pb8255 = 0x00; //PB口初始化
delay(2);

while (1)
{
unsigned int j = 0;
while (j < 150)
{
pb8255 = myTable[m % 10];
pa8255 = ~wei[5];
delay(2);
pb8255 = myTable[m / 10];
pa8255 = ~wei[4];
delay(2);
j++;
}
m++;
if (m == 60)
{
m = 0;
}
}
}