您的位置:首页>投资 >

第5章-定时器与数码管基础

2023-08-22 07:43:04    来源:哔哩哔哩

软件清零,写程序控制

硬件清零,cpu自动完成


(相关资料图)

第一次学会定时器,之前RZ-51和TX-1C都没学会

课后第三题

#include <>

typedef unsigned char uchar;

typedef unsigned int uint;

typedef unsigned long ulong;

sbit ADDR0 = P1 ^ 0;

sbit ADDR1 = P1 ^ 1;

sbit ADDR2 = P1 ^ 2;

sbit ADDR3 = P1 ^ 3;

sbit ENLED = P1 ^ 4;

uchar code LedShift[] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F,

0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD};

void main(void)

{

uchar cnt = 0;

uchar i = 0;

ENLED = 0; // 138总开关打开

ADDR3 = 1; // led选通

ADDR2 = 1;

ADDR1 = 1;

ADDR0 = 0;

TMOD = 0x01;

TH0 = 0xB8;

TL0 = 0;

TR0 = 1;

while (1)

{

if (TF0 == 1)

{

TF0 = 0;

TH0 = 0xB8;

TL0 = 0;

P0 = LedShift[i];

if (++cnt >= 25)

{

cnt = 0;

if (++i == 14)

i = 0;

}

}

}

}

课后第五题

#include <>typedef unsigned char uchar;typedef unsigned int uint;typedef unsigned long ulong;sbit ADDR0 = P1 ^ 0;sbit ADDR1 = P1 ^ 1;sbit ADDR2 = P1 ^ 2;sbit ADDR3 = P1 ^ 3;sbit ENLED = P1 ^ 4;uchar code LedChar[] = {    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};void main(void){    uchar cnt = 0;    uchar sec = 15;    ENLED = 0; // 138总开关打开    ADDR3 = 1; // 1000是数码管的译码输出:8    ADDR2 = 0;    ADDR1 = 0;    ADDR0 = 0;    TMOD = 0x01;    TH0 = 0xB8;    TL0 = 0;    TR0 = 1;    while (1)    {        if (TF0 == 1)        {            TF0 = 0;            TH0 = 0xB8;            TL0 = 0;            P0 = LedChar[sec];            if (++cnt >= 50)            {                cnt = 0;                sec--;                sec &= 0x0F; //[0-15]            }        }    }}

关键词:

相关阅读