赞
踩
今天玩玩语音模块,用到了所谓的一线串口通信(就是用一个IO口发脉冲)不说了,上代码
JQ8900-16P语音模块资料自取:链接:https://pan.baidu.com/s/1O3P1Ro4Rc4cVMACuJVdqaA 提取码:1u4p
接线:
模块——————————单片机
ONE LINE—————————PB11
DC-5V———————————5V
GND———————————GND
- OneUart.c文件
-
-
- #include "OneUart.h"
- #include "delay.h"
- #include "stm32f10x.h"
-
- ///
- //函 OnUart_GPIO(void)
- //功 能:语音模块一线串口IO口
- //输入参数: void
- //输出参数: void
- //说 明:
- //
- void OnUart_GPIO(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC->APB2ENR|=1<<3; //GPIOB
-
- //GPIOB.11
- GPIOB->CRH&=0xFFFF0FFF; //清零
- GPIOB->CRH|=0x00003000; //推挽输出 50MHZ
- GPIOB->ODR=~(1<<11); //B.11低
-
-
- }
- ///
- //函 数:SendData(u8 addr)
- //功 能:语音模块一线串口
- //输入参数: addr要发送的0x数
- //输出参数: void
- //说 明:
- //
- void SendData ( u8 addr )//发送函数。
- {
- u8 i;
-
- /*发送时关掉中断,防止中断影响时序 */
- SDA = 1; /*开始拉高*/
- delay_us ( 1000 );
- SDA = 0; /*开始引导码*/
- delay_us ( 3200 );/*此处延时最少要大于2ms*/
- for ( i = 0; i < 8; i++ ) /*总共8位数据 */
- {
- SDA = 1;
- if ( addr & 0x01 ) /*3:1表示数据位1,每个位用两个脉冲表示 */
- {
- delay_us ( 600 );
- SDA = 0;
- delay_us ( 200 );
- }
- else /*1:3表示数据位0 ,每个位用两个脉冲表示 */
- {
- delay_us ( 200 );
- SDA = 0;
- delay_us ( 600 );
- }
- addr >>= 1;
- }
- SDA = 1;
-
- //恢复中断
- }

- .h文件
-
- #ifndef __ONEUART_H
- #define __ONEUART_H
-
- #include "sys.h"
-
- #define SDA PBout(11)
-
- void SendData ( u8 addr ); //发送函数。
- void OnUart_GPIO(void); //GPIO
-
- #endif
-
-
- main文件
-
-
- #include "stm32f10x.h"
- #include "delay.h"
- #include "OneUart.h"
-
-
-
-
- int main(void)
- {
- OnUart_GPIO();
- delay_init();
- while(1)
- {
- //发送0x013
- SendData(0x0a); //清空数字
- SendData(0x01); //曲目号
- SendData(0x03);
- SendData(0x0b); //选曲播放
- delay_ms(2000); //延时
-
- //发送0x01
- SendData(0x0a);
- SendData(0x01);
- SendData(0x0b);
- delay_ms(2000);
-
- //发送0x07
- SendData(0x0a);
- SendData(0x07);
- SendData(0x0b);
- delay_ms(2000);
-
- //发送0x03
- SendData(0x0a);
- SendData(0x03);
- SendData(0x0b);
- delay_ms(2000);
-
- }
-
-
- }

在网上找了很多的延时函数,终于找到了一个达到要求的
- delay_us延时函数
-
-
- ///
- //函 数:delay_us(u6)
- //功 能:us延时函数
- //输入参数:u16 nTimer(延时时间)
- //输出参数:void
- //说 明:__NOP()【用__NOP()函数延时 72次】
- //
- void delay_us(u32 nTimer)
- {
- u32 i=0;
- for(i=0;i<nTimer;i++){
- __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
- __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
- __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
- __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
- __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。