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

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

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.dtcms.com/a/356032.html

相关文章:

  • 亚远景-ISO/PAS 8800:如何通过认证提升企业技术管理能力?
  • 入职体检空腹血糖 6.15mmol/L合格吗?
  • amis上传组件导入文件接口参数为base64格式的使用示例
  • NumPy 2.x 完全指南【三十一】布尔数组索引
  • ConcurrentHashMap的原理
  • 实战笔记——构建智能Agent:SpreadJS代码助手
  • vue3使用reactive和ref
  • 【python】python进阶——生成器
  • JDK的ConcurrentHashMap为什么放弃了分段锁
  • 大模型开发之:LangChain4j【附资料】
  • C++基础知识:虚函数和纯虚函数
  • 基于MATLAB的FIR滤波器设计与信号分离实现
  • 线性回归的法方程:原理与解析
  • 复习笔记11
  • 【K8s】整体认识K8s之pod
  • 【Git】Git 常用指令
  • 使用华为 USG6000防火墙配置安全策略
  • 今日行情明日机会——20250828
  • 驾驭巨量数据:HTTP 协议与大文件传输的多种策略
  • 【Python开源环境】Anaconda/Miniconda
  • 印度尼西亚数据源 PHP 对接文档
  • 从零搭建安全帽检测(8)— 泛化性检验:构建独立测试集与模型性能公正评估
  • 动态加载和异步调用tasklet/workqueue day63 ay64
  • 卷积神经网络搭建及应用
  • 对象之间属性拷贝(Bean Mapping)的工具MapStruct 和 BeanUtils
  • 多据点协作下的数据库权限与版本管理实战
  • BeforeEach与AfterEach注解的使用
  • React学习教程,从入门到精通, ReactJS - 安装:初学者指南(3)
  • iPhone17新品曝光!未来已来主题发布会即将登场
  • CSS入门学习