[STM32F103] serial port UART configuration

l Serial port clock enable, GPIO clock enable:

  RCC_APB2PeriphClockCmd();

l Serial reset:< /p>

  USART_DeInit(); This step is not necessary

l GPIO port mode setting:

  GPIO_Init();

l Serial port parameter initialization:

  USART_Init();

l Turn on the interrupt and initialize the NVIC (this step is only required if you need to turn on the interrupt)

NVIC_Init();

USART_ITConfig();

l Enable serial port:

  USART_Cmd();

l Write interrupt handling function:

  USARTx_IRQHandler();

l Serial data transmission and reception:

  void USART_SendData();//Send data to the serial port, DR

  uint16_t USART_ReceiveData();//Receive data, read the received data from DR

l Get the serial port transmission status:

  FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);

  void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART);

p>

l Source code:

 1 < span style="color: #0000ff">#pragma import(__use_no_semihosting) 
2 struct __FILE
3 span> {
4 int handle;
5 };
6
7 FILE __stdout;
8 void span> _sys_exit(int x)
9 {
10 x = x;
11 }
span>12 int fputc(int ch, FILE *f)
13 {
span>14 while((USART1->SR&0X40)==0);
15 USART1->DR = (u8) ch;
16 return ch;
17 }
18
19 void uart_init(u32 bound){< br />20 //GPIO???úéè??
21 GPIO_InitTypeDef GPIO_InitStructure;
22 USART_InitTypeDef USART_InitStructure;
23 NVIC_InitTypeDef NVIC_InitStructure;
24
25 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ê1?üUSART1£?GPIOAê±?ó
26
27 //USART1_TX GPIOA.9
28 GPIO_InitStructure.GPIO_Pin = G PIO_Pin_9; //PA.9
29 GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
30 GPIO_InitStructure. GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
31 GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉGPIOA.9
32
33< /span> //USART1_RX GPIOA.103?ê??ˉ
34 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10< /span>
35 GPIO_InitSt ructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
36 GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉGPIOA.10
37
38< /span> //Usart1 NVIC ????
39 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
40 span> NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//?à??ó??è??3
41 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óó??è??3
42 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1?ü
43 NVIC_Init(&NVIC_InitStructure); // ?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷
44
45 //USART 3?ê ??ˉÉè??
46 USART_InitStructure.USART_BaudRate = bound;//′??ú2¨ì??ê
47 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê? span>
48 USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??
49 USART_InitStructure.USART_Parity = USART_Parity_No;//?T????D£?é??
50 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//?Tó2?têy?Yá ÷????
51 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // ê?·¢?£ê?
52
53 USART_Init(USART1, &USART_InitStructure); //3?ê?? ˉ′??Ú1
54 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);/ /?a??′??ú?óêü?D??
55 USART_Cmd(USART1, ENABLE); //ê1?ü′??ú1
56
57 }
58
59 void USART1_IRQHandler(void) //′??ú1?D??·t??3ìDò
60 {
61 u8 Res;
62 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) / /?óê??D??(?óê?μ?μ?êy?Y±?D?ê?0x0d 0x0a?á?2)
63 {
64 Res =USART_ReceiveData(USART1); //?áè??óê?μ? μ?êy?Y
65
66 < span style="color: #0000ff">if
((USART_RX_STA&0x8000)==0< /span>)//?óê??′íê3é
67 {
68 if(USART_RX_STA&0x4000)//?óê?μ?á?0x0d
69 {< br />70 if(Res!=0x0a)USART_RX_STA=0;//?óê?′í?ó,??D??aê?
71 else USART_RX_STA|=0x8000; //< span style="color: #008000">?óê?íê3éá?

72 }
73 else //?1??ê?μ?0X0D
74 {
75 if(Res==0x0d)USART_RX_STA|=0x4000;
76 else< /span>
77 {
78 USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
79 USART_RX_STA++;
80 if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0 ;//?óê?êy?Y′í?ó,??D??aê??óê?
81 }
82 }
83 }
84}

 1 #pragma import(__use_no_semihosting) 
2 struct __FILE
3 {
4 int handle;
5 };
6
7 FILE __stdout;
8 void _sys_exit(int x)
9 {
10 x = x;
11 }
12 int fputc( int ch, FILE *f)
13 {
14 while((USART1->SR& 0X40)==0);
15 USART1->DR = (u8) ch;
16 return ch;
17 }
18
19 void uart_init(u32 bound){
20 // GPIO???úéè??
21 GPIO_InitTypeDef GPIO_InitStructure ;
22 USART_InitTypeDef USART_InitStructure;
23 NVIC_InitTypeDef NVIC_InitStructure;
24
25 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ê1?üUSART1£?GPIOAê±?ó
26
27 //USART1_TX GPIOA.9
28 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
29 GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MH z;
30 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //< span style="color: #008000">?′ó?í?íìê?3?

31 GPIO_Init(GPIOA, &GPIO_InitStructure) ;//3?ê??ˉGPIOA.9
32
33 //USART1_RX GPIOA.103?ê??ˉ
34 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
35 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
36 GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉGPIOA.10
37
38 //Usart1 NVIC ????
39 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
40 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;< span style="color: #008000">//?à??ó??è??3
41 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // ×óó??è??3
42 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; < span style="color: #008000">//< span style="color: #008000">IRQí¨μàê1?ü
43 NVIC_Init(&NVIC_InitStructure); //?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷
44
45 //USART 3?ê??ˉéè??
46 USART_InitStructure.USART_BaudRate = bound;//′??ú2¨ì??ê
47 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê?
48 USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??
49 USART_InitStructure.USART_Parity = USART_Parity_No;//?T??? ?D£?é??
50 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// ?Tó2?têy?Yá÷????
51 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ê?·¢?£ê?
52
53 USART_Init(USART1, &USART_InitStructure); //3?ê??ˉ′??ú1
54 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//?a??′? ?ú?óêü?D??
55 USART_Cmd(USART1, ENABLE); //ê1 ?ü′??ú1
56
57 }
58
59 void USART1_IRQHandler(void) //′??ú1?D??·t??3ìDò
60 {
61 u8 Res;
62 if(USART_GetITStatus(USART1 , USART_IT_RXNE) != RESET) //?óê??D??(?óê?μ?μ? êy?Y±?D?ê ?0x0d 0x0a?á?2)
63 {
64 Res =USART_ReceiveData(USART1); //?áè??óê?μ?μ?êy?Y
65
66 if((USART_RX_STA&0x8000)==0)//?óê??′íê3é< /span>
67 {
68 if(USART_RX_STA&0x4000)//?óê?μ?á?0x0d
69 {
70 if (Res!=0x0a)USART_RX_STA=0;//?óê?′í?ó,??D??aê?
71 else USART_RX_STA|=0x8000; //?óê?íê3éá?
72 }
73 else //?1??ê?μ?0X0D
74 {
75 if(Res==0x0d)USART_RX_STA|=0x4000;
76 else
77 {
78 USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
79 USART_RX_STA++;
< span style="color: #008080">80
if(USART_RX_STA>(USART_REC_LEN-1< /span>))USART_RX_STA=0;//?óê?êy?Y′í? ó,??D??aê??óê?
81 }
82 }
83 }
84}

< /p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1251 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.