Skip to main content
SGasp.1
Associate III
July 11, 2023
Solved

Generation of clock signal of 800 khz on port PC2 STM32f29 mcro

Hi amazing community.. 

I am working with stm32f429 and hal.

I have to generate a clock signal of 800 khz and duty cycle of 50% in a specific pin PC2

I cannot change pin since the hw is blocked and fixed.. 

The only way is to create it by using a delay ? 

Is it correct in this way ? 

 

#include "stm32f4xx_hal.h"

GPIO_InitTypeDef GPIO_InitStruct;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void delay_us(uint32_t us);

int main(void)
{
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 
 while (1)
 {
 // Toggle PC2
 HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);
 
 // Delay to approximate the desired frequency
 delay_us(625);
 }
}

void SystemClock_Config(void)
{
 // Same as before, no changes required
}

static void MX_GPIO_Init(void)
{
 __HAL_RCC_GPIOC_CLK_ENABLE();
 
 GPIO_InitStruct.Pin = GPIO_PIN_2;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}

void delay_us(uint32_t us)
{
 // The actual delay may need adjustment based on the MCU clock frequency and compiler optimizations
 uint32_t count = (us * (SystemCoreClock / 1000000)) / 3;
 
 while (count--)
 {
 __asm__("nop");
 }
}

any other ideas to try?+

Thanks

    This topic has been closed for replies.
    waclawek.jan
    waclawek.janBest answer
    Super User
    July 11, 2023

    You can try toggling the pin in timer interrupt, but don't use Cube/HAL for that, write it yourself.

    Another option is timer-triggered-DMA transferring from memory to GPIOx_BSRR.

    JW

    3 replies

    waclawek.jan
    waclawek.janBest answer
    Super User
    July 11, 2023

    You can try toggling the pin in timer interrupt, but don't use Cube/HAL for that, write it yourself.

    Another option is timer-triggered-DMA transferring from memory to GPIOx_BSRR.

    JW

    Tesla DeLorean
    Guru
    July 11, 2023

    TIM+DMA+GPIO  +1

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..