一,正确函数 ,该程序红 {MOD}部分可以直接输出到串口,且输出正确,Transform()函数为转换函数,纯数学计算。 void WriteAll_Byte(float *tData)函数不在主函数中,在另一个c文件中。
void WriteAll_Byte(float *tData)
{
//陀螺仪
Write_Byte(YGYRO_OUT);
while(!flag);
tData[0] = Transform(RxData[0])*0.005;
flag = 0;
printf("%f ",tData[0]);
Write_Byte(ZGYRO_OUT);
while(!flag);
tData[1] = Transform(RxData[1])*0.005;
flag = 0;
printf("%f ",tData[1]);
//加速度计
Write_Byte(XACCL_OUT);
while(!flag);
tData[2] = Transform(RxData[2])*0.005;
flag = 0;
printf("%f ",tData[2]);
Write_Byte(YACCL_OUT);
while(!flag);
tData[3] = Transform(RxData[3])*0.00333;
flag = 0;
printf("%f ",tData[3]);
Write_Byte(ZACCL_OUT);
while(!flag);
tData[4] = Transform(RxData[4])*0.00333;
flag = 0;
printf("%f ",tData[4]);
//磁阻
Write_Byte(XMAGN_OUT);
while(!flag);
tData[5] = Transform(RxData[5])*0.00333;
flag = 0;
printf("%f ",tData[5]);
Write_Byte(YMAGN_OUT);
while(!flag);
tData[6] = Transform(RxData[6])*0.5;
flag = 0;
printf("%f ",tData[6]);
Write_Byte(YMAGN_OUT);
while(!flag);
tData[7] = Transform(RxData[7])*0.5;
flag = 0;
printf("%f ",tData[7]);
Write_Byte(XGYRO_OUT);
while(!flag);
tData[8] = Transform(RxData[8])*0.5;
flag = 0;
printf("%f ",tData[8]);
spi_num = 0;
}
二 错误函数 :以指针rxdata作为函数WriteAll_Byte()的入口参数,下面红 {MOD}9个printf()函数却输出到串口的数据是错误的,为什么会出现这种情况?
注:该采集程序是用SPI中断采集的陀螺仪数据,flag为中断结束标志位,中断结束,则标志位为1;主函数执行WriteAll_Byte()后,则flag置0;
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "lcd.h"
#include "sdram.h"
#include "adis16405.h"
#include "spi.h"
#include "matrix.h"
#include "sins.h"
int main(void)
{
u8 i=0;
float rxdata[9]= {0};
float A[4] = {1,2,3,4};
float B[4] = {1,2,3,4};
float C[4] = {0};
Cache_Enable(); //打开L1-Cache
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz
delay_init(216); //延时初始化
uart_init(115200); //串口初始化
LED_Init(); //初始化LED
KEY_Init(); //初始化按键
SPI2_Init();
SPI2_SetSpeed(SPI_BAUDRATEPRESCALER_256);
Write_Byte(XGYRO_OUT);
while(!flag);
flag = 0;
while(1)
{
WriteAll_Byte((float*)rxdata);
printf("%f ",rxdata[0]);
printf("%f ",rxdata[1]);
printf("%f ",rxdata[2]);
printf("%f ",rxdata[3]);
printf("%f ",rxdata[4]);
printf("%f ",rxdata[5]);
printf("%f ",rxdata[6]);
printf("%f ",rxdata[7]);
printf("%f ",rxdata[8]);
printf("
");
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>