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
