概要
1.实验名称:LED和蜂鸣器实验
2.实验环境:STM32F103C8T6
3.实验内容:LED闪烁实验,LED流水灯实验,蜂鸣器开关控制实验
技术实现
1.硬件连接
1.LED闪烁
2.LED流水灯实验
3.蜂鸣器实验
2.部分器件原理图
1.蜂鸣器
3.代码实现
1.LED闪烁
#include "stm32f10x.h" // Device header
#include "Delay.h" //延时函数头文件
int main(void)
{
/*
APB2高速总线外围时钟设置
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //启用高速总线外围时钟
/*
初始化GPIO外围设备
*/
//结构体定义
GPIO_InitTypeDef GPIO_InitStruct ;
//结构体初始化
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //使用推挽输出,高电平和低电平均有驱动能能力
// GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD; //使用开漏输出,低电平有驱动能力,高电平没有驱动能力
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO初始化
GPIO_Init(GPIOA,&GPIO_InitStruct);
/*
端口输出
*/
//使用GPIO_ResetBits()和 GPIO_SetBits()函数
// GPIO_ResetBits(GPIOA,GPIO_Pin_0);
// GPIO_SetBits(GPIOA,GPIO_Pin_0);
//使用GPIO_WriteBits()函数
// GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
// GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
while(1)
{
//使用GPIO_ResetBits()函数控制LED亮灭
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
Delay_ms(500);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
Delay_ms(500);
//使用PIO_WriteBits()函数控制LED亮灭
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
Delay_ms(500);
}
}
2.LED流水灯实验
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
/*
高速总线APHB2时钟设置
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //启用高速总线APB2外围时钟
/*
初始化GPIO外围设备
*/
GPIO_InitTypeDef GPIO_InitStruct;
//结构体初始化
//设置PA0~7共8个引脚
// GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All; //All pins selected
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //设置为开漏输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
while(1)
{
GPIO_Write(GPIOA,~0x0001); //0000 0000 0000 0001
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);
}
}
3.蜂鸣器控制实验
#include "stm32f10x.h" // Device header
#include "Delay.h" //
int main(void)
{
/*
高速总线APHB2时钟设置
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //启用高速总线APB2外围时钟
/*
初始化GPIO外围设备
*/
GPIO_InitTypeDef GPIO_InitStruct;
//结构体初始化
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //设置为开漏输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
while(1)
{
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET); //开启蜂鸣器
Delay_ms(100);
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET); //关闭蜂鸣器
Delay_ms(100);
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
Delay_ms(100);
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);
Delay_ms(500);
}
}
此处涉及到延时函数Delay_xs,其被放置在名为System的文件夹中,该文件夹存放系统资源,在此给出他们的代码
Delay.h
#ifndef __DELAY_H
#define __DELAY_H
void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);
#endif
Delay.c
#include "stm32f10x.h"
/**
* @brief 微秒级延时
* @param xus 延时时长,范围:0~233015
* @retval 无
*/
void Delay_us(uint32_t xus)
{
SysTick->LOAD = 72 * xus; //设置定时器重装值
SysTick->VAL = 0x00; //清空当前计数值
SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
SysTick->CTRL = 0x00000004; //关闭定时器
}
/**
* @brief 毫秒级延时
* @param xms 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_ms(uint32_t xms)
{
while(xms--)
{
Delay_us(1000);
}
}
/**
* @brief 秒级延时
* @param xs 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_s(uint32_t xs)
{
while(xs--)
{
Delay_ms(1000);
}
}
技术要点
1.在LED闪烁实验中, GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct),该函数用于初始化GPIO的外围设备,该函数的第一个形参用于指定要初始化的GPIO外围设备,第二个参数为一个指向GPIO_InitTypeDef的结构体指针,我们需要将结构体的地址传递给他。在程序中我们要先定义一个GPIO_InitTypeDe类型的结构体,该结构体涉及三个成员,GPIO_Pin,GPIO_Mode,GPIO_Speed,其中GPIO_Mode用于选定IO口的输出模式。有两种比较常见的输出模式GPIO_Mode_Out_PP和GPIO_Mode_Out_OD,分别为推挽输出和开楼输出。由他们的结构和原理可知:推挽输出高低电平均有驱动能力,而开漏输出只有低电平由驱动能力,而高电平没有驱动能力。
2.在控制端口输出高低电平时,有两个函数可以同时使用3种函数对IO口进行设置,GPIO_SetBits()和GPIO_ResetBits()分别能够将引脚设置为高电平和低电平,他们均接收两个参数。第一个参数为要设置的外围设备,第二个参数为要设置的引脚,由于引脚是使用16位数表示,每一个引脚对应一位,故第二个参数可以是任意引脚的组合(这里可以使用每个引脚按位与(因为16位数据中每一位控制一个引脚),更方便的方法是使用定义好的GPIO_Pin_All)。或者使用GPIO_WriteBit()函数,该函数接受三个参数,第一个为要设置的外围设备,第二个参数为要设置引脚,第三个参数为要设置电平状态,为Bit_Set和Bit_Reset枚举值。或者使用GPIO_Write() 该函数接受两个参数,分别是要设置的寄存器和要写入数据寄存器的值。
问题记录
1.在设置高速总线APB2时钟时,用错了函数,错误地使用成了void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);这个函数,该函数是用于强制或释放高速总线APB2复位的。应使用地函数为void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);