文件名称:
Tutorials - Application Timer .pdf
开发工具:
文件大小: 353kb
下载次数: 0
上传时间: 2019-07-27
详细说明:Nordic BLE 开发资料,介绍App Timer的应用步骤方法,架构等等!对于蓝牙开发非常有帮助!26/04/2016
Tutorials- Nordic Developer Zone
Now go to Project-> Options for target .->C/C++. From there, add the following directories to the
Include paths
A.\\\\components\ drivers nrf\clock
.\.......components\libraries\timer
.\.\.\.\.\components\drivers_nrf \delay
Then include the required header files by adding the following lines below the existing include
statements
*include app timer.h
#include "nrf dry clock, h
Initialization
As a Soft Device is not enabled in this tutorial, the lfclK must be requested explicitly. One way of
doing this is using the Clock driver. Add the following function somewhere before main()
// Function starting the internal lfclk oscillat
// This is needed by rTcI which is used by the application timer
//(When SoftDevice is enabled the Lfclk is aLways running and this is not needed)
static void lfclk request(void)
uint32 t err code nrf dry clock init(;
APP_ERROR CHECK(err code)
nrf drv clock lfclk request (NULL)
Add a line calling Ifclk_request() in the start of the main()function
// Request LF CLOCk
lfclk request:
The Application Timer should be initialized with the App_TIMER_INIT() macro. This must be called
before any other calls to the Application Timer APl. The four parameters must be selected so that
they meet the requirements of the application
PRESCALER: will be written to the rtc1 PRESCALer register. This determines the time resolution
of the timer, and thus the amount of time it can count before it wrap around On the nRF52 the
RTC is a 24-bit counter with a 12 bit prescaler that run on the 32.768 LFCLK. The counter
increment frequency (tick rate) frTC [kHz]=32 768/(PRESCALER+1). For example, a prescaler
value of 15 means that the tick rate or time resolution is 32.768 kHz* 1/(15+1)=2.048 kHz and
the timer will wrap around every(2 24)*1/2.048 kHz=8192S
OP_QUEUES_ SIZE: determines the maximum number of events that can be queued. Let's say you are
calling the api function several times in a row to start a single shot timer, this determines how
many times you can have queued before the queue gets full
SchEduLER FUNC should be set to false when scheduler is not used as is the case in this tutorial
See the Scheduler tutorial for how to use the scheduler with the application timer.
In this tutorial you will create 2 timers. Add the following defines(which represent the parameters
described above) close to the top of your code
/ General application timer settings
#define APP TIMER PRESCALER
15 / value of the rTC1 PRESCALER register
#define APP TIMER OP QUEUE SIZE
// Size of timer operation queues
https:/devzonenordicsemi.com/tutorials/19/
37
26/04/2016
Tutorials- Nordic developer Zone
Then put this line in main() in order to initialize the app timer(after the call to Ifclk_request())
// Initialize the application timer module
APP TIMER INIT(APP TIMER PRESCALER, APP TIMER OP QUEUE SIZE, false);
Create a repeated timer
An application timer in the repeated mode will restart each time it expires. Every time it expires the
timeout handler will be called. This makes it suitable for performing a task at a regular interval,
uch as toggling a LED, which is what you will do now. In this section we will modify the application
so that button 1 starts toggling of LEd 1 using an application timer in repeated mode. Button 2 will
stop the toggling by stopping the timer.
Application timers are created using app_timer_create(). this function takes three parameters
p_timer_id: pointer to the Id of the timer, which will be populated during the execution of this
l
ca
mode: either single shot( App_TIMERMODE_SINGLE_SHoT )or repeated( APP_TIMER_MODE_REPEATED)
timeout handler: pointer to the timeout handler.
First, create a variable that can hold the timer id to be populated by app_timer_create(). Add the
following line to your code, close to the top of the file
APP_ TIMER_ DEF(m led a timer id)
Then you will have to create the timeout event handler. This will toggle led 1 every time it is called
Add the following function to your code somewhere before the main() function
// Timeout handler for the repeated time
static void timer a handler(void p context
nrf drv gpiote out toggle(LED 1 PIn);
I suggest you wrap the creation of the timer in a function, in order to keep a minimal main()
function. Then the next timer you create in this tutorial can be created within the same function
Add the following function to your code
/ Create timers
static void create timers()
uint32 t err code;
/ Create timers
err_ code= app timer create(m led a timer id,
APPT工 MER MODE REPEAT
timer a handler
APP_ERROR CHECK(err code)
Add the following to main() before the main loop in order to call the function you just made
// Create application timers
create timers o;
https:/devzone.nordicsemi.com/tutorials/19/
47
26/04/2016
Tutorials- Nordic Developer Zone
Now the timer is created, but not started. In fact, we have not specified the time between timeouts
yet. The timeout is specified in the number of ticks of the rtc1 including prescaling.
It is normally easier to relate to milliseconds than RTC ticks, and the App_ TIMER_TIcKs macro is useful
for converting a value in milliseconds to the corresponding number of ticks depending on the
prescaler settings
You will now have to modify the button_handler() that is present in the example project in order to
let button 1 start the timer. Remove the following code from button handler(
case button1P工N
nrf drv gpiote out clear(LED_1_PIN);
break;
and replace it with this snippet:
case button 1 pin
err_ code= app timer start(m led a timer id, APp TIMER_TICKS(200, APp TIMER PRESCALER), NULL);
APP ERROR CHECK(err code);
break
Declare the err_code variable before the switch statement by inserting the following line
uint32 t err code
The app_timer_start function starts the specified timer (first parameter), and specifies how many
ticks from now it shall time out(second parameter). the third parameter is a general purpose
pointer that is passed to the timeout handler It is set to null in this case as we do not use it.(It is
represented by p_context in the timeout handler timer_a_ handler() in this tutorial
With this code in place, you should be able to build the application without any errors or warnings.
Try to build and download it to the development kit(DK). then you should be able to start toggling
of LEd 1 by pressing button 1. However, once the led is toggling there is no way to stop it (other
than resetting the chip)
In order to stop the led from blinking you have to stop the timer this is done using the
app_timer_stop() function, which takes the timer Id as the only parameter. Modify the
button_handler()so that app_timer_stop() is called when button 2 is pressed, by replacing the
following code in the button _handler()
ase button2P工N
drv gpiote out set(LED_1 PIN)
with this
case button 2 pin
p(m leda timer id)
APP ERROR CHECK (err_ code);
break
With the latest addition, build the code and download it to the target. You should now be able to
stop the toggling of the led by pressing button 2. the timer can be re-started any time by pressing
button
https:/devzone.nordicsemi.com/tutorials/19/
57
26/04/2016
Tutorials- Nordic Developer Zone
Create a single shot timer
a application timer in single shot mode will expire only once. However, it can always be restarted
Moreover, the number of ticks before it times out can be set to a different value every time it is
called (this is also the case for repeated timers)
In this part of the tutorial we will let button 3 start a single shot timer that lights LED 2 when it
timeout will be incremented by 1 second every time the timer is started. We will keep the n the
times out. The first time button 3 is pressed the timer will have a timeout of 1 second. Then the
functionality of button 4 that it is used to turn off the led
Add a new timer id below the existing m_led_a_timer_id by adding the following line
APP_TIMER_ DEF(m led b timer id);
Then create the timeout handler shall light LED 2 every time it is called. add this snippet to your
code:
// Timeout handLer for the single shot timer
static void timer b handler(void p context)
nrf drv gpiote out clear(LED 2_ PIN);
Then modify the create_timers() by appending the following lines to the end of the function(note
that the mode is APP_TIMER_MODE_SINGLE_SHOT
err_code= app timer create(&m led b timer id,
APP TIMER MODE SINGLE SHOT
timer b handler )i
APP ERROR CHECK(err code );
Then update the button_handler() by removing
case BUTTON
nrf drv gpiote out clear (LED_ 2 PIN)
break i
and replacing it with
case bUtton 3 piN
// Start single shot timer. Increase the timeout with 1 second every time
timeout +=1000
err code app timer start(m led b timer id, app tIMEr TICks(timeout, APP TIMER PRESCALER),NU
APP_ ERROR_CHECK(err_code);
break
Declare the static timeout variable before the switch statement by inserting the following line:
static uint32 t timeout =0
Build the code and download it to the target. If you press button 3 you will see that LEd 2 is lit after
1 second. Then press button 4, and the Led turns dark. Now press button 3 again and observe that it
takes 2 seconds before the led is lit
https:/devzone.nordicsemi.com/tutorials/19/
67
26/04/2016
Tutorials- Nordic Developer Zone
Further Reading
See the Scheduler tutorial for how to use the scheduler with the application timer.
The Timer library section of the SDK documentation
See the Application Timer API Reference for a complete overview of the application timer APl
4 comments
thanks a lot, it is important for new guy to learn hawk
nRF51. Hope more tutorials just like these
25●7●4
series from sdk.0 to sdk 9.0, there are lots of Posted Nov 8, 2015, 1: 25 p.m.
differences. and more high level sdk are provided but maybe for new guy, it is
very hard to understand the detail of low leveL.
It was very hard(almost impossible)to figure
G. Leonidas
out valid SDK versions and the corresponding
1·1●1
Posted Jan. 6, 2016, 7: 16 p.m
drivers S especially libraries. After reading this tutorial it becomes much easier
to get a functional application code using Timers
Excellent work einar
We need more tutorials like this!
Nice and simple, love it. The"required files
Jaukur
and includes"section was extremely helpful
386●2·6
Posted Jan 10, 2016, 2: 22 p. m
Very helpful for newbies to the SDk like me. kane
Thanks!!!
47·3●4
Posted march 14. 2016. 12: 01 a.m.
Sign in to comment
faq I terms and conditions I give feedback
遇iny
dic on
https:/devzonenordicsemi.com/tutorials/19/
77
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.