代码设计上,通过串口将温度数据的高字节和低字节输出,可以对高字节和低字节按照公式计算,得到浮点格式的温度数据。主要的实现代码(main.c)如下:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*Written by Pegasus Yu in 2022
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
//us delay functions
__IO float usDelayBase;
void PY_usDelayTest(void)
{
__IO uint32_t firstms, secondms;
__IO uint32_t counter = 0;
firstms = HAL_GetTick()+1;
secondms = firstms+1;
while(uwTick!=firstms) ;
while(uwTick!=secondms) counter++;
usDelayBase = ((float)counter)/1000;
}
void PY_Delay_us_t(uint32_t Delay)
{
__IO uint32_t delayReg;
__IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);
delayReg = 0;
while(delayReg!=usNum) delayReg++;
}
void PY_usDelayOptimize(void)
{
__IO uint32_t firstms, secondms;
__IO float coe = 1.0;
firstms = HAL_GetTick();
PY_Delay_us_t(1000000) ;
secondms = HAL_GetTick();
coe = ((float)1000)/(secondms-firstms);
usDelayBase = coe*usDelayBase;
}
void PY_Delay_us(uint32_t Delay)
{
__IO uint32_t delayReg;
__IO uint32_t msNum = Delay/1000;
__IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);
if(msNum>0) HAL_Delay(msNum);
delayReg = 0;
while(delayReg!=usNum) delayReg++;
}
//MLX90614 I2C access protocol
#define us_num 10
#define SCL_OUT_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)
#define SCL_OUT_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)
#define SDA_OUT_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET)
#define SDA_OUT_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET)
#define SDA_IN HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6)
void I2C_Init(void)
{
SDA_OUT_H;
SCL_OUT_L;
PY_Delay_us_t(2000) ; //to enable i2c if previous mode PWM
SCL_OUT_H;
SDA_OUT_H;
PY_Delay_us_t(2000000) ;
}
void I2C_Start(void)
{
PY_Delay_us_t(us_num) ;
SDA_OUT_H;
SCL_OUT_H;
PY_Delay_us_t(us_num/2) ;
SDA_OUT_L;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_L;
}
void I2C_Stop(void)
{
SCL_OUT_L;
PY_Delay_us_t(us_num) ;
SDA_OUT_L;
PY_Delay_us_t(us_num) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SDA_OUT_H;
PY_Delay_us_t(us_num) ;
}
void I2C_Write_Ack(void)
{
PY_Delay_us_t(us_num/2) ;
SDA_OUT_L;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
SDA_OUT_H;
}
uint8_t I2C_Read_Ack(void)
{
uint8_t status=0;
SCL_OUT_L;
PY_Delay_us_t(us_num/2) ;
SDA_OUT_H;
PY_Delay_us_t(us_num/2) ;
status = SDA_IN;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
SDA_OUT_L;
return status;
}
void I2C_Send_Byte(uint8_t txd){
for(uint8_t i=0;i<8;i++)
{
PY_Delay_us_t(us_num/2) ;
if((txd&0x80)>>7) SDA_OUT_H;
else SDA_OUT_L;
txd<<=1;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
}
SDA_OUT_L;
}
uint8_t I2C_Read_Byte(unsigned char rdack)
{
uint8_t rxd=0;
for(uint8_t i=0;i<8;i++ )
{
SCL_OUT_L;
PY_Delay_us_t(us_num/2) ;
SDA_OUT_H;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_H;
rxd<<=1;
if(SDA_IN) rxd++;
PY_Delay_us_t(us_num) ;
}
SCL_OUT_L;
SDA_OUT_H;
if (rdack) I2C_Write_Ack();
return rxd;
}
uint8_t PY_CRC_MLX90614_READ(uint8_t daddr, uint8_t Raddr, uint8_t dl, uint8_t dh)
{ //Written by Pegasus Yu 2022/02/22
uint64_t cdata = 0; //Computed total data
uint16_t data_t = 0; //Process data of CRC computing
uint16_t crc_poly = 0x0107; //X^8+X^2+X^1+1 total 9 effective bits. Computed total data shall be compensated 8-bit '0' before CRC computing from 9-1=8.
uint16_t index_t = 47; ///bit shifting index for initial '1' searching
uint16_t index = 47; //bit shifting index for CRC computing
uint8_t rec = 0; //bit number needed to be compensated for next CRC computing
cdata |= (((uint64_t)daddr)<<40); //device write address
cdata |= (((uint64_t)Raddr)<<32); //register access address
cdata |= (((uint64_t)(daddr+1))<<24); //device read address
cdata |= (((uint64_t)dl)<<16); //data LSB
cdata |= (((uint64_t)dh)<<8); //data HSB
//8-bit '0' compensated into cdata so cdata involves 48 bits stored in 64-bit format.
while(index_t>0)
{
if( (cdata>>index_t)&1 )
{
index = index_t;
index_t = 0;
data_t |= (cdata>>(index-8));
{
data_t = data_t ^ crc_poly;
}
while(index!=0xffff)
{
if ((data_t>>7)&1) rec = 1;
else if ((data_t>>6)&1) rec = 2;
else if ((data_t>>5)&1) rec = 3;
else if ((data_t>>4)&1) rec = 4;
else if ((data_t>>3)&1) rec = 5;
else if ((data_t>>2)&1) rec = 6;
else if ((data_t>>1)&1) rec = 7;
else if ((data_t>>0)&1) rec = 8;
else rec = 9; ///
if((index-8)<rec)
{
data_t = data_t<<(index-8);
index = 0xffff;
}
else
{
for(uint8_t i=1;i<=rec;i++)
{
data_t = (data_t<<1)|((cdata>>(index-8-i))&1) ;
}
if(rec!= 9)
{
data_t = data_t ^ crc_poly;
index -= rec;
}
else
{
data_t = 0;
index_t = index-8-1;
index = 0xffff;
}
}
}
}
else
{
index_t--;
if(index_t<8) break;
}
}
return (uint8_t)data_t;
}
uint32_t Get_Temp_DATA( uint8_t ReaAd)
{
uint8_t Pecreg = 0;
uint8_t DataL = 0 ,DataH = 0;
uint32_t Result = 0;
uint8_t daddr = 0x00; //0x00 or 0xB4(0x5A<<1) for MLX90614 default device address
I2C_Start();
I2C_Send_Byte(daddr);
I2C_Read_Ack();
//I2C_Write_Ack();
I2C_Send_Byte(ReaAd);
//I2C_Write_Ack();
I2C_Read_Ack();
I2C_Start();
I2C_Send_Byte(daddr+1);
I2C_Read_Ack();
DataL=I2C_Read_Byte(1);
DataH=I2C_Read_Byte(1);
Pecreg=I2C_Read_Byte(0);
I2C_Stop();
Result |= (((uint32_t)DataH)<<24);
Result |= (((uint32_t)DataL)<<16);
Result |= (((uint32_t)Pecreg)<<8);
Result |= PY_CRC_MLX90614_READ(daddr, ReaAd, DataL, DataH);
return Result;
}
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
float temperature_f;
uint32_t temperature_d;
uint8_t temprst[4];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
PY_usDelayTest();
PY_usDelayOptimize();
I2C_Init();
PY_Delay_us(1000000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
temperature_d=Get_Temp_DATA(0x07);
temprst[0]= (temperature_d>>24)&0xff;
temprst[1]= (temperature_d>>16)&0xff;
temprst[2]= (temperature_d>>8)&0xff;
temprst[3]= (temperature_d>>0)&0xff;
if(temprst[2]==temprst[3]) HAL_UART_Transmit(&huart1, temprst, 2, 2700);
//HAL_UART_Transmit(&huart1, temprst, 4, 2700);
/*
temperature_f = (((float)((temprst[0]<<8)|temprst[1])) * 2 - 27315)/100; //T= (DataH:DataL)*0.02-273.15
HAL_UART_Transmit(&huart1, &temperature_f, 4, 2700);
*/
PY_Delay_us(2000000); //adjustable output delay
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
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_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_SET);
/*Configure GPIO pins : PA5 PA6 */
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
|