stm32f103r6写一个按键控制led的代码
时间: 2024-05-14 12:13:41 浏览: 102
好的,以下是一个简单的stm32f103r6按键控制led的代码:
```
#include "stm32f10x.h"
int main(void)
{
// Enable GPIOC and GPIOA clock
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
// Set PC13 as output (LED)
GPIOC->CRH &= ~(GPIO_CRH_CNF13 | GPIO_CRH_MODE13);
GPIOC->CRH |= GPIO_CRH_MODE13_0;
// Set PA0 as input (button)
GPIOA->CRL &= ~(GPIO_CRL_CNF0 | GPIO_CRL_MODE0);
while (1)
{
if (GPIOA->IDR & GPIO_IDR_IDR0)
{
// Button is not pressed, turn off LED
GPIOC->BSRR |= GPIO_BSRR_BR13;
}
else
{
// Button is pressed, turn on LED
GPIOC->BSRR |= GPIO_BSRR_BS13;
}
}
}
```
希望能对你有所帮助!
阅读全文