使用寄存器的方法,配置引脚PA1,PC2位流水灯引脚,共阳接法,延时函数直接使用Delay(int ms ),延时时间300ms
时间: 2023-05-21 07:01:34 浏览: 69
以下是使用寄存器的方法,配置引脚PA1,PC2位流水灯引脚,共阳接法,延时函数直接使用Delay(int ms ),延时时间300ms的代码:
```
#include <stm32f10x.h>
void Delay(int ms);
int main(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN; // Enable GPIOA and GPIOC clock
GPIOA->CRL &= ~(GPIO_CRL_MODE1 | GPIO_CRL_CNF1); // Configure PA1 as output push-pull
GPIOA->CRL |= GPIO_CRL_MODE1_0;
GPIOC->CRL &= ~(GPIO_CRL_MODE2 | GPIO_CRL_CNF2); // Configure PC2 as output push-pull
GPIOC->CRL |= GPIO_CRL_MODE2_0;
while (1)
{
GPIOA->BSRR = GPIO_BSRR_BS1; // Turn on PA1
Delay(300);
GPIOA->BSRR = GPIO_BSRR_BR1; // Turn off PA1
GPIOC->BSRR = GPIO_BSRR_BS2; // Turn on PC2
Delay(300);
GPIOC->BSRR = GPIO_BSRR_BR2; // Turn off PC2
}
}
void Delay(int ms)
{
for (int i = 0; i < ms * 1000; i++);
}
```