STM32 learning to write using serial communication library functions

Note: chip using STM32F103ZET6

A serial port initialization part of the program

1. initialize the clock

  To use the serial port 1 because the peripheral, so before you need to start the clock using the serial port 1, and then also need to set the input and output IO mode, where we also need to open GPIO clock. Procedures are as follows:

//初始化串口时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
 //初始化GPIO时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE)

  But why do you want to initialize the clock There is a saying "when peripheral clock is not enabled, the software can not read the value of peripheral registers, the return value is always 0x0." In "STM32 Chinese reference manual _V10" of the RCC, that is the only open the corresponding peripheral clock in order to operate peripherals.
  This section explains the following reference document:
https://blog.csdn.net/dp29sym41zygndvf/article/details/82321133

2. Initialize the GPIO

  Initialize GPIO pin is set to use the mode and speed of the port. 1 because of the use of a serial, so USART_TX on STM32F103ZET6 chip, USART_RX corresponding pin and is PIOA.9 GPIOA.10. Pin different multiplexing function, the corresponding pin mode setting is different, detailed refer to "STM32 Chinese reference manual _V10" of 8.1.11, the following table is to use a reference manual of 21 USART table.

Here is the quote "STM32 Chinese Reference Manual" in the table 21Interception "STM32 Chinese Reference Manual" in the map

  Generally set serial port configured for full duplex mode, thus setting a push-pull mode GPIOA.9 multiplexed output is provided GPIOA.10 floating input mode. Pin speed is not much of a limitation. The end procedure is as follows:

GPIO_InitTypeDef GPIO_InitStruct;

//初始化GPIO
 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;  //GPIOA.9是USART1的TX
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //设置其模式为推挽复用输出
 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; //速度为10MHz
 GPIO_Init(GPIOA,&GPIO_InitStruct);
 
 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;  //GPIOA.10是USART1的RX
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置其模式为浮空输入
 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;  //速度为10MHz
 GPIO_Init(GPIOA,&GPIO_InitStruct);

3. initialize the serial port

  Initializing the serial port is provided stop bits, parity, word size, baud rate, serial mode, and hardware flow control.
   Bit Stop : stop bit can be set to 0.5, 1, 1.5 and 2 stop bits, typically provided as a stop bit.
  Word length : 8 data bits can be set or nine data bits.
  Parity bit : When set 8 data bits, no parity bit; provided when nine data bits, the 9th bit is a parity bit, in order to ensure the accuracy of data transmission. If odd parity is set, if the first 8 data bits is an odd number 1, this bit is 0 if an even number, the bit is 1. If even parity, if the number is an even number of 1 bit is 0, the bit 1 is odd.
  Baud rate : is the rate of data transmission, the receiver baud rate the baud rate to the same sender.
  Serial mode : Set Transmit Enable and Receive Enable
  hardware flow control : This is used only in half duplex, generally does not turn on this feature.
  This section of the program are as follows:

USART_InitTypeDef USART_InitStruct;

//初始化串口
 USART_InitStruct.USART_BaudRate = 115200; //设置波特率 
 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;  //1位停止位
 USART_InitStruct.USART_WordLength = USART_WordLength_8b; //8位数据位
 USART_Init(USART1,&USART_InitStruct);

4. Serial Port Enable

  Are just some configuration settings before serial port, serial port to work and did not make, so we enable the serial port, you need to call USART_Cmd (USART_TypeDef * USARTx, FunctionalState NewState ) function, which ultimately is a para 1 (USART_CR1) control register 13 operation, if the bit is. 1, USART module is enabled, if it is 0, and the output of the frequency divider USART prohibited.
  This section of the program are as follows:

//使能串口
 USART_Cmd(USART1,ENABLE);

  Commonly accepted data need to use the serial port interrupt, and serial port can not use the serial port interrupt. If you do not open the serial port interrupt, this step has been completed initialization part serial. If you need to use the serial port is interrupted, and then requires the following two steps.

5. Initialization NVIC

  The first is to find out what NVIC Shi, NVIC, is Nested vectored interrupt controller, namely Nested Vectored Interrupt Controller.
  In the chip has 16 cores STM32F103ZET6 interrupts, maskable interrupts 60, 16 programmable interrupt priority, how many interrupt how to manage it, this time NVIC Nested Vectored Interrupt Manager comes in handy. First, the first interrupt group, as shown in the following table:

group AIRCR[10:8] IP bit [7: 4] distribution Allocation results
0 111 0:4 0 preemption priority, in response to priority 4
1 110 1:3 A preemption priority, in response to priority 3
2 101 2:2 2 preemption priority, in response to priority 2
3 100 3:1 3 preemption priority, a priority response
4 011 4:0 4 preemption priority, in response to the priority 0

