当前位置:网站首页>Sending and interrupt receiving of STM32 serial port
Sending and interrupt receiving of STM32 serial port
2022-07-03 09:55:00 【Fake iron man】
stm32 Serial port is an important means to realize the communication between MCU and host computer , Usually use usb turn ttl The module connects the single chip microcomputer with the upper computer , In order to realize the functions of serial port sending and receiving . Today I learned the serial port sending and interrupt receiving , It is summarized as follows :
Step summary :
Configure two GPIO Respectively RX、TX-> Configure the serial port structure ->( Please leave the flag blank )-> Start timer interrupt -> Enable timer interrupt -> To configure NVIC Structure -> Write interrupt service function -> Write relevant implementation programs
UART.h:
#include "UART.h"
#include "stm32f10x.h"
#include "stdio.h"
void UART_Config(void)
{
// Turn on the clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
// Define structure variables
GPIO_InitTypeDef GPIOInitStructure;
USART_InitTypeDef UARTInitStructure;
NVIC_InitTypeDef NVICInitStructure;
// To configure PA9 TX
GPIOInitStructure.GPIO_Pin = GPIO_Pin_9;
GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIOInitStructure);
// To configure PA10 RX
GPIOInitStructure.GPIO_Pin = GPIO_Pin_10;
GPIOInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIOInitStructure);
// Configure the serial port structure
UARTInitStructure.USART_BaudRate = 115200;
UARTInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
UARTInitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
UARTInitStructure.USART_Parity = USART_Parity_No;
UARTInitStructure.USART_StopBits = USART_StopBits_1;
UARTInitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &UARTInitStructure);
// Clear flag bit
USART_ClearFlag(USART1,USART_FLAG_RXNE);
// Start timer interrupt
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
// Enable timer
USART_Cmd(USART1,ENABLE);
// To configure NVIC Structure
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVICInitStructure.NVIC_IRQChannel = USART1_IRQn;
NVICInitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVICInitStructure.NVIC_IRQChannelSubPriority = 3;
NVICInitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVICInitStructure);
}
// Send byte
void USART_SendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData(USARTx,Data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
// Send string
void USART_SendStr(USART_TypeDef* USARTx, char *str)
{
uint16_t i = 0;
do{
USART_SendByte(USARTx, *(str + i));
i++;
}while(*(str + i) !='\0');
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
}
// Redirect printf
int fputc(int ch, FILE *f)
{
USART_SendData(USART1,(uint8_t) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
// Redirect putchar
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
stm32f10x_it.c:
void USART1_IRQHandler(void)
{
char temp;
if(USART_GetITStatus(USART1, USART_IT_RXNE ) != RESET)
{
temp = USART_ReceiveData(USART1);
if(temp == 'O')
{
GPIO_ResetBits( LED_G_GPIO_PORT, LED_G_GPIO_PIN);
USART_SendStr(USART1,"led is ON");
}
if(temp == 'C')
{
GPIO_SetBits( LED_G_GPIO_PORT, LED_G_GPIO_PIN);
USART_SendStr(USART1,"led is OFF");
}
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);// Be sure to clear the flag bit !
}main.c:
#include "stm32f10x.h"
#include "UART.h"
#include "stdio.h"
#include "led.h"
int main(void)
{
UART_Config();
LED_GPIO_Confing();
GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
while(1)
{
}
}
The function of receiving experiment is : Send through serial assistant O, Then light up LED, send out C,LED Extinguish .
边栏推荐
- Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
- How does the nr-prach receiver detect the relationship between prembleid and Ta
- Development of electrical fire system
- STM32 serial communication principle
- [graduation successful] [1] - tour [Student Management Information System]
- [combinatorics] Introduction to Combinatorics (context of combinatorics | skills of combinatorics | thought of combinatorics 1: one-to-one correspondence)
- 2021-09-26
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- STM32 external interrupt experiment
- Windows下MySQL的安装和删除
猜你喜欢

端午节快乐!—— canvas写的粽子~~~~~

Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode

IDEA远程断点调试jar包项目

MySQL Data Definition Language DDL common commands

Project cost management__ Topic of comprehensive calculation

Electronic product design

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

Exception handling of arm
![[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation

Oracle database SQL statement execution plan, statement tracking and optimization instance
随机推荐
2020-08-23
Introduction to chromium embedded framework (CEF)
Successful graduation [2] - student health management system function development...
STM32 external interrupt experiment
A lottery like scissors, stone and cloth (C language)
UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
03 FastJson 解决循环引用
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
How does the memory database give full play to the advantages of memory?
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
Matlab reads hexadecimal numbers and converts them into signed short
STM32 serial port usart1 routine
[male nanny style] teach you to open the first wechat applet
Install local sources using yum
Characteristics of PUCCH formats
Notes on C language learning of migrant workers majoring in electronic information engineering
Idea remote breakpoint debugging jar package project
[combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)
CEF download, compile project
Vector processor 9_ Basic multilevel interconnection network