• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个C++初学者
博客园    首页    新随笔    联系   管理    订阅  订阅

2017.11.19 C语言基础及流水灯实现

/* 从右往左*/

#include <reg52.h>

sbit ADDR0 = P1^0;

sbit ADDR1 = P1^1;

sbit ADDR2 = P1^2;

sbit ADDR3 = P1^3;

sbit ENLED = P1^4;

void main()

{

  unsigned int count = 0;

  unsigned int cnt = 1;

  ADDR0=0;

  ADDR1=1;

  ADDR2=1;

  ADDR3=1;

  ENLED=0;

  while(1)

  {

  P0=~(0x01<<cnt);

  for(count=0;count<30000;count++);

  cnt++;

    if(cnt>=8)

      {

      cnt=0;

      }

  }

 

}

 

 

/*来回闪烁*/   自己编写

#include <reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void main()
{
  unsigned int count = 0;
  unsigned int cnt = 0;
 unsigned int i=0;
  ADDR0=0;
  ADDR1=1;
  ADDR2=1;
  ADDR3=1;
  ENLED=0;
 for(cnt=0;cnt<8;cnt++)
 {
  P0=~(0x01<<cnt);
  for(count=0;count<30000;count++);
 }
 if(cnt>=7)
 {
  for(i=0;i<8;i++)
  {
   P0=~(0x80>>i);
  for(count=0;count<30000;count++);
  }
 }
 
}

 

 

/*来回闪烁*/     参考

#include <reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void main()
{
  unsigned int i=0;
  unsigned int j;
  unsigned int k=0x01;
  ADDR0=0;
  ADDR1=1;
  ADDR2=1;
  ADDR3=1;
  ENLED=0;
  while(1)
  {
   P0=~k;
   for(j=0;j<30000;j++);
   if(i==0)
   {
    k=k<<1;
    if(k==0x80)
     i=1;
   }
   else
   {
   k=k>>1;
    if(k==0x01)
     i=0;
   }
  }
}

posted @ 2017-11-19 19:19  一个C++初学者  阅读(1414)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3