Search code examples
cstm32microcontrollergpiokeil

How to make switching frequency for two lines of ports different?


It is necessary that the switching frequency of one output be twice as large as the second, and not the same. I don't understand how I can set it up. Here is my code and received signals.

#include "RTE_Components.h"
#include CMSIS_device_header

void delay(volatile uint32_t count)
{
    while(count--)
    {
        __nop();
    }
}

int main()
{
    *(uint32_t*)(0x40021018) |= 0x00000004;//RCC->APB2ENR |= RCC_APB2ENR_IOPAEN
    
    RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
    
    *(uint32_t*)(0x40010804) &= ~(0x00003000 | 0x0000C000);//GPIOB->CRH &= ~(GPIO_CRH_MODE11 | GPIO_CRH_CNF11)
    
    GPIOB->CRH &= ~(GPIO_CRH_MODE13 | GPIO_CRH_CNF13);
    
    *(uint32_t*)(0x40010804) |= 0x00002000;//SET_BIT(GPIOA->CRH, GPIO_CRH_MODE11_1)
    SET_BIT(GPIOB->CRH, GPIO_CRH_MODE13_1);
    
    for(;;)
    {
        *(uint32_t*)(0x40010810) = 0x00000800;//GPIOA->BSRR = GPIO_BSRR_BS11
        delay(6000);
        GPIOB->BSRR = GPIO_BSRR_BS13;
        delay(1560);
        *(uint32_t*)(0x40010814) = 0x00000800;//GPIOA->BRR = GPIO_BRR_BR11
        delay(6000);
        GPIOB->BRR = GPIO_BRR_BR13;
        delay(1560);
    }
}

image signals


Solution

  • You need a counter in your endless loop which is increased on every execution of the loop.

    You than need to toggle the first output every time the loop is executed.

    Than the second output needs to be toggled every second time. That is every time the counter % 2 == 0.

    int cnt = 0;
    for(;;) 
    {
        output1 ^= 1;
        if (cnt % 2 == 0) 
            output2 ^= 1;
        delay(sometime);
        cnt++;
    }