如上面所述,雷达在定高模式,悬停模式和定点模式中起作用。只有当雷达数据超过RNGFND_MAX_CM后才会转换为使用气压计定高。 |
// 全局变量 u16 distance = 0; // 串口使用的变量 static u8 Usart1buf[USART1_BUF_SIZE]; static u8 pointer = 0; // 串口1初始化函数 void USART1_Init(void) { USART_InitTypeDef USART1_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // A口时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // A9 -> TX , A10 -> RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStructure); // 复用功能配置 GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); // USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // USART1中断优先级 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // USART1初始化 USART_DeInit(USART1); USART1_InitStructure.USART_BaudRate = 115200; USART1_InitStructure.USART_WordLength = USART_WordLength_8b; USART1_InitStructure.USART_StopBits = USART_StopBits_1; USART1_InitStructure.USART_Parity = USART_Parity_No; USART1_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; USART1_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1,&USART1_InitStructure); USART_Cmd(USART1,ENABLE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); } // 串口中断函数 void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET || (USART_GetITStatus(USART1, USART_IT_ORE_RX) != RESET)) { USART_ClearITPendingBit(USART1, USART_IT_RXNE); Usart1buf[pointer++%USART1_BUF_SIZE] = USART_ReceiveData(USART1); // 在此写代码接收程序 if((pointer%USART1_BUF_SIZE >= 9)) { // 包头包尾确认 // 最好加上校验和的验证,此处为了简单,省略了校验和 if( (Usart1buf[pointer%USART1_BUF_SIZE-3]==0x59) &&(Usart1buf[pointer%USART1_BUF_SIZE-4]==0x59)) { //距离 distance=((u16)((Usart1buf[pointer%USART1_BUF_SIZE-1])<<8) |(u16)(Usart1buf[pointer%USART1_BUF_SIZE-2])); } } } } |
void DAC_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_Init(GPIOA, &GPIO_InitStructure); DAC_InitTypeDef DAC_InitStructure; DAC_DeInit(); DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1,ENABLE); DAC_SetChannel1Data(DAC_Align_12b_R,0x1fff); DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); } |
// TF01模块测试的高度,单位mm float test_height; // 高度偏置,单位mm float bias = 180; // 应该输出的模拟值 s16 analog=0; // 主函数 void main(void) { // 中断分组的配置 NVIC_Config(); // 串口初始化 USART1_Init(); // DAC配置 DAC_Config(); // 主循环 while(1) { // 去除偏置 test_height = distance - bias; // 数据转换,3V对应高度为12m,则: // test_height * 3.3 * 4096/(3 * 1200)= test_height * 4096 / 1320 analog = (s16)(test_height*4096/1320); // 范围限制 analog = analog < 4095 ? analog : 4095; analog = analog > 0 ? analog : 0; // 电压输出 DAC_SetChannel1Data(DAC_Align_12b_R,analog); DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); } } |
图 4 模拟输入方式连接示意图.png (99.96 KB, 下载次数: 398)
欢迎光临 机器人与人工智能爱好者论坛 (http://robot-ai.org/) | Powered by Discuz! X3.2 |