【嵌入式硬件芯片开发笔记】HART协议调制解调芯片AD5700配置流程

【嵌入式硬件芯片开发笔记】HART协议调制解调芯片AD5700配置流程
【嵌入式硬件芯片开发笔记】HART协议调制解调芯片AD5700配置流程_第1张图片

XTAL_EN接地,CLK_CFG的两个引脚由同一个GPIO控制
初始时HART_CLK_CFG输出低电平

由RTS引脚控制调制/解调。当RTS处于高电平时,为解调(输入);否则为调制(输出)。切换时,延时1ms

HART协议部分,符合HART物理层要求(修订版8.1)。
一个标准UART帧,该帧包含一个起始位、8位数据、一个奇偶校验和一个停止位
UART:1200波特率 1停止位 无校验 数据长度8
开始运行

  1. 检测HART_VDD,至少大于2.7V
  2. 引脚HART_CLK_CFG初始时为低电平,拉高后CLKOUT输出1.2288MHz,用定时器进行捕获,计算频率是否达标
  3. 最后开启调制或解调
    初始化
  4. 使能CFG引脚,进行时钟检测
  5. 采用定时器捕获的方式计算频率(计算速度不够则可能算不准)
  6. 关闭CFG引脚
  7. 解调模式接收数据
  8. 当串口接收中断发生时,解析命令并切换调制模式,返回数据

代码例程

uint8_t UART4_RxBuffer[256]={0};
uint8_t UART4_RxFlag=0;

【嵌入式硬件芯片开发笔记】HART协议调制解调芯片AD5700配置流程_第2张图片
【嵌入式硬件芯片开发笔记】HART协议调制解调芯片AD5700配置流程_第3张图片

/**
  * @brief UART4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_UART4_Init(void)
{

  /* USER CODE BEGIN UART4_Init 0 */

  /* USER CODE END UART4_Init 0 */

  /* USER CODE BEGIN UART4_Init 1 */

  /* USER CODE END UART4_Init 1 */
  huart4.Instance = UART4;
  huart4.Init.BaudRate = 1200;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART4_Init 2 */
	HAL_UART_Receive_IT(&huart4,UART4_RxBuffer,1);
  /* USER CODE END UART4_Init 2 */

}
/*!
 * @brief       	初始化AD5421
 *
 * @param 				None
 *
 * @return				None
 */
void Init_AD5421(void)
{	
	uint16_t offset = 0;
	uint16_t gain = 0;
	
	Ctrl_AD5421(&hspi2,AD5421_RESET,0,true);
	delay_us(50);
	
	Ctrl_AD5421(&hspi2,AD5421_Control,0xFC00,true);	
	
	offset = Ctrl_AD5421(&hspi2,AD5421_Offset_Adjust,0,false);	
	gain = Ctrl_AD5421(&hspi2,AD5421_Gain_Adjust,0,false);

	Ctrl_AD5421_DAC(20,true,offset,gain);
	
	Ctrl_AD5421_ADC(1,true);
	Ctrl_AD5421_ADC(0,true);
}

/*!
 * @brief       	使能HART_CLK_OUT的定时器,用于输入捕获,上升沿捕获,4次有效
 *
 * @param 				None
 *
 * @return				freq: 浮点型频率变量,单位为Hz
 */
float Get_HART_CLK_Cycle(void)
{
	uint32_t Count=0;
	float freq=0;
	TIM3_CH2_Flag=0;
	TIM3_CH2_Overflow=0;
	TIM3_CH2_Count[0]=0;
	TIM3_CH2_Count[1]=0;	
	TIM_RESET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_2);	
	__HAL_TIM_SET_CAPTUREPOLARITY(&htim3, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_FALLING);	
	HAL_TIM_Base_Start_IT(&htim3);
	HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
	
	while(TIM3_CH2_Flag<2);
	TIM3_CH2_Flag=0;
	Count=TIM3_CH2_Overflow*(0xFFFF+1)+TIM3_CH2_Count[1]-TIM3_CH2_Count[0];	
	freq=50000000.0f/Count;
	TIM3_CH2_Overflow=0;
	TIM3_CH2_Count[0]=0;
	TIM3_CH2_Count[1]=0;
	
	printf("[INFO] HART_CLK: %0.4f Hz\n",freq);
	return freq;
}

/*!
 * @brief       	操作AD5700的HART调制/解调
 *
 * @param 	[in]	dat_buf: 要写入的数据,可以填NULL
 *         	[in]	size: 要写入的数据个数,单位byte
 *        	[in]	WriteNotRead: true,写入(调制),发送UART数据然后通过HART发出来
 *  							false,读取(解调),接收HART数据,解调以后以中断的方式接收UART数据
 *
 * @return				None
 */
void Ctrl_AD5700_HART(uint8_t *dat_buf,uint8_t size,bool WriteNotRead)
{
	if(WriteNotRead)
	{
		Disable_AD5700_RTS;
		delay_ms(1);
		HAL_UART_Transmit(&huart4,dat_buf,size,0xFFFF);
	}
	else
	{
		Enable_AD5700_RTS;
		delay_ms(1);
		HAL_UART_Receive_IT(&huart4,&UART4_RxBuffer[UART4_RxFlag],1);  //开启中断 填入的buf最好是HART专用的数据buf 全局变量 然后在回调里面也改成HART的buf
	}
}

/*!
 * @brief       	初始化AD5700
 *
 * @param 				None
 *
 * @return				None
 */
void Init_AD5700(void)
{
	//ADC检测	
	
	Enable_HART_CLK_CFG;
//	//时钟检测
//	Get_HART_CLK_Cycle();
	
//	Disable_HART_CLK_CFG;
	
	Ctrl_AD5700_HART(NULL,0,false);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart==&huart2)
  {		
		HAL_UART_Transmit(&huart2,&RxBuffer,1,0xFFFF);
		HAL_UART_Receive_IT(&huart2,&RxBuffer,1);
  }
	else if(huart==&huart3)
  {		
		HAL_UART_Transmit(&huart3,&RxBuffer,1,0xFFFF);
		HAL_UART_Receive_IT(&huart3,&RxBuffer,1);
  }
	else if(huart==&huart4)
  {		
//		HAL_UART_Transmit(&huart4,&UART4_RxBuffer[UART4_RxFlag],1,0xFFFF);
		UART4_RxFlag++;
		HAL_UART_Receive_IT(&huart4,&UART4_RxBuffer[UART4_RxFlag],1);
  }
	else if(huart==&huart5)
  {		
		HAL_UART_Transmit(&huart5,&RxBuffer,1,0xFFFF);
		if((RxBuffer>>5)==6)
		{
			load_dac=(RxBuffer&0x1F);
		}
		HAL_UART_Receive_IT(&huart5,&RxBuffer,1);
  }
}

你可能感兴趣的:(嵌入式硬件芯片开发笔记,嵌入式硬件,单片机,stm32)