NRF52840学习历程(三)串口中断

时间在2021126,寒假放假在家好好学一学

 

开发板:初雪的100出头那块 NRF52840 EVAL KIT

下载工具:JINLK V11(最好是JLINK V9以上 也有人用JLINK OB也行,其他的下载器STLINK,DAP不建议用)

版本号: KEIL5编程环境,CMSIS为5.3.0, NRF52840的CMSIS为8.35.0

参考资料: NRF52840-Eval-Kit-Schematic.pdf(原理图)

nRF5_SDK_17.0.2_d674dde(官方例程)

nRF5_SDK_17.0.0_offline_doc(官方文档)

 

实现功能: 串口通信中断 一般是用EasyDMA来搬运数据,搬运完成后触发中断

 

先开启UARTE功能

 

初始化代码基本保持不变

const app_uart_comm_params_t comm_params =
      {
          8,
          6,
          0,
          0,
          APP_UART_FLOW_CONTROL_DISABLED,
          false,
          NRF_UART_BAUDRATE_115200
      };

变的是写入寄存器的时候, 赋予一个处理中断的函数,上一节是错误处理中断函数

	uint32_t err_code;
    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_interrupt,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

于是重点就放在了中断函数的编写上面来

串口中断产生的事件如上图, 1)接收到串口数据 2)FIFO模块出现传输问题 3)通信错误 4)发送空 5)在不用FIFO缓存时作为串口, 那么数据就放在第五个里面读取

 

代码如下

void uart_interrupt(app_uart_evt_t * p_event)
{
	uint8_t dat;
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
	
    else if (p_event->evt_type == APP_UART_DATA_READY)
	{//数据已到达串口 , 可以读数据了
		app_uart_get(&dat); //读取数据
		app_uart_put(dat); // 原路发回
	}
    else if (p_event->evt_type == APP_UART_TX_EMPTY) 
	{//发送完成
		//发送完成不知道要做什么的,可以点个灯提醒
		nrf_gpio_pin_toggle(LED0);
	}
}

可以看到 每次发送数据时 LED0灯翻转, 然后串口助手回显

 

完整代码如下:

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_drv_gpiote.h"

#include "nrf_uart.h"
#include "app_uart.h"

uint32_t LED0,LED1,LED2,LED3;
uint32_t KEY0,KEY1,KEY2,KEY3;

void KEY_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);


#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

void uart_interrupt(app_uart_evt_t * p_event);


/**
 * @brief Function for application main entry.
 */
int main(void)
{
	nrf_drv_gpiote_in_config_t key_ex_config; //按键中断配置用
	
	LED0 =  NRF_GPIO_PIN_MAP(0,13);
	LED1 =  NRF_GPIO_PIN_MAP(0,14);
	LED2 =  NRF_GPIO_PIN_MAP(1,9);
	LED3 =  NRF_GPIO_PIN_MAP(0,16);

	KEY0 =  NRF_GPIO_PIN_MAP(0,11);
	KEY1 =  NRF_GPIO_PIN_MAP(0,24);
	KEY2 =  NRF_GPIO_PIN_MAP(0,20);
	KEY3 =  NRF_GPIO_PIN_MAP(0,17);

	nrf_gpio_cfg_output(LED0);
	nrf_gpio_cfg_output(LED1);
	nrf_gpio_cfg_output(LED2);
	nrf_gpio_cfg_output(LED3);

	nrf_gpio_pin_set(LED0);
	nrf_gpio_pin_set(LED1);
	nrf_gpio_pin_set(LED2);
	nrf_gpio_pin_set(LED3);
	
	nrf_gpio_cfg_input(KEY0,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(KEY1,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(KEY2,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(KEY3,NRF_GPIO_PIN_PULLUP );
	
	nrf_drv_gpiote_init();//启动GPIOTE时钟,可以这么说
	
	key_ex_config.hi_accuracy=false; // 启用低精确度PORT事件
	key_ex_config.pull = NRF_GPIO_PIN_PULLUP ; //上啦
	key_ex_config.sense = NRF_GPIOTE_POLARITY_HITOLO ;//下降沿
	
	nrf_drv_gpiote_in_init(KEY0, &key_ex_config, KEY_Interrupt);
	nrf_drv_gpiote_in_init(KEY1, &key_ex_config, KEY_Interrupt);
	nrf_drv_gpiote_in_init(KEY2, &key_ex_config, KEY_Interrupt);
	nrf_drv_gpiote_in_init(KEY3, &key_ex_config, KEY_Interrupt);

	nrf_drv_gpiote_in_event_enable(KEY0, true);//启动KEY0中断
	nrf_drv_gpiote_in_event_enable(KEY1, true);//启动KEY1中断
	nrf_drv_gpiote_in_event_enable(KEY2, true);//启动KEY2中断
	nrf_drv_gpiote_in_event_enable(KEY3, true);//启动KEY3中断
	
	
	
    const app_uart_comm_params_t comm_params =
      {
          8,
          6,
          0,
          0,
          APP_UART_FLOW_CONTROL_DISABLED,
          false,
          NRF_UART_BAUDRATE_115200
      };
	uint32_t err_code;
    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_interrupt,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);
	  
	uint8_t str[]="hello world!\r\n";
	uint8_t i=0,str1[20];
	while(1)
	{				
//		printf("hello world! \r\n");
		
//		while(str[i]!='\0')
//		{
//			app_uart_put(str[i]);
//			i++;
//		}
//		nrf_delay_ms(1000);
//		i=0;
		
		if( NRF_SUCCESS == app_uart_get(str1))
			printf("%s\r\n",str1);
	}
	
}

void KEY_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	if(KEY0 == pin)
		nrf_gpio_pin_toggle(LED0);
	if(KEY1 == pin)
		nrf_gpio_pin_toggle(LED1);
	if(KEY2 == pin)
		nrf_gpio_pin_toggle(LED2);
	if(KEY3 == pin)
		nrf_gpio_pin_toggle(LED3);
}

void uart_interrupt(app_uart_evt_t * p_event)
{
	uint8_t dat;
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
	
    else if (p_event->evt_type == APP_UART_DATA_READY)
	{//数据已到达串口 , 可以读数据了
		app_uart_get(&dat); //读取数据
		app_uart_put(dat); // 原路发回
	}
    else if (p_event->evt_type == APP_UART_TX_EMPTY) 
	{//发送完成
		//发送完成不知道要做什么的,可以点个灯提醒
		nrf_gpio_pin_toggle(LED0);
	}
}

猜你喜欢

转载自blog.csdn.net/jwdeng1995/article/details/113183552