van7 发表于 2016-8-16 04:03 PM

小雨儿Stm32学习板-蜂鸣器实验

<font color="#ff0000">这一次做一个简单的蜂鸣器实验</font><br />
大家对蜂鸣器应该都有所熟悉了就是一个用来发出滴滴滴的元器件<br />
然后就是看原理图<br />

<ignore_js_op>



</ignore_js_op>
<br />
小雨儿STM32板子蜂鸣器是接到PB10的,<font color="#ff0000">所以我们写程序的时候一定要打开GPIOB的时钟</font><br />
蜂鸣器的驱动部分很简单就是三极管、二极管、电阻、电容<br />
硬件基本就是这样了,下面是进行软件的编写<br />
软件设计<br />
首先我们要用到的库文件有STM32F10X_GPIO.C、STM32F10X_rcc.c<br />
然后就是公共文件里的延时函数,这个是我在网上拷贝过来用的<br />
&nbsp; &nbsp;<font color="#ff0000">&nbsp;&nbsp;SystemInit();&nbsp; &nbsp;&nbsp;&nbsp;</font>每个工程必须开始时要启动系统时钟<br />
&nbsp; &nbsp;&nbsp;&nbsp;<font color="#ff0000">RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);</font>&nbsp;&nbsp;//打开GPIOB的时钟<br />
&nbsp; &nbsp;while()主程序的函数<br />
&nbsp; &nbsp; &nbsp; &nbsp; <font color="#ff0000">{</font><br />
<font color="#ff0000">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPIO_SetBits(GPIOB,GPIO_Pin_10);&nbsp;&nbsp;</font><font color="#000000">&nbsp;&nbsp;//高电平输出到PB10</font><br />
<font color="#ff0000">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delay_ms(1000);//0.5S&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;延时</font><br />
<font color="#ff0000">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPIO_ResetBits(GPIOB,GPIO_Pin_10);&nbsp; &nbsp;</font><font color="#000000">//低电平输出到PB10</font><br />
<font color="#ff0000">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delay_ms(1000);//0.5S&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;</font><br />
<font color="#ff0000">&nbsp; &nbsp; &nbsp; &nbsp; }</font><br />
<br />
<font color="#ff0000">下面就是GPIO配置</font><br />
<font color="#ff00">{</font><br />
<font color="#ff0000">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;GPIO_InitTypeDef GPIO_InitStructure;</font><font color="#ff00">&nbsp; &nbsp; &nbsp; &nbsp; </font><font color="#ff0000">&nbsp;&nbsp;</font><font color="#000000">//定义一个函数名</font><br />
<br />
<font color="#ff00">&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;&nbsp; &nbsp; </font><font color="#000000">//定义PB10为输出端口</font><br />
<font color="#ff00">&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;&nbsp; &nbsp;</font><font color="#000000">//设置端口频率</font><br />
<font color="#ff00">&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;&nbsp; &nbsp; </font><font color="#000000"> //设置推挽输出</font><br />
<font color="#ff00">&nbsp; &nbsp; &nbsp; &nbsp; GPIO_Init(GPIOB,&amp;GPIO_InitStructure);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;</font><br />
<font color="#ff00">}</font><br />
<br />
<br />
写好后然后进行编译,如果没用问题可以直接下载到板子上。<br />
实验效果没办法展示,可以通过调节延时的快慢发出的声音也是不一样的。<br />

<ignore_js_op>



</ignore_js_op>
<br />

<ignore_js_op>



</ignore_js_op>
<br />
这是我KEIL里写的截图<br />

<ignore_js_op>



</ignore_js_op>
<br />
<font color="#ff0000">毫秒延时函数</font><br />

<ignore_js_op>



</ignore_js_op>
<br />
<font color="#ff0000">微妙延时函数</font><br />
<font color="#000000">写的不对的地方请大神们指点</font><br />
<font color="#000000">附件里是蜂鸣器程序</font><br />
<br />

javid 发表于 2016-8-16 04:06 PM


点个赞,支持

liyine 发表于 2016-8-16 04:25 PM


板凳坐起!
页: [1]
查看完整版本: 小雨儿Stm32学习板-蜂鸣器实验