[nRF51822] 8、基础实验代码解析大全 · 实验11 - PPI
前一篇分析了前十个基础实验的代码,从这里开始分析后十个~
一、PPI原理:
PPI(Programmable Peripheral Interconnect),中文翻译为可编程外设互连。
在nRF51822 内部设置了PPI 方式,可以通过任务和事件让不同外设之间进行互连,而不需要CPU 进行参与。
PPI 通过通道让任务和事件连接在一起。PPI 通道由两个端点组成:
- 任务端点:Task End-Point (TEP)。
- 事件端点:Event End-Point (EEP)。
所谓的互联就是将任务端点写入需要连接的任务寄存器地址,事件端点写入需要连接事件寄存器地址,之后,使能该PPI 通道,即实现了任务和事件的互联。
可以通过如下两种方式使能和关闭PPI 通道:
- 1) 通过独立设置CHEN,CHENSET 和CHENCLR 寄存器。
- 2) 通过PPI 通道组的使能和关闭任务。使用这种方式,在触发任务之前,需要先配置好哪些PPI 通道属于哪个组。
二、运行逻辑:
实验中,用到了3 个定时器:Timer 0、Timer 1 和Timer 2。
1) Timer 0 配置为计数器,在主循环中每100ms 被触发一次,并通过串口打印出计数值。
2) Timer 1 每个偶数秒(2、4、6、8……)产生一次比较匹配事件,该事件通过PPI通道0 和Timer 0 的STOP Task 互联,互联后通过该事件触发Timer 0 的STOP Task。
3) Timer 2 每个奇数秒(1、3、5、7……)产生一次比较匹配事件,该事件通过PPI通道1 和Timer 0 的START Task 互联,互联后通过该事件触发Timer 0 的START Task。
实验原理框图如图1 所示:

三、核心代码分析
系统运行后,在循环中Timer 0 计数器的计数值每100ms 增加一次,在偶数秒时,Timer2 产生比较匹配事件,通过PPI 触发Timer 0 的STOP Task,Timer 0 停止计数。此时,尽管主循环中每隔100ms 触发一次Timer 0 计数,但是由于Timer 0 已经停止,所以,计数值不会增加。每个奇数秒,Timer2 产生比较匹配事件,通过PPI 触发Timer 0 的START Task,Timer 0 恢复计数。
main函数部分:
int main(void)
{
timer0_init(); // Timer used to blink the LEDs.
timer1_init(); // Timer to generate events on even number of seconds.
timer2_init(); // Timer to generate events on odd number of seconds.
ppi_init(); // PPI to redirect the event to timer start/stop tasks. 串口初始化(略) // Enabling constant latency as indicated by PAN 11 "HFCLK: Base current with HFCLK
// running is too high" found at Product Anomaly document found at
// https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
//
// @note This example does not go to low power mode therefore constant latency is not needed.
// However this setting will ensure correct behaviour when routing TIMER events through
// PPI (shown in this example) and low power mode simultaneously.
NRF_POWER->TASKS_CONSTLAT = ; // Start clock.
nrf_drv_timer_enable(&timer0);
nrf_drv_timer_enable(&timer1);
nrf_drv_timer_enable(&timer2); // Loop and increment the timer count value and capture value into LEDs. @note counter is only incremented between TASK_START and TASK_STOP.
while (true)
{ printf("Current cout: %d\r\n", (int)nrf_drv_timer_capture(&timer0,NRF_TIMER_CC_CHANNEL0)); /* increment the counter */
nrf_drv_timer_increment(&timer0); nrf_delay_ms();
}
}
定时器初始化部分:
// Timer even handler. Not used since timer is used only for PPI.
void timer_event_handler(nrf_timer_event_t event_type, void * p_context){} /** @brief Function for Timer 0 initialization, which will be started and stopped by timer1 and timer2 using PPI.
*/
static void timer0_init(void)
{
ret_code_t err_code = nrf_drv_timer_init(&timer0, NULL, timer_event_handler);
APP_ERROR_CHECK(err_code);
} /** @brief Function for Timer 1 initialization.
* @details Initializes Timer 1 peripheral, creates event and interrupt every 2 seconds,
* by configuring CC[0] to timer overflow value, we create events at even number of seconds
* for example, events are created at 2,4,6 ... seconds. This event can be used to stop Timer 0
* with Timer1->Event_Compare[0] triggering Timer 0 TASK_STOP through PPI.
*/
static void timer1_init(void)
{
// Configure Timer 1 to overflow every 2 seconds. Check TIMER1 configuration for details
// The overflow occurs every 0xFFFF/(SysClk/2^PRESCALER).
// = 65535/31250 = 2.097 sec
ret_code_t err_code = nrf_drv_timer_init(&timer1, NULL, timer_event_handler);
APP_ERROR_CHECK(err_code); nrf_drv_timer_extended_compare(&timer1, NRF_TIMER_CC_CHANNEL0, 0xFFFFUL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);//比较模式,Timer 1 每个偶数秒(2、4、6、8……)产生一次比较匹配事件,该事件通过PPI通道0 和Timer 0 的STOP Task 互联,互联后通过该事件触发Timer 0 的STOP Task。
} /** @brief Function for Timer 2 initialization.
* @details Initializes Timer 2 peripheral, creates event and interrupt every 2 seconds
* by configuring CC[0] to half of timer overflow value. Events are created at odd number of seconds.
* For example, events are created at 1,3,5,... seconds. This event can be used to start Timer 0
* with Timer2->Event_Compare[0] triggering Timer 0 TASK_START through PPI.
*/
static void timer2_init(void)
{
// Generate interrupt/event when half of time before the timer overflows has past, that is at 1,3,5,7... seconds from start.
// Check TIMER1 configuration for details
// now the overflow occurs every 0xFFFF/(SysClk/2^PRESCALER)
// = 65535/31250 = 2.097 sec */
ret_code_t err_code = nrf_drv_timer_init(&timer2, NULL, timer_event_handler);
APP_ERROR_CHECK(err_code); nrf_drv_timer_extended_compare(&timer2, NRF_TIMER_CC_CHANNEL0, 0x7FFFUL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);//Timer 2 每个奇数秒(1、3、5、7……)产生一次比较匹配事件,该事件通过PPI通道1 和Timer 0 的START Task 互联,互联后通过该事件触发Timer 0 的START Task。
}
PPI连接事件部分:
/** @brief Function for initializing the PPI peripheral.
*/
static void ppi_init(void)
{
uint32_t err_code = NRF_SUCCESS; err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code); // Configure 1st available PPI channel to stop TIMER0 counter on TIMER1 COMPARE[0] match, which is every even number of seconds.
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1);
APP_ERROR_CHECK(err_code);
13 err_code = nrf_drv_ppi_channel_assign(ppi_channel1,//PPI连接事件
14 nrf_drv_timer_event_address_get(&timer1, NRF_TIMER_EVENT_COMPARE0),
15 nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_STOP));
APP_ERROR_CHECK(err_code); // Configure 2nd available PPI channel to start timer0 counter at TIMER2 COMPARE[0] match, which is every odd number of seconds.
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel2);
APP_ERROR_CHECK(err_code);
21 err_code = nrf_drv_ppi_channel_assign(ppi_channel2,
22 nrf_drv_timer_event_address_get(&timer2, NRF_TIMER_EVENT_COMPARE0),
23 nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_START));
APP_ERROR_CHECK(err_code); // Enable both configured PPI channels
err_code = nrf_drv_ppi_channel_enable(ppi_channel1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel2);
APP_ERROR_CHECK(err_code);
}
@beautifulzzzz - 物联网&普适计算实践者
e-mail:beautifulzzzz@qq.com
i-blog:blog.beautifulzzzz.com
[nRF51822] 8、基础实验代码解析大全 · 实验11 - PPI的更多相关文章
- [nRF51822] 12、基础实验代码解析大全 · 实验19 - PWM
一.PWM概述: PWM(Pulse Width Modulation):脉冲宽度调制技术,通过对一系列脉冲的宽度进行调制,来等效地获得所需要波形. PWM 的几个基本概念: 1) 占空比:占空比是指 ...
- [nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写
一.实验内容: 通过串口发送单个字符到NRF51822,NRF51822 接收到字符后将其写入到FLASH 的最后一页,之后将其读出并通过串口打印出数据. 二.nRF51822芯片内部flash知识 ...
- [nRF51822] 10、基础实验代码解析大全 · 实验15 - RTC
一.实验内容: 配置NRF51822 的RTC0 的TICK 频率为8Hz,COMPARE0 匹配事件触发周期为3 秒,并使能了TICK 和COMPARE0 中断. TICK 中断中驱动指示灯D1 翻 ...
- [nRF51822] 9、基础实验代码解析大全 · 实验12 - ADC
一.本实验ADC 配置 分辨率:10 位. 输入通道:5,即使用输入通道AIN5 检测电位器的电压. ADC 基准电压:1.2V. 二.NRF51822 ADC 管脚分布 NRF51822 的ADC ...
- [nRF51822] 7、基础实验代码解析大全(前十)
实验01 - GPIO输出控制LED 引脚输出配置:nrf_gpio_cfg_output(LED_1); 引脚输出置高:nrf_gpio_pin_set(LED_1); 引脚电平转换:nrf_gpi ...
- 基础Gan代码解析
initializer总结: #f.constant_initializer(value) 将变量初始化为给定的常量,初始化一切所提供的值. #tf.random_normal_initializer ...
- MYSQL常见出错mysql_errno()代码解析
如题,今天遇到怎么一个问题, 在理论上代码是不会有问题的,但是还是报了如上的错误,把sql打印出來放到DB中却可以正常执行.真是郁闷,在百度里面 渡 了很久没有相关的解释,到时找到几个没有人回复的 & ...
- 【原创】大数据基础之Spark(5)Shuffle实现原理及代码解析
一 简介 Shuffle,简而言之,就是对数据进行重新分区,其中会涉及大量的网络io和磁盘io,为什么需要shuffle,以词频统计reduceByKey过程为例, serverA:partition ...
- 【原创】大数据基础之Spark(4)RDD原理及代码解析
一 简介 spark核心是RDD,官方文档地址:https://spark.apache.org/docs/latest/rdd-programming-guide.html#resilient-di ...
随机推荐
- redhat6下安装Lighttpd1.4.43
学完了C语言,自信满满地冲着开源软件去了,首选了Lighttpd,这个软件代码量不多,适合初入开源的朋友 redhat下安装Lighttpd,一定要先安装依赖库,pcre和bzip2,这两个自行下载, ...
- PHP环境搭建
1.安装wampserver,安装成功后浏览器中输入:localhost可以打开下面的页面 2.想配置本机IP打开文件的话,单击wampserver-Apache-httped.conf,打开文件,修 ...
- 机器学习之K-近邻算法
机器学习可分为监督学习和无监督学习.有监督学习就是有具体的分类信息,比如用来判定输入的是输入[a,b,c]中的一类:无监督学习就是不清楚最后的分类情况,也不会给目标值. K-近邻算法属于一种监督学习分 ...
- C语言中使用系统自带的快排函数
题目 . 德才论 () 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取 ...
- JAVA正则表达式中如何匹配反斜杠 \
有时候我们需要匹配反斜杠,你可能会把对应的正则表达式写成 "\\" 然后可能会有如下输出: Exception in thread "main" java.ut ...
- Visual Studio 常用快捷键
作为一个使用VisualStudio的程序员,使用快捷键会为你的开发提供助力. 下附个人开发过程中感觉比较实用的快捷键: 开始运行"devenv",启动相应版本的VisualStu ...
- pair的使用
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...
- Android 学习笔记之一 “Unable to establish loopback connection”
今天碰到一个错误:Unable to establish loopback connection,在网上找各种方法都解决不了,后来看一个帖子说是要关闭系统防火墙,尝试了下还是不行.最后是进任务管理器杀 ...
- Junit测试框架 Tips
关于Junit测试框架使用的几点总结: 1.Junit中的测试注解: @Test →每个测试方法前都需要添加该注解,这样才能使你的测试方法交给Junit去执行. @Before →在每个测试方法执行前 ...
- UDP和TCP的区别
UDP(User Datagram Protocol 用户数据报协议) TCP(Transmission Control Protocol 传输控制协议) UDP是一种非面向连接的传输协议,它的实现是 ...