|
发表于 2022-7-14 08:57:27
|
显示全部楼层
上传一个,我一直在用的,自已觉得还是比较好用的
- #ifndef __BSP_USART1_H__
- #define __BSP_USART1_H__
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "main.h"
- #ifdef __cplusplus
- }
- #endif
- void USART1_SendChar(unsigned char ch);
- void USART1_SendBuff(unsigned char *p,int L);
- void USART1_SendStr(char *p);
- void USART1_SendProcess(void);
- unsigned int USART1_CheckLen(void);
- unsigned char USART1_ReadByte(void);
- unsigned char USART1_CheckByte(unsigned short n);
- void USART1_Discard(unsigned short n);
- void USART1_Config(uint32_t BaudRate);
- void USART1_Test(void);
- #endif /* __USART1_H */
复制代码
- #include "main.h"
- /*--------------------环形缓存 发送-----------------------------*/
- static const unsigned int TxLen=1024;
- static volatile unsigned char TxBuf[TxLen];
- static volatile unsigned char* TxW=TxBuf;
- static volatile unsigned char* TxR=TxBuf;
- static unsigned int TxCheckLen(void)
- {
- unsigned int Len; //short
- volatile unsigned char *W=TxW; volatile unsigned char *R=TxR;
- if(W>=R)Len=W-R;else Len=(W+TxLen)-R; //这样正确(中途中断改变也变不了结果)
- return Len;
- }
- static unsigned char TxReadByte(void)
- {
- unsigned char R=*TxR; //读数
- if(TxR!=TxW){ if(TxR+1>=(TxBuf+TxLen))TxR =TxBuf; else TxR++;}//下标
- return R;
- }
- void USART1_SendProcess(void)
- {//发送缓存,有数据就全部发送出去
- unsigned char ch;
- while(TxCheckLen()){
- ch = TxReadByte();
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- USART_SendData(USART1, ch );
- }
- }
- void USART1_SendChar(unsigned char ch) //将命令压入缓存等待发送
- {
- volatile unsigned char *W=TxW; //这里要与上面指针相同
- W=TxW+1; if(W>=TxBuf+TxLen)W=TxBuf; //取下一位置(到顶转到底)
- if(W!=TxR){*TxW=ch; TxW=W; }
- }
- // void USART1_SendChar(unsigned char ch)//发送一位数锯
- // {
- // /* 等待发送完毕 */
- // while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- // USART_SendData(USART1, ch );
- // }
- void USART1_SendBuff(unsigned char *p,int L)
- {
- unsigned int len = L;
- while (len>0){
- USART1_SendChar(*p++);
- len--;
- }
- }
- //void USART1_SendBuff(unsigned char *p,int L) //直接发送
- //{
- // SCUART_Write(UART1, p, L); //发送给串口
- //}
- void USART1_SendStr(char *p)//发送字符串
- {
- while (*p)
- {
- USART1_SendChar(*p++);
- }
- }
- /*--------------------环形缓存-----------------------------*/
- static const unsigned int RxLen=1024;
- static volatile unsigned char RxBuf[RxLen];
- static volatile unsigned char* RxW=RxBuf;
- static volatile unsigned char* RxR=RxBuf;
- void USART1_IRQHandler(void) //中断接收
- {
- volatile unsigned char *W=RxW; //这里要与上面指针相同
- uint8_t ch;
-
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- ch = USART_ReceiveData(USART1);
- W=RxW+1; if(W>=RxBuf+RxLen)W=RxBuf; //取下一位置(到顶转到底)
- if(W!=RxR){*RxW=ch; RxW=W; }
- }
- }
- unsigned int USART1_CheckLen(void) //检查RX接收了多少数据
- {
- unsigned int Len; //short
- volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
- if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
- // if( Rx2W>=Rx2R){ Len=Rx2W-Rx2R;}else { Len=Rx2Len+Rx2W-Rx2R; }//接收了多少数据
- return Len;
- }
- unsigned char USART1_ReadByte(void) //读RX中数锯,地指加一,和丢弃
- {
- unsigned char R=*RxR; //读数
- if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
- return R;
- }
- unsigned char USART1_CheckByte(unsigned short n) //看RX中数锯,地指不变,
- {
- volatile unsigned char *R=RxR+n;
- if(R>=(RxBuf+RxLen))R-=RxLen;
- return *R;
- }
- void USART1_Discard(unsigned short n) //丢弃RX数据几位
- {
- while(n){
- n--;
- if(RxR==RxW) return;
- if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
- }
- }
- /*-------------------- 初始化 -----------------------------*/
- void USART1_Config(uint32_t BaudRate)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //定义结端口构体
- USART_InitTypeDef USART_InitStructure;//定义结串口构体
- NVIC_InitTypeDef NVIC_InitStructure; //定义中断结构体
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //macUSART1_CLK
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //macUSART1_GPIO_CLK
-
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//
- /*2、配置接口:接收端 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//
-
- /*3、配置串口USART参数: */
- USART_InitStructure.USART_BaudRate = BaudRate; //波特率 115200
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据字长---8 字长8bit://带奇偶校验时字长为9bit,无奇偶校验时字长为8bit
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位-----1
- USART_InitStructure.USART_Parity = USART_Parity_No; //校验-------无校验USART_Parity_No
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//模式-------收发
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制-无
- USART_Init(USART1, &USART_InitStructure); //初始化串口3
- /*4、配置中断NVIC参数: */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //选择优先级组1 ==主优先级1位(0-1)+副优先级3位(0-7)
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //USART1全局中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级0,1,2,3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能USART1通道
- NVIC_Init(&NVIC_InitStructure); //初始化NVIC
-
- /*5、初始化缓冲:*/
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//先使能接收中断;(打开这允许接收,同时也开了ORE )
- USART_ClearFlag(USART1, USART_FLAG_TC);//清除移发送位传输完成标志位,否则可能会丢失第1个字节的数据.网友提供.
- USART_ITConfig(USART1, USART_IT_TC, DISABLE); //关闭TC中断
- USART_Cmd(USART1, ENABLE); //使能--串口3
-
- USART1_SendStr("USART1_Config is OK!\r\n");
- }
- //*************** 测试 ***************//
- void USART1_Test(void)
- {
- while(USART1_CheckLen()>0){
- USART1_SendChar(USART1_ReadByte());
- }
- }
- /**************************************************************
- ** 函数名 :fputc
- ** 功能 :重定向c库函数printf到USART
- ** 注意 :由printf调用
- ***************************************************************/
- int fputc(int ch, FILE *f)//模拟
- {
- USART_SendData(USART1, (unsigned char) ch);/* 将Printf内容发往串口 */
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* 等待发送结束 */
- return (ch);
- }
- /**************************************************************
- ** 函数名 :fgetc
- ** 功能 :重定向c库函数scanf到USART
- ** 注意 :由scanf调用
- ***************************************************************/
- int fgetc(FILE *f)//模拟
- {
- while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);// 等待串口3输入数据
- return (int)USART_ReceiveData(USART1);
- }
- /*********************************************END OF FILE**********************/
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|