完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include "user_usart.h" #include "stm32f10x_usart.h" /*使用microLib的方法*/ /* int fputc(int ch, FILE *f) { USART_SendData(USART1, (uint8_t) ch); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} return ch; } int GetKey (void) { while (!(USART1->SR & USART_FLAG_RXNE)); return ((int)(USART1->DR & 0x1FF)); } */ //#if EN_USART1_RX //如果使能了接收 //#if EN_USART1_RX //如果使能了接收 //串口1中断服务程序 //注意,读取USARTx->SR能避免莫名其妙的错误 //u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. //接收状态 //bit15, 接收完成标志 //bit14, 接收到0x0d //bit13~0, 接收到的有效字节数目 //串口1队列定义 u8 UART1SendBuff[UART1BuffSize]; //发送数据 u8 UART1ReceBuff[UART1BuffSize]; //接收数据? u16 UART1ReceIn = 0;//接收状态标记数据位 u8 UART1ReceFullFlag = 0;//接收完数据标志位 //串口2队列定义 u8 UART2SendBuff[UART2BuffSize]; u8 UART2ReceBuff[UART2BuffSize]; u16 UART2ReceIn = 0; u8 UART2ReceFullFlag = 0; //串口3队列定义 u8 UART3SendBuff[UART3BuffSize]; u8 UART3ReceBuff[UART3BuffSize]; u16 UART3ReceIn = 0; u8 UART3ReceFullFlag = 0; //串口1初始化 void USART1_Configuration(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure;; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//开启GPIOA和USART1时钟 //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART1, &USART_InitStructure); //初始化串口1 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //开启串口接收中断 USART_Cmd(USART1, ENABLE); //使能串口1 } //串口2初始化 void USART2_Configuration(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure;; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA和USART1时钟 //USART2_TX GPIOA.2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.2 //USART2_RX GPIOA.3初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.3 //Usart2 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3 响应 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART2, &USART_InitStructure); //初始化串口2 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开启串口接收中断 USART_Cmd(USART2, ENABLE); //使能串口2 } //串口3初始化 void USART3_Configuration(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure;; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIOA和USART1时钟 //USART3_TX GPIOB.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA.10 //USART3_RX GPIOB.3初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.11 //Usart3 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART3, &USART_InitStructure); //初始化串口3 USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); //开启串口接收中断 USART_Cmd(USART3, ENABLE); //使能串口3 } //串口1发送一帧数据 void USART1_SendOneData(uint8_t SendOneData) { USART_SendData(USART1, SendOneData); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} } //串口2发送一帧数据 void USART2_SendOneData(uint8_t SendOneData) { USART_SendData(USART2, SendOneData); while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} } //串口3发送一帧数据 void USART3_SendOneData(uint8_t SendOneData) { USART_SendData(USART3, SendOneData); while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) {} } //串口1发送一列数据 void USART1_SendUnfixedData(uint8_t *Buffer, uint8_t Length) { uint8_t i; for(i=0;i { USART1_SendOneData(*Buffer++); } } //串口2发送一列数据 void USART2_SendUnfixedData(uint8_t *Buffer, uint8_t Length) { uint8_t i; for(i=0;i { USART2_SendOneData(*Buffer++); } } //串口3发送一列数据 void USART3_SendUnfixedData(uint8_t *Buffer, uint8_t Length) { uint8_t i; for(i=0;i { USART3_SendOneData(*Buffer++); } } //串口1中断服务函数 void USART1_IRQHandler(void) { u8 Res;//数据暂存 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断 { Res =USART_ReceiveData(USART1); //读取接收到的数据 switch(UART1ReceIn)//读取接收到的数据有几位 每一位对应的数据协议校验 { case 0: if(Res==0xFF) UART1ReceBuff[UART1ReceIn++] = Res; else UART1ReceIn = 0; break; case 1: if(Res==0xFE) UART1ReceBuff[UART1ReceIn++] = Res; else UART1ReceIn = 0; break; case 2: if(Res==0x01) UART1ReceBuff[UART1ReceIn++] = Res; else UART1ReceIn = 0; break; case 3: if(Res==0xFE) UART1ReceBuff[UART1ReceIn++] = Res; else UART1ReceIn = 0; break; case 4: if(Res==0x31) UART1ReceBuff[UART1ReceIn++] = Res; else UART1ReceIn = 0; break; default: UART1ReceBuff[UART1ReceIn++] = Res; break; } if(UART1ReceIn >= 4) { UART1ReceFullFlag = 1; //数据完整接受完 } |
|
|
|
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除相对应的中断位 清除中断预处理位USART_ClearITPendingBit左移八位是USART_ClearFlag
} else if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // 发送中断 USART_GetITStatus { USART_ClearITPendingBit(USART1, USART_IT_TXE); // clear interrupt 清除中断预处理位 } } //串口2中断服务函数 void USART2_IRQHandler(void) { u8 Res;//数据暂存 if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断 { Res =USART_ReceiveData(USART2); //读取接收到的数据 USART_SendData(USART1,Res);//将读取到的数据通过串口1发送 switch(UART2ReceIn)//读取接收到的数据有几位 每一位对应的数据协议校验 { case 0: if(Res==0xFF) UART2ReceBuff[UART2ReceIn++] = Res; else UART2ReceIn = 0; break; case 1: if(Res==0xFF) UART2ReceBuff[UART2ReceIn++] = Res; else UART2ReceIn = 0; break; case 2: if(Res==0x0f) UART2ReceBuff[UART2ReceIn++] = Res; else UART2ReceIn = 0; break; case 3: if(Res==0x01) UART2ReceBuff[UART2ReceIn++] = Res; else UART2ReceIn = 0; break; default: UART2ReceBuff[UART2ReceIn++] = Res; break; } if(UART2ReceIn >= 4) { UART2ReceFullFlag = 1; //数据完整接受完 } USART_ClearITPendingBit(USART2, USART_IT_RXNE);//清除相对应的中断位 清除中断预处理位USART_ClearITPendingBit左移八位是USART_ClearFlag } else if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET) // 发送中断 USART_GetITStatus { USART_ClearITPendingBit(USART2, USART_IT_TXE); // clear interrupt 清除中断预处理位 } } //串口3中断服务函数 void USART3_IRQHandler(void) { u8 Res;//数据暂存 if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断 { Res =USART_ReceiveData(USART3); //读取接收到的数据 USART_ReceiveData USART_SendData(USART1,Res);//将读取到的数据通过串口1发送 switch(UART3ReceIn)//读取接收到的数据有几位 每一位对应的数据协议校验 { case 0: if(Res==0xFF) UART3ReceBuff[UART3ReceIn++] = Res; else UART3ReceIn = 0; break; case 1: if(Res==0xFF) UART3ReceBuff[UART3ReceIn++] = Res; else UART3ReceIn = 0; break; case 2: if(Res==0x0f) UART3ReceBuff[UART3ReceIn++] = Res; else UART3ReceIn = 0; break; case 3: if(Res==0x01) UART3ReceBuff[UART3ReceIn++] = Res; else UART3ReceIn = 0; break; case 4: if(Res==0x31) UART3ReceBuff[UART3ReceIn++] = Res; else UART3ReceIn = 0; break; default: UART3ReceBuff[UART3ReceIn++] = Res; break; } if(UART3ReceIn >= 57) { UART3ReceFullFlag = 1; //数据完整接受完 } USART_ClearITPendingBit(USART3, USART_IT_RXNE);//清除相对应的中断位 清除中断预处理位USART_ClearITPendingBit左移八位是USART_ClearFlag } else if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // 发送中断 USART_GetITStatus { USART_ClearITPendingBit(USART3, USART_IT_TXE); // clear interrupt 清除中断预处理位 } } #ifndef __USER_USART_H #define __USER_USART_H #include "stm32f10x.h" #define UART1BuffSize 64 //接收数据的位 #define UART2BuffSize 64 //接收缓存数据的字节 #define UART3BuffSize 64 //接收缓存数据的字节 extern u16 USART_RX_STA; //接收状态标记 extern u8 UART1SendBuff[UART1BuffSize]; //发送数据 extern u8 UART1ReceBuff[UART1BuffSize]; //接收缓存数据 extern u16 UART1ReceIn;//接收状态标记数据位数 extern u8 UART1ReceFullFlag; //接收数据状态 extern u8 UART2SendBuff[UART2BuffSize]; extern u8 UART2ReceBuff[UART2BuffSize]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符 extern u16 UART2ReceIn; extern u8 UART2ReceFullFlag; extern u8 UART3SendBuff[UART3BuffSize]; extern u8 UART3ReceBuff[UART3BuffSize]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符 extern u16 UART3ReceIn; extern u8 UART3ReceFullFlag; void USART1_Configuration(u32 bound);//穿口初始化 void USART2_Configuration(u32 bound); void USART3_Configuration(u32 bound); void USART1_SendOneData(uint8_t SendOneData); //串口1发送一帧数据 void USART2_SendOneData(uint8_t SendOneData); void USART3_SendOneData(uint8_t SendOneData); void USART1_SendUnfixedData(uint8_t *Buffer, uint8_t Length); void USART2_SendUnfixedData(uint8_t *Buffer, uint8_t Length); void USART3_SendUnfixedData(uint8_t *Buffer, uint8_t Length); void SendInputData(void); void SendAnalogData(void); // 通过串口发送模拟量数据 #define USART_REC_LEN 200 //定义最大接收字节数 200 #define EN_USART1_RX 1 //使能(1)/禁止(0)串口1接收 //如果想串口中断接收,请不要注释以下宏定义 #endif |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1763 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1617 浏览 1 评论
1059 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
723 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1670 浏览 2 评论
1933浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
726浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
567浏览 3评论
592浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
550浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 15:21 , Processed in 1.010670 second(s), Total 78, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号