STM32 PROGRAMMING FROM SCRATCH: ROTARY ENCODER WITH STM32
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 Rotary Encoder to detect direction of rotation and also amount of rotation.
Requirement for this project:-Software:-
- Keil uvision5
- STM32L476 Nucleo board(any other nucleo baord from ST will also work)
- USB
Steps to make a new project in keil:-
Circuit diagram:-
Connections:-
Nucleo board Rotary Encoder
3.3 V Vcc
GND GND
PA8 DT
PA9 CLK
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)|(1U<<18);
GPIO->AFR[1] &=(1U<<1)|(1U<<4);
GPIO->PUPDR|=(1U<<17)|(1U<<19);
add code to timer registers
Initialize the GPIO registers also.
GPIO->MODER &=~(1U<<16)|(1U<<18);
GPIO->AFR[1] &=(1U<<1)|(1U<<4);
GPIO->PUPDR|=(1U<<17)|(1U<<19);
add code to timer registers
TIMER->PSC =0
TIMER->ARR =0xFFFF;
TIMER->CNT =1;
TIMER->SMCR |=(1U<<0)|(1U<<1);
TIMER->CCER |=(1U<<0)|(1U<<3)|(1U<<5)|(1U<<7);
TIMER->CR1 |=(1U<<0);//To Run the timer
The complete source code:-
#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 &=~((1<<16)|(1<<18));
GPIO->AFR[1]|=((1<<0)|(1<<4));
GPIO->PUPDR |=((1<<17)|(1<<19));
//Assign values to TIMER BASIC REGISTERS
TIMER->PSC=0;
TIMER->ARR=0xFFFF;
TIMER->CNT=1;
//OTHE TIMER REGISTERS ASSIGNMENT IN ENCODER MODE
TIMER->SMCR|=((1<<1)|(1<<0));
//CC1NP=0,CC1P=0
//TIMER->CR1|=(1<<4);
TIMER->CCER|=((1<<0)|(1<<3)|(1<<5)|(1<<7));
TIMER->CR1|=(1<<0);
while(1)
{
}
}
This code will give you reading (increasing when you move strobe of Rotary Encoder in clockwise direction and decreasing count when move rotary encoder in anticlockwise direction)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.
Amazon link to Stm32 nucleo board: https://amzn.to/3BBuNvi
Amazon link to Hantek oscilloscope: https://amzn.to/3ocbo0S
Video Link:
Comments
Post a Comment