stm32f103c8t6与k210的串口通信代码
时间: 2024-04-11 21:24:25 浏览: 242
stm32f103c8t6和k210是两种不同的芯片,分别属于不同的系列和品牌。它们的串口通信代码会有所不同。下面我将分别介绍它们的串口通信代码:
对于stm32f103c8t6,它属于STMicroelectronics的STM32系列单片机,可以使用STM32Cube库来进行串口通信。以下是一个简单的示例代码:
```c
#include "stm32f1xx_hal.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
while (1)
{
char data[] = "Hello, World!\r\n";
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data), HAL_MAX_DELAY);
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
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();
}
}
static void MX_USART1_UART_Init(void)
{
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;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
while (1)
{
}
}
```
对于k210,它属于Kendryte K210系列AI芯片,可以使用Kendryte Standalone SDK来进行串口通信。以下是一个简单的示例代码:
```c
#include "fpioa.h"
#include "uart.h"
#include "sysctl.h"
int main(void)
{
fpioa_set_function(4, FUNC_UART1_RX + UART_DEVICE_1 * 2);
fpioa_set_function(5, FUNC_UART1_TX + UART_DEVICE_1 * 2);
uart_init(UART_DEVICE_1);
uart_configure(UART_DEVICE_1, 115200, 8, UART_STOP_1, UART_PARITY_NONE);
while (1)
{
char data[] = "Hello, World!\r\n";
uart_send_data(UART_DEVICE_1, (uint8_t*)data, strlen(data));
sleep(1);
}
}
```
以上是两种芯片的简单串口通信代码示例。如果你有更具体的需求或问题,请提供更详细的信息,我将尽力帮助你。
阅读全文