会眨眼的小灯

环境

Cortex-M0

LPC1114FBD48/301

1 均衡眨眼的小灯

源代码

/* Main.c file generated by New Project wizard
 *
 * Created:   周六 2月 15 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>
void Delay(unsigned nCount)
{
     for(;nCount!=0;--nCount);
}

int main (void)
 { 
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL |=(1<<6);
   LPC_GPIO3->DIR=0x01;
   while (1)
   {
      LPC_GPIO3->DATA=0x00;
      Delay(4000);
      LPC_GPIO3->DATA=0x01;
      Delay(4000);
   }
   return 0;
 }   

电路

仿真之后会出现“灯”交替暗灭

2 流水灯

8个发光二极管,分别连接到PIO2_0~PIO2_7

代码

/* Main.c file generated by New Project wizard
 *
 * Created:   周六 2月 15 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>
void Delay(unsigned nCount)
{
     for(;nCount!=0;--nCount);
}

int main (void)
 { 
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL |=(1<<6);
   LPC_GPIO2->DIR=0xFF; //有哪些引脚是当作输出的,1为有效 0无效,此处设置8个引脚都是有效的
   while (1)
   {
      LPC_GPIO2->DATA=0xFE;  Delay(100000); //1111 1110 0亮 
      LPC_GPIO2->DATA=0xFD;  Delay(100000); //1111 1101 1亮,以下同理
      LPC_GPIO2->DATA=0xFB;  Delay(100000);
      LPC_GPIO2->DATA=0xF7;  Delay(100000);
      
      LPC_GPIO2->DATA=0xEE;  Delay(100000);
      LPC_GPIO2->DATA=0xDE;  Delay(100000);
      LPC_GPIO2->DATA=0xBE;  Delay(100000);
      LPC_GPIO2->DATA=0x7E;  Delay(100000);
      
   }
   return 0;
 }   

原理图

仿真之后出现流水灯。

改进代码,使用移位操作,也可以实现流水灯

/* Main.c file generated by New Project wizard
 *
 * Created:   周六 2月 15 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>
void Delay(unsigned nCount)
{
     for(;nCount!=0;--nCount);
}

int main (void)
 {
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL |=(1<<6);
   LPC_GPIO2->DIR=0xFF;
   /*原代码
   while (1)
   {
     
      LPC_GPIO2->DATA=0xFE;  Delay(100000); //1111 1110
      LPC_GPIO2->DATA=0xFD;  Delay(100000);
      LPC_GPIO2->DATA=0xFB;  Delay(100000);
      LPC_GPIO2->DATA=0xF7;  Delay(100000);
      
      LPC_GPIO2->DATA=0xEE;  Delay(100000);
      LPC_GPIO2->DATA=0xDE;  Delay(100000);
      LPC_GPIO2->DATA=0xBE;  Delay(100000);
      LPC_GPIO2->DATA=0x7E;  Delay(100000);
    }*/
    
    //改进代码
    unsigned int i=0xFE,j=0;
    while(1)
    {
       for(;j<7;j++)
       {
        LPC_GPIO2->DATA=i;
        Delay(80000);
        i=~i;//第一次为0000 0001
        i=i<<1; //0000 0010
        i=~i; //1111 1101
       }
      
       LPC_GPIO2->DATA=0xFFFFFF7F;
       Delay(80000);
       i=0xFE;
       j=0;
    }
   
   return 0;
 }   

 第三步完成

原文地址:https://www.cnblogs.com/caishunzhe/p/12315028.html