Developing the timer driver
In this section, we will apply the knowledge gained about the TIM peripheral to develop a driver for generating delays.
First, create a copy of your previous project in your IDE, following the steps outlined in earlier chapters. Rename this copied project GTIM
. Next, create a new file named tim.c
in the Src
folder and another file named tim.h
in the Inc
folder.
Our goal is to develop a driver that initializes TIM2 to generate a 1 Hz timeout. Populate your tim.c
file with the following code:
#include "tim.h" #define TIM2EN (1U<<0) #define CR1_CEN (1U<<0) void tim2_1hz_init(void) { /*Enable clock access to tim2*/ RCC->APB1ENR |=TIM2EN; /*Set prescaler value*/ TIM2->PSC = 1600 - 1 ; /*Set auto-reload...