1 void Configuration(void)
2 {
3 USART_InitTypeDef USART_InitStructure;
4 GPIO_InitTypeDef GPIO_InitStructure;
5 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
6 //TX
7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 8 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
10 GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
11 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
12 GPIO_Init(GPIOB, &GPIO_InitStructure);
13 //RX
14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; 15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
16 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
17 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
18 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
19 GPIO_Init(GPIOB, &GPIO_InitStructure);
20
21 GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
22 GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
23
24
25 USART_OverSampling8Cmd(USART3, ENABLE);
26 USART_InitStructure.USART_BaudRate = 9600;
27 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
28 USART_InitStructure.USART_StopBits = USART_StopBits_1;
29 USART_InitStructure.USART_Parity = USART_Parity_No;
30 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
31 USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
32 USART_Init(USART3, &USART_InitStructure);
33 USART_Cmd(USART3, ENABLE);
34 }
35 void UART_PutChar(char Data)
36 {
37 USART_SendData(USART3,Data);
38 while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET){}
39 }
40 void UART_PutStr(char *str)
41 { 42 while (0 != *str)
43 {
44 USART3-》SR;45 UART_PutChar(*str);
46 str++;
47 }
48 }
49 int main(void)
50 {
51 Configuration();
52 UART_PutStr(“play,0001,$”);
53 }
1 void Configuration(void)
2 {
3 USART_InitTypeDef USART_InitStructure;
4 GPIO_InitTypeDef GPIO_InitStructure;
5 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
6 //TX
7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 8 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
10 GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
11 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
12 GPIO_Init(GPIOB, &GPIO_InitStructure);
13 //RX
14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; 15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
16 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
17 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
18 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
19 GPIO_Init(GPIOB, &GPIO_InitStructure);
20
21 GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
22 GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
23
24
25 USART_OverSampling8Cmd(USART3, ENABLE);
26 USART_InitStructure.USART_BaudRate = 9600;
27 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
28 USART_InitStructure.USART_StopBits = USART_StopBits_1;
29 USART_InitStructure.USART_Parity = USART_Parity_No;
30 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
31 USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
32 USART_Init(USART3, &USART_InitStructure);
33 USART_Cmd(USART3, ENABLE);
34 }
35 void UART_PutChar(char Data)
36 {
37 USART_SendData(USART3,Data);
38 while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET){}
39 }
40 void UART_PutStr(char *str)
41 { 42 while (0 != *str)
43 {
44 USART3-》SR;45 UART_PutChar(*str);
46 str++;
47 }
48 }
49 int main(void)
50 {
51 Configuration();
52 UART_PutStr(“play,0001,$”);
53 }
举报