当前位置: 首页 > java >正文

嵌入式学习日志————实验:串口发送串口发送+接收

1.配置流程

①开启时钟,把需要用到的USART和GPIO的时钟打开

②GPIO初始化,把TX配置成复用输出,RX配置成输入模式

③配置USART,直接使用一个结构体来配置

④如果只需要发送功能,就直接开启USART,初始化结束了,如果需要接受的功能,那就还需要配置中断:在开启USART之前,加上ITConfig和NVIC的代码

2.库函数

配置同步时钟输出(时钟是否要输出、时钟极性、相位等参数)

`void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);`
`void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);`

中断输出控制

void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);

开启USART到DMA的触发通道

void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);

发送数据(写DR寄存器)

void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);

接收数据(读DR寄存器)

uint16_t USART_ReceiveData(USART_TypeDef* USARTx);

3.实验一——串口发送~代码

(1)main.c

#include "stm32f10x.h"                  // Device header
#include "delay.h"
#include "OLED.h"
#include "Serial.h"int main( void)
{OLED_Init();Serial_Init();//Serial_SendByte(0x41);uint8_t MyArray[] = {0x42,0x43,0x44,0x45};Serial_SendArray(MyArray,4);Serial_SendString("\r\nNum1 = ");Serial_SendNumber(123,3);printf("\r\nNum2 = %d",666);char string[100];sprintf(string,"\r\nNum3 = %d",789);Serial_SendString(string);Serial_Printf("\r\nNum4 = %d",456);Serial_Printf("\r\n");while(1){}	  
}    

(2)Serial.h

#ifndef __SERIAL_H
#define __SERIAL_H#include <stdio.h>void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);void Serial_Printf(char *format,...);#endif


(3)Serial.c

