2023-07-11 02:39 AM
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
Solved! Go to Solution.
2023-07-11 03:23 AM
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
2023-07-11 03:23 AM
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
2023-07-11 04:08 AM
TIM+DMA+GPIO +1
2023-07-11 08:00 AM
Hi waclawek.. can you try an example for this ?
You can try toggling the pin in timer interrupt
Thanks