NOTE: This table is the reference atom punctual Lecture 24 PPT NVIC interrupt priority management
  here interrupt packet is not to say each interrupt assigned to these groups, but each group corresponding to said preemption priority and in response different priorities digits, according to their needs and how many bits preemption priority in response to several priority setting, a total of four bits to control.
  Preemption priority explanation : to seize the high priority interrupt can interrupt preempt lower priority interrupts, which interrupt bit like 51 priority 0 when seize the highest priority.
  In response to explain priority : low priority response to the high priority can not interrupt response only in the same preemption priority function, when the same preemption priority, a high priority response, and when the low priority response when the same interrupt occurs in response to a high-priority interrupt is triggered.
  Mentioned above, a total of 4 bits to set both priority and therefore 2 4 to 16, there are 16 programmable interrupt priority.
  For example :
  I want to set the serial port interrupt 1, 2 preemption priority, also in response to the priority of 2.
  1. First set the interrupt packet.
  Since preemption priority 2, the group 1 can not be used as a preemptive priority group may be set to 0, not set, so it should be selected from group 1 to 4, because the response priority is 2, then the group can not be used 4 in response to the group of 4 bits are not assigned a priority, and therefore may be selected from the group 2-3, group 2 I choose.
  2. Set priorities.
  Because Group 2, preemption priority assigned 2 bits, in response to the priority assigned 2 bits may be provided so preemption priority 2 24 is a priority level, in response to the priority level is 4, i.e., 0-3. To preemptive priority 2, priority is also responsive 2, the second write register bits 6 and 7 of NVIC_IP, preemption priority; 2 writes the bit 4 and bit 5 NVIC_IP register, priority setting response priority.
  When we use library function programming, void NVIC_PriorityGroupConfig (uint32_t NVIC_PriorityGroup); is interrupted grouping, NVIC_Init (& NVIC_InitStruct); is NVIC initialization, that is preemption priority, response priority, IRQ serial channel opening and IRQ channel enable these four parameters.
  Preemption priority and priority response are a few settings are on the line.
  Why open IRQ corresponding serial channel 1, because IRQ is interrupt
Request, namely an interrupt request channel, what is the option to trigger an interrupt, because using the serial port 1, and therefore need to open the IRQ of Serial Port 1 channel, choose to use the serial port 1 trigger . Enables the serial port IRQ channel 1 would not have said.
  This section of the program are as follows:

NVIC_InitTypeDef NVIC_InitStruct;

//初始化NVIC(设置抢占优先级和响应优先级)
 NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;  //选择IRQ为USART1
 NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能IRQ通道
 NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; //设置抢占优先级
 NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //设置响应优先级
 NVIC_Init(&NVIC_InitStruct);  

  Interrupt packets relating to library functions in the beginning of the main function, not in this part of the program.

6. Turn on the Serial Port 1 Interrupt

  In the previous steps to configure the serial port is configured about the good, and this step is to set the serial port 1 through what triggered the interrupt, for example, is sent after the completion of an interrupt or receive data interrupt. In the library function void USART_ITConfig (USART_TypeDef * USARTx, uint16_t USART_IT, FunctionalState NewState); is on the serial port 1 interrupt, this function is to set the second parameter is the serial port interrupt is triggered by way of which, under normal circumstances we use is USART_IT_RXNE, when the received data is set to trigger an interrupt.
  This section of the program are as follows:

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //设置接受到数据触发中断

  Set the serial port IRQ channel 1 before, just set the interrupt is triggered via the serial port 1, which is the interrupt source, but the specific trigger an interrupt is controlled by the serial port which way 1, you need to set up this step.

  This, serial port initialization has been fully configured.

Second, the interrupt service routine part

  Since this procedure is to write a simple serial transceivers, so the interrupt service routine part of what it is prepared to receive data on what data is sent, the procedure is as follows:

void USART1_IRQHandler(){  //串口1中断服务函数
 u8 Recv;
 if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET){
  Recv = USART_ReceiveData(USART1);
  USART_SendData(USART1,Recv);
 }
}

  After entering the serial port 1 interrupt service function, first by calling USART_GetITStatus (USART_TypeDef * USARTx, uint16_t USART_IT) This function is not to judge because the received data and triggers an interrupt, and perhaps the strange, before obviously set that receives data triggers an interrupt, the interrupt service function into Why still need to determine, because although select trigger mode interrupt, but may be other cases the trigger, the following quote "STM32 Chinese reference Manual _V10" 25.4 interrupt request words to explain:

  USART various interrupt events are connected to the same interrupt vector, following various interrupt events:
● During Send: send the complete, clear to send, transmit data register empty.
During reception ●: idle bus detection, overrun error, the reception data register is not empty, an error check, the LIN break symbol detection, the noise flag (multiple communication only buffer) and the frame error (multiple communication only buffer).
  If you set the corresponding enable control bit, these events can generate its own interrupt. Universal Synchronous Asynchronous Receiver Transmitter (The USART)

  So you need to determine whether the location of a corresponding flag is triggered interrupts, then that is a function call library functions serial port to receive data, read out the data received, in order to see the phenomenon, it is also a library function call data from serial port function, the data is sent out, the computer's serial port assistant you can see the consistent receive serial data and transmit data box.

Third, the main function part

  Because the function is relatively simple to achieve, so in the main function main calls the initialization function. The main functions are the following:

int main(){
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组
   Uart_Init(); //初始化串口

   while(1);
} 

  The beginning is calling abort packet, this section explains explains in Initialization NVIC section, then that is invoked to initialize the serial port function that is not a library function I have written in, this function is to include the first part of the serial port initialization section above statement to all of the listed programs. Then is to use the while loop, the program has been run down.

  This article is to explain after I learned punctuality summary atomic STM32 video and self-understanding, if there is something wrong, please enlighten.

References
"STM32 Chinese Reference Manual _V10"
"the STM32F10xxx Cortex-M3 Programming Manual"
"STM32F1 Developer's Guide (Elite Edition) - library function version _V1.0" (punctuality atom)
the STM32 official data sheet

Published an original article · won praise 0 · Views 136

Guess you like

Origin blog.csdn.net/sunshine123S/article/details/104226302