串口通信
来源:互联网 发布:人工智能用go语言开发 编辑:程序博客网 时间:2024/05/23 11:09
main.c
#include "led.h"#include "delay.h"void My_USART1_Init(void){ GPIO_InitTypeDef GPIO_InitStructrue; USART_InitTypeDef USART_InitStrue; NVIC_InitTypeDef NVIC_InitStrue; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIO时钟使能; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // USART1时钟使能; GPIO_InitStructrue.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructrue.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructrue.GPIO_Pin = GPIO_Pin_9; // 发送端 USART1_TX GPIO_Init(GPIOA, &GPIO_InitStructrue); GPIO_InitStructrue.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_InitStructrue.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructrue.GPIO_Pin = GPIO_Pin_10; // 接收端 USART1_RX GPIO_Init(GPIOA, &GPIO_InitStructrue); USART_InitStrue.USART_BaudRate = 115200; // 波特率 USART_InitStrue.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制; USART_InitStrue.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; // 收发模式; USART_InitStrue.USART_Parity = USART_Parity_No; // 无奇偶校验位; USART_InitStrue.USART_StopBits = USART_StopBits_1; // 1位停止位; USART_InitStrue.USART_WordLength = USART_WordLength_8b; // 字长为8位; USART_Init(USART1, &USART_InitStrue); // 初始化串口; USART_Cmd(USART1, ENABLE); // 使能串口; USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); // 开启中断; NVIC_InitStrue.NVIC_IRQChannel = USART1_IRQn; // 中断通道; NVIC_InitStrue.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级; NVIC_InitStrue.NVIC_IRQChannelSubPriority = 1; // 子优先级; NVIC_Init(&NVIC_InitStrue);}// 串口1中断服务函数;void USART1_IRQHandler(void) { u8 res; if (USART_GetITStatus(USART1,USART_IT_RXNE)) // 接收中断; { res = USART_ReceiveData(USART1); // 读取收到的数据; USART_SendData(USART1, res); // 发送数据; }}int main(){ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 设置优先级分组; My_USART1_Init(); while (1);}
实验结果:
总结:
实验过程中有两点需要注意:
1、要移除System文件夹中的usart.c文件,不然会有函数重定义。
2、打开XCOM.exe串口调试工具时不要勾选RTS、DTR两个选项,不然接收不到数据。
DTR表示数据终端准备好,RTS表示请求发送。为什么RTS、DTR不能勾选,什么时候需要勾选,我还没有弄明白,希望知道的朋友多多指教。因为看视频时不认真,随手勾选RTS、DTR两个选项,然后白白浪费了3个小时,最后在论坛里找到原因,吸取教训。
0 0
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- “串口通信”
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- 串口通信
- poj2151 Check the difficulty of problems(概率dp)
- 数据结构java实现之简单排序
- 访问yy直播页面点击播放无响应分析
- Android闪屏页动画不起作用的解决方法
- Java-hibernate框架2(1、框架搭建;2、框架接口;3、框架的增删改;4、主键ID生成方式)
- 串口通信
- Sybase ASE 15.0/15.5/15.7安装介质下载地址
- 二叉树学习小结(序列化和反序列化、二叉树遍历、二叉树镜像、二叉搜索树、二叉树子结构等)
- iOS-Xcode 8解决真机测试Log被屏蔽的问题
- TensorFlow学习笔记(四):Tensorflow网络构建和TensorBoard进行训练过程可视化
- C Number Steps(九度OJ 1136)
- Spring Session + Redis实现分布式Session共享
- jstorm 源码分析: supervisor
- 数据结构之线性表(C++) -- 链式描述