#include "main.h"
#include "usart.h"
#include "gpio.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned int chuli(char *shuju,unsigned int *shuzhi);
int fputc(int ch, FILE *f){
HAL_UART_Transmit (&huart1,(uint8_t *)&ch,1,0xffff);
return ch;
}
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
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]);
}
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
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();
}
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();
}
}
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;
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
|