串口收发实验_第1页
串口收发实验_第2页
串口收发实验_第3页
串口收发实验_第4页
串口收发实验_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

1、11.串口收发实验:/* Includes -*/#include stm32f10x.h#include platform_config.h/* addtogroup STM32F10x_StdPeriph_Examples * */* addtogroup USART_Interrupt * */ GPIO_InitTypeDef GPIO_InitStructure;/* Private typedef -*/typedef enum FAILED = 0, PASSED = !FAILED TestStatus;/* Private define -*/#define TxBuffer

2、Size1 (countof(TxBuffer1) - 1)#define TxBufferSize2 (countof(TxBuffer2) - 1)#define RxBufferSize1 TxBufferSize2#define RxBufferSize2 TxBufferSize1/* Private macro -*/#define countof(a) (sizeof(a) / sizeof(*(a)/* Private variables -*/USART_InitTypeDef USART_InitStructure;uint8_t TxBuffer1 = 串口中断收发示例:

3、 串口1 - 串口2 (中断收发);uint8_t TxBuffer2 = 串口中断收发示例: 串口2 - 串口1 (中断收发);uint8_t RxBuffer1RxBufferSize1;uint8_t RxBuffer2RxBufferSize2;_IO uint8_t TxCounter1 = 0x00;_IO uint8_t TxCounter2 = 0x00;_IO uint8_t RxCounter1 = 0x00; _IO uint8_t RxCounter2 = 0x00;uint8_t NbrOfDataToTransfer1 = TxBufferSize1;uint8_t

4、 NbrOfDataToTransfer2 = TxBufferSize2;uint8_t NbrOfDataToRead1 = RxBufferSize1;uint8_t NbrOfDataToRead2 = RxBufferSize2;_IO TestStatus TransferStatus1 = FAILED; _IO TestStatus TransferStatus2 = FAILED;/* Private function prototypes -*/void RCC_Configuration(void);void GPIO_Configuration(void);void N

5、VIC_Configuration(void);TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);/* Private functions -*/* * brief Main program * param None * retval None */int main(void) /*! At this stage the microcontroller clock setting is already configured, this is done through SystemI

6、nit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Config

7、uration(); /* Configure the GPIO ports */ GPIO_Configuration(); GPIO_ResetBits(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN);/*关闭所有的LED指示灯*/* USART1 and USART2 configuration -*/ /* USART1 and USART2 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware

8、 flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = ; /*设置波特率为*/ USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*设置数据位为8*/ USART_InitStructure.USART_StopBits = USART_StopBits_1; /*设置停止位为1位*/ USART_InitStructure.USART_Parity

9、 = USART_Parity_No; /*无奇偶校验*/ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/*无硬件流控*/ USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*发送和接收*/ /*配置串口1 */ USART_Init(USART1, &USART_InitStructure); /*配置串口2*/ USART_Init(USART2, &USART_InitStructure); /*

10、使能串口1的发送和接收中断*/ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_TXE, ENABLE); /*使能串口2的发送和接收中断*/ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_TXE, ENABLE); /* 使能串口1 */ USART_Cmd(USART1, ENABLE); /* 使能串口2 */ USART_Cmd(USART2, ENABLE); /*

11、 Wait until end of transmission from USART1 to USART2 */ while(RxCounter2 RxBufferSize2) /* Wait until end of transmission from USART2 to USART1 */ while(RxCounter1 RxBufferSize1) /* Check the received data with the send ones */ TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1); /* Tr

12、ansferStatus1 = PASSED, if the data transmitted from USART2 and received by USART1 are the same */ /* TransferStatus1 = FAILED, if the data transmitted from USART2 and received by USART1 are different */ TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2); /* TransferStatus2 = PASSED, i

13、f the data transmitted from USART1 and received by USART2 are the same */ /* TransferStatus2 = FAILED, if the data transmitted from USART1 and received by USART2 are different */ while (1) if(TransferStatus1 = PASSED) GPIO_SetBits(GPIO_LED,LD1_PIN);/*点亮LD1,串口1接收的数据与串口2发送的数据相同*/ else if(TransferStatu

14、s1 = FAILED) GPIO_SetBits(GPIO_LED,LD2_PIN);/*点亮LD2,串口1接收的数据与串口2发送的数据不相同*/ if(TransferStatus2 = PASSED) GPIO_SetBits(GPIO_LED,LD3_PIN);/*点亮LD3,串口2接收的数据与串口1发送的数据相同*/ else if(TransferStatus2 = FAILED) GPIO_SetBits(GPIO_LED,LD4_PIN);/*点亮LD4,串口2接收的数据与串口1发送的数据不相同*/ /* * brief Configures the different sys

15、tem clocks. * param None * retval None */void RCC_Configuration(void) /*使能串口1和串口2使用的GPIO时钟*/ RCC_APB2PeriphClockCmd(USART1_GPIO_CLK |USART2_GPIO_CLK, ENABLE); /* Enable USART1 Clock */ /*使能串口1时钟*/ RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE); /*使能串口2时钟*/ RCC_APB1PeriphClockCmd(USART2_CLK, ENABLE); /*使

16、能LED灯使用的GPIO时钟*/ RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE);/* * brief Configures the different GPIO ports. * param None * retval None */void GPIO_Configuration(void) GPIO_InitTypeDef GPIO_InitStructure; /*串口1 RX管脚配置*/ /* Configure USART1 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = USART

17、1_RxPin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(USART1_GPIO, &GPIO_InitStructure); /*串口2 RX管脚配置*/ /* Configure USART2 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = USART2_RxPin; GPIO_Init(USART2_GPIO, &GPIO_InitStructure); /*串口1 TX管脚配置*/ /* Configure USART1 Tx as alt

18、ernate function push-pull */ GPIO_InitStructure.GPIO_Pin = USART1_TxPin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(USART1_GPIO, &GPIO_InitStructure); /*串口2 TX管脚配置*/ /* Configure USART2 Tx as alternate function push-pull */ GPIO_InitSt

19、ructure.GPIO_Pin = USART2_TxPin; GPIO_Init(USART2_GPIO, &GPIO_InitStructure); /* 配置LED灯使用的GPIO管脚模式*/ GPIO_InitStructure.GPIO_Pin = LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIO_LED, &GPIO_InitStructu

20、re); /* * brief Configures the nested vectored interrupt controller. * param None * retval None */void NVIC_Configuration(void) NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART1 Interrupt */ NVI

21、C_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);/* * brief Compares two buffers. * param pBuffer1, pBuffer2: buffers to be compared. * param BufferLength: buffers length * retval PASSED: pBuffer1 identical to pBuffer2 * FAILED: pBuffer

温馨提示

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

评论

0/150

提交评论