stm32串口间通信实验.doc_第1页
stm32串口间通信实验.doc_第2页
stm32串口间通信实验.doc_第3页
stm32串口间通信实验.doc_第4页
stm32串口间通信实验.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

STM32串口间通信该工程主要实现了两块实验板之间的通信以及接收实验板和PC间的通信,通过发送实验板串口1发送数据,然后由接受实验板串口3接收数据后再又串口1发送出去通过PC查看实验效果。发送串口及子函数配置:#include sys.h#include usart.h/ char USART_TX_BUF12=0123456789rn; /发送缓冲 void uart_init(u32 bound) /GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;/NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); /USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /Usart1 NVIC 配置 /NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;/NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;/NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;/NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/IRQ通道使能/NVIC_Init(&NVIC_InitStructure);/根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1 /USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;/一般设置为9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;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); USART_ITConfig(USART1, USART_IT_TXE, ENABLE);/开启中断 USART_Cmd(USART1, ENABLE); /使能串口 void Put_String(u8 *p) while(*p) USART_SendData(USART1, *p+); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)= RESET) USART_ClearITPendingBit(USART1, USART_IT_TXE); /这些函数很有用所以附上来了void Uart_PutChar(u8 ch) USART_SendData(USART1, (u8) ch); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) = RESET) USART_ClearITPendingBit(USART1, USART_IT_TXE); /return ch; void Uart_PutString(u8* buf,u8 len) u8 i;for(i=0;i12) i=0; LED0=!LED0; delay_ms(250); 接收端串口及子函数配置#include sys.h#include usart.h/*串口试验*/char USART_RX_BUF12; /接收缓冲,最大12个字节.u8 Rx_Lenght; u8 Rx_flg;void uart_init(u32 bound) /GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;/NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); /USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /Usart1 NVIC 配置 /NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;/NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;/NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;/NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/IRQ通道使能/NVIC_Init(&NVIC_InitStructure);/根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1 /USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;/一般设置为9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;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); USART_ITConfig(USART1, USART_IT_TXE, ENABLE);/开启中断 USART_Cmd(USART1, ENABLE); /使能串口 void uart3_init(u32 bound) GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /USART1_TX PB.9 PB.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); /USART1_RX PB11GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); /Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/IRQ通道使能NVIC_Init(&NVIC_InitStructure);/根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1 /USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;/一般设置为9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;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); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);/开启中断串口3 USART_Cmd(USART3, ENABLE); /使能串口3 /*void USART1_IRQHandler(void) /串口1中断服务程序/u8 i=0;if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) /接收中断(接收到的数据必须是0x0d 0x0a结尾) flag1=1; USART_ClearITPendingBit(USART1, USART_IT_TXE); /USART_SendData(USART1, USART_RX_BUFi+); elseflag1=0;*/ /*void USART3_IRQHandler(void) /串口3中断服务程序/u8 i=0;if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) /接收中断(接收到的数据必须是0x0d 0x0a结尾) flag3=1; USART_ClearITPendingBit(USART3,USART_IT_RXNE); /USART_RX_BUFi+=USART_ReceiveData(USART3); elseflag3=0; */ void USART3_IRQHandler(void) u8 tmp; if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) tmp=USART_ReceiveData(USA

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论