小小玩了一下LED灯,实现了从左到右亮,再从右到左,然后再亮7个灭一个,左右循环···算是小试牛刀···开发板用的51HEI的
#include <reg52.h> //52系列单片机头文件
#include <51hei.H>
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit WEI2=P2^7; //U2 74HC573的LE端 数码管的位选端
sbit WEI1=P2^6; //U1 的LE端 点阵LED的位选端
sbit SJ=P1^4; //LED发光管的使能端
sbit LED = P0^6;
uint8 j=0;
void dddd(uint8 i, uint8 dir);
void main(void)
{
uint8 counter=0;
uint8 j=0;
uint8 lightType=0;
uint8 cc=0;
TH0 = 0x3C;
TL0 = 0xB0;
TR0 = 1;
TMOD = 0×01; //计时模式选01模式
SJ=0; //三极管基极应该是低电平
WEI2 = 1; //打开U2锁存器
P0 = 0xff; //设置成全1,高电平,关闭数码管
WEI2 =0 ; //保持锁存器
WEI1 = 1; //打开U1锁存器
P0 = 0xff; //设成全1,高电平,关闭点阵LED
WEI1 = 0; ///保持锁存器
while(1)
{
if(TF0==1) //每次计时是50ms,达到50ms后计时器0的溢出位位1,进行软件清零和计时器初始化.
{
counter++;
TF0=0;
TH0 = 0x3C; //12MHZ的晶振算出来是从15536开始计时,十六进制就是 0x3CB0
TL0 = 0xB0; //高位取0x3C,低位取0xB0
}
if(counter==20) //20*50ms=1000ms=1s
{
counter=0;
switch(lightType)
{
case 0: dddd(1,3);
break;
case 1: dddd(128,1);
break;
case 2: dddd(1,2);
break;
case 3: dddd(128,0);
break;
default: break;
}
cc++;
if(cc==8)
{
cc=0;
lightType++;
if(lightType==4)
{
lightType=0;
}
}
}
}
}
////////////////////////////////////////////////////
// 第一个参数,要位移的数
// 第二个参数,位移的方向和取反的选项
/////////////////////////////////////////////////////
void dddd(uint8 i, uint8 dir)
{
if(dir==0)
{
P0=i>>j++;
}
else if(dir==1)
{
P0=~(i>>j++);
}
else if(dir==2)
{
P0=i< |