企业信息化建设方案 网站,网站目录结构设计应注意的问题,域名链接网站,wordpress主页显示浏览数1、简要说明与电路图
LED灯与蜂鸣器都是GPIO的输出操作#xff0c;给高低电平实现。GPIO操作也是后续操作的基础#xff0c;没有什么难度#xff0c;记不住寄存器没关系#xff0c;只要把流程理清楚就可以了。 端口配置成推挽输出模式#xff0c;高低电平均有驱动能力。 …1、简要说明与电路图
LED灯与蜂鸣器都是GPIO的输出操作给高低电平实现。GPIO操作也是后续操作的基础没有什么难度记不住寄存器没关系只要把流程理清楚就可以了。 端口配置成推挽输出模式高低电平均有驱动能力。
端口配置成开漏输出模式低电平有驱动能力高电平无驱动能力。 2、具体步骤
1开启外设时钟
为了实现低功耗STM32默认所有外设总线时钟全部关闭。要使用相应的外设首先开启相应外设总线的时钟。由上图可知四个LED灯都是接GPIOA组的0-3口。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
APB1是低速外设总线APB2是高速外设总线GPIO都是挂在APB2上的。 2配置GPIOA的引脚属性
a、定义GPIO结构体变量属于GPIO结构体类型
GPIO_InitTypeDef GPIO_InitStructure;
b、配置4个端口的属性为推挽输出模式和速度为50MHZ
GPIO_InitStructure.GPIO_PinGPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //引脚4 GPIO_InitStructure.GPIO_ModeGPIO_Mode_Out_PP; //引脚输入输出模式为推挽输出模式 GPIO_InitStructure.GPIO_SpeedGPIO_Speed_50MHz; //引脚输出速度为50MHZ
c、初始化各端口和设置端口初值 GPIO_Init(GPIOA, GPIO_InitStructure); //根据上面设置好的GPIO_InitStructure参数初始化引脚GPIOA_PIN0-3 GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3); //初始化设置引脚GPIOA0-3为高电平
3将这些过程操作封装成函数可以在主程序中调用
led.c与led.h
#include led.h
#include stm32f10x_gpio.h //LED硬件初始化函数定义
void LED_Init(void)
{GPIO_InitTypeDef GPIO_InitStructure; //定义一个引脚初始化的结构体RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟GPIOA挂载在APB2时钟下在STM32中使用IO口前都要使能对应时钟GPIO_InitStructure.GPIO_PinGPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //引脚4GPIO_InitStructure.GPIO_ModeGPIO_Mode_Out_PP; //引脚输入输出模式为推挽输出模式GPIO_InitStructure.GPIO_SpeedGPIO_Speed_50MHz; //引脚输出速度为50MHZGPIO_Init(GPIOA, GPIO_InitStructure); //根据上面设置好的GPIO_InitStructure参数初始化引脚GPIOA_PIN0-3GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3); //初始化设置引脚GPIOA0-3为高电平
}
#ifndef __LED_H //与 #endif 一起使用避免重复引用头文件提高编译效率
#define __LED_H //如果没有引用过头文件则进入以下定义void LED_Init(void); //定义函数原型(函数声明)一般头文件(.h文件)内都是函数原型//.c文件内都是函数定义
//该函数为LED硬件初始化函数
#endif //定义完毕或者引用过头文件到达这一步3、main函数
#include stm32f10x.h // Device header #include delay.h #include LED.h #include sys.h
int main(void) { LED_Init(); //调用初始化LED函数引用led.h后可使用 delay_init(); //调用初始化延迟函数引用delay.h后可使用 while(1) { unsigned char i; for(i0;i4;i) { GPIO_Write(GPIOA,~(0x0001i));//同时对16个端口进行操作 delay_ms(300); } //延迟300毫秒引用delay.h后可使用 } }
蜂鸣器的操作和LED一模一样只是换个GPIO口而已也是低电平鸣叫这个要根据电路图来。 文章转载自: http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.zpqlf.cn.gov.cn.zpqlf.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.rhnn.cn.gov.cn.rhnn.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.qwdlj.cn.gov.cn.qwdlj.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.kpxnz.cn.gov.cn.kpxnz.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn