#include "debug.h" #include "ch32v20x.h" /********************************************************************* */ void GPIO_Toggle_INIT(void) { GPIO_InitTypeDef GPIO_InitStructure = {0}; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } //USART 2 void USART2_CFG(void) { /* USART2 TX-->A2 RX-->A3 * for CH32V203C8T6*/ GPIO_InitTypeDef GPIO_InitStructure = {0}; USART_InitTypeDef USART_InitStructure = {0}; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // TX Pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // RX Pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); } void Uart2BufferSend(uint8_t* buffer, uint16_t length) { uint16_t tmp = 0; for(tmp =0; tmp < length; tmp++) { while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) /* waiting for sending finish */ {} USART_SendData(USART2, buffer[tmp]); } } //USART 1 void USART1_CFG(void) { /* USART1 TX-->A9 RX-->A10 * for CH32V203C8T6*/ GPIO_InitTypeDef GPIO_InitStructure = {0}; USART_InitTypeDef USART_InitStructure = {0}; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // TX Pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // RX Pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } void Uart1BufferSend(uint8_t* buffer, uint16_t length) { uint16_t tmp = 0; for(tmp =0; tmp < length; tmp++) { while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) /* waiting for sending finish */ {} USART_SendData(USART1, buffer[tmp]); } } /********************************************************************* * @fn main * * @brief Main program. * * @return none */ int main(void) { u8 i = 0; // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); SystemCoreClockUpdate(); Delay_Init(); USART1_CFG(); Delay_Ms(1000); USART2_CFG(); Delay_Ms(1000); GPIO_Toggle_INIT(); while(1) { Delay_Ms(200); GPIO_WriteBit(GPIOB, GPIO_Pin_2, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET)); // Uart1BufferSend("111 Hallo", 9); Delay_Ms(20); Uart2BufferSend("222 Hallo", 9); } }