cancel
Showing results for 
Search instead for 
Did you mean: 

PWM using Timer DMA

DJ1
Associate III

Here i am trying to generate a 24 bit PWM signal using timer 2 in STM32F401RE controller. I followed as per the steps only but still i not getting any output waveform on my logic analyzer. Please share your suggestions.

 

void tim_init();

GPIO_HANDLE pwm;
 
uint8_t green[24]={13,13,13,13,13,13,13,13,  7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7};  // Green
 
void dma_init()
{
// Enable DMA1
DMA1_CLK_EN;
// Disable the Stream
DMA1->DMA_S5CR = 0;
// Wait till DMA stream is disabled
while(DMA1->DMA_S5CR & 1);
 
// Clear Error Flags
// Direct Mode Error
DMA1->DMA_HIFCR |= (1 << 8);
// Transfer Error Flag
DMA1->DMA_HIFCR |= (1 << 9);
// Transfer Complete Flag
DMA1->DMA_HIFCR |= (1 << 11);
 
 
// Select the channel 3
DMA1->DMA_S5CR |= (3 << 25);
// Memory Increment
DMA1->DMA_S5CR |= (1 << 10);
// Transfer Direction Mem to Peri
DMA1->DMA_S5CR |= (1 << 6);
 
 
// Transfer Length
DMA1->DMA_S5NDTR = (uint16_t)24;
// Peripheral Address
DMA1->DMA_S5PAR = (uint32_t)(&TIM2->TIMx_CCR1);
// Memory Address
DMA1->DMA_S5M0AR = (uint32_t)&green;
 
 
// Enable DMA Stream
DMA1->DMA_S5CR |= (1 << 0);
// Enable the timer
TIM2->TIMx_CR1 |= (1 << 0);
}
 
 
void delay(uint16_t x)
{
TIM4->TIMx_ARR = x;
TIM4->TIMx_CNT = 0;
TIM4->TIMx_CR1 |= (1 << 0);
while(TIM4->TIMx_CNT < x);
}
 
 
 
int main()
{
GPIOA_PCLK_EN;
 
// PWM Alternate Functionality
pwm.pGPIOx = GPIOA;
pwm.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_AF;
pwm.GPIO_PinConfig.GPIO_PinAltFunMode = 1;
pwm.GPIO_PinConfig.GPIO_PinNumber = 0;
GPIO_Init(&pwm);
 
tim_init();
 
while(1)
{
dma_init();
delay(1000);
}
}
 
 
 
void tim_init()
{
 
// Enable clock for timer 2
TIM2_EN;
// Pre-scaler is 1 to generate 800 KHz freq using 16MHz HSI
TIM2->TIMx_PSC = 1 - 1;
// Store the count in ARR
TIM2->TIMx_ARR = 20-1; // 1.25 micro seconds
// Reset Counter
TIM2->TIMx_CNT = 0;
 
// Enable OC1PE bit and OC1M bit
TIM2->TIMx_CCMR1 |= ((6 << 4) | (1 << 3));
 
// Enable Capture Compare 1 DMA request  // Enable Update DMA request enable  // Enable Trigger DMA request 
TIM2->TIMx_DIER |= ((1 << 9)|(1 << 8)|(1 << 14));
 
// Timer CCER bit enable
TIM2->TIMx_CCER |= (1<<0);
 
 
 
// Timer for Delay generation
TIM4_EN;
TIM4->TIMx_PSC = 16000-1;
}
2 REPLIES 2
Sarra.S
ST Employee

Hello @DJ1

Did you set a value of CCR1 anywhere else in the code? 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

DJ1
Associate III

Nope, i am just giving the address of CCR1 register in the DMA_PAR register. No where else.