对串口进行了深层次的理解了解到之前还有许多欠缺发现跟着教程学习的程序不能完全代表自己对程序的理解相反自己还会有许多欠缺
应该尽量先尝试自己写一遍才能更好的巩固知识
利用串口控制流水灯(基本与前面学的相差无几)
#include<reg52.h>
unsigned char code ledchar[]= {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char ledbuff[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
void ConfigTimer0(unsigned int ms);
void ConfigUART(unsigned int baud);
void Lightflow();
unsigned char rxdbyte=0;
unsigned char flaglight=1;
unsigned char flaglight200=0;
unsigned char T0RH=0;
unsigned char T0RL=0;
void main()
{
ConfigTimer0(1);
ConfigUART(9600);
while(1)
{
if(flaglight==0)
{
ledbuff[6]=0xFF;
}
else
{
if(flaglight200!=0)
{
flaglight200=0;
Lightflow();
}
}
ledbuff[0] = ledchar[rxdbyte & 0x0F];
ledbuff[1] = ledchar[rxdbyte>> 4];
}
}
void Lightflow()
{
static unsigned char dir=0;
static unsigned char shift=0x01;
ledbuff[6]=~shift;
if(dir==0)
{
shift=shift<<1;
if(shift==0x80)
{
shift=0x01;
dir=1;
}
}
else
{
shift=shift>>1;
if(shift==0x01)
{
shift=0x80;
dir=0;
}
}
}
void LedScan()
{
static unsigned char i=0;
P0=0xFF;
P1=(P1&0xF8)|i;
P0=ledbuff[i];
if(i<6)
i++;
else
i=0;
}
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp;
tmp=11059200/12;
tmp=(tmp*ms)/1000;
tmp=65536-tmp;
tmp=tmp+18;
T0RH=(unsigned char)(tmp>>8);
T0RL=(unsigned char)tmp;
TMOD=TMOD&0xF0;
TMOD=TMOD&0x01;
TH0=T0RH;
TL0=T0RL;
ET0=1;
TR0=1;
}
void ConfigUART(unsigned int baud)
{
SCON=0X50;
TMOD=TMOD&0x0F;
TMOD=TMOD|0x01;
TH1=256-(11059200/12/32)/baud;
TL1=256-TH1;
ET1=0;
ES=1;
TR1=1;
}
void InterruptTimer0() interrupt 1
{
unsigned char tmr;
TH0=T0RH;
TL0=T0RL;
LedScan();
tmr++;
if(tmr>=200)
{
tmr=0;
flaglight200=1;
}
}
void InterruptUART() interrupt 4
{
if(RI)
{
RI=0;
rxdbyte=SBUF;
SBUF=rxdbyte;
flaglight = !flaglight;
}
if(TI)
{
TI=0;
}
}
|