程序如下,仿真见附件。有问题请追问。
#include
#define uchar unsigned char
#define uint unsigned int
bit flag_1s=0;
uchar code tab[8]={1,2,4,8,16,32,64,128};//[第1小题用这个表]
//uchar code tab[8]={0x05,0x0a,0x50,0xa0,0xaa,0x55,0xff,0x00};//[第2小题用这个表]
void InitTimer1(void)
{
TMOD = 0x10;
TH1 = 0x3C;
TL1 = 0x0B0;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void main(void)
{
static uchar j;
InitTimer1();
P0=0;
P1=0;
P2=0;
P3=0;
flag_1s=1;
while(1)
{
if(flag_1s==1)
{
flag_1s=0;
P2=tab[j];
j++;
if(j>=8) j=0;
}
}
}
void Timer1Interrupt(void) interrupt 3
{
static uchar i;
TH1 = 0x3C;
TL1 = 0x0B0;
//add your code here!
i++;
if(i>=20)//50ms*20=1000ms
{
i=0;
flag_1s=1;
}
}