找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1744|回复: 0
打印 上一主题 下一主题
收起左侧

STM32读取两个陀螺仪

[复制链接]
跳转到指定楼层
楼主
ID:626958 发表于 2019-12-25 14:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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);//溢出中断
}       



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表