当前位置:网站首页>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 .
边栏推荐
- Nr--- Pusch I: sorting out the agreement process
- STM32 interrupt priority management
- Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
- UCI and data multiplexing are transmitted on Pusch - determine the bit number of harqack, csi1 and csi2 (Part II)
- [CSDN]C1訓練題解析_第三部分_JS基礎
- STM32 external interrupt experiment
- 嵌入式系统没有特别明确的定义
- [untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
- QT qcombobox QSS style settings
- 2020-08-23
猜你喜欢

Project cost management__ Cost management technology__ Article 6 prediction

嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
![[CSDN]C1訓練題解析_第三部分_JS基礎](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1訓練題解析_第三部分_JS基礎

Stm32-hal library learning, using cubemx to generate program framework

Error output redirection

SSB Introduction (PbCH and DMRs need to be supplemented)

Development of fire power monitoring system

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

Nr--- Pusch I: sorting out the agreement process

Difference of EOF
随机推荐
Introduction to chromium embedded framework (CEF)
Characteristics of PUCCH formats
Project cost management__ Cost management technology__ Article 8 performance review
The third paper of information system project manager in soft examination
一个可执行的二进制文件包含的不仅仅是机器指令
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
Intelligent home design and development
MySQL 数据库基础知识(系统化一篇入门)
In third tier cities and counties, it is difficult to get 10K after graduation
An executable binary file contains more than machine instructions
Stm32-hal library learning, using cubemx to generate program framework
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
C language enumeration type
Nr-prach: access scenario and access process
我想各位朋友都应该知道学习的基本规律就是:从易到难
CEF download, compile project
Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
Chromium Embedded Framework (CEF) 介绍
没有多少人能够最终把自己的兴趣带到大学毕业上