1. 选择芯片,配置串口引脚, 开启中断,设置波特率:9600
2. 源码如下
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* stm32 f103 c8xx 实现串口数据接收和发送
*
******************************************************************************
*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
uint8_t UART1_len=1; //1次只收1个
uint8_t UART1_arr[1]={0};
uint8_t UART1_buf[2]={0};//最大收多少
uint8_t UART1_buf_i=0;
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
//开启串口接收中断
HAL_UART_Receive_IT(&huart1, (uint8_t *)UART1_arr, UART1_len);
while (1)
{
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {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_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
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_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
//crc modbus485 声明
uint8_t crc16_1 =0;
uint8_t crc16_2 =0;
uint16_t crc_table[] = {
0x0000, 0xcc01, 0xd801, 0x1400,
0xf001, 0x3c00, 0x2800, 0xe401,
0xa001, 0x6c00, 0x7800, 0xb401,
0x5000, 0x9c01, 0x8801, 0x4400
};
void rs485_crc16(uint8_t *arr,uint8_t len)
{
uint16_t crc = 0xffff;
uint8_t i;
uint8_t temp;
for(i = 0; i < len; i++) {
temp = *arr++;
crc = crc_table[(temp ^ crc) & 15] ^ (crc >> 4);
crc = crc_table[((temp >> 4) ^ crc) & 15] ^ (crc >> 4);
}
crc16_1 = (uint8_t)(crc & 0xff ); //高8位
crc16_2 = (uint8_t)(crc >> 8); //低8位
}
//串口接收回调
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
if(huart->Instance == USART1){ //uart1 端口
UART1_buf[UART1_buf_i] = UART1_arr[0];
UART1_buf_i++;
if(0x0a == UART1_arr[0]){ //检测到结束符 是 0A
//简单业务逻辑处理
if(0x01==UART1_buf[0] && 0x0A==UART1_buf[1] ){ //如果发送的是 01 0A 则返回,发送的内容,其他的不返回
//我们返回485
uint8_t return_arr[6] = {0x01,0x0A,0x02,0x00,0x00,0x00};
rs485_crc16(return_arr, 4);
return_arr[4] = crc16_1;
return_arr[5] = crc16_2;
HAL_UART_Transmit(&huart1, (uint8_t *)return_arr, sizeof(return_arr), 1000);
}
//i置0
UART1_buf_i = 0;
}
//再次开启打印
HAL_UART_Receive_IT(&huart1, (uint8_t *)UART1_arr, UART1_len);
}
}
/**
* @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 */
数据预览:
感谢您的支持,写的文章如对您有所帮助,开源不易,请您打赏,谢谢啦~