description
use stm32f103c8t6 3 uarts
send char and can recive each uart port
send char and can recive each uart port
main.c
#include "stm32f10x.h" #include "UART.h" int main(void) { USART1_Init(460800); USART2_Init(460800); USART3_Init(460800); USART1_PutString("HI~"); USART2_PutString("HI~"); USART3_PutString("HI~"); while(1) { char recive1 = USART1_GetChar(); USART1_PutChar(recive1); char recive2 = USART2_GetChar(); USART2_PutChar(recive2); char recive3 = USART3_GetChar(); USART3_PutChar(recive3); } }
UART.h
#ifndef __UART_H #define __UART_H #ifdef __cplusplus extern "C" { #endif #include "stm32f10x.h" #include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include#define BUF_SIZE1 16 #define BUF_SIZE2 16 #define BUF_SIZE3 16 //uart1 void USART1_Init(uint32_t baudrate); void USART1_PutChar(char c); void USART1_PutString(char *s); uint16_t USART1_GetChar(void); //uart2 void USART2_Init(uint32_t baudrate); void USART2_PutChar(char c); void USART2_PutString(char *s); uint16_t USART2_GetChar(void); //uart3 void USART3_Init(uint32_t baudrate); void USART3_PutChar(char c); void USART3_PutString(char *s); uint16_t USART3_GetChar(void); #ifdef __cplusplus } #endif #endif
UART.c
https://github.com/windyman24/STM32F103_learn
#include "UART.h" char buf1[BUF_SIZE1]; char buf2[BUF_SIZE2]; char buf3[BUF_SIZE3]; uint8_t receivedByte1; uint8_t receivedByte2; uint8_t receivedByte3; //UART1 void USART1_Init(uint32_t baudrate) { //Initialization struct USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; //USART1 initialization RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_Init(USART1, &USART_InitStruct); USART_Cmd(USART1, ENABLE); //GPIO initialization for Tx and Rx pin RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Tx pin initialization as push-pull alternate function GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); // Rx pin initialization as input floating GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); } void USART1_PutChar(char c) { // Wait until transmit data register is empty while (!USART_GetFlagStatus(USART1, USART_FLAG_TXE)); // Send a char using USART1 USART_SendData(USART1, c); } void USART1_PutString(char *s) { // Send a string while (*s) { USART1_PutChar(*s++); } } uint16_t USART1_GetChar() { // Wait until data is received while (!USART_GetFlagStatus(USART1, USART_FLAG_RXNE)); // Read received char return USART_ReceiveData(USART1); } //UART2 void USART2_Init(uint32_t baudrate) { // Initialization struct USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; //USART2 initialization RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_Init(USART2, &USART_InitStruct); USART_Cmd(USART2, ENABLE); //GPIO initialization for Tx and Rx pin RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Tx pin initialization as push-pull alternate function GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); // Rx pin initialization as input floating GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); } void USART2_PutChar(char c) { // Wait until transmit data register is empty while (!USART_GetFlagStatus(USART2, USART_FLAG_TXE)); // Send a char using USART2 USART_SendData(USART2, c); } void USART2_PutString(char *s) { // Send a string while (*s) { USART2_PutChar(*s++); } } uint16_t USART2_GetChar() { // Wait until data is received while (!USART_GetFlagStatus(USART2, USART_FLAG_RXNE)); // Read received char return USART_ReceiveData(USART2); } void USART3_Init(uint32_t baudrate) { // Initialization struct USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; //USART3 initialization RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_Init(USART3, &USART_InitStruct); USART_Cmd(USART3, ENABLE); //GPIO initialization for Tx and Rx pin RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Tx pin initialization as push-pull alternate function GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &GPIO_InitStruct); // Rx pin initialization as input floating GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &GPIO_InitStruct); } void USART3_PutChar(char c) { // Wait until transmit data register is empty while (!USART_GetFlagStatus(USART3, USART_FLAG_TXE)); // Send a char using USART2 USART_SendData(USART3, c); } void USART3_PutString(char *s) { // Send a string while (*s) { USART3_PutChar(*s++); } } uint16_t USART3_GetChar() { // Wait until data is received while (!USART_GetFlagStatus(USART3, USART_FLAG_RXNE)); // Read received char return USART_ReceiveData(USART3); }
沒有留言:
發佈留言