广东建设职业技术学院官方网站,浙江企业黄页大全,产品推广有哪些平台,济南建设高端网站文章目录 1 单片机最小系统板按键原理图介绍2 库函数程序设计3 寄存器程序设计4 效果展示 1 单片机最小系统板按键原理图介绍
从图中看出单片机的PB12引脚接到了按键上。 根据按键的原理图#xff0c;可以分析得到#xff0c;如果不按下按键的时候#xff0c;引脚输入的是… 文章目录 1 单片机最小系统板按键原理图介绍2 库函数程序设计3 寄存器程序设计4 效果展示 1 单片机最小系统板按键原理图介绍
从图中看出单片机的PB12引脚接到了按键上。 根据按键的原理图可以分析得到如果不按下按键的时候引脚输入的是高电平。按下按键的时候引脚输入的是低电平。
2 库函数程序设计
main.c文件。
#include stm32f10x.h
#include led.h
#include delay.h
#include usart.h
#include key.h
#include stdio.hint main(void)
{RCC-APB2ENR | (uint32_t)0x00000010;//打开GPIOC时钟GPIOC-CRH (uint32_t)0xFF0FFFFF;//使用前清零GPIOC-CRH | (uint32_t)0x00300000;//配置PC13为推挽输出,最大速度50MHzDelay_Init();Usart_Init(115200);Key_Init();printf(hello world!\r\n);while(1){GPIOC-BSRR (uint32_t)0x00002000;//PC13引脚输出高电平Delay_ms(100); //延时100msGPIOC-BRR (uint16_t)0x2000; //PC13引脚输出低电平Delay_ms(100); //延时100msif(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) 0){Delay_ms(10); if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) 0)printf(key is pressed!\r\n);}}
}key.c文件。
#include stm32f10x.h
#include key.h void Key_Init(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIOB时钟GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; //上拉输入GPIO_InitStructure.GPIO_Pin GPIO_Pin_12; //PB12GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; //频率为50mhzGPIO_Init(GPIOB,GPIO_InitStructure); //初始化
}key.h文件。
#ifndef __KEY_H
#define __KEY_Hvoid Key_Init(void);#endif
3 寄存器程序设计
因为寄存器操作可以直接在main.c文件中写完这里只提供给大家main.c文件哈。
#include stm32f10x.h
#include led.h
#include delay.h
#include usart.h
#include key.h
#include stdio.hint main(void)
{RCC-APB2ENR | (uint32_t)0x00000010;//打开GPIOC时钟GPIOC-CRH (uint32_t)0xFF0FFFFF;//使用前清零GPIOC-CRH | (uint32_t)0x00300000;//配置PC13为推挽输出,最大速度50MHzDelay_Init();Usart_Init(115200);//库函数版本
// //---------------------------------------------------------//
// Key_Init();//---------------------------------------------------------////寄存器版本//---------------------------------------------------------//RCC-APB2ENR | (uint32_t)0x00000008;//打开GPIOB时钟GPIOB-CRH (uint32_t)0xFFF0FFFF;//使用前清零GPIOB-CRH | (uint32_t)0x00080000;//配置PB12为上/下拉输入GPIOB-BSRR (uint32_t)0x00001000;//配置PB12为上拉//---------------------------------------------------------//printf(hello world!\r\n);while(1){GPIOC-BSRR (uint32_t)0x00002000;//PC13引脚输出高电平Delay_ms(100); //延时100msGPIOC-BRR (uint16_t)0x2000; //PC13引脚输出低电平Delay_ms(100); //延时100ms//库函数版本//---------------------------------------------------------//
// if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) 0)
// {
// Delay_ms(10);
// if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) 0)
// printf(key is pressed!\r\n);
// }//---------------------------------------------------------////寄存器版本//---------------------------------------------------------//if((GPIOB-IDR (uint16_t)0x1000) 0){Delay_ms(10);if((GPIOB-IDR (uint16_t)0x1000) 0)printf(key is pressed!\r\n);}//---------------------------------------------------------//}
}
4 效果展示