cancel
Showing results for 
Search instead for 
Did you mean: 

pwm generation problem stm32f303

lamarcagiuseppe977
Associate II
Posted on June 11, 2016 at 01:55

Hi to all, I'm giuseppe and I'm new here,

I've a big problem with the generation of pwm signal, in the code below i'm trying to create a pwm of 50% duty cycle in port PA0, the pwm would be of a period of 1s, so i can try it over a led, in many instructions I don't know what i'm doing but I'm just trying to do what the refman says, so any help would be really appreciated.

#include ''stm32f30x.h''
#include ''startup_stm32f30x.s''
int cnt=0; 
int main()
{
RCC->APB1ENR |= 1<<
0
; //clock for TIM2
RCC->AHBENR |= 1<<
17
; //clock for GPIOA
GPIOA->MODER |= 3<<
0
; //select alternative function over A port
GPIOA->AFR[0] = 0x0;
TIM2->PSC=0; 
TIM2->ARR=72000000; // 1 second period
TIM2->CNT=0; 
TIM2->CR1 |= 1<<
7
; 
TIM2->CCR1= 36000000; //0.5 second 
TIM2->CCER |= 1<<
0
; //set the CC1E bit
TIM2->EGR = 1<<
0
; //set the UEG bit 
TIM2->CCMR1 |= 3<<
5
; 
TIM2->CCMR1 |= 1<<
3
; 
TIM2->DIER |= 1<<
1
; 
TIM2->CR1 |=1<<0 //start the timer
while(1); 
//return 0;
}

5 REPLIES 5
Posted on June 11, 2016 at 03:22

GPIOA->AFR[0] =
(GPIOA->AFR[0] & ~0xF) | 1; // AF1 (TIM2_CH1_ETR)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 11, 2016 at 03:24

TIM2->ARR=72000000 - 1; // N steps 0..N-1 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lamarcagiuseppe977
Associate II
Posted on June 11, 2016 at 08:44

hi clive1, 

thanks for the answer I tried that, but still doesn't work, any idea? 

lamarcagiuseppe977
Associate II
Posted on June 11, 2016 at 09:01

I noticed that when i try to set the UG bit in the EGR register, I can't do that but I don't know why it goes to modify the UIF bit of SR register, I'll add a pic of the situation after the TIM2->EGR = 1<<0; istruction.0690X00000605MvQAI.png

lamarcagiuseppe977
Associate II
Posted on June 11, 2016 at 23:02

clive1 thanks for the help, i solved it i was getting wrong because i written 3 instead of 2 in GPIOA->MODER (because 3 let me select analog function while i needed to select the alternate function) and i was using AF0 while i needed to use AF1, really thanks for your time :)