STM32字符转整型处理

26 篇文章 6 订阅
20 篇文章 2 订阅

在这里插入图片描述

/* USER CODE BEGIN Header */
 
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>


/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
unsigned int chuli(char *shuju,unsigned int *shuzhi);
/* USER CODE END PTD */
int fputc(int ch, FILE *f){
HAL_UART_Transmit (&huart1,(uint8_t *)&ch,1,0xffff);
	return ch;
}
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(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 */
 unsigned int shuzhi[20];
 unsigned int count;
 unsigned int i;
 char data[100]="KBCA1A22A333A4444A5555A66666A12345A7654A12389A";
 int s = strlen(data);
 printf("一共有 %d 个字符\r\n",s);
 data[s]= '\r';
 data[s+1]= '\n';
 printf("%s\r\n",data);
 count = chuli( data, shuzhi);
 printf("一共有 %d 个数\r\n",count);
 for(i=0;i<count;i++)
 {
  printf("%d. %d \r\n",i+1,shuzhi[i]);
	 }
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* 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};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
unsigned int chuli(char *shuju,unsigned int *shuzhi)
{
 unsigned int num = 0;
 char zancun[20];
 unsigned char i;
 while(*(shuju+1) != '\r' && *(shuju+2) != '\n')
 {
  if(*shuju != 'A')
   shuju++;
  else
  {
   i = 0;
   shuju++;
   while(*shuju != 'A')
   {
    zancun[i] = *shuju;
    i++;
    shuju++;
   }
   zancun[i] = '\0';
   *shuzhi = atoi(zancun);
   shuzhi++;
   num++;
   printf("已存了 %d 个数\r\n",num);
   printf("暂存字符为:%s\r\n",zancun);
  }
 }
 return num;
}
/* 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 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要实现STM32字符整数的接收程序,可以使用库函数`strtol`。下面是一个实现的示例代码: ```c #include "stm32f4xx.h" #include <stdlib.h> #define BUFFER_SIZE 10 char buffer[BUFFER_SIZE]; int number; void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { static int byte_count = 0; char data = USART_ReceiveData(USART1); // 当接收到的字符是数字或者正负号时,将其保存到缓冲区中 if ((data >= '0' && data <= '9') || data == '+' || data == '-') { buffer[byte_count++] = data; // 如果接收到的字符个数达到缓冲区的大小,即表示接收到一个完整的字符串 if (byte_count >= BUFFER_SIZE) { // 将缓冲区的字符换为整数并保存到number变量中 number = strtol(buffer, NULL, 10); byte_count = 0; } } } } int main(void) { // 初始化USART1和GPIO等相关的操作... // 使能USART1接收中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 进入主循环 while (1) { // 其他的操作... } } ``` 在上述代码中,我们首先定义了一个缓冲区`buffer`和一个整数变量`number`来保存字符换后的整数。然后,在USART1接收中断处理程序`USART1_IRQHandler`中,我们每当接收到一个字符时,判断该字符是否为数字或者正负号。如果是,则将其存储到缓冲区中。当缓冲区中接收到的字符个数达到预设的大小时(例如10个字符),我们通过调用`strtol`函数将缓冲区的字符换为整数,并将结果保存到`number`变量中。 在`main`函数中,我们将使能USART1的接收中断使能位,并进入主循环执行其他操作。这样,在接收到符合条件的字符串后,中断处理程序会自动将其换为整数并保存到`number`变量中,其他代码可以读取并使用该整数。 ### 回答2: 要实现STM32字符整型的接收程序,可以按照以下步骤进行操作: 1. 首先,定义一个字符串变量来接收串口发送过来的字符串数据。可以使用数组来定义这个字符串变量,例如: ```c char receivedData[20]; // 假设接收的字符串长度不超过20 ``` 2. 接着,使用串口的接收函数(例如`HAL_UART_Receive()`)将接收到的字符存储到定义的字符串变量中,直到接收到结束符为止。例如: ```c HAL_UART_Receive(&huart1, (uint8_t *)receivedData, sizeof(receivedData)-1, HAL_MAX_DELAY); receivedData[sizeof(receivedData)-1] = '\0'; // 添加结束符 ``` 3. 然后,使用标准库函数`atoi()`将接收到的字符换成整型。例如: ```c int number = atoi(receivedData); ``` 4. 最后,可以将整型数据发送回给发送端或进行其他处理。例如: ```c sprintf(receivedData, "%d", number); // 将整型数据换为字符串 HAL_UART_Transmit(&huart1, (uint8_t *)receivedData, strlen(receivedData), HAL_MAX_DELAY); // 发送整型数据给发送端 ``` 在以上步骤中,需要根据实际情况调整字符串的长度和串口的配置。另外,需要确保发送端发送的字符串能够被正确解析为整型数据,在发送时可以使用`sprintf()`函数将整型数据换为字符串。 ### 回答3: 要将字符换为整型,我们可以使用C语言的标准库函数atoi()或者sscanf()。 首先,我们需要将接收到的字符串存储在一个字符数组中。我们可以使用UART串口接收数据,并将数据存储在一个缓冲区中。 接收程序的伪代码如下: ``` #include <stdio.h> #include <string.h> int main() { char buffer[20]; int number; printf("请输入一个整数字符串:"); fgets(buffer, sizeof(buffer), stdin); // 从用户输入获取字符串 buffer[strlen(buffer) - 1] = '\0'; // 去除末尾的换行符 number = atoi(buffer); // 使用atoi函数将字符换为整型 printf("换后的整数为:%d\n", number); return 0; } ``` 另一种方法是使用sscanf()函数,可以根据指定的格式从字符串中提取整型数据。 ``` #include <stdio.h> #include <string.h> int main() { char buffer[20]; int number; printf("请输入一个整数字符串:"); fgets(buffer, sizeof(buffer), stdin); buffer[strlen(buffer) - 1] = '\0'; sscanf(buffer, "%d", &number); // 使用sscanf函数将字符换为整型 printf("换后的整数为:%d\n", number); return 0; } ``` 以上是在C语言中进行字符串到整型换的两种常用方法。在使用这些方法时需要注意,字符串必须符合整型的格式要求,否则可能会得到错误的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

创客阿蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值