uart.c
如果使用#pragma import(__use_no_semihosting),则在MDK中不勾选use Microlib。当前代码直接重定向没使用#pragma import(__use_no_semihosting),故需要选择use Microlib。
#include "usart.h" #include <stdio.h> #include <string.h> #include "ad5592.h" #include "ad5766.h" #include "ad7699.h" ////////////////////////////////////////////////////////////////////////////////// #define SELF_CHECK_CMD_R 0x01 #define SELF_CHECK_CMD_T 0x02 #define LASER_CMD_R 0x03 #define LASER_CMD_T 0x04 #define LASER_PARAMETER_CMD_R 0x05 #define LASER_PARAMETER_CMD_T 0x06 #define TEC_PARAMETER_CMD_R 0x07 #define TEC_PARAMETER_CMD_T 0x08 #define MODULATOR_CMD_R 0x09 #define MODULATOR_CMD_T 0x0A #define FFT_CMD_R 0x0B #define FFT_CMD_T 0x0C //如果使用os,则包括下面的头文件即可. #if SYSTEM_SUPPORT_OS #include "includes.h" //os 使用 #endif UART_HandleTypeDef UART1_Handler; //UART句柄 //加入以下代码,支持printf函数,而不需要选择use MicroLIB //#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #if 0 #pragma import(__use_no_semihosting) //标准库需要的支持函数 struct __FILE { int handle; }; FILE __stdout; //定义_sys_exit()以避免使用半主机模式 void _sys_exit(int x) { x = x; } //重定义fputc函数 int fputc(int ch, FILE *f) { while((USART1->ISR&0X40)==0);//循环发送,直到发送完毕 USART1->TDR=(u8)ch; return ch; } #endif #if 1 //重定义fputc函数 int fputc(int ch, FILE *f) { while((USART1->ISR&0X40)==0);//循环发送,直到发送完毕 //USART1->RQR |= USART_RQR_TXFRQ; USART1->TDR=(u8)ch; return ch; } int fgetc(FILE *f) { while((USART1->ISR & UART_FLAG_RXNE) == RESET); //USART1->RQR |= USART_RQR_RXFRQ; return (int)(USART1->RDR); } #endif #if EN_USART1_RX //如果使能了接收 //串口1中断服务程序 //注意,读取USARTx->SR能避免莫名其妙的错误 u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. //接收状态 //bit15, 接收完成标志 //bit14, 接收到0x0d //bit13~0, 接收到的有效字节数目 u16 USART_RX_STA=0; //接收状态标记 u16 gUsartOneReceiveFlag = 0; //接收到一帧数据标志 u8 aRxBuffer[RXBUFFERSIZE]; //HAL库使用的串口接收缓冲 u8 gFrameInfo[RXBUFFERSIZE]; u16 gADC5767_ADCVal[8]; u8 FFT_Flag = 0; //FFT实时数据上报标志 extern ad5766_dev *dev; typedef struct{ uint16_t Head; uint8_t SourceID; uint8_t DestinationID; uint8_t Command; uint8_t Data[9]; uint16_t Tail; }FrameInfo_TypeDef; //初始化IO 串口1 //bound:波特率 void uart_init(u32 bound) { //UART 初始化设置 UART1_Handler.Instance=USART1; //USART1 UART1_Handler.Init.BaudRate=bound; //波特率 UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B; //字长为8位数据格式 UART1_Handler.Init.StopBits=UART_STOPBITS_1; //一个停止位 UART1_Handler.Init.Parity=UART_PARITY_NONE; //无奇偶校验位 UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //无硬件流控 UART1_Handler.Init.Mode=UART_MODE_TX_RX; //收发模式 HAL_UART_Init(&UART1_Handler); //HAL_UART_Init()会使能UART1 //HAL_UART_Receive_IT(&UART1_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量 } //UART底层初始化,时钟使能,引脚配置,中断配置 //此函数会被HAL_UART_Init()调用 //huart:串口句柄 void HAL_UART_MspInit(UART_HandleTypeDef *huart) { //GPIO端口设置 GPIO_InitTypeDef GPIO_Initure; if(huart->Instance==USART1)//如果是串口1,进行串口1 MSP初始化 { __HAL_RCC_GPIOA_CLK_ENABLE(); //使能GPIOA时钟 __HAL_RCC_GPIOB_CLK_ENABLE(); //使能GPIOA时钟 __HAL_RCC_USART1_CLK_ENABLE(); //使能USART1时钟 #if 1 GPIO_Initure.Pin=GPIO_PIN_9; //PA9 GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出 GPIO_Initure.Pull=GPIO_PULLUP; //上拉 GPIO_Initure.Speed=GPIO_SPEED_FAST; //高速 GPIO_Initure.Alternate=GPIO_AF7_USART1; //复用为USART1 HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA9 GPIO_Initure.Pin=GPIO_PIN_10; //PA10 HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA10 #endif #if 0 GPIO_Initure.Pin=GPIO_PIN_6; //PB6 GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出 GPIO_Initure.Pull=GPIO_PULLUP; //上拉 GPIO_Initure.Speed=GPIO_SPEED_FAST; //高速 GPIO_Initure.Alternate=GPIO_AF7_USART1; //复用为USART1 HAL_GPIO_Init(GPIOB,&GPIO_Initure); //初始化PB6 GPIO_Initure.Pin=GPIO_PIN_7; //PB7 HAL_GPIO_Init(GPIOB,&GPIO_Initure); //初始化PB7 #endif #if EN_USART1_RX HAL_NVIC_EnableIRQ(USART1_IRQn); //使能USART1中断通道 HAL_NVIC_SetPriority(USART1_IRQn,0,0); //抢占优先级3,子优先级3 #endif } } //接收完成处理回调函数 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance==USART1)//如果是串口1 { // if((USART_RX_STA&0x8000)==0)//接收未完成 // { // if(USART_RX_STA&0x4000)//接收到了0x0d // { // if(aRxBuffer[0]!=0x0a)USART_RX_STA=0;//接收错误,重新开始 // else USART_RX_STA|=0x8000; //接收完成了 // } // else //还没收到0X0D // { // if(aRxBuffer[0]==0x0d)USART_RX_STA|=0x4000; // else // { // USART_RX_BUF[USART_RX_STA&0X3FFF]=aRxBuffer[0] ; // USART_RX_STA++; // if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收 // } // } memset(gFrameInfo, 0, RXBUFFERSIZE); memcpy(gFrameInfo, aRxBuffer, RXBUFFERSIZE); gUsartOneReceiveFlag = 1; //接收一帧数据标志,待处理 HAL_UART_Receive_IT(&UART1_Handler,(u8 *)aRxBuffer, RXBUFFERSIZE); //} } } #if 0 //串口1中断服务程序 void USART1_IRQHandler(void) { u32 timeout=0; u32 maxDelay=0x1FFFF; #if SYSTEM_SUPPORT_OS //使用OS OSIntEnter(); #endif HAL_UART_IRQHandler(&UART1_Handler); //调用HAL库中断处理公用函数 timeout=0; while (HAL_UART_GetState(&UART1_Handler)!=HAL_UART_STATE_READY)//等待就绪 { timeout++;////超时处理 if(timeout>maxDelay) break; } timeout=0; while(HAL_UART_Receive_IT(&UART1_Handler,(u8 *)aRxBuffer, RXBUFFERSIZE)!=HAL_OK)//一次处理完成之后,重新开启中断并设置RxXferCount为1 { timeout++; //超时处理 if(timeout>maxDelay) break; } #if SYSTEM_SUPPORT_OS //使用OS OSIntExit(); #endif } #endif //end of //串口1中断服务程序 #endif// end of EN_USART1_RX #if 1 //串口1中断服务程序 void USART1_IRQHandler(void) { u8 res; #if SYSTEM_SUPPORT_OS //使用OS OSIntEnter(); #endif // if(__HAL_UART_GET_FLAG(&UART1_Handler, UART_FLAG_RXNE)!=RESET) // { // HAL_UART_Receive(&UART1_Handler, &res, 1, 1000); // if((USART_RX_STA&0x8000)==0)//接收未完成 // { // if(USART_RX_STA&0x4000)//接收到了0x0d // { // if(res!=0x0a)USART_RX_STA=0;//接收错误,重新开始 // else USART_RX_STA|=0x8000; //接收完成了 // } // else //还没收到0X0D // { // if(res==0x0d)USART_RX_STA|=0x4000; // else // { // USART_RX_BUF[USART_RX_STA&0X3FFF]=res ; // USART_RX_STA++; // if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收 // } // } // } // } HAL_UART_IRQHandler(&UART1_Handler); //调用HAL库中断处理公用函数 #if SYSTEM_SUPPORT_OS //使用OS OSIntExit(); #endif } #endif /*下面代码我们直接把中断控制逻辑写在中断服务函数内部。*/ /* //串口1中断服务程序 void USART1_IRQHandler(void) { u8 Res; #if SYSTEM_SUPPORT_OS //使用OS OSIntEnter(); #endif if((__HAL_UART_GET_FLAG(&UART1_Handler,UART_FLAG_RXNE)!=RESET)) //接收中断(接收到的数据必须是0x0d 0x0a结尾) { HAL_UART_Receive(&UART1_Handler,&Res,1,1000); if((USART_RX_STA&0x8000)==0)//接收未完成 { if(USART_RX_STA&0x4000)//接收到了0x0d { if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始 else USART_RX_STA|=0x8000; //接收完成了 } else //还没收到0X0D { if(Res==0x0d)USART_RX_STA|=0x4000; else { USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ; USART_RX_STA++; if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收 } } } } HAL_UART_IRQHandler(&UART1_Handler); #if SYSTEM_SUPPORT_OS //使用OS OSIntExit(); #endif } #endif */ /**---------------------------------------------------------------------------- ** @Function: FrameInfoProcess ** ** @Descriptions: ** ** @parameters: ** ** ** ** @Returned: none ** ** @Author: MCU Application Team ** ** @Date: 2017-11-16 ** -------------------------------------------------------------------------------*/ void FrameInfoProcess(void) { u8 RxBuffer[RXBUFFERSIZE]; FrameInfo_TypeDef* sFrameInfo = (FrameInfo_TypeDef*)RxBuffer; // uint16_t FrameHead = 0; // uint8_t SourceID = 0, DestinationID = 0; memcpy(RxBuffer, gFrameInfo, RXBUFFERSIZE); // FrameHead = RxBuffer[0] + (RxBuffer[1]<<8); // SourceID = RxBuffer[2]; // DestinationID = RxBuffer[3]; // sFrameInfo = (FrameInfo_TypeDef*)&RxBuffer; if(sFrameInfo->Head == 0x7E7E) { if((sFrameInfo->SourceID == 1)&&(sFrameInfo->DestinationID == 81)) { switch(sFrameInfo->Command) { /* #define SELF_CHECK_CMD_R 0x01 #define SELF_CHECK_CMD_T 0x02 #define LASER_CMD_R 0x03 #define LASER_CMD_T 0x04 #define LASER_PARAMETER_CMD_R 0x05 #define LASER_PARAMETER_CMD_T 0x06 #define TEC_PARAMETER_CMD_R 0x07 #define TEC_PARAMETER_CMD_T 0x08 #define MODE_CMD_R 0x09 #define MODE_CMD_T 0x0A #define FFT_CMD_R 0x0B #define FFT_CMD_T 0x0C */ case SELF_CHECK_CMD_R: //自检状态下发 { FFT_Flag = 0; HAL_UART_Transmit(&UART1_Handler, (uint8_t*)RxBuffer, RXBUFFERSIZE, 50); break; } case SELF_CHECK_CMD_T: //自检状态上报 { FFT_Flag = 0; break; } case LASER_CMD_R: //激光器命令下发 { FFT_Flag = 0; break; } case LASER_CMD_T: //激光器命令上报 { FFT_Flag = 0; break; } case LASER_PARAMETER_CMD_R: //激光器参数下发 { FFT_Flag = 0; break; } case LASER_PARAMETER_CMD_T: //激光器参数上报 { FFT_Flag = 0; break; } case TEC_PARAMETER_CMD_R: //TEC参数下发 { FFT_Flag = 0; break; } case TEC_PARAMETER_CMD_T: //TEC参数上报 { FFT_Flag = 0; break; } case MODULATOR_CMD_R: //调制器参数下发 { FFT_Flag = 0; break; } case MODULATOR_CMD_T: //调制器参数上报 { FFT_Flag = 0; break; } case FFT_CMD_R: //FFT实时数据状态下发 { FFT_Flag = 1; break; } case FFT_CMD_T: //FFT实时数据上报 { break; } default:break; } } } } void SelfCheck(uint8_t *rData) { } /*---------------------------------------------------------------------------- ** Function name: LaserCMDReciveProcess ** ** Descriptions: 激光器命令下发处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void LaserCmdReciveProcess(uint8_t* rData) { uint8_t temp[8]; uint8_t ChannelCtl = 0; memcpy(temp, &rData[5], 9); ChannelCtl = temp[0]&0x0f; if(ChannelCtl&0x0E) //LD1_ALS { HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4 , GPIO_PIN_RESET); } if(ChannelCtl&0x0D) //LD2_ALS { HAL_GPIO_WritePin(GPIOG, GPIO_PIN_5 , GPIO_PIN_RESET); } if(ChannelCtl&0x0B) //LD3_ALS { HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6 , GPIO_PIN_RESET); } if(ChannelCtl&0x07) //LD4_ALS { HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7 , GPIO_PIN_RESET); } } /*---------------------------------------------------------------------------- ** Function name: LaserCMDTransmitProcess ** ** Descriptions: 激光器上报命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void LaserCmdTransmitProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; FrameInfo_TypeDef *sFrameInfo = (FrameInfo_TypeDef *)&temp; memcpy(temp, rData, RXBUFFERSIZE); sFrameInfo->Head = 0x7e7e; sFrameInfo->Command = 0x04; sFrameInfo->SourceID = 0x51; sFrameInfo->DestinationID = 0x01; sFrameInfo->Tail = 0xFDFD; HAL_UART_Transmit(&UART1_Handler, (uint8_t*)temp, RXBUFFERSIZE, 50); } /*---------------------------------------------------------------------------- ** Function name: LaserParameterCmdReciveProcess ** ** Descriptions: 激光参数下发命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void LaserParameterCmdReciveProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; uint8_t ChannelCtl = 0; uint16_t ADC_Val = 0; memcpy(temp, &rData[5], 9); //只复制数据内容 ChannelCtl = temp[0]&0x0f; if(ChannelCtl&0x0E) // { ADC_Val = (temp[2]&0x0F)*256 + temp[1]; AD5592_DAC_Set(ADDR_DAC4, ADC_Val); } if(ChannelCtl&0x0D) // { ADC_Val = (temp[3]&0x0F)*256 + temp[4]; AD5592_DAC_Set(ADDR_DAC5, ADC_Val); } if(ChannelCtl&0x0B) // { ADC_Val = (temp[5]&0x0F)*256 + temp[6]; AD5592_DAC_Set(ADDR_DAC6, ADC_Val); } if(ChannelCtl&0x07) // { ADC_Val = (temp[7]&0x0F)*256 + temp[8]; AD5592_DAC_Set(ADDR_DAC7, ADC_Val); } } /*---------------------------------------------------------------------------- ** Function name: LaserParameterCmdReciveProcess ** ** Descriptions: 激光参数上报命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void LaserParameterCmdTransmitProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; FrameInfo_TypeDef *sFrameInfo = (FrameInfo_TypeDef *)&temp; memcpy(temp, rData, RXBUFFERSIZE); sFrameInfo->Head = 0x7e7e; sFrameInfo->Command = 0x06; sFrameInfo->SourceID = 0x51; sFrameInfo->DestinationID = 0x01; sFrameInfo->Tail = 0xFDFD; HAL_UART_Transmit(&UART1_Handler, (uint8_t*)temp, RXBUFFERSIZE, 50); } /*---------------------------------------------------------------------------- ** Function name: TECCmdReciveProcess ** ** Descriptions: TEC下发命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-19 ** -------------------------------------------------------------------------------*/ void TECCmdReciveProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; uint8_t ChannelCtl = 0; uint16_t ADC_Val = 0; memcpy(temp, &rData[5], 9); //只复制数据内容 ChannelCtl = temp[0]&0x0f; if(ChannelCtl&0x0E) // { ADC_Val = (temp[1]&0x0F)*256 + temp[2]; AD5592_DAC_Set(ADDR_DAC0, ADC_Val); } if(ChannelCtl&0x0D) // { ADC_Val = (temp[3]&0x0F)*256 + temp[4]; AD5592_DAC_Set(ADDR_DAC1, ADC_Val); } if(ChannelCtl&0x0B) // { ADC_Val = (temp[5]&0x0F)*256 + temp[6]; AD5592_DAC_Set(ADDR_DAC2, ADC_Val); } if(ChannelCtl&0x07) // { ADC_Val = (temp[7]&0x0F)*256 + temp[8]; AD5592_DAC_Set(ADDR_DAC3, ADC_Val); } } /*---------------------------------------------------------------------------- ** Function name: TECCmdTransmitProcess ** ** Descriptions: TEC上报命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void TECCmdTransmitProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; FrameInfo_TypeDef *sFrameInfo = (FrameInfo_TypeDef *)&temp; memcpy(temp, rData, RXBUFFERSIZE); sFrameInfo->Head = 0x7e7e; sFrameInfo->Command = 0x08; sFrameInfo->SourceID = 0x51; sFrameInfo->DestinationID = 0x01; sFrameInfo->Tail = 0xFDFD; HAL_UART_Transmit(&UART1_Handler, (uint8_t*)temp, RXBUFFERSIZE, 50); } /*---------------------------------------------------------------------------- ** Function name: ModulatorCmdReciveProcess ** ** Descriptions: 调制参数下发命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void ModulatorCmdReciveProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; uint8_t ChannelCtl = 0; uint16_t ADC_Val = 0; memcpy(temp, &rData[5], 9); //只复制数据内容 ChannelCtl = temp[0]&0x0f; if(ChannelCtl&0x10) //第5位为高为被控工作模式 { if(ChannelCtl&0x0E) // { ADC_Val = (temp[1]&0x0F)*256 + temp[2]; gADC5767_ADCVal[0] = ADC_Val; ad5766_set_dac_reg(dev, AD5766_DAC_0, ADC_Val); } if(ChannelCtl&0x0D) // { ADC_Val = (temp[3]&0x0F)*256 + temp[4]; gADC5767_ADCVal[1] = ADC_Val; ad5766_set_dac_reg(dev, AD5766_DAC_1, ADC_Val); } if(ChannelCtl&0x0B) // { ADC_Val = (temp[5]&0x0F)*256 + temp[6]; gADC5767_ADCVal[2] = ADC_Val; ad5766_set_dac_reg(dev, AD5766_DAC_2, ADC_Val); } if(ChannelCtl&0x07) // { ADC_Val = (temp[7]&0x0F)*256 + temp[8]; gADC5767_ADCVal[3] = ADC_Val; ad5766_set_dac_reg(dev, AD5766_DAC_3, ADC_Val); } } else//自主模式 { } } /*---------------------------------------------------------------------------- ** Function name: ModulatorCmdTansmitProcess ** ** Descriptions: 调制参数上报命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void ModulatorCmdTansmitProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; uint32_t adcval = 0; uint32_t WriteData = 0; FrameInfo_TypeDef *sFrameInfo = (FrameInfo_TypeDef *)&temp; memcpy(temp, &rData, RXBUFFERSIZE); sFrameInfo->Head = 0x7e7e; sFrameInfo->Command = 0x0C; sFrameInfo->SourceID = 0x51; sFrameInfo->DestinationID = 0x01; sFrameInfo->Tail = 0xFDFD; /* IN0,IN2,IN4,IN6:直流数据 */ sFrameInfo->Data[0] = 0x00; WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN0 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[1] = (adcval&0xff00) >> 8; sFrameInfo->Data[2] = (adcval&0x00ff); WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN2 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[3] = (adcval&0xff00) >> 8; sFrameInfo->Data[4] = (adcval&0x00ff); WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN4 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[5] = (adcval&0xff00) >> 8; sFrameInfo->Data[6] = (adcval&0x00ff); WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN6 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[7] = (adcval&0xff00) >> 8; sFrameInfo->Data[8] = (adcval&0x00ff); HAL_UART_Transmit(&UART1_Handler, (uint8_t*)temp, RXBUFFERSIZE, 50); /* IN1,IN3,IN5,IN7 交流数据 */ sFrameInfo->Data[0] = 0x00; WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN1 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[1] = (adcval&0xff00) >> 8; sFrameInfo->Data[2] = (adcval&0x00ff); WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN3 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[3] = (adcval&0xff00) >> 8; sFrameInfo->Data[4] = (adcval&0x00ff); WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN5 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[5] = (adcval&0xff00) >> 8; sFrameInfo->Data[6] = (adcval&0x00ff); WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN7 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[7] = (adcval&0xff00) >> 8; sFrameInfo->Data[8] = (adcval&0x00ff); HAL_UART_Transmit(&UART1_Handler, (uint8_t*)temp, RXBUFFERSIZE, 50); } /*---------------------------------------------------------------------------- ** Function name: FFTCmdReciveProcess ** ** Descriptions: FFT实时数据状态下发命令处理 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void FFTCmdReciveProcess(uint8_t* rData) { uint8_t temp[RXBUFFERSIZE]; uint32_t adcval = 0; uint32_t WriteData = 0; if(FFT_Flag == 0) //不实时上报FFT数据 { return; } else{ // FrameInfo_TypeDef *sFrameInfo = (FrameInfo_TypeDef *)&temp; // memcpy(temp, &rData, RXBUFFERSIZE); // sFrameInfo->Head = 0x7e7e; // sFrameInfo->Command = 0x0C; // sFrameInfo->SourceID = 0x51; // sFrameInfo->DestinationID = 0x01; // sFrameInfo->Tail = 0xFDFD; // /*第一通道*/ // // //直流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN0 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[1] = (adcval&0xff00) >> 8; // sFrameInfo->Data[2] = (adcval&0x00ff); // //交流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN1 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[3] = (adcval&0xff00) >> 8; // sFrameInfo->Data[4] = (adcval&0x00ff); // //FFT主杂比 // //待写 // sFrameInfo->Data[7] = (gADC5767_ADCVal[0]&0xff00) >> 8; // sFrameInfo->Data[8] = (gADC5767_ADCVal[0]&0x00ff); // // /*第二通道*/ // // //直流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN2 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[1] = (adcval&0xff00) >> 8; // sFrameInfo->Data[2] = (adcval&0x00ff); // //交流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN3 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[3] = (adcval&0xff00) >> 8; // sFrameInfo->Data[4] = (adcval&0x00ff); // //FFT主杂比 // //待写 // sFrameInfo->Data[7] = (gADC5767_ADCVal[1]&0xff00) >> 8; // sFrameInfo->Data[8] = (gADC5767_ADCVal[1]&0x00ff); // // /*第三通道*/ // // //直流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN4 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[1] = (adcval&0xff00) >> 8; // sFrameInfo->Data[2] = (adcval&0x00ff); // //交流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN5 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[3] = (adcval&0xff00) >> 8; // sFrameInfo->Data[4] = (adcval&0x00ff); // //FFT主杂比 // //待写 // sFrameInfo->Data[7] = (gADC5767_ADCVal[2]&0xff00) >> 8; // sFrameInfo->Data[8] = (gADC5767_ADCVal[2]&0x00ff); // // /*第四通道*/ // // //直流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN6 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[1] = (adcval&0xff00) >> 8; // sFrameInfo->Data[2] = (adcval&0x00ff); // //交流电压 // WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN7 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; // adcval = AD7699_data_write_and_read(WriteData); // sFrameInfo->Data[3] = (adcval&0xff00) >> 8; // sFrameInfo->Data[4] = (adcval&0x00ff); // //FFT主杂比 // //待写 // sFrameInfo->Data[7] = (gADC5767_ADCVal[3]&0xff00) >> 8; // sFrameInfo->Data[8] = (gADC5767_ADCVal[3]&0x00ff); } } /*---------------------------------------------------------------------------- ** Function name: FFT_DataRealReport ** ** Descriptions: FFT实时数据上报 ** ** parameters: rData:接收到的数据 ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-11-18 ** -------------------------------------------------------------------------------*/ void FFT_DataRealReport(void) { uint8_t temp[RXBUFFERSIZE]; uint32_t adcval = 0; uint32_t WriteData = 0; if(FFT_Flag == 0) //不实时上报FFT数据 { return; } else{ FrameInfo_TypeDef *sFrameInfo = (FrameInfo_TypeDef *)&temp; sFrameInfo->Head = 0x7e7e; sFrameInfo->Command = 0x0C; sFrameInfo->SourceID = 0x51; sFrameInfo->DestinationID = 0x01; sFrameInfo->Tail = 0xFDFD; /*第一通道*/ //直流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN0 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[1] = (adcval&0xff00) >> 8; sFrameInfo->Data[2] = (adcval&0x00ff); //交流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN1 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[3] = (adcval&0xff00) >> 8; sFrameInfo->Data[4] = (adcval&0x00ff); //FFT主杂比 //待写 sFrameInfo->Data[7] = (gADC5767_ADCVal[0]&0xff00) >> 8; sFrameInfo->Data[8] = (gADC5767_ADCVal[0]&0x00ff); /*第二通道*/ //直流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN2 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[1] = (adcval&0xff00) >> 8; sFrameInfo->Data[2] = (adcval&0x00ff); //交流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN3 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[3] = (adcval&0xff00) >> 8; sFrameInfo->Data[4] = (adcval&0x00ff); //FFT主杂比 //待写 sFrameInfo->Data[7] = (gADC5767_ADCVal[1]&0xff00) >> 8; sFrameInfo->Data[8] = (gADC5767_ADCVal[1]&0x00ff); /*第三通道*/ //直流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN4 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[1] = (adcval&0xff00) >> 8; sFrameInfo->Data[2] = (adcval&0x00ff); //交流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN5 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[3] = (adcval&0xff00) >> 8; sFrameInfo->Data[4] = (adcval&0x00ff); //FFT主杂比 //待写 sFrameInfo->Data[7] = (gADC5767_ADCVal[2]&0xff00) >> 8; sFrameInfo->Data[8] = (gADC5767_ADCVal[2]&0x00ff); /*第四通道*/ //直流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN6 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[1] = (adcval&0xff00) >> 8; sFrameInfo->Data[2] = (adcval&0x00ff); //交流电压 WriteData = CFG_OVERWRITE | INCC_UNIPOLAR_TO_GND | IN7 | BW_FULL | REF_IN_4V096 | SEQ_DISABLE | RB_DISABLE; adcval = AD7699_data_write_and_read(WriteData); sFrameInfo->Data[3] = (adcval&0xff00) >> 8; sFrameInfo->Data[4] = (adcval&0x00ff); //FFT主杂比 //待写 sFrameInfo->Data[7] = (gADC5767_ADCVal[3]&0xff00) >> 8; sFrameInfo->Data[8] = (gADC5767_ADCVal[3]&0x00ff); } }
main.c
/** * Copyright(C) 2017, * ALL rights reserved. * * @file main.c * @author MCU Application Team * @version V0.1 MDK version:4.70.0.0 * @date 9-June-2015 * @brief Project for WangChangXi * */ /*--------------------------Includes---------------------------------*/ #include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #include "adc.h" #include "dac.h" #include "timer.h" #include "dma.h" #include "timer.h" #include "spi.h" #include "ad5766.h" #include "ad7699.h" #include "ad5592.h" #include "arm_math.h" #include "arm_const_structs.h" #include "arm_const_structs.h" #include "FIR_user.h" #include <stdlib.h> #include <stdio.h> /*--------------------------Private define---------------------------*/ #define SIN_ON 1 #define SIN_OFF #define DELTA 0.00005f //误差值 #define FFT_LENGTH 4096 //FFT长度,默认是1024点FFT float fft_inputbuf[FFT_LENGTH*2]; //FFT输入数组 float fft_outputbuf[FFT_LENGTH]; //FFT输出数组 arm_cfft_radix4_instance_f32 scfft; float time; extern u16 gUsartOneReceiveFlag; //接收到一帧数据标志 extern u8 FFT_Flag; //FFT实时数据上报标志 extern float32_t testOutput[FFT_LENGTH]; /* 滤波后的输出 */ ad5766_dev *dev; /*--------------------------Private volitale------------------------*/ ad5766_init_param default_init_param = { // /* SPI */ // 0, // spi_chip_select // SPI_MODE_1, // spi_mode // SPI_ENGINE, // spi_type // 0, // spi_device_id // /* GPIO */ // PS7_GPIO, // gpio_type // GPIO_DEVICE_ID, // gpio_device_id // GPIO_RESET, // gpio_reset /* Device Settings */ AD5766_DISABLE, // daisy_chain_en 禁止菊花链 AD5766_ZERO, // clr 上电后保持零电平,中间电平,还是满量程。 AD5766_M_10V_TO_P_10V, // span 输出电压范围-10 ~ +10 0xfff0, // pwr_dac_setting :DAC0-DAC4输出通道开启 0xfff0, // pwr_dither_setting:DAC0-DAC4输出通道关断 0x0000000f, // dither_signal_setting:DAC0-DAC4 use N0 dither 0, // inv_dither_setting 0, // dither_scale_setting }; u8 timeout; extern uint16_t ADC1_ConvertData[2]; /*--------------------------Function declare-------------------------*/ void SinWave(uint8_t flag); void DeviceInit(void); void DSP_BasicMatchTest(void); void SignalGenerate(void); void FFT_Test(void); /**---------------------------------------------------------------------------- ** @Function: main ** ** @Descriptions: ** ** @parameters: ** ** ** ** @Returned: none ** ** @Author: MCU Application Team ** ** @Date: 2017-09-19 ** -------------------------------------------------------------------------------*/ int main(void) { u16 dacval = 0; u16 adcval = 0; int8_t input = 0; uint8_t str[100]; float fVal = 0; //char *str = "hellowankui!\r\n"; float temp; DeviceInit(); printf("Scanf test....\r\n"); // printf("\r\n================Choose================\r\n\r\n"); // printf("\r\n 1:ADN8810 S1\r\n"); // printf("\r\n 2:ADN8810 S2\r\n"); // printf("\r\n 3:ADN8810 S3\r\n"); // printf("\r\n 4:ADN8810 S4\r\n"); // printf("\r\n======================================\r\n\r\n"); //arm_cfft_radix4_init_f32(&scfft,FFT_LENGTH,0,1);//初始化scfft结构体,设定FFT相关参数 while(1) { ; #if 0 //SinWave(SIN_ON); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET); //LED0对应引脚PB1拉低,亮,等同于LED0(0) // HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET); //LED1对应引脚PB0拉高,灭,等同于LED1(1) delay_ms(100); //延时500ms printf("Led0 turn on, led1 turn off.\r\n"); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET); //LED0对应引脚PB1拉高,灭,等同于LED0(1) // HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET); //LED1对应引脚PB0拉低,亮,等同于LED1(0) printf("Led1 turn on, led0 turn off.\r\n"); delay_ms(100); //延时500ms #endif #if 0 dacval = 3103; HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,dacval);//设置DAC值 delay_ms(10); adcval = HAL_DAC_GetValue(&DAC1_Handler,DAC_CHANNEL_1);//读取前面设置DAC的值 temp=(float)adcval*(3.3/4096); //得到DAC电压值 printf("The DAC Output voltage is: %.3f\r\n", temp); #endif #if 0 adcval=Get_Adc_Average(ADC_CHANNEL_5,10); //得到ADC转换值 temp=(float)adcval*(3.3/4096); //得到DAC电压值 printf("The ADC Sample voltage is: %.3f\r\n", temp); #endif if(gUsartOneReceiveFlag == YES) { gUsartOneReceiveFlag = NO; FrameInfoProcess(); } // FFT_DataRealReport(); // HAL_UART_Transmit(&UART1_Handler, (uint8_t*)str, strlen(str)+1, 50); //DSP_BasicMatchTest(); //ADC_DMA_test(); //FFT_Test(); // printf("Hello world!\r\n"); //scanf("%s", str); //gets((char *)&str); printf("\r\n================Choose================\r\n\r\n"); printf("\r\n 1:ADN8810 S1\r\n"); printf("\r\n 2:ADN8810 S2\r\n"); printf("\r\n 3:ADN8810 S3\r\n"); printf("\r\n 4:ADN8810 S4\r\n"); printf("\r\n======================================\r\n\r\n"); gets((char *)&input); switch(input) { case'1': { printf("ADN8810 S1 Setting\r\n\r\n"); gets((char *)&str); fVal = atof((char *)&str); printf("fval = %.5f\r\n\r\n", fVal); break; } case'2': { printf("ADN8810 S2 Setting\r\n\r\n"); gets((char *)&str); fVal = atof((char *)&str); printf("fval = %.5f\r\n\r\n", fVal); break; } case'3': { printf("ADN8810 S3 Setting\r\n\r\n"); gets((char *)&str); fVal = atof((char *)&str); printf("fval = %.5f\r\n\r\n", fVal); break; } case'4': { printf("ADN8810 S4 Setting\r\n\r\n"); gets((char *)&str); fVal = atof((char *)&str); printf("fval = %.5f\r\n\r\n", fVal); break; } default:break; } //printf("%s\r\n", str); //delay_ms(1000); } } /*---------------------------------------------------------------------------- ** Function name: SinWave ** ** Descriptions: ** ** parameters: ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-09-21 ** -------------------------------------------------------------------------------*/ void SinWave(uint8_t flag) { int i =0; if(flag == SIN_ON) { for(; i<256; i++) { // DAC_SetChannel2Data(DAC_Align_12b_R, SinSheet[i]); //DAC_SetChannel2Data(DAC_Align_12b_R, gtemp); // HAL_DAC_SetValue(&DAC1_Handler, DAC_CHANNEL_1, DAC_ALIGN_12B_R, Sin[i]);//设置DAC值 delay_ms(10); } } else { //DAC_SetChannel2Data(DAC_Align_12b_R, 0); } } void DeviceInit(void) { Cache_Enable(); //打开L1-Cache HAL_Init(); //初始化HAL库 Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz delay_init(216); //延时初始化 uart_init(115200); //串口初始化 LED_Init(); GPIO_Init(); // MY_ADC_Init(); //ADC_DMA_Config(DMA2_Stream0, DMA_CHANNEL_0); // DAC1_Init(); // DAC_DMA_Config(DMA1_Stream5, DMA_CHANNEL_7); TIM3_Init(65535,10800-1); //10Khz计数频率,最大计时6.5秒超出 // TIM2_Init(540-1, 2-1); //定时器2初始化,定时器时钟为108M,分频系数为2-1, //所以定时器2的频率为108M/2 =54MHz,自动重装载为180-1. //那么定时器频率为:54MHz/180 = 300KHz,去触发DAC转换。 //由于正弦波波表100个点,故300KHz/100 = 3KHz。 // TIM4_Init(90-1 , 2-1); SPI_Init(); //FIR_Test(); //FFT_Test(); // ad5766_setup(&dev, default_init_param); // ad5766_set_dac_reg(dev, AD5766_DAC_0|AD5766_DAC_1|AD5766_DAC_2|AD5766_DAC_3, 0xfff); //输出10V Vout={spanxD/N}+Vmin = (10-(-10)x1-10=10V // ad5766_set_pwr_dac(dev, 0x000f); // //HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0);//初始值为0 } /*使用寄存器操作实现跑马灯 int main(void) { Cache_Enable(); //打开L1-Cache HAL_Init(); //初始化HAL库 Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz delay_init(216); //延时初始化 uart_init(115200); //串口初始化 LED_Init(); while(1) { GPIOB->BSRR=GPIO_PIN_1; //LED0亮 GPIOB->BSRR=GPIO_PIN_0<<16; //LED1灭 delay_ms(500); //延时500ms GPIOB->BSRR=GPIO_PIN_1<<16; //LED0灭 GPIOB->BSRR=GPIO_PIN_0; //LED1亮 delay_ms(500); //延时500ms } } */ //sin cos测试 //angle:起始角度 //times:运算次数 //mode:0,不使用DSP库;1,使用DSP库 //返回值:0,成功;0XFF,出错 u8 sin_cos_test(float angle,u32 times,u8 mode) { float sinx,cosx; float result; u32 i=0; if(mode==0) { for(i=0;i<times;i++) { cosx=cosf(angle); //不使用DSP优化的sin,cos函数 sinx=sinf(angle); result=sinx*sinx+cosx*cosx; //计算结果应该等于1 result=fabsf(result-1.0f); //对比与1的差值 if(result>DELTA)return 0XFF;//判断失败 angle+=0.001f; //角度自增 } }else { for(i=0;i<times;i++) { cosx=arm_cos_f32(angle); //使用DSP优化的sin,cos函数 sinx=arm_sin_f32(angle); result=sinx*sinx+cosx*cosx; //计算结果应该等于1 result=fabsf(result-1.0f); //对比与1的差值 if(result>DELTA)return 0XFF;//判断失败 angle+=0.001f; //角度自增 } } return 0;//任务完成 } /*---------------------------------------------------------------------------- ** Function name: DSP_BasicMatchTest ** ** Descriptions: ** ** parameters: ** ** ** ** Returned value: none ** ** Author: MCU Application Team ** ** Date:2017-09-21 ** -------------------------------------------------------------------------------*/ void DSP_BasicMatchTest(void) { float time; u8 buf[50]; u8 res; //不使用DSP优化 __HAL_TIM_SET_COUNTER(&TIM3_Handler,0);//重设TIM3定时器的计数器值 timeout=0; res=sin_cos_test(PI/6,200000,0); time=__HAL_TIM_GET_COUNTER(&TIM3_Handler)+(u32)timeout*65536; if(res==0)printf("Not Use DSP test time is:%0.1fms\r\n", time/10); else printf("Not Use DSP test ERROR!\r\n"); //使用DSP优化 __HAL_TIM_SET_COUNTER(&TIM3_Handler,0);//重设TIM3定时器的计数器值 timeout=0; res=sin_cos_test(PI/6,200000,1); time=__HAL_TIM_GET_COUNTER(&TIM3_Handler)+(u32)timeout*65536; if(res==0)printf("Use DSP test time is:%0.1fms\r\n", time/10); else printf("Use DSP test ERROR!\r\n"); LED0_Toggle; } void SignalGenerate(void) { int i; float temp = 0; // for(i=0; i<ADC_DMA_LEN; i++) // { // temp=(float)ADC1_ConvertData[i]*(3.3/4096); // printf("The ADC1_ConvertData[%d]= %x, vol = %.3f\r\n", i, ADC1_ConvertData[i], temp); // delay_ms(100); // } #if 0 SCB_DisableDCache(); for(i=0;i<FFT_LENGTH;i++)//生成信号序列 { // fft_inputbuf[2*i]=100+ // 10*arm_sin_f32(2*PI*i/FFT_LENGTH)+ // 30*arm_sin_f32(2*PI*i*4/FFT_LENGTH)+ // 50*arm_cos_f32(2*PI*i*8/FFT_LENGTH); //生成输入信号实部 temp=(float)ADC1_ConvertData[i]*(3.3/4096.0); printf("Temp[%d] = %.3f\r\n", i, temp); fft_inputbuf[2*i] = temp; //fft_inputbuf[2*i+1]= 3.3*arm_sin_f32(2*PI*i/FFT_LENGTH); fft_inputbuf[2]=0;//虚部全部为0 } SCB_EnableDCache(); #endif for(i=0;i<FFT_LENGTH;i++)//生成信号序列 { fft_inputbuf[2*i] = testOutput[i]; fft_inputbuf[2]=0;//虚部全部为0 } } void FFT_Test(void) { int i = 0; float temp; SignalGenerate(); __HAL_TIM_SET_COUNTER(&TIM3_Handler,0);//重设TIM3定时器的计数器值 timeout=0; //arm_cfft_radix4_f32(&scfft,fft_inputbuf); //FFT计算(基4) /* CFFT变换 */ arm_cfft_f32(&arm_cfft_sR_f32_len4096, fft_inputbuf, 0, 1); time=__HAL_TIM_GET_COUNTER(&TIM3_Handler)+(u32)timeout*65536;//计算所用时间 // sprintf((char*)buf,"%0.3fms\r\n",time/1000); // LCD_ShowString(30+12*8,160,100,16,16,buf); //显示运行时间 printf("\r\n------------------Start of FFT Test-------------------------\r\n"); arm_cmplx_mag_f32(fft_inputbuf,fft_outputbuf,FFT_LENGTH); //把运算结果复数求模得幅值 printf("\r\n%d point FFT runtime:%0.3fms\r\n",FFT_LENGTH,time/1000); printf("FFT Result:\r\n"); printf("\r\n------------------End of FFT Test-------------------------\r\n"); for(i=0;i<FFT_LENGTH;i++) { printf("%.5f,",fft_outputbuf[i]); delay_ms(2); } printf("\r\n------------------End of FFT Test-------------------------\r\n"); }
实验效果: