小小玩了一下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< |