LED流水灯
大概思路:
①在原理图上找到LED原理图
LED的亮灭由锁存器控制。当使能端(LE)为高电平时,左右两侧信号相同,当使能端(LE)为低电平时,右侧信号不变,维持之前的状态。所以我得找到控制LE(Y4C)的部分。
②74HC02是一个或非门,WR接地,因此当Y4为低电平时,Y4C为高电平。
③通过74HC138译码器,选择Y4置零,通过或非门,来将锁存器LE(使能端)打开,P27 = 1, P26 = 0, P25 = 0。
方法:
- 打表
- 位移运算(<<和>>)
#include <REGX52.H> #include <intrins.h> sbit HC138_C = P2^7; sbit HC138_B = P2^6; sbit HC138_A = P2^5; //延时函数 void Delay(unsigned char t) //@12.000MHz { unsigned char i, j; while(t) { _nop_(); _nop_(); i = 12; j = 168; do { while (--j); } while (--i); t--; } } void LEDrunning() { unsigned int i; HC138_C = 1; HC138_B = 0; HC138_A = 0; //选择Y4置零,通过或非门,来将锁存器LE(使能端)打开。 for(i = 0; i <= 8; i++) { P0= ~(0x80 >> i); Delay(500); } /* for(i = 0; i <= 8; i++) { P0= ~(0x01 << i); Delay(500); } */ } void main() { while(1) { LEDrunning(); } }
- 调用函数
#include <intrins.h>中的
_cror_(unsigned char, unsigned char)
_crol_ (unsigned char, unsigned char)
#include <REGX52.H>
#include <intrins.h>
sbit HC138_C = P2^7;
sbit HC138_B = P2^6;
sbit HC138_A = P2^5;
void Delay(unsigned char t) //@12.000MHz
{
unsigned char i, j;
while(t)
{
_nop_();
_nop_();
i = 12;
j = 168;
do
{
while (--j);
} while (--i);
t--;
}
}
void LEDrunning()
{
unsigned int i, temp = 0xfe;
HC138_C = 1;
HC138_B = 0;
HC138_A = 0;
//选择Y4置零,通过或非门,来将锁存器LE(使能端)打开。
for(i = 0; i <= 8; i++)
{
P0 = temp;
temp = _cror_(temp, 1);
Delay(500);
}
/*
for(i = 0; i <= 8; i++)
{
P0 = temp;
temp = _crol_(temp, 1);
Delay(500);
}
*/
}
void main()
{
while(1)
{
LEDrunning();
}
}