链接: https://pan.baidu.com/s/1YoBRkn7s77zyUKNqScT4LQ?pwd=63us 提取码: 63us 复制这段内容后打开百度网盘手机App,操作更方便哦
//#include "xunji.h"
#include "OLED.h"
#include "PWM.h"
//#include "SERVO.h"
#include "delay.h"
#include "stm32f10x.h"
#include "myusart.h"
#include "led.h""
//#include "bluetooth.h"
//#include "ENCODER.h"
//#include "PID.h"
#include "wit_c_sdk.h"
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到调试串口 */
USART_SendData(USART3, (uint8_t) ch);
/* 等待串口数据发送完毕 */
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
return (ch);
}
/**
* 函数功能: 重定向c库函数getchar,scanf到USARTx
*/
int fgetc(FILE *f)
{
/* 等待串口输入数据 */
while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART3);
}
uint32_t uiBuad = 115200;
uint8_t ucRxData ;
void USART1_IRQHandler(void) {
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
// 从接收数据寄存器读取一个字节
uint8_t data = USART_ReceiveData(USART1);
// 处理接收到的数据
WitSerialDataIn(data);
// 重新启动接收中断
// UART_Start_Receive_IT();
// 清除USART1接收中断
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
#define ACC_UPDATE 0x01
#define GYRO_UPDATE 0x02
#define ANGLE_UPDATE 0x04
#define MAG_UPDATE 0x08
#define READ_UPDATE 0x80
static char s_cDataUpdate = 0;
static void AutoScanSensor(void);
static void SensorUartSend(uint8_t *p_data, uint32_t uiSize);
static void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum);
#define speed 20 //速度就是占空比,0-100
#define tar_psoition 1000
#define tar_psoition1 1000//位置式pid的目标位置
#define tar_speed 30 //位置式共同目标速度
#define tar_speed1 30
uint8_t time,key;
int16_t now_speed,now_position,now_speed1,now_position1;
float pwm,pwm1;
uint8_t i;
void USART3_IRQHandler(void);//二维码中断
int main(void)
{
OLED_Init();
LED_Init();
My_USART1_Init();
My_USART3_Init();
//motor_int();//俩电机初始化
//Encoder_Init();//A6,7
//Encoder1_Init();//A8,9
//xunji_gpio();
//pwm_motor();
OLED_ShowString(1, 1, "start");
OLED_ShowString(2, 1, "speed");
float fAcc[3], fGyro[3], fAngle[3], fYaw;
int i;
WitInit(WIT_PROTOCOL_NORMAL, 0x50);
WitSerialWriteRegister(SensorUartSend);
WitRegisterCallBack(CopeSensorData);
printf("\r\n********************** wit-motion normal example ************************\r\n");
AutoScanSensor();
Delay_ms(500);
while(1)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_13,1);
Delay_ms(1000);
GPIO_WriteBit(GPIOC, GPIO_Pin_13,0);
OLED_ShowSignedNum(2, 1, now_speed, 3);
Delay_ms(1000);
if(s_cDataUpdate)
{
for(i = 0; i < 3; i++)
{
fAngle[i] = sReg[Roll+i] / 32768.0f * 180.0f;
}
if(s_cDataUpdate & ANGLE_UPDATE)
{
fYaw = (float)((unsigned short)sReg[Yaw]) / 32768 * 180.0;
printf("angle:%.3f %.3f %.3f(%.3f)\r\n", fAngle[0], fAngle[1], fAngle[2], fYaw);
s_cDataUpdate &= ~ANGLE_UPDATE;
}
s_cDataUpdate = 0;
}
// calculatePID(fAngle[2],90);
}
}
//void USART1_IRQHandler()
//{
// if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
// {
// WitSerialDataIn(ucRxData);
// ucRxData=USART_ReceiveData(USART1);
// //UART_Start_Receive_IT(huart, &ucRxData, 1);
// }
//}
void USART3_IRQHandler()
{
u8 res;
if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)
{
res= USART_ReceiveData(USART3);
if(res=='1')
{
//forward(speed);
OLED_ShowString(1, 1, "buletooth1 ");
GPIO_WriteBit(GPIOC, GPIO_Pin_13,1);
//Delay_ms(1500);
//stop();
}
else if(res=='2')
{
// left_2(speed);
OLED_ShowString(1, 1, "buletooth2");
GPIO_WriteBit(GPIOC, GPIO_Pin_13,0);
}
}
}
void TIM2_IRQHandler(void)//定时器2中断->给编码器
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
time++;
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
static void SensorUartSend(uint8_t *p_data, uint32_t uiSize) {
for (uint32_t i = 0; i < uiSize; i++) {
// 发送一个字节
USART_SendData(USART1, p_data[i]);
// 等待发送完成
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {
}
}
}
static void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum)
{
int i;
for(i = 0; i < uiRegNum; i++)
{
switch(uiReg)
{
case AZ:
s_cDataUpdate |= ACC_UPDATE;
break;
case GZ:
s_cDataUpdate |= GYRO_UPDATE;
break;
case HZ:
s_cDataUpdate |= MAG_UPDATE;
break;
case Yaw:
s_cDataUpdate |= ANGLE_UPDATE;
break;
default:
s_cDataUpdate |= READ_UPDATE;
break;
}
uiReg++;
}
}
static void AutoScanSensor(void)
{
const uint32_t c_uiBaud[9] = {4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};
int i, iRetry;
for(i = 0; i < 9; i++)
{
uiBuad = c_uiBaud[i];
My_USART1_Init();
iRetry = 2;
do
{
s_cDataUpdate = 0;
WitReadReg(AX, 3);
Delay_ms(100);
if(s_cDataUpdate != 0)
{
printf("%d baud find sensor\r\n\r\n", c_uiBaud[i]);
return ;
}
iRetry--;
}while(iRetry);
}
printf("can not find sensor\r\n");
printf("please check your connection\r\n");
}
我在串口三上连接了蓝牙模块,串口一上连了jy61p陀螺仪,手机连接并获取数据