实验内容:跑马灯、蜂鸣器和按键输入
官方资料:
1)《STM32中文参考手册V10》第8章 通用和复用功能IO(GPIO和AFIO)
2)《Cortex-M3权威指南(中文)》第5章 位带操作
原理图:
1)如图所示,跑马灯,PB5接LED0,PE5接LED1;
2)如图所示,蜂鸣器,PB8接BEEP(蜂鸣器);
3)如图所示,PE4接KEY0,PE3接KEY1,PE2接KEY2,PA0接WK_UP。
工作原理不细讲,一定要看清外设与哪个GPIO管脚对接。
涉及已学GPIO库函数:
- 1)void GPIO_Init(GPIO_TypeDef* GPIOx,GPIO_InitTypeDef* GPIO_InitStruct); //初始化作用
复制代码
新知识:位操作地址计算方式:
假设要令GPIOA的第五个管脚为输出:PAout(5),查看sys.h和stm32f10x.h两个头文件可知:
因此,GPIOA_ODR_Addr=0x4001080C,n=5。之前给的紫色公式与头文件中的关键代码一一对应,可算出PAout(5)的别名区地址。
因此,位操作可以这么玩:
在led.h头文件中定义:
在led.c文件中定义:
- PBout(5)=0; //设GPIOB的第五管脚为低电平。
复制代码
最后再提一下为什么是GPIOA_BASE+12?STM32中文参考手册讲到,GPIOx_ODR的地址偏移是0Ch,因此是+12。对应的,GPIOx_IDR的地址偏移是0x08,因此是+8。至于GPIOA_BASE为什么等于APB2PERIPH_BASE+0x0800,没找到答案,大牛们可以解释下。
实验步骤:
1) 新建工程模板;
2) 新建HARDWARE空文件夹(与USER文件夹并列);
3) 在MDK软件中,新建led.c,beep.c和key.c三个.c文件以及对应的led.h,beep.h和key.h头文件,均保存在HARDWARE文件夹中;
4)led.h头文件代码分别如下:
- #ifndef __LED_H
- #define __LED_H
- #include "sys.h"
- void LED_Init(void); //初始化函数
- #define LED0 PBout(5) // PB5
- #define LED1 PEout(5) // PE5
- #endif
复制代码
5)beep.h头文件代码分别如下:
- #ifndef __BEEP_H
- #define __BEEP_H
- void BEEP_Init(void); //初始化函数
- #define BEEP PBout(8) // PB8
- #endif
复制代码
6)key.h头文件代码分别如下:
- #ifndef __KEY_H
- #define __KEY_H
- #include "sys.h"
- #define KEY0 PEin(4) //PE4
- #define KEY1 PEin(3) //PE3
- #define KEY2 PEin(2) //PE2
- #define WK_UP PAin(0) //PA0 WK_UP
- void KEY_Init(void);
- u8 KEY_Scan(u8);
- #endif
复制代码
7)led.c文件代码分别如下:
- #include "led.h"
- #include "sys.h"
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //使能PE端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
- LED0=1; //PB.5 输出高,LED0灭
- GPIO_Init(GPIOE, &GPIO_InitStructure); //根据设定参数初始化GPIOE.5
- LED1=1; //PE.5 输出高,LED1灭
- }
复制代码
8)beep.c文件代码分别如下:
- #include "beep.h"
- #include "sys.h"
- void BEEP_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0-->PB.5 端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
- BEEP=0; //PB8 输出低,LED0灭
- }
复制代码
9)key.c文件代码分别如下:
- #include "key.h"
- #include "sys.h"
- #include "delay.h"
- void KEY_Init(void) //IO初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE); //使能PA,PE端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //KEY0-KEY2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
- GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化GPIOE2,3,4
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.0
- }
- u8 KEY_Scan(u8 mode)
- {
- static u8 key_up=1; //按键按松开标志
- if(mode)key_up=1; //支持连按
- if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
- {
- delay_ms(10); //去抖动
- key_up=0;
- if(KEY0==0)return 1; //KEY0按下,即PE4为低电平
- else if(KEY1==0)return 2; //KEY1按下,即PE3为低电平
- else if(KEY2==0)return 3; //KEY2按下,即PE2为低电平
- else if(WK_UP==1)return 4; //WK_UP按下,即PA0为高电平
- }else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;
- return 0; // 无按键按下
- }
复制代码
10)在MDK软件中,右键Target1,单击Manage Project Items…,在Project Items界面、Groups一栏,新建HARDWARE,并将led.c,beep.c和key.c三个.c文件添加进来;
11)在MDK软件中,单击魔术棒,在C/C++选项里,单击Include
Paths右边的…,添加led.h,beep.h和key.h三个头文件的目录(即HARDWARE);
12)新建main.c主函数,代码如下:
- #include "led.h"
- #include "beep.h"
- #include "key.h"
- #include "sys.h"
- #include "delay.h"
- int main(void)
- {
- vu8 key=0;
- delay_init();
- LED_Init();
- BEEP_Init();
- KEY_Init();
- while(1)
- {
- key=KEY_Scan(1);
- if(key)
- {
- switch(key)
- {
- case 4:
- BEEP=1; //控制蜂鸣器叫
- break;
- case 3:
- LED0=0; //控制LED0亮
- break;
- case 2:
- LED1=0; //控制LED1亮
- break;
- case 1:
- LED0=0; //控制LED0亮
- LED1=0; //控制LED1亮
- break;
- }
- delay_ms(300); //延迟300ms
- BEEP=0; //控制蜂鸣器不叫
- LED0=1; //控制LED0灭
- LED1=1; //控制LED1灭
- }else delay_ms(10);
- }
- }
复制代码
13) 完成后即可运行看效果。
————————————————
版权声明:天亮继续睡
|