#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "UART3.h"
#include "UART2.h"
#include <string.h>
#include <stdio.h>
#include "JY901.h"
extern float dx2[3],vx2[3],aax2[3],dx3[3],vx3[3],aax3[3];
int main(void)
{
int i;
uart_init(115200);//接PC的串口
USART2_Config();//接JY-901模塊的串口
USART3_Config();//接JY-901模塊的串口
LED_Init();
delay_init();
NVIC_Configuration();
delay_ms(2000);//等等JY-91初始化完成
printf("角度01x\ty\tz\t02x\ty\tz\t每行數據間隔50毫秒\r\n");
while(1)
{
printf("%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n",dx2[0],dx2[1],dx2[2],dx3[0],dx3[1],dx3[2]);
delay_ms(100);
}
}
#include "led.h"
#include "sys.h"
#include "UART2.h"
#include <string.h>
#include <stdio.h>
#include "JY901.h"
float dx2[3],vx2[3],aax2[3];
struct SAcc Acc;//加速度
struct SGyro Gyro;//角速度
struct SAngle Angle;//角度
void CopeSerial2Data(unsigned char ucData)//讀jy901
{
static unsigned char ucRxBuffer[250];
static unsigned char ucRxCnt = 0;
ucRxBuffer[ucRxCnt++]=ucData;
if (ucRxBuffer[0]!=0x55) //數據頭不對,則重新開始尋找0x55數據頭
{
ucRxCnt=0;LED0=!LED0;
return;
}
if (ucRxCnt<11) {return;}//數據不滿11個,則返回
else
{
switch(ucRxBuffer[1])
{
case 0x51: memcpy(&Acc,&ucRxBuffer[2],8);break;
case 0x52: memcpy(&Gyro,&ucRxBuffer[2],8);break;
case 0x53: memcpy(&Angle,&ucRxBuffer[2],8);break;
}
ucRxCnt=0;
}
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|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 = 9600;//一般來講9600的波特率,采樣頻率是不能超過20Hz的。
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_Init(USART2, &USART_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//設置優先級配置的模式,第1組:搶占優先級0(0:7),搶占優先級1(0:7),
NVIC_InitStructure.NVIC_IRQChannel =USART2_IRQn; //B(右)碼盤中斷函數,在encoder.c中定義使用
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5; //0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
void USART2_IRQHandler(void)
{
int j;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
CopeSerial2Data((unsigned char)USART2->DR);//處理數據讀到JY901的數據
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
for(j=0;j<3;j++)
{
dx2[j]=(float)Angle.Angle[j]/32768*180;
}
}
USART_ClearITPendingBit(USART2,USART_IT_ORE);//溢出中斷
}
#include "led.h"
#include "sys.h"
#include "UART3.h"
#include <string.h>
#include <stdio.h>
#include "JY901.h"
float dx3[3] ,vx3[3], aax3[3];
struct SAcc Acc3;//加速度
struct SGyro Gyro3;//角速度
struct SAngle Angle3;//角度
void CopeSerial3Data(unsigned char ucData)//讀jy901
{
static unsigned char ucRxBuffer[250];
static unsigned char ucRxCnt = 0;
ucRxBuffer[ucRxCnt++]=ucData;
if (ucRxBuffer[0]!=0x55) //數據頭不對,則重新開始尋找0x55數據頭
{
ucRxCnt=0;LED0=!LED0;
return;
}
if (ucRxCnt<11) {return;}//數據不滿11個,則返回
else
{
switch(ucRxBuffer[1])
{
case 0x51: memcpy(&Acc3,&ucRxBuffer[2],8);break;
case 0x52: memcpy(&Gyro3,&ucRxBuffer[2],8);break;
case 0x53: memcpy(&Angle3,&ucRxBuffer[2],8);break;
}
ucRxCnt=0;
}
}
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;//一般來講9600的波特率,采樣頻率是不能超過20Hz的。
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_Init(USART3, &USART_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//設置優先級配置的模式,第1組:搶占優先級0(0:7),搶占優先級1(0:7),
NVIC_InitStructure.NVIC_IRQChannel =USART3_IRQn; //B(右)碼盤中斷函數,在encoder.c中定義使用
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7; //0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC);
}
void USART3_IRQHandler(void)
{
int j;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
CopeSerial3Data((unsigned char)USART3->DR);//處理數據讀到JY901的數據
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
for(j=0;j<3;j++)
{
dx3[j]=(float)Angle3.Angle[j]/32768*180;
}
}
USART_ClearITPendingBit(USART3,USART_IT_ORE);//溢出中斷
}
|