2016-06-10 04:55 PM
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;
}
2016-06-10 06:22 PM
GPIOA->AFR[0] =
(GPIOA->AFR[0] & ~0xF) | 1; // AF1 (TIM2_CH1_ETR)
2016-06-10 06:24 PM
TIM2->ARR=72000000 - 1; // N steps 0..N-1
2016-06-10 11:44 PM
hi clive1,
thanks for the answer I tried that, but still doesn't work, any idea?2016-06-11 12:01 AM
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.
2016-06-11 02:02 PM
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 :)