The chip used here is STM32F103, which uses interrupts to receive and send serial port data.
Here we use the method of directly operating the registers. Using the library function is similar to this, except that we just call the corresponding library function instead.
Configure serial ports and multiplexed IO ports
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);//frequency division
mantissa=temp; //integer part obtained by frequency division
fraction=(temp-mantissa)*16; //fractional part
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2; //Enable GPIOA clock
RCC->APB2ENR|=1<<14; //Enable serial port 1 clock
RCC->APB2ENR|=1<<0; //(+)Alternate function I/O clock enable It seems that it is not necessary. Is it enabled by default?
GPIOA->CRH&=0XFFFFF00F;//Clear IO corresponding
bitGPIOA->CRH|=0X000008B0;//IO status settingRCC-
>APB2RSTR|=1<<14; //Reset serial port 1
RCC->APB2RSTR&=~(1<<14);//Stop resetUSART1-
>BRR=mantissa;//Set baud rateUSART1-
>CR1|=0X200C; //1 stop bit, no parity bit
//Enable send interruptUSART1-
>CR1|=1<<6;
USART1->SR&=(~(1<<6));//Clear interrupt flag
//Enable receiving terminal
USART1->CR1|=1<<8; //PE interrupt enable?
USART1->CR1|=1<<5; //Receive buffer not empty interrupt enable
MY_NVIC_Init(3,3,USART1_IRQn,2); //Second group of interrupts, lowest priority
}
void MY_NVIC_Init(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group)
{
u32 temp;
MY_NVIC_PriorityGroupConfig(NVIC_Group);//Set group
temp=NVIC_PreemptionPriority<<(4-NVIC_Group);
temp|=NVIC_SubPriority&(0x0f>>NVIC_Group);
temp&=0xf;//Take the lower four bits
if(NVIC_Channel<32)NVIC->ISER[0]|=1<
NVIC->IP[NVIC_Channel]|=temp<<4;//Set response priority and preemption priority
}
//Send interrupt settings
void USART_SendXByte(USART_TypeDef* USARTx, u16 SendByteNum)
{
HAVE_SEND_LEN=0;//Clear the sent data length mark (global variable)
ALL_SEND_LEN=SendByteNum;//ALL_SEND_LEN is the length of data to be sent this time
IsSending=1;
USART1->DR=SEND_BUF[HAVE_SEND_LEN];
HAVE_SEND_LEN++;
}
Interrupt handling function
u16 USART_RX_STA=0; //Used to mark that one bit of data has been received
//u16 USART_SEND_COMPLETE=1; //Send completion mark
u8 REC; //Used to receive one byte of data received by the serial port, global variable, read this variable in other programs to get the data received by the serial port
u16 HAVE_SEND_LEN=0;
u16 ALL_SEND_LEN=0;
char SEND_BUF[USART_SEND_LEN]; //Unused
char REC_BUF[USART_REC_LEN]; //Unused
void USART1_IRQHandler(void)
{
if(USART1->SR&(1<<5))//Received data
{
USART_RX_STA=1; //Set the flag bit, read this flag bit as true, then read REC to get the received data
CountReciByte++;
REC=USART1->DR;
}
if(USART1->SR&(1<<6))//(+)·Sending a byte completes interrupt
{
if(HAVE_SEND_LEN
USART1->DR=SEND_BUF[HAVE_SEND_LEN];//SEND_BUF[] is a global variable, storing the data to be
sentHAVE_SEND_LEN++;
}
if(HAVE_SEND_LEN==ALL_SEND_LEN)
{
USART_SEND_COMPLETE=1;
while(!(USART1->SR&(1<<6)))
{
//Wait for the last byte to be sent
}
USART1->SR&=(~(1<<6)); //Clear the send interrupt flag
IsSending=0;
}
}
}
main function
int main(void)
{
Stm32_Clock_Init(9);
uart_init(72,115200);
delay_init(72);
while(1)//echo the first 10 bytes received "each time"
{
if((1==USART_RX_STA)&&(CountReciByte<=10))
{
SEND_BUF[SerialBufOffset] = REC;
USART_RX_STA=0;
SerialBufOffset++;
if(10==SerialBufOffset)
{
ReadZiku(100,100,SEND_BUF,Black, White);
//strcpy(SEND_BUF, "ABCDEFGHIJKLDR");
USART_SendXByte(USART1, SEND_BUF, 10);
//memset(SEND_BUF,0,sizeof(SEND_BUF));
SerialBufOffset=0;
CountReciByte=0;
//delay_ms(1000);
}
USART_RX_STA=0;//In order to prevent the next time from receiving the byte that was not taken out last time
}
}
Previous article:STM32 serial port interrupt sending
Next article:STM32 serial port transmission is abnormal
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- About DSP data calibration
- How to consider and design ESD of RF modules?
- 【NUCLEO-L552ZE Review】+ Bluetooth communication experiment (2)
- 【XMC4800 Relax EtherCAT Kit Review】+ Webserver based on XMC4800
- Help with an electrometer data acquisition circuit
- Adafruit Circuit Playground Express Schematic
- Mid-Autumn Festival is coming, EEWorld calls on you to exchange chip coins for mooncakes!
- Motor drive development communication 4: Classification and standards of engineers
- Basic knowledge of digital circuits
- 【GE32E231_DIY】FreeRTOS+DAP_RTT+Multi-function button (single click, double click, multi-click, press, pop up, HOLD)