前言
一个学习32单片机的小白~ 有问题评论或私信指出~
提示:以下是本篇文章正文内容,下面案例可供参考
1.LED闪烁
思路:
①打开RCC对应的GPIO时钟
②配置GPIO的结构体,配置为推挽输出,选择GPIOA_Pin_1,输出速度随意
③初始化LED为灭GPIO_ResetBits();
④主函数调用LED_Init()函数
⑤模拟delay函数
⑥主函数亮delay灭delay
//主函数代码
#include "stm32f10x.h"
#include "main.h"
#include "LED.h"
void delay_ms(uint32_t xms);
int main()
{
LED_Init();
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
delay_ms(1000);
}
}
void delay_ms(uint32_t xms)
{
uint32_t i;
while(xms--)
{
while(i--);
{
i = 12000;
}
}
}
//led
#include "led.h"
#include "stm32f10x.h" // Device header
void LED_Init(void)
{
GPIO_InitTypeDef LED_Structure;
//初始化时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//打开GPIOA
LED_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
LED_Structure.GPIO_Pin = GPIO_Pin_1;
LED_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&LED_Structure);
}
2.蜂鸣器
1.蜂鸣器的种类
蜂鸣器是一种常用的电子发声元器件,采用直流电压供电。广泛应用于计算机,打印机,报警器,电子玩具,汽车电子设备灯等产品中常见的蜂鸣器可分为有源蜂鸣器和无源蜂鸣器。
2.蜂鸣器的控制方式
有源蜂鸣器:内部有震荡源,只要通电即可自动发出固定频率的声音。(频率固定无法控制音色)
无源蜂鸣器:内部无震荡源,需要外部脉冲信号驱动发声,声音频率可变。(可改变频率来改变音色)
3.区分蜂鸣器
从外观上来看两种蜂鸣器形状相似,有源蜂鸣器底部有黑胶密封。
无源蜂鸣器底部可以看到电路板,两种蜂鸣器都标注有正负极。
GPIO控制蜂鸣器
软件设计流程
- 初始化系统
- 初始化GPIO外设时钟
- 初始化蜂鸣器的引脚
- 输出电平控制蜂鸣器
//主函数代码
#include "stm32f10x.h"
#include "main.h"
#include "buzzer.h"
void delay_ms(uint32_t xms);
int main()
{
LED_Init();
BUZZER_Init();
while(1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
delay_ms(1000);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
delay_ms(1000);
}
}
void delay_ms(uint32_t xms)
{
uint32_t i;
while(xms--)
{
while(i--);
{
i = 12000;
}
}
}
//BUZZER模块代码
#include "buzzer.h"
#include "stm32f10x.h" // Device header
void BUZZER_Init(void)
{
GPIO_InitTypeDef BUZZER_Structure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
BUZZER_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
BUZZER_Structure.GPIO_Pin = GPIO_Pin_12;
BUZZER_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&BUZZER_Structure);
}
按键基础知识
1.深入理解GPIO输入
GPIO的特点:
- 具有内部上拉或下拉的功能
- 可以使用外部下拉或上拉
按键控制LED灯
软件设计流程
- 初始化系统
- 初始化GPIO外设时钟
- 初始化按键和LED的引脚
- 检测按键输入电平来控制LED灯
//主函数
#include "stm32f10x.h"
#include "main.h"
#include "LED.h"
#include "Key.h"
uint8_t KeyNum;
int main()
{
LED_Init();
Key_Init();
while(1)
{
KeyNum = Key_GetKeyNum();
if(KeyNum == 1)
{
LED_ON();
}
if(KeyNum == 2)
{
LED_OFF();
}
}
}
//LED模块
#include "led.h"
#include "stm32f10x.h" // Device header
void LED_Init(void)
{
GPIO_InitTypeDef LED_Structure;
//初始化时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//打开GPIOA
LED_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
LED_Structure.GPIO_Pin = GPIO_Pin_1;
LED_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&LED_Structure);
}
void LED_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
//Key模块代码
#include "Key.h"
#include "stm32f10x.h" // Device header
void delay_ms(uint32_t xms)
{
uint32_t i;
while(xms--)
{
while(i--);
{
i = 12000;
}
}
}
void Key_Init(void)
{
GPIO_InitTypeDef KEY_Structure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
KEY_Structure.GPIO_Mode = GPIO_Mode_IPU;
KEY_Structure.GPIO_Pin = GPIO_Pin_0;
KEY_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&KEY_Structure);
KEY_Structure.GPIO_Mode = GPIO_Mode_IPU;
KEY_Structure.GPIO_Pin = GPIO_Pin_13;
KEY_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&KEY_Structure);
}
uint8_t Key_GetKeyNum(void)
{
u8 Keynum = 0;
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == 0)
{
delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == 0);
delay_ms(20);
Keynum = 1;
}
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13) == 0)
{
delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13) == 0);
delay_ms(20);
Keynum = 2;
}
return Keynum;
}