C51联盟 —— 点灯

版权声明: https://blog.csdn.net/JRK_CSDN/article/details/84866204
/* ****************************************************************************************************
- 文件名:第一讲:LED流水灯.c
- 开发板:普中HC6800-ES V2.0  @STC 89C52RC
- 作者:  C51联盟
- 时间: 2018-11-28
**************************************************************************************************** */
#include<reg52.h> //这个头文件里定义了每组I/O口的寄存器地址,我们只需要用类似P0的代号即可,也可像下面一样使用sbit来使用一位I/O口
#include<intrins.h> //子函数Delay1000ms()中_nop()_需要使用这个头文件,不加这个头文件,不能编译
#define LED P2

sbit P1_0 = P1^0; //定义一位I/O口

void Delay1000ms(); //延时1000ms函数
void FlowLED(); //流水灯函数

void main()
{
  P1_0 = 0; //关闭74HC245来关闭数码管
  while(1)     
  {FlowLED();}
}

void FlowLED()
{
  static u8 buff=0; //定义 静态 标志位
  static u8 shift=0x01; //定义 静态 数据位
  P2 = ~shift;
  if(buff == 0)
{
  shift <<= 1;
  if(shift == 0x80) //到达最左
  buff = 1;   //更换标志位
}
 else
{
  shift >>= 1;
  if(shift == 0x01) //到达最右
  buff = 0;  //更换标志位
}
 Delay1000ms();
}

void Delay1000ms() //@12.000MHz,可从STC-ISP中选择STC-Y1后得到
{
  unsigned char i, j, k;

  _nop_();
  i = 8;
  j = 154;
  k = 122;
  do
  {
    do
    {
       while (--k);
   } while (--j);
 } while (--i);
}

猜你喜欢

转载自blog.csdn.net/JRK_CSDN/article/details/84866204
C51