写一个stm32c8t6按键1控制led,按键2控制蜂鸣器程序
时间: 2024-03-09 12:35:21 浏览: 81
#include "stm32f1xx.h"
int main(void)
{
// 使能GPIOB和AFIO的时钟
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN;
// 配置GPIOB.5和GPIOB.6为推挽输出
GPIOB->CRL &= ~(GPIO_CRL_MODE5 | GPIO_CRL_CNF5);
GPIOB->CRL |= GPIO_CRL_MODE5_0;
GPIOB->CRL &= ~(GPIO_CRL_MODE6 | GPIO_CRL_CNF6);
GPIOB->CRL |= GPIO_CRL_MODE6_0;
// 配置GPIOA.0和GPIOA.1为上拉输入
GPIOA->CRL &= ~(GPIO_CRL_MODE0 | GPIO_CRL_CNF0);
GPIOA->CRL |= GPIO_CRL_CNF0_1 | GPIO_CRL_MODE0_0;
GPIOA->ODR |= GPIO_ODR_ODR0;
GPIOA->CRL &= ~(GPIO_CRL_MODE1 | GPIO_CRL_CNF1);
GPIOA->CRL |= GPIO_CRL_CNF1_1 | GPIO_CRL_MODE1_0;
GPIOA->ODR |= GPIO_ODR_ODR1;
while (1)
{
// 检测按键1是否按下
if ((GPIOA->IDR & GPIO_IDR_IDR0) == 0)
{
GPIOB->BSRR |= GPIO_BSRR_BS5; // 点亮LED
}
else
{
GPIOB->BSRR |= GPIO_BSRR_BR5; // 熄灭LED
}
// 检测按键2是否按下
if ((GPIOA->IDR & GPIO_IDR_IDR1) == 0)
{
GPIOB->BSRR |= GPIO_BSRR_BS6; // 打开蜂鸣器
}
else
{
GPIOB->BSRR |= GPIO_BSRR_BR6; // 关闭蜂鸣器
}
}
}
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)