1、stm32和ky9250的接线
接线
STM32f103 ky-9250
VCC ---- VCC
GND ---- GND
RX2 (pa10) ---- TX
TX2 (pa9) ---- RX
接线(如果想在电脑串口助手中看数据,stm32接usb头)
USB-TTL工具 STM32f103
VCC ----- VCC
TX ----- RX1 (pa3)
RX ----- TX1 (pa2)
GND ----- GND
2、按装ch340-64串口驱动
3、在keil中修改stm32 main代码(见下图)文章后有完整例子下载包
#include <include.h>
#include "stdio.h"
#define LENGTH (52)
unsigned char str[LENGTH];
float decode_data[RAW_DATA_SIZE];
float gx, gy, gz, ax, ay, az, mx, my, mz, roll, pitch, yaw, q1, q2, q3, q4;
void decode(unsigned char *encode_data, float *decode_data)
{
int i = 0;
if (encode_data[0] != 0x50)
{
return;
}
for (i = 0; i < RAW_DATA_SIZE; i++)
{
decode_data[i] = (encode_data[3 * i + 1] * 256 * 256 \
+ encode_data[3 * i + 2] * 256 + encode_data[3 * i + 3] - 1000000) * 0.001;
}
}
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
return;
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
return;
}
int fputc(int ch,FILE *p)
{
USART_SendData(USART2,(u8)ch);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
return ch;
}
int main()
{
delay_init(72);
USART1_Config();
USART2_Config();
while(1)
{
if(str[0] == 0x50)
{
decode(str, decode_data);
gx = decode_data[0];
gy = decode_data[1];
gz = decode_data[2];
ax = decode_data[3];
ay = decode_data[4];
az = decode_data[5];
mx = decode_data[6];
my = decode_data[7];
mz = decode_data[8];
roll = decode_data[9];
pitch = decode_data[10];
yaw = decode_data[11];
q1 = decode_data[12];
q2 = decode_data[13];
q3 = decode_data[14];
q4 = decode_data[15];
printf("gx = %.3f, gy = %.3f, gz=%.3f\n", gx, gy, gz);
printf("ax = %.3f, ay = %.3f, az=%.3f\n", ax, ay, az);
printf("mx = %.3f, my = %.3f, mz=%.3f\n", mx, my, mz);
printf("roll = %.3f, pitch = %.3f, yaw=%.3f\n", roll, pitch, yaw);
printf("q1 = %.3f, q2 = %.3f, q3=%.3f, q4=%.3f\n", q1, q2, q3, q4);
}
delay_ms(500);
}
}
include.h中添加如下代码:
#ifndef __INCLUDE_H
#define __INCLUDE_H
#include<stm32f10x.h>
#include<delay.h>
#define RAW_DATA_SIZE (17) //加入一位最后位 校验位 固定值128
#define RAW_DATA_RANGE (3)
#define FRAME_HEADER_SIZE (1)
#define ENCODE_DATA_SIZE (FRAME_HEADER_SIZE + RAW_DATA_SIZE * RAW_DATA_RANGE)
#define FRAME_HEADER (0x50)
#endif
stm32f10x_it.c中最后行添加如下代码:
#define LENGTH (52)
extern unsigned char str[LENGTH];
unsigned char cnt = 0;
void USART1_IRQHandler(void)
{
unsigned char tmp_char;
if( USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET )
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
tmp_char = USART_ReceiveData(USART1);
str[cnt++] = tmp_char;
if(cnt>=LENGTH || str[0] != FRAME_HEADER)
{
cnt = 0;
}
}
}
4、keil中编译通过后写入stm32即可取得数据
17位ky9250软件包(内有stm32\arduino\C#\Matlab\树莓\Unity3d\python\ROS\英飞凌\Nvidia jetson linux 访问例程)
2024年3月3日驱动和上位机
链接:https://pan.baidu.com/s/16zoUz5U4WCZIcFwD02ytgA
提取码:abcd
--来自百度网盘超级会员V5的分享