写代码之前,我们需要先按照下图安装好面包板上的模块,注意STM32最小系统板和STLINK上线的连接顺序。
连接好面包板,我们先来了解一下常用的库函数
RCC主要的库函数
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState); RCCAPB1外设时钟控制
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState); RCCAPB2外设时钟控制
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState); RCCAPB3外设时钟控制
第一个参数选择外设,第二个参数使能或失能
GPIO常用的库函数
- void GPIO_DeInit(GPIO_TypeDef* GPIOx);
调用这个函数,所指定的GPIO外设就会被复位
- void GPIO_AFIODeInit(void);
和上一个函数相似,可以复位AFIO外设
- void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
这个函数较为重要,它的作用是用结构体的参数来初始化GPIO口;
我们需要先定义一个结构体变量,然后再给结构体赋值,最后调用这个函数。
- void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
这个函数可以把结构体变量赋一个默认值
- uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
- uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
- uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
- uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
这四个函数是GPIO的读取函数
- void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
- void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
- void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
- void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
这四个是GPIO的写入函数
上面八个函数可以实现读写GPIO口的功能
接着我们先学会点亮一个LED灯
#include "stm32f10x.h" // Device header
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InisStructure;
GPIO_InisStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InisStructure.GPIO_Pin=GPIO_Pin_12;
GPIO_InisStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InisStructure);
GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_RESET);
这条语句也可以写成GPIO_ResetBits(GPIOA,GPIO_Pin_0);
while(1)
{}
}
学会了如何点亮一个LED灯,我们就可以通过Delay函数来控制LED灯的闪烁了;
GPIO闪烁的代码
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InisStructure;
GPIO_InisStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InisStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InisStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InisStructure);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
while(1)
{ GPIO_ResetBits(GPIOA,GPIO_Pin_0);
Delay_ms(500);
GPIO_SetBitsO(GPIOA,GPIO_Pin_0);
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
Delay_ms(500);
}
}
LED流水灯
按下图连接好面包板
LED流水灯代码
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InisStructure;
GPIO_InisStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InisStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InisStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InisStructure);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
while(1)
{
GPIO_Write(GPIOA,~0x0001);//0000 0000 0000 0001
这里二进制的16位从左到右分别对应PA0-PA15总共16个端口,这条语句是第一个LED点亮,其他都熄灭。
Delay_ms(500);
GPIO_Write(GPIOA,~0x0002);//0000 0000 0000 0010
Delay_ms(500);
GPIO_Write(GPIOA,~0x0004);//0000 0000 0000 0100
Delay_ms(500);
GPIO_Write(GPIOA,~0x0008);//0000 0000 0000 1000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0010);//0000 0000 0001 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0020);//0000 0000 0010 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0040);//0000 0000 0100 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0080);//0000 0000 1000 0000
Delay_ms(500);
}
}
蜂鸣器
按下图连接好面包板
蜂鸣器代码
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InisStructure;
GPIO_InisStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InisStructure.GPIO_Pin=GPIO_Pin_12;
GPIO_InisStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InisStructure);
GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_SET);
while(1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
Delay_ms(100);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
Delay_ms(700);
}
}