寄存器读写驱动
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Read Bl0942 register.
- * [url=home.php?mod=space&uid=536309]@NOTE[/url] the reslt will be stored in BL0942_Elect,the converted I/V Parameter will be stored in BL0942_Elect
- * @param ICAddr Uart address of BL0942 .
- * @param Reg Target register to read data from.
- * @param Timeout Timeout duration.
- * @retval Flag_BL0942_R
- */
- uint8_t BL0942_Uart1_R(uint8_t ICAddr,uint8_t Reg,uint32_t Timeout)
- {
- uint8_t tmp_2,aTxBuffer[2] = {0},aRxBuffer[4] = {0};
- FourBytes_Type tmp_D;
- tmp_2=huart1.Instance->RDR;//???
- aTxBuffer[0]= ICAddr;
- aTxBuffer[1]= Reg;
- if(HAL_UART_Transmit(&huart1, (uint8_t*)aTxBuffer, 2, Timeout)!= HAL_OK)
- {
- Error_Handler();
- }
- if(HAL_UART_Receive(&huart1, (uint8_t *)aRxBuffer, 4, Timeout) != HAL_OK)
- {
- Error_Handler();
- }
- tmp_D.uByte[0]=aRxBuffer[0];
- tmp_D.uByte[1]=aRxBuffer[1];
- tmp_D.uByte[2]=aRxBuffer[2];
- tmp_2 = aTxBuffer[0] + aTxBuffer[1] + aRxBuffer[0] + aRxBuffer[1] + aRxBuffer[2];
- tmp_2=~tmp_2; //计算的校验和字节
- if (aRxBuffer[3]==tmp_2)
- {
- Flag_BL0942_R = 1;
- switch (Reg)
- {
- case Addr_I_RMS:
- tmp_D.uLongs=tmp_D.uLongs*100/Current_K;
- BL0942_Elect.Current_Value=tmp_D.uLongs;
- break;
- case Addr_V_RMS:
- tmp_D.uLongs=tmp_D.uLongs*100/Voltage_K;
- BL0942_Elect.Voltage_Value=tmp_D.uLongs;
- break;
- case Addr_WATT:
- tmp_D.uLongs=tmp_D.uLongs*100/Power_K;
- BL0942_Elect.Power_Value=tmp_D.uLongs;
- break;
- case Addr_FREQ:
- tmp_D.uLongs=100000000/tmp_D.uLongs; //转换为实际频率
- BL0942_Elect.Freq=tmp_D.uLongs;
- break;
- case Addr_I_FAST_RMS: //I_FAST_RMS
- tmp_D.uLongs=tmp_D.uLongs*100/Current_K;
- BL0942_Elect.F_Current_Value=tmp_D.uLongs;
- break;
- case Addr_CF_CNT: //电能脉冲计数
- if (tmp_D.uLongs>=BL0942_Elect.Fir_CF_CNT)
- {
- BL0942_Elect.Mid_CF_CNT+=(tmp_D.uLongs-BL0942_Elect.Fir_CF_CNT);
- }
- else //电能脉冲计数累积满出现溢出情况
- {
- BL0942_Elect.Mid_CF_CNT+=(tmp_D.uLongs+(0xFFFFFF-BL0942_Elect.Fir_CF_CNT));
- }
- BL0942_Elect.Fir_CF_CNT=tmp_D.uLongs;
- //判断是否累积超过1度电的脉冲数,需要确保读取时间间隔内的电量不超过1度电,否则用整除
- tmp_D.uLongs=Energy_K;
- if (BL0942_Elect.Mid_CF_CNT>=tmp_D.uLongs)
- {
- BL0942_Elect.Energy_kwh++;
- BL0942_Elect.Mid_CF_CNT-=Energy_K;
- }
- break;
- case Addr_WA_CREEP:
- if( tmp_D.uByte[0] == 11)
- {
- //communication success between WB55 and BL0942
- }
- else
- {
- //communication fail between WB55 and BL0942
- }
- break;
- case Addr_MASK:
- BL0942_Uart_Buffer[2] = tmp_D.uByte[0] |0x01;
- break;
- case Addr_MODE:
- BL0942_Uart_Buffer[2] = tmp_D.uByte[0];
- BL0942_Uart_Buffer[3] = tmp_D.uByte[1] | 0x03;
- break;
- case Addr_FAST_RMS_TH:
- break;
- default:
- break;
- }
- }
- else
- {
- Flag_BL0942_R = 0;
- }
- return Flag_BL0942_R;
- }
- /**
- * @brief Write Bl0942 user configuraetion register.
- * @note
- * @param PData[0] Uart address of BL0942 .
- * @param PData[1] Target register to be written.
- * @param PData[2...4] the config data.
- * @param PData[5] CHSUM.
- * @param Timeout Timeout duration.
- * @retval
- */
- void BL0942_Uart1_W(uint8_t *pData,uint32_t Timeout)
- {
- //send ICAddr & Reg address
- if(HAL_UART_Transmit(&huart1, (uint8_t*)pData, 6, Timeout)!= HAL_OK)
- {
- Error_Handler();
- }
- }
- void USART1_BaudRate_Reconfig(uint32_t baud)
- {
- huart1.Instance = USART1;
- huart1.Init.BaudRate = baud;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_8;
- huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
- huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- }
|