We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am trying to write code for toggling a pin(Pin 15) of a port (PORTC) using timers(specifically TIM2). I have to use the prescale registers to divide the clock frequency such that I obtain the required delay for toggling the pin.So I wrote some code after 3 hrs of going through Reference Manual of the STM32F103RB. I checked what is wrong with my program and noticed that the Counter register is not updating even though in enabled the counter. Could anyone tell where I am going wrong? I have attached my code for reference.
#include "stm32f10x.h"
void Q2() { int step = 0; RCC->APB2ENR |= (1<<4); //To enable the PORTC RCC->APB1ENR |= (1<<0); // Enable Timer 2 GPIOC->CRH &= (0<<30); // setting CNF GPIOC->CRH |= (1<<28); // Setting mode to get general purpose Output push pull TIM2->PSC = 0xD2F0; // Prescalar value set such that the 0.5ms is over till overflow TIM2->CR1 |= 1<<7; // Auto reload preload enable is buffered TIM2->ARR = 0x0000; // Auto reload value is 0 TIM2->CR1 |= 1<<0; //Counter enabled
while (1) { TIM2->PSC = 0xD2F0; // Prescalar value set such that the 0.5ms is over till overflow TIM2->CR1 |= 1<<7; // Auto reload preload enable is buffered TIM2->ARR = 0x0000; // Auto reload value is 0 TIM2->CR1 |= 1<<0; //Counter enabled TIM2->SR &= 0<<0; while(step!=2) { if((TIM2->SR && 0x0001) == 0x0001) {
step++; } } step = 0; GPIOC->BSRR |= (1<<15); // set pin 15 of port C TIM2->PSC = 0xD2F0; // Prescalar value set such that the 0.5ms is over till overflow TIM2->CR1 |= 1<<7; // Auto reload preload enable is buffered TIM2->ARR = 0x0000; // Auto reload value is 0 TIM2->CR1 |= 1<<0; //Counter enabled TIM2->SR &= 0<<0; while(step!=2) { if((TIM2->SR && 0x0001) == 0x0001) {
step++; } } step = 0; GPIOC->BSRR |= (1<<31); // reset pin 15 of Port C } }int main (){ SystemInit (); Q2();}
Thank you