STM32 Programming from scratch: Timer output Compare
In this tutorial you can learn how to make a project for STM32 microcontroller using CMSIS and after that how to write a code for Timer output compare to get accurate timing pulses from microcontroller
Requirement for this project:-Software:-
- Keil uvision5
- STM32L476 Nucleo board(any other nucleo baord from ST will also work)
- USB Port of laptop/desktop
Steps to make a new project in keil:-
The coding part:-
1)Include the header file of microcontroller in main.c
In main write the relevant code:-
start with declaring the declaring the structure pointer to RCC_TypeDef and pointer to GPIO_TypeDef
RCC_TypeDef *clock;
GPIO_TypeDef *GPIO;
1)Include the header file of microcontroller in main.c
In main write the relevant code:-
start with declaring the declaring the structure pointer to RCC_TypeDef and pointer to GPIO_TypeDef
RCC_TypeDef *clock;
GPIO_TypeDef *GPIO;
TIMER_TypeDef *TIMER;
Assign the address of GPIOA and RCC to respective pointers.
clock=RCC;
GPIO=GPIOA;
Assign the address of GPIOA and RCC to respective pointers.
clock=RCC;
GPIO=GPIOA;
TIMER=TIM1;
Select the clock source of microcontroller unit.I am using HSE in this code
clock->CR |=(1<<16);//clock source is on board 8 MHz crytal
Initialise the other required register also.
while(!((clock->CR)&(1<<17)));//Wait till HSERDY FLAG IS SET
clock->CFGR &=(1U<<1);
clock->AHB2ENR|=(1U<<1);//clock of port A is enabled
Select the clock source of microcontroller unit.I am using HSE in this code
clock->CR |=(1<<16);//clock source is on board 8 MHz crytal
Initialise the other required register also.
while(!((clock->CR)&(1<<17)));//Wait till HSERDY FLAG IS SET
clock->CFGR &=(1U<<1);
clock->AHB2ENR|=(1U<<1);//clock of port A is enabled
clock->APB2ENR |=(1U<<11);//Timer1 clock is enabled
Initialize the GPIO registers also.
GPIO->MODER &=~(1U<<16);
Initialize the GPIO registers also.
GPIO->MODER &=~(1U<<16);
GPIO->OSPEEDR |=(1U<<16)|(1U<<17);
GPIO->AFR[1] &=(1U<<0);//PA8 PIN IS IN ALTERNATE FUNCTION MODE
GPIO->AFR[1] &=(1U<<0);//PA8 PIN IS IN ALTERNATE FUNCTION MODE
//IT MEANS YOU ARE SETTING PA8 TO GET OUTPUT
add code to timer registers
add code to timer registers
TIMER->PSC =0
TIMER->ARR =999;
TIMER->CCR1=1;
TIMER->CCMR1 |=(1<<4)|(1<<5);
TIMER->CNT =1;
TIMER->BDTR |=(1U<<15)|(1U<<11);
TIMER->CR1 |=(1U<<0);//To Run the timer
The complete source code:-
Note:This code will work for all STM32 microcontrollers with change in registers value.Refer your microcontroller's reference manual and datasheet.This code have been tested for STM32L476RG nucleo board.
#include "stm32l476xx.h"
RCC_TypeDef *clock;
TIM_TypeDef *TIMER;
GPIO_TypeDef *GPIO;
int main()
{
clock=RCC;
TIMER=TIM1;
GPIO=GPIOA;
clock->CR |=(1U<<16);
while(!((clock->CR)&(1<<17)));
clock->CFGR |=(1U<<1);
//enable port A
clock->AHB2ENR |=(1U<<0);
// enable Timer 1 clock
clock->APB2ENR |=(1<<11);
//GPIO ALTERNATE FUNCTION MODE
GPIO->MODER &=~(1U<<16);
GPIO->OSPEEDR |=(1U<<16)|(1U<<17);
GPIO->AFR[1] &=(1U<<0);
GPIO->AFR[1] &=(1U<<0);
//Assign values to TIMER BASIC REGISTERS
TIMER->PSC =0
TIMER->ARR =999;
TIMER->CCR1=1;
TIMER->CCMR1 |=(1<<4)|(1<<5);
TIMER->CNT =1;
TIMER->BDTR |=(1U<<15)|(1U<<11);
TIMER->CR1|=(1<<0);//RUN the timer
while(1)
{
}
}
This code will give you pulse of required frequency on PA8 pin .Note:This code will work for all STM32 microcontrollers with change in registers value.Refer your microcontroller's reference manual and datasheet.This code have been tested for STM32L476RG nucleo board.
Comments
Post a Comment