本次测评主要围绕前期申请的类目展开,本次测评基于自主开发的MAX6675热电偶温度采集模块展开,通过uart进行上下位机通讯。 一、STM32CubeMX调试本次调试主要围绕几项需要用到的硬件展开,分别是SPI通讯口,UART通讯口和按键口,PC13;PA1、PA2(UART2);PA5、PA6、PA7、PB0(SPI1)。 具体调试参数如下图,这里需要根据自己需求展开: (一)UART参数设置:(二)SPI参数设置(三)时钟设置设置完成后,即可生成代码进行软件调试。 二、软件程序调试主程序:int main(void) { / USER CODE BEGIN 1 / float K_Temperature ; //K型热电偶 温度值 HAL_Init(); SystemClock_Config(); MAX6675_Init(); //K型热电偶初始化 / 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_SPI1_Init(); MX_USART2_UART_Init(); / USER CODE BEGIN 2 / / USER CODE END 2 / / Infinite loop / / USER CODE BEGIN WHILE / while (1) { / USER CODE END WHILE / / USER CODE END WHILE /
MAX6675_ReadTemperature(); //读取热电偶温度值
= K_Temperature*100;
char str[1];//接收数据存放位置
K_Temperature);//搞定
//HAL_UART_Transmit(&huart2,(uint8_t *)"测\r",sizeof("测\r"),0xffff); / USER CODE BEGIN 3 / } / USER CODE END 3 / } MAX6675子程序://MAX6675 片选引脚初始化 void GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; / GPIO Ports Clock Enable / __HAL_RCC_GPIOB_CLK_ENABLE(); HAL_GPIO_WritePin(GPIOB, MAX6675_CS_Pin, GPIO_PIN_SET); /引脚配置 / GPIO_InitStruct.Pin = MAX6675_CS_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /** @brief max66675模块初始化 @param None @retval None */ void MAX6675_Init(void) { GPIO_Init(); //片选引脚初始化 MX_SPI1_Init(); //spi总线初始化 } /** @brief max6675模块读写一个字节的数据 @param txData:要发送的数据 @retval 接收到的数据 */ uint8_t MAX6675_ReadWriteByte(uint8_t txData) { unsigned char txdata,rxdata; txdata = txData; HAL_SPI_TransmitReceive(&hspi1,&txdata,&rxdata,1,1000); return rxdata; } /** @brief max6675模块读取测得的原始数据 @param None @retval 温度的原始数据 */ uint16_t MAX6675_ReadRawValue(void) { uint16_t tmp; MAX6675_CS(0); tmp = MAX6675_ReadWriteByte(0xff); //read MSB //printf("%d\n\r",tmp); tmp <<= 8; tmp |= MAX6675_ReadWriteByte(0xff); //read LSB //printf("%d\n\r",tmp); MAX6675_CS(1); if (tmp & 4) { // thermocouple open tmp = 4095; //未检测到热电偶 } else { tmp = tmp >> 3; } //printf("%d\n\r",tmp); return tmp; } /** @brief max6675模块读取温度 @param None @retval 温度值(单位:℃) */ float MAX6675_ReadTemperature(void) { return (MAX6675_ReadRawValue() * 1024.0 / 4096); } //MAX6675 片选控制 void MAX6675_CS(unsigned char choose) { if(choose == 1) { HAL_GPIO_WritePin(GPIOB, MAX6675_CS_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOB, MAX6675_CS_Pin, GPIO_PIN_RESET); } } /** @brief SPI1 Initialization Function @param None @retval None */ static void MX_SPI1_Init(void) { / USER CODE BEGIN SPI1_Init 0 / / USER CODE END SPI1_Init 0 / / USER CODE BEGIN SPI1_Init 1 / / USER CODE END SPI1_Init 1 / / SPI1 parameter configuration/ hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.Direction = SPI_DIRECTION_2LINES; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.CRCPolynomial = 7; hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; if (HAL_SPI_Init(&hspi1) != HAL_OK) { Error_Handler(); } / USER CODE BEGIN SPI1_Init 2 / / USER CODE END SPI1_Init 2 / } /** @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 / } /特别注意/ 采用系统自带UART传输数值类型数据时会存在乱码等问题,这里我采用重定向的方法解决,代码如下 int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff); return ch; } int fgetc(FILE *f) { uint8_t ch = 0; HAL_UART_Receive(&huart2, &ch, 1, 0xffff); return ch; } 三、心得体会分享最后就是实验结果啦,如图所示: 模块图: PS:采集的温度为室温条件下和手捏住热电偶的温度,该温度没有经过校准等处理,直接输出MAX6675反馈的数字量。 好啦,分享基本上到这里就告一段落啦,还是非常感谢主办方给予的宝贵机会,后面有机会还会继续尝试,继续分享,会去继续做实验啦! |
【狂欢三】STM32C031使用TIM定时器DMA方式实现PWM输出(二)
【狂欢三】STM32C031使用TIM定时器PWM输出
【STM32MP257】🥳STM32MP257-DK-开发板开箱体验
NUCLEO-U083RC学习历程-空闲中断+DMA接收不定长数据
刘氓兔的杂谈【001】-片上USB 高速PHY
【NUCLEO-WB09KE评测】+使用STM32CubeMX新建蓝牙工程
【NUCLEO-WB09KE评测】+基于STM32CubeMX创建工程点灯
刘氓兔的64位入门挑战【2】——MP257和cubemx
【NUCLEO-WB09KE评测】+实现读取温湿度传感器SHTC3数据
【NUCLEO-WB09KE评测】+实现串口printf打印和shell移植