完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
这几天帮师姐做实验,需要将mpu6050模块测量得到的数据实时反馈至stm32f407中进行计算。师姐买的是mpu6050模块,自带编程且直接串口连接的那种(详情可见淘宝),不需要I2C驱动,通过串口得到的数据也是模块自己计算好的数据,因此只需要搭建好串口通讯程序即可,下面进入正题。
连线 两块mpu6050模块,可见下图,但是没有图中的SCL、SDA引脚,只有RX、TX引脚,因此只能利用stm32f407上的串口usart2、usart3等串口。 本文利用usart1与上位PC机进行通讯,打印读取的数据值,因此用跳线帽或者杜邦线将PA10与TXD、PA9与RXD连接起来;usart2接模块1,将模块1的VCC接5V输出、GND接地、TX接PA3、RX接PA2;usart3接模块2,TX接PB11、RX接PB10。 DMA方式 利用的串口有点多,并且stm32f407除了需要读取传输数据之前,还需要进行电机can通讯、反馈控制计算等等。我查资料发现,还有DMA这种神奇且方便的串口通讯方式。 DMA,全称为:Direct Memory Access,即直接存储器访问。DMA传输方式无需CPU直接控制传输,也没有中断处理方式那样保留现场和恢复现场的过程,通过硬件为RAM与I/O设备开辟一条直接传送数据的通路,能使CPU的效率大为提高。查手册中的DMA请求映射,找到USART2_RX与USART3_RX对应的通道及数据流,如下 USART2_RX --> DMA1通道4数据流5 USART3_RX --> DMA1通道4数据流1 编程 串口用DMA方式发送和接收,分以下几步: 1)串口初始化 2)DMA初始化 3)发送数据 4)接收数据 其中,串口2数据缓冲数组为RxBuffer_1[11],串口3数据缓冲数组为RxBuffer_2[11]。 串口usart2初始化(包含DMA初始化) void uart2_init(u32 bound){ //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //使能USART2时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能串口复位时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//开启DMA时钟 //串口2对应引脚复用映射 GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2复用为USART2 GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3复用为USART2 //USART2端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //GPIOA2与GPIOA3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA2,PA3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIOA2与GPIOA3 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA2,PA3 USART_OverSampling8Cmd(USART2, ENABLE); //USART2 初始化设置 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(USART2, &USART_InitStructure); //初始化串口2 /* Configure DMA Initialization Structure */ DMA_InitStructure.DMA_BufferSize = BUFFERSIZE ; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式 DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(USART2->DR)) ;//DMA外设地址 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_Priority = DMA_Priority_High; /* Configure RX DMA */ DMA_InitStructure.DMA_Channel = DMA_Channel_4 ;//UASRT3_RX映射引脚,数据流1 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)RxBuffer_1; //DMA存储器0地址 DMA_Init(DMA1_Stream5,&DMA_InitStructure); USART_ITConfig(USART2,USART_IT_TC,DISABLE); USART_ITConfig(USART2,USART_IT_RXNE,DISABLE); USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); //Usart2 NVIC 配置 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//串口2中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority =1; //子优先级1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 DMA_Cmd(DMA1_Stream5,ENABLE); USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE); USART_Cmd(USART2, ENABLE); //使能串口2 } 串口usart2中断服务子程序如下 void USART2_IRQHandler(void) { uint16_t i = 0; uint32_t temp = 0; if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET) { temp = USART2->SR; temp = USART2->DR; //清除USART_IT_IDLE中断标志 DMA_Cmd(DMA1_Stream5,DISABLE); if(RxBuffer_1[0] != 0x55) return; LED0 = 1;//红灯灭表明串口2正常进入接收中断 //设置传输数据长度 DMA_SetCurrDataCounter(DMA1_Stream5,BUFFERSIZE); //打开DMA DMA_Cmd(DMA1_Stream5,ENABLE); } } 串口usart3初始化(包含DMA初始化) void uart3_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //使能USART3时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能串口3复位时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//开启DMA时钟 //串口3对应引脚复用映射 GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //PB10复用为USART3 GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //PB11复用为USART3 //USART3端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB,&GPIO_InitStructure); USART_OverSampling8Cmd(USART3, ENABLE); //USART3 初始化设置 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(USART3, &USART_InitStructure); //初始化串口3 /* Configure DMA Initialization Structure */ DMA_InitStructure.DMA_BufferSize = BUFFERSIZE ; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式 DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(USART3->DR)) ;//DMA外设地址 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_Priority = DMA_Priority_High; /* Configure RX DMA */ DMA_InitStructure.DMA_Channel = DMA_Channel_4 ;//UASRT3_RX映射引脚,数据流1 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)RxBuffer_2; //DMA存储器0地址 DMA_Init(DMA1_Stream1,&DMA_InitStructure);//DMA1,stream1,ch4,外设为串口3 USART_ITConfig(USART3,USART_IT_TC,DISABLE); USART_ITConfig(USART3,USART_IT_RXNE,DISABLE); USART_ITConfig(USART3,USART_IT_IDLE,ENABLE); //Usart3 NVIC 配置 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口3中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级1,暂时先和串口2的一样 NVIC_InitStructure.NVIC_IRQChannelSubPriority =1; //子优先级1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 DMA_Cmd(DMA1_Stream1,ENABLE); USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE); USART_Cmd(USART3, ENABLE); //使能串口3 } |
|
|
|
串口usart3中断服务子程序如下
void USART3_IRQHandler(void) { uint16_t i = 0; uint32_t temp = 0; if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) { LED1 = 1;//绿灯灭表明串口3正常进入接收中断 temp = USART3->SR; temp = USART3->DR; //清除USART_IT_IDLE中断标志 DMA_Cmd(DMA1_Stream1,DISABLE); if(RxBuffer_2[0] != 0x55) return; //设置传输数据长度 DMA_SetCurrDataCounter(DMA1_Stream1,BUFFERSIZE); //打开DMA DMA_Cmd(DMA1_Stream1,ENABLE); } } 关于两个串口传输数据的中断优先级问题,这里我没有管,直接依靠硬件的优先级顺序进行数据的接收。 下面是串口usart1的初始化 void uart1_init(u32 bound){ //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; //NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟 //串口1对应引脚复用映射 GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1 //USART1端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10 //USART1 初始化设置 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_Cmd(USART1, ENABLE); //使能串口1 } mian.c如下 #include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #include "string.h" #include "stm32f4xx.h" extern u8 RxBuffer_1[BUFFERSIZE]; extern u8 RxBuffer_2[BUFFERSIZE]; //串口2接收mpu6050数据,利用DMA传输方式 //USART2_RX --> DMA1通道4数据流5 //DMA1,stream5,ch4,外设为串口2,存储器为RxBuffer_1,长度为BUFFERSIZE //USART3_RX --> DMA1通道4数据流1 //DMA1,stream1,ch4,外设为串口3,存储器为RxBuffer_2,长度为BUFFERSIZE int main(void) { uint8_t i, j = 0; float y = 0; uint8_t Re_buf_1[11] = {0,}; uint8_t Re_buf_2[11] = {0,}; float a[3],w[3],angle[3],T; delay_init(168); //延时初始化 uart2_init(115200);//串口2初始化波特率为115200,串口2用于接收mpu6050x1信号 uart3_init(115200);//串口3初始化波特率为115200,串口3用于接收mpu6050x2信号 uart1_init(115200); //串口1发送数据 delay_ms(200); /* Waiting the end of Data transfer */ while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); printf("串口2通"); while (DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) == RESET); while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); printf("串口3通"); while (DMA_GetFlagStatus(DMA1_Stream1, DMA_FLAG_TCIF1) == RESET); /* Clear DMA Transfer Complete Flags */ DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF1); DMA_ClearFlag(DMA1_Stream1,DMA_FLAG_TCIF1); /* Clear USART Transfer Complete Flags */ USART_ClearFlag(USART2,USART_FLAG_TC); USART_ClearFlag(USART3,USART_FLAG_TC); delay_ms(200); LED_Init(); //初始化与LED连接的硬件接口 LED0 = 0; //先点亮红灯 LED1 = 0; //点亮绿灯,红灯与绿灯点亮显示程序正常运行 while (1) { for (i = 0; i < 11; i++){ Re_buf_1 = RxBuffer_1; } j++; if(Re_buf_1[0] == 0x55)//检查帧头 { switch(Re_buf_1[1]) { case 0x51: //标识这个包是加速度包 a[0] = ((short)(Re_buf_1[3]<<8 | Re_buf_1[2]))/32768.0*16; //X轴加速度 a[1] = ((short)(Re_buf_1[5]<<8 | Re_buf_1[4]))/32768.0*16; //Y轴加速度 a[2] = ((short)(Re_buf_1[7]<<8 | Re_buf_1[6]))/32768.0*16; //Z轴加速度 T = ((short)(Re_buf_1[9]<<8 | Re_buf_1[8]))/340.0+36.25; //温度 break; case 0x52: //标识这个包是角速度包 w[0] = ((short)(Re_buf_1[3]<<8| Re_buf_1[2]))/32768.0*2000; //X轴角速度 w[1] = ((short)(Re_buf_1[5]<<8| Re_buf_1[4]))/32768.0*2000; //Y轴角速度 w[2] = ((short)(Re_buf_1[7]<<8| Re_buf_1[6]))/32768.0*2000; //Z轴角速度 T = ((short)(Re_buf_1[9]<<8| Re_buf_1[8]))/340.0+36.25; //温度 break; case 0x53: //标识这个包是角度包 angle[0] = ((short)(Re_buf_1[3]<<8| Re_buf_1[2]))/32768.0*180; //X轴滚转角(x 轴) angle[1] = ((short)(Re_buf_1[5]<<8| Re_buf_1[4]))/32768.0*180; //Y轴俯仰角(y 轴) angle[2] = ((short)(Re_buf_1[7]<<8| Re_buf_1[6]))/32768.0*180; //Z轴偏航角(z 轴) T = ((short)(Re_buf_1[9]<<8| Re_buf_1[8]))/340.0+36.25; //温度 //printf("usart2: X轴角度:%.2f Y轴角度:%.2f Z轴角度:%.2frn",angle[0],angle[1],angle[2]); break; default: break; } printf("usart2: X角度:%.2f Y角度:%.2f Z角度:%.2f rn",angle[0],angle[1],angle[2]); } for (j = 0; j < 11; j++){ Re_buf_2[j] = RxBuffer_2[j]; } if(Re_buf_2[0] == 0x55)//检查帧头 { switch(Re_buf_2[1]) { case 0x51: //标识这个包是加速度包 a[0] = ((short)(Re_buf_2[3]<<8 | Re_buf_2[2]))/32768.0*16; //X轴加速度 a[1] = ((short)(Re_buf_2[5]<<8 | Re_buf_2[4]))/32768.0*16; //Y轴加速度 a[2] = ((short)(Re_buf_2[7]<<8 | Re_buf_2[6]))/32768.0*16; //Z轴加速度 T = ((short)(Re_buf_2[9]<<8 | Re_buf_2[8]))/340.0+36.25; //温度 break; case 0x52: //标识这个包是角速度包 w[0] = ((short)(Re_buf_2[3]<<8| Re_buf_2[2]))/32768.0*2000; //X轴角速度 w[1] = ((short)(Re_buf_2[5]<<8| Re_buf_2[4]))/32768.0*2000; //Y轴角速度 w[2] = ((short)(Re_buf_2[7]<<8| Re_buf_2[6]))/32768.0*2000; //Z轴角速度 T = ((short)(Re_buf_2[9]<<8| Re_buf_2[8]))/340.0+36.25; //温度 break; case 0x53: //标识这个包是角度包 angle[0] = ((short)(Re_buf_2[3]<<8| Re_buf_2[2]))/32768.0*180; //X轴滚转角(x 轴) angle[1] = ((short)(Re_buf_2[5]<<8| Re_buf_2[4]))/32768.0*180; //Y轴俯仰角(y 轴) angle[2] = ((short)(Re_buf_2[7]<<8| Re_buf_2[6]))/32768.0*180; //Z轴偏航角(z 轴) T = ((short)(Re_buf_2[9]<<8| Re_buf_2[8]))/340.0+36.25; //温度 //printf("usart3: X轴角度:%.2f Y轴角度:%.2f Z轴角度:%.2frn",angle[0],angle[1],angle[2]); break; default: break; } printf("usart3: X角度:%.2f Y角度:%.2f Z角度:%.2f rn",angle[0],angle[1],angle[2]); } delay_ms(50); } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1641 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1562 浏览 1 评论
990 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
691 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1610 浏览 2 评论
1869浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
655浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
525浏览 3评论
541浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
514浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-26 17:03 , Processed in 0.962313 second(s), Total 52, Slave 43 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号