当前位置:网站首页>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 .
边栏推荐
- When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
- Development of fire evacuation system
- 01仿B站项目业务架构
- 2020-08-23
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- Notes on C language learning of migrant workers majoring in electronic information engineering
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- PIP references domestic sources
- Nr-prach:prach format and time-frequency domain
- Design and development of biological instruments
猜你喜欢
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
要选择那种语言为单片机编写程序呢
[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation
单片机学到什么程度能找到工作,这个标准不好量化
STM32 interrupt switch
2.Elment Ui 日期选择器 格式化问题
端午节快乐!—— canvas写的粽子~~~~~
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
PRACH --- originator
随机推荐
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
Windows下MySQL的安装和删除
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Education is a pass and ticket. With it, you can step into a higher-level environment
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
STM32 interrupt switch
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
STM32 general timer 1s delay to realize LED flashing
MySQL data manipulation language DML common commands
MYSQL数据库底层基础专栏
[graduation successful] [1] - tour [Student Management Information System]
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
(1) 什么是Lambda表达式
Process communication - semaphore
03 FastJson 解决循环引用
IDEA远程断点调试jar包项目
Chromium Embedded Framework (CEF) 介绍