#include "stm32f10x.h"
#include <stdio.h>
#include <stdarg.h>void Serial_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);USART_InitTypeDef  USART_InitStructure;USART_InitStructure.USART_BaudRate = 9600;                           //波特率USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;    //硬件流控USART_InitStructure.USART_Mode = USART_Mode_Tx;               //发送模式USART_InitStructure.USART_Parity = USART_Parity_No;                          //校验位USART_InitStructure.USART_StopBits = USART_StopBits_1;               //停止位USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_Init(USART1,&USART_InitStructure);USART_Cmd(USART1,ENABLE);
}void Serial_SendByte(uint8_t Byte)
{USART_SendData(USART1,Byte);while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}void Serial_SendArray(uint8_t *Array,uint16_t Length)
{uint16_t i;for (i = 0;i < Length;i++){Serial_SendByte(Array[i]);}
}void Serial_SendString(char *String)
{uint16_t i;for (i = 0;String[1] != 0;i++){Serial_SendByte(String[i]);}
}uint32_t Serial_Pow(uint32_t x,uint32_t y)
{uint32_t result = 1;while(y--){result *= x;}return result;
}void Serial_SendNumber(uint32_t Number,uint8_t Length)
{uint8_t i;for(i = 0;i < Length;i++){Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');}
}int fputc(int ch,FILE *f)                         //重定向printf输出到串口{Serial_SendByte(ch);return ch;}void Serial_Printf(char *format,...)
{char string[100];va_list arg;va_start(arg,format);vsprintf(string,format,arg);va_end(arg);        //释放参数表Serial_SendString(string);}

4.实验二——串口发送+接收~代码

(1)main.c

#include "stm32f10x.h"                  // Device header
#include "delay.h"
#include "OLED.h"
#include "Serial.h"uint8_t RxData;int main( void)
{OLED_Init();Serial_Init();while(1){if(Serial_GetRxFlag()==1){RxData = Serial_GetRxData();OLED_ShowHexNum(1,1,RxData,2);}}	  
}    

(2)Serial.h

#ifndef __SERIAL_H
#define __SERIAL_H#include <stdio.h>void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);void Serial_Printf(char *format,...);uint8_t Serial_GetRxData(void);uint8_t Serial_GetRxFlag(void);void USART1_IRQHandler(void);#endif

(3)Serial.c

#include "stm32f10x.h"
#include <stdio.h>
#include <stdarg.h>uint8_t Serial_RxData;
uint8_t Serial_RxFlag;void Serial_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);USART_InitTypeDef  USART_InitStructure;USART_InitStructure.USART_BaudRate = 9600;                           //波特率USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;    //硬件流控USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;               //发送模式USART_InitStructure.USART_Parity = USART_Parity_No;                          //校验位USART_InitStructure.USART_StopBits = USART_StopBits_1;               //停止位USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_Init(USART1,&USART_InitStructure);USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_Init(&NVIC_InitStructure);USART_Cmd(USART1,ENABLE);
}void Serial_SendByte(uint8_t Byte)
{USART_SendData(USART1,Byte);while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}void Serial_SendArray(uint8_t *Array,uint16_t Length)
{uint16_t i;for (i = 0;i < Length;i++){Serial_SendByte(Array[i]);}
}void Serial_SendString(char *String)
{uint16_t i;for (i = 0;String[1] != 0;i++){Serial_SendByte(String[i]);}
}uint32_t Serial_Pow(uint32_t x,uint32_t y)
{uint32_t result = 1;while(y--){result *= x;}return result;
}void Serial_SendNumber(uint32_t Number,uint8_t Length)
{uint8_t i;for(i = 0;i < Length;i++){Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');}
}int fputc(int ch,FILE *f)                         //重定向printf输出到串口{Serial_SendByte(ch);return ch;}void Serial_Printf(char *format,...)
{char string[100];va_list arg;va_start(arg,format);vsprintf(string,format,arg);va_end(arg);        //释放参数表Serial_SendString(string);}uint8_t Serial_GetRxFlag(void)
{if(Serial_RxFlag==1){Serial_RxFlag = 0;return 1;}else{return 0;}
}uint8_t Serial_GetRxData(void)
{return Serial_RxData;
}void USART1_IRQHandler(void)
{if(USART_GetITStatus(USART1,USART_IT_RXNE) ==SET){Serial_RxData = USART_ReceiveData(USART1);Serial_RxFlag = 1;USART_ClearITPendingBit(USART1,USART_IT_RXNE);}
}

距离返校还有6天!!!加油~

http://www.xdnf.cn/news/18978.html

相关文章:

  • 2025年渗透测试面试题总结-37(题目+回答)
  • 2024年06月 Python(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 零基础-力扣100题从易到难详解(持续更新1-10题)
  • 【链表 - LeetCode】25. K 个一组翻转链表
  • DAY 58 经典时序预测模型2
  • Kubernetes 的20 个核心命令分类详解
  • Linex系统网络管理(二)
  • 数据结构第8章 排序(竟成)
  • SqlHelper类库的使用方法
  • .NET周刊【8月第3期 2025-08-17】
  • 鸿蒙ArkUI 基础篇-06-组件基础语法-Column/Row/Text
  • 车载诊断架构 --- 基于整车功能的正向诊断需求开发
  • Dubbo加标签方式
  • Vue3 + 高德地图实现车辆电子围栏监控与报警
  • banner这个文件是怎么请求到后端数据的
  • codeforces(1045)(div2)D. Sliding Tree
  • zookeeper-集群扩缩容
  • 二次校验请求源 IP 是否在 WAF 官方 IP 段内” + “校验是否携带 WAF 专属 HTTP 头
  • 全域管控,一触可达:复合机器人远程监控方案重塑智能制造
  • volitale伪共享问题及解决方案
  • 高效管理远程连接!Remote Desktop Manager 全方位使用指南
  • 对接连连支付(四)-- 收款查询
  • 数据结构:单链表的应用(力扣算法题)第一章
  • 迅睿CMS自定义网站表单:HTML方式调用Select下拉选项数据指南
  • Winsock 操作指南
  • 宝塔面板零基础搭建 WordPress 个人博客与外贸网站 | 新手10分钟上手指南
  • vscode 调试 指定 python文件 运行路径
  • 嵌入式Linux自学不走弯路!670+讲课程!系统学习路线:入门+应用+ARM+驱动+移植+项目 (STM32MP157开发板)
  • Libvio访问异常排查指南
  • 《从有限元到深度学习:我的金属疲劳研究进阶之路》