源码:STM32F407接收openmv传回的数据

实验器材:
探索者STM32F4开发板

实验目的:
本实验为新建工程实验,仅供大家新建工程时参考。
新建工程详细步骤,请看《STM32F4开发指南-库函数版本》第3.3节。

硬件资源:
1,串口1(波特率:115200,PA9/PA10连接在板载USB转串口芯片CH340上面)

实验现象:
本实验下载后,串口1将不停的输出t的值,打开电脑串口调试助手即可查看现象。

#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "usart2.h"
#include "usart3.h"
#include "usart4.h"
#include "usart5.h"
#include "usart6.h"
#include "Openmv_receive.h"

int main(void)
{
    int T=0;
    int Yaw=0;
    int Pitch=0;
    u8 Key_Num=0;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	uart_init(115200);
    KEY_Init();
    uart2_init(115200);
//    uart3_init(115200);
//    uart4_init(115200);
//    uart5_init(115200);
//    uart6_init(115200);
    
	delay_init(84);	
    while(1){
        Key_Num=KEY_Scan(0);
        if(Key_Num==1){
        //USART_SendData(USART2,0x01);
       // USART_SendData(USART2,'\r\n');
        USART_SendData(USART2,0x01);   
        //USART_SendData(USART1,'\r\n');            
        }
        if(Openmv_read(&T,&Yaw,&Pitch)){
        printf("%d\r\n%d\r\n%d\r\n",T,Yaw,Pitch);
        }
        
	}
}
#include "Openmv_receive.h"

u16 RxBuffer[6];

u8 RxCounter1 = 0;

void Openmv_Receive_Data(u16 data)
{
    static u8 state = 0;

    if(state==0&&data==0x55){
          state=1;
          RxBuffer[RxCounter1++]=data;
    }else if(state==1&&data==0x55){
          state=2;
          RxBuffer[RxCounter1++]=data;
    }else if(state==2){
          RxBuffer[RxCounter1++]=data;
          if(RxCounter1 >5||data==0xff)state=3;
    }if(state==3){
          if(RxBuffer[RxCounter1-1]==0xff){
             state = 0;
             RxFlag = 1;
             RxCounter1 = 0; 
             //USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);  //完成一次接收,关闭串口
          }else{
             state = 0;
             RxCounter1 = 0;                       //数组溢出,循环存数据
          }
    }
}

u8 Openmv_read(int *T,int *yaw,int *pitch)
{
    static u8 read_pos = 0;
     if(RxFlag == 1){
        if(RxBuffer[read_pos-1]==0x55&&RxBuffer[read_pos]==0x55){
           *T=(int)RxBuffer[++read_pos];
           *yaw=(int)RxBuffer[++read_pos];
           *pitch=(int)RxBuffer[++read_pos];   
           RxFlag = 0;           
           read_pos = 0;  
           return 1;            
         }else{
          read_pos++;
          return 0;
         }
     }
     

}

#ifndef __OPENMV_RECEIVE__H
#define __OPENMV_RECEIVE__H
#include "sys.h"
#include "stm32f4xx_conf.h"

extern u16 RxBuffer[6];
static u8 RxFlag = 0; 

void Openmv_Receive_Data(u16 data);
u8 Openmv_read(int *T,int *yaw,int *pitch);

#endif

完整工程:STM32F407接收openmv传回的数据

你可能感兴趣的:(单片机,32单片机,F407,openmv,串口通信)