[nRF51822]2. 跑马灯

硬件电路

 

 实现功能

1. 四个LED轮流闪烁,实现跑马灯效果

2. 四个LED同时闪烁

3. 四个LED同时熄灭

4. 循环上面的1,2,3

 

代码

 

 main.c

#include "nrf_delay.h"
#include "nrf_gpio.h"

#define LED_START    21
#define LED_1    21                       /*P0.21连接LED_1*/
#define LED_2    22
#define LED_3    23
#define LED_4    24
#define LED_STOP    24

int main(void)
{
    int i= 0;
    
    nrf_gpio_range_cfg_output(LED_START,LED_STOP);    //配置P0.21 ~ P0.24为输出
    
    nrf_gpio_pin_set(LED_1);                        //设置LED熄灭
    nrf_gpio_pin_set(LED_2);
    nrf_gpio_pin_set(LED_3);
    nrf_gpio_pin_set(LED_4);
        
    
    while(1)
    {
        //所有LED轮流闪烁3次
        for(i=0;i<3;i++)
        {
            nrf_gpio_pin_toggle(LED_1);        //设置单个引脚电平翻转
            nrf_delay_ms(200);                //延时200ms(非精确延时)
            nrf_gpio_pin_toggle(LED_2);        //设置单个引脚电平翻转
            nrf_delay_ms(200);                //延时200ms(非精确延时)
            nrf_gpio_pin_toggle(LED_3);        //设置单个引脚电平翻转
            nrf_delay_ms(200);                //延时200ms(非精确延时)
            nrf_gpio_pin_toggle(LED_4);        //设置单个引脚电平翻转
            nrf_delay_ms(200);                //延时200ms(非精确延时)
        }
        
        //所有LED同时闪烁3次
        for(i=0;i<3;i++)
        {
            nrf_gpio_pin_set(LED_1);
            nrf_gpio_pin_set(LED_2);
            nrf_gpio_pin_set(LED_3);
            nrf_gpio_pin_set(LED_4);
            
            nrf_delay_ms(200);
            
            nrf_gpio_pin_clear(LED_1);
            nrf_gpio_pin_clear(LED_2);
            nrf_gpio_pin_clear(LED_3);
            nrf_gpio_pin_clear(LED_4);
            nrf_delay_ms(200);
        }
        
        //熄灭所有LED
        nrf_gpio_pin_set(LED_1);                        //设置LED熄灭
        nrf_gpio_pin_set(LED_2);
        nrf_gpio_pin_set(LED_3);
        nrf_gpio_pin_set(LED_4);
        nrf_delay_ms(500);
        
    }
}

 

知识点

void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)

  配置多个相邻引脚号的引脚为输出,包含起始引脚号和末尾引脚号

 

void nrf_gpio_pin_clear(uint32_t pin_number)

  设置单个引脚为低电平

 

void nrf_gpio_pin_set(uint32_t pin_number)

  设置单个引脚为高电平

 

void nrf_gpio_pin_toggle(uint32_t pin_number)

  设置单个引脚输出电平翻转

 

void nrf_delay_ms(uint32_t volatile number_of_ms)

  非精确延时

 

posted @ 2020-10-26 13:06  doitjust  阅读(232)  评论(0编辑  收藏  举报