初级会员

- 积分
- 156
- 金钱
- 156
- 注册时间
- 2021-7-22
- 在线时间
- 28 小时
|
串口1(myus.c)代码(负责蓝牙的数据接收和数据处理)
- #include "myus.h"
- #include "mp3.h"
- #include "usart.h"
- void My_US_Init(void)
- {
- u8 i;
- u8 len;
- u16 times=0;
- GPIO_InitTypeDef GPIO_InitStrue;
- USART_InitTypeDef USART_InitStrue;
- NVIC_InitTypeDef NVIC_InitStrue;
- //使能串口,GPIo
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口,GPIO的时钟使能
- //初始化GPIO
- //PA9-TX,PA10-RX
- GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽
- GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
- GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOA,&GPIO_InitStrue);
- GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空
- GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
- GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOA,&GPIO_InitStrue);
- //初始化串口
- USART_InitStrue.USART_BaudRate=115200;//9600 115200 38400
- USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//不使用硬件流
- USART_InitStrue.USART_Mode=USART_Mode_Rx |USART_Mode_Tx ;//RXTX都要
- USART_InitStrue.USART_Parity=USART_Parity_No;//不用奇偶校验
- USART_InitStrue.USART_StopBits=USART_StopBits_1;//停止位为1
- USART_InitStrue.USART_WordLength=USART_WordLength_8b;//没有奇偶校验,8位
- USART_Init(USART1,&USART_InitStrue);
- USART_Cmd(USART1,ENABLE);//使能串口
- //开启中断
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//ITConfig就是在什么情况下开启中断 接收缓存区非空
- //初始化中断
- NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;
- NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
- NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级
- NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
- NVIC_Init(&NVIC_InitStrue);//中断初始化
- }
- void USART1_IRQHandler(void)
- {
- u8 res;
- if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)
- {
- res=USART_ReceiveData(USART1);
- switch(res)
- {
- (此处省略一些无关case)
- //---------------------------------MP3模块------------------------------------------------------------------------------
- case 0x10:
- MP3_PLAY();
- break;
- }
- }
- }
复制代码
usart.c代码(主要用来配置串口3,串口3用来发送数据来实现控制mp3模块)
- #include "sys.h"
- #include "usart.h"
- //////////////////////////////////////////////////////////////////////////////////
- //如果使用ucos,则包括下面的头文件即可.
- #if SYSTEM_SUPPORT_OS
- #include "includes.h" //ucos 使用
- #endif
- //////////////////////////////////////////////////////////////////////////////////
- //本程序只供学习使用,未经作者许可,不得用于其它任何用途
- //ALIENTEK STM32开发板
- //串口1初始化
- //正点原子@ALIENTEK
- //技术论坛:www.openedv.com
- //修改日期:2012/8/18
- //版本:V1.5
- //版权所有,盗版必究。
- //Copyright(C) 广州市星翼电子科技有限公司 2009-2019
- //All rights reserved
- //********************************************************************************
- //V1.3修改说明
- //支持适应不同频率下的串口波特率设置.
- //加入了对printf的支持
- //增加了串口接收命令功能.
- //修正了printf第一个字符丢失的bug
- //V1.4修改说明
- //1,修改串口初始化IO的bug
- //2,修改了USART_RX_STA,使得串口最大接收字节数为2的14次方
- //3,增加了USART_REC_LEN,用于定义串口最大允许接收的字节数(不大于2的14次方)
- //4,修改了EN_USART1_RX的使能方式
- //V1.5修改说明
- //1,增加了对UCOSII的支持
- //////////////////////////////////////////////////////////////////////////////////
-
- //////////////////////////////////////////////////////////////////
- //加入以下代码,支持printf函数,而不需要选择use MicroLIB
- #if 1
- #pragma import(__use_no_semihosting)
- //标准库需要的支持函数
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- //定义_sys_exit()以避免使用半主机模式
- _sys_exit(int x)
- {
- x = x;
- }
- //重定义fputc函数
- int fputc(int ch, FILE *f)
- {
- while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
- USART1->DR = (u8) ch;
- return ch;
- }
- #endif
- /*使用microLib的方法*/
- /*
- int fputc(int ch, FILE *f)
- {
- USART_SendData(USART1, (uint8_t) ch);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
-
- return ch;
- }
- int GetKey (void) {
- while (!(USART1->SR & USART_FLAG_RXNE));
- return ((int)(USART1->DR & 0x1FF));
- }
- */
-
- #if EN_USART1_RX //如果使能了接收
- //串口1中断服务程序
- //注意,读取USARTx->SR能避免莫名其妙的错误
- u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
- //接收状态
- //bit15, 接收完成标志
- //bit14, 接收到0x0d
- //bit13~0, 接收到的有效字节数目
- u16 USART_RX_STA=0; //接收状态标记
-
- void uart_init(u32 bound){
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
-
- //USART1_TX GPIOA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
-
- //USART1_RX GPIOA.10初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
-
- //USART 初始化设置
- USART_InitStructure.USART_BaudRate = bound;//串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
- USART_Init(USART1, &USART_InitStructure); //初始化串口1
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
- USART_Cmd(USART1, ENABLE); //使能串口1
- }
- #endif
- #if EN_USART3 //如果使能了接收
- u8 USART3_RX_BUF[USART3_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
- //接收状态
- //bit15, 接收完成标志
- //bit14, 接收到0x0d
- //bit13~0, 接收到的有效字节数目
- u16 USART3_RX_STA=0; //接收状态标记
- /*
- USART3专用的printf函数
- 当同时开启2个以上串口时,printf函数只能用于其中之一,其他串口要自创独立的printf函数
- 调用方法:USART3_printf("123"); //向USART3发送字符123
- */
- void USART3_printf (char *fmt, ...){
- char buffer[USART3_REC_LEN+1]; // 数据长度
- u8 i = 0;
- va_list arg_ptr;
- va_start(arg_ptr, fmt);
- vsnprintf(buffer, USART3_REC_LEN+1, fmt, arg_ptr);
- while ((i < USART3_REC_LEN) && (i < strlen(buffer))){
- USART_SendData(USART3, (u8) buffer[i++]);
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
- }
- va_end(arg_ptr);
- }
- void USART3_Init(u32 BaudRate){ //USART3初始化并启动
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能UART3所在GPIOB的时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能串口的RCC时钟
- //串口使用的GPIO口配置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//设置USART3的RX接口是PB11
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//接口模式 浮空输入
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//设置USART3的TX接口是PB10
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出速度50MHz
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//接口模式 复用推挽输出
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //Usart1 NVIC 配置
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
- //配置串口
- USART_InitStructure.USART_BaudRate = BaudRate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
- USART_Init(USART3, &USART_InitStructure);//配置串口3
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//使能串口接收中断
- USART_Cmd(USART3, ENABLE);//使能串口3
- }
- //串口3中断服务程序
- void USART3_IRQHandler(void){
- u8 Res;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){ //接收中断
- Res =USART_ReceiveData(USART3);//读取接收到的数据
- if(Res=='S'){//判断数据是否是STOP(省略读取S)
- USART3_RX_STA=1;//如果是STOP则标志位为1
- }else if(Res=='K'){//判断数据是否是OK(省略读取K)
- USART3_RX_STA=2;//如果是OK则标志位为2
- }
- }
- }
- #endif
复制代码
mp3.c代码
- #include "mp3.h"
- #include "usart.h"
- void MP3_Init(void){ //初始化
- USART3_Init(9600);//串口3初始化并启动
- MP3_STOP(); //上电初始化后发送一次指令激活MP3芯片
- }
- void MP3_PLAY(void){ //播放
- USART3_printf("\x7E \xFF \x06 \x03 \x00 \x00 \x01 \xEF"); //其中 \x 后接十六进制数据
- }
- void MP3_PAUSE(void){ //暂停
- USART3_printf("\x7e\x03\x12\x11\xef");
- }
- void MP3_PREV(void){ //上一曲
- USART3_printf("\x7e\x03\x14\x17\xef");
- }
- void MP3_NEXT(void){ //下一曲
- USART3_printf("\x7e\x03\x13\x10\xef");
- }
- void MP3_VUP(void){ //音量加1
- USART3_printf("\x7e\x03\x15\x16\xef");
- }
- void MP3_VDOWN(void){ //音量减1
- USART3_printf("\x7e\x03\x16\x15\xef");
- }
- void MP3_STOP(void){ //停止
- USART3_printf("\x7e\x03\x1E\x1D\xef");
- }
复制代码 main.c代码
- #include "stm32f10x.h"
- #include "delay.h"
- #include "myus.h"
- #include "mp3.h"
- #include "usart.h"
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- delay_init();
- My_US_Init();
- MP3_Init();
- while(1)
- {
- }
- }
复制代码 总工程师编译没有错误,只有有个别警告。不知道是配置还是思路逻辑的问题,发送0x01,串口3也不知道有没有数据传出去(MP3模块是没有反应),MP3模块小白求助!
|
|