c-如何使两行端口的切换频率不同



一个输出的开关频率必须是第二个输出的两倍,并且不相同。我不明白该怎么安排。这是我的代码和收到的信号。

#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);
}
}

图像信号

在无休止循环中需要一个计数器,该计数器在每次执行循环时都会增加。

您需要在每次执行循环时切换第一个输出。

第二个输出需要每秒钟切换一次。这是每次计数器%2==0。

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

最新更新