11串口收发实验.docx
- 文档编号:14353066
- 上传时间:2023-06-22
- 格式:DOCX
- 页数:4
- 大小:16.20KB
11串口收发实验.docx
《11串口收发实验.docx》由会员分享,可在线阅读,更多相关《11串口收发实验.docx(4页珍藏版)》请在冰点文库上搜索。
11串口收发实验
11.串口收发实验
11.串口收发实验:
/*Includes------------------------------------------------------------------*/#include\ #include\ /**@addtogroupSTM32F10x_StdPeriph_Examples*@{*/ /**@addtogroupUSART_Interrupt*@{*/ GPIO_InitTypeDefGPIO_InitStructure; /*Privatetypedef-----------------------------------------------------------*/typedefenum{FAILED=0,PASSED=!
FAILED}TestStatus; /*Privatedefine------------------------------------------------------------*/#defineTxBufferSize1(countof(TxBuffer1)-1)#defineTxBufferSize2(countof(TxBuffer2)-1)#defineRxBufferSize1TxBufferSize2#defineRxBufferSize2TxBufferSize1 /*Privatemacro-------------------------------------------------------------*/#definecountof(a)(sizeof(a)/sizeof(*(a))) /*Privatevariables---------------------------------------------------------*/USART_InitTypeDefUSART_InitStructure; uint8_tTxBuffer1=\串口中断收发示例:
串口1->串口2(中断收发)%uint8_tTxBuffer2=\串口中断收发示例:
串口2->串口1(中断收发)%uint8_tRxBuffer1[RxBufferSize1];uint8_tRxBuffer2[RxBufferSize2];__IOuint8_tTxCounter1=0x00;__IOuint8_tTxCounter2=0x00;__IOuint8_tRxCounter1=0x00;__IOuint8_tRxCounter2=0x00; uint8_tNbrOfDataToTransfer1=TxBufferSize1;uint8_tNbrOfDataToTransfer2=TxBufferSize2;uint8_tNbrOfDataToRead1=RxBufferSize1;uint8_tNbrOfDataToRead2=RxBufferSize2;__IOTestStatusTransferStatus1=FAILED;__IOTestStatusTransferStatus2=FAILED; /*Privatefunctionprototypes-----------------------------------------------*/voidRCC_Configuration(void);voidGPIO_Configuration(void);voidNVIC_Configuration(void); TestStatusBuffercmp(uint8_t*pBuffer1,uint8_t*pBuffer2,uint16_tBufferLength); /*Privatefunctions---------------------------------------------------------*//** *@briefMainprogram*@paramNone*@retvalNone*/ intmain(void){ /*!
ToreconfigurethedefaultsettingofSystemInit()function,refertosystem_file */ /*SystemClocksConfiguration*/RCC_Configuration(); /*NVICconfiguration*/NVIC_Configuration(); /*ConfiguretheGPIOports*/GPIO_Configuration(); GPIO_ResetBits(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN);/*关闭所有的LED指示灯*//*USART1andUSART2configuration------------------------------------------------------*/ /*USART1andUSART2configuredasfollow:
-BaudRate=9600baud -WordLength=8Bits -OneStopBit -Noparity -Hardwareflowcontroldisabled(RTSandCTSsignals) -Receiveandtransmitenabled*/ USART__BaudRate=115200; /*设置波特率为115200*/ USART__WordLength=USART_WordLength_8b;/*设置数据位为8*/USART__StopBits=USART_StopBits_1; /*设置停止位为1位*/USART__Parity=USART_Parity_No; /*无奇偶校验*/ USART__HardwareFlowControl=USART_HardwareFlowControl_None;/*无硬件流控*/ USART__Mode=USART_Mode_Rx|USART_Mode_Tx;/*发送和接收*/ /*配置串口1*/ USART_Init(USART1,&USART_InitStructure);/*配置串口2*/ USART_Init(USART2,&USART_InitStructure); /*使能串口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); /*WaituntilendoftransmissionfromUSART1toUSART2*/while(RxCounter2 /*WaituntilendoftransmissionfromUSART2toUSART1*/while(RxCounter1 /*Checkthereceiveddatawiththesendones*/ TransferStatus1=Buffercmp(TxBuffer2,RxBuffer1,RxBufferSize1); /*TransferStatus1=PASSED,ifthedatatransmittedfromUSART2andreceivedbyUSART1arethesame*/ /*TransferStatus1=FAILED,ifthedatatransmittedfromUSART2andreceivedbyUSART1aredifferent*/ TransferStatus2=Buffercmp(TxBuffer1,RxBuffer2,RxBufferSize2); /*TransferStatus2=PASSED,ifthedatatransmittedfromUSART1andreceivedbyUSART2arethesame*/ /*TransferStatus2=FAILED,ifthedatatransmittedfromUSART1andreceivedbyUSART2aredifferent*/ while
(1){ if(TransferStatus1==PASSED) {GPIO_SetBits(GPIO_LED,LD1_PIN);/*点亮LD1,串口1接收的数据与串口2发送的数据相同*/ } elseif(TransferStatus1==FAILED) { GPIO_SetBits(GPIO_LED,LD2_PIN);/*点亮LD2,串口1接收的数据与串口2发送的数据不相同*/ } if(TransferStatus2==PASSED) {GPIO_SetBits(GPIO_LED,LD3_PIN);/*点亮LD3,串口2接收的数据与串口1发送的数据相同*/ } elseif(TransferStatus2==FAILED) {GPIO_SetBits(GPIO_LED,LD4_PIN);/*点亮LD4,串口2接收的数据与串口1发送的数据不相同*/ } }}/** *@briefConfiguresthedifferentsystemclocks.*@paramNone*@retvalNone*/ voidRCC_Configuration(void){ /*使能串口1和串口2使用的GPIO时钟*/ RCC_APB2PeriphClockCmd(USART1_GPIO_CLK|USART2_GPIO_CLK,ENABLE);/*EnableUSART1Clock*//*使能串口1时钟*/ RCC_APB2PeriphClockCmd(USART1_CLK,ENABLE); /*使能串口2时钟*/ RCC_APB1PeriphClockCmd(USART2_CLK,ENABLE); /*使能LED灯使用的GPIO时钟*/ RCC_APB2PeriphClockCmd(RCC_GPIO_LED,ENABLE);}/** *@briefConfiguresthedifferentGPIOports.*@paramNone*@retvalNone*/ voidGPIO_Configuration(void){ GPIO_InitTypeDefGPIO_InitStructure; /*串口1RX管脚配置*/ /*ConfigureUSART1Rxasinputfloating*/GPIO__Pin=USART1_RxPin; GPIO__Mode=GPIO_Mode_IN_FLOATING;GPIO_Init(USART1_GPIO,&GPIO_InitStructure); /*串口2RX管脚配置*/ /*ConfigureUSART2Rxasinputfloating*/GPIO__Pin=USART2_RxPin;GPIO_Init(USART2_GPIO,&GPIO_InitStructure); /*串口1TX管脚配置*/ /*ConfigureUSART1Txasalternatefunctionpush-pull*/GPIO__Pin=USART1_TxPin; GPIO__Speed=GPIO_Speed_50MHz;GPIO__Mode=GPIO_Mode_AF_PP;GPIO_Init(USART1_GPIO,&GPIO_InitStructure); /*串口2TX管脚配置*/ /*ConfigureUSART2Txasalternatefunctionpush-pull*/GPIO__Pin=USART2_TxPin;GPIO_Init(USART2_GPIO,&GPIO_InitStructure); /*配置LED灯使用的GPIO管脚模式*/ GPIO__Pin=LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN;GPIO__Mode=GPIO_Mode_Out_PP;GPIO__Speed=GPIO_Speed_50MHz;GPIO_Init(GPIO_LED,&GPIO_InitStructure);}/**
*@briefConfiguresthenestedvectoredinterruptcontroller.*@paramNone*@retvalNone*/ voidNVIC_Configuration(void){ NVIC_InitTypeDefNVIC_InitStructure; /*ConfiguretheNVICPreemptionPriorityBits*/NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /*EnabletheUSART1Interrupt*/ NVIC__IRQChannel=USART1_IRQn;NVIC__IRQChannelSubPriority=0;NVIC__IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure); /*EnabletheUSART2Interrupt*/ NVIC__IRQChannel=USART2_IRQn;NVIC__IRQChannelSubPriority=1;NVIC__IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);}/** *@briefComparestwobuffers. *@parampBuffer1,pBuffer2:
bufferstobecompared.*@paramBufferLength:
buffer’slength *@retvalPASSED:
pBuffer1identicaltopBuffer2*FAILED:
pBuffer1differsfrompBuffer2*/ TestStatusBuffercmp(uint8_t*pBuffer1,uint8_t*pBuffer2,uint16_tBufferLength){ while(BufferLength--){ if(*pBuffer1!
=*pBuffer2) { returnFAILED; } pBuffer1++;pBuffer2++;} returnPASSED;} #ifdefUSE_FULL_ASSERT/** *@briefReportsthenameofthesourcefileandthesourcelinenumber* wheretheassert_paramerrorhasoccurred.*@paramfile:
pointertothesourcefilename *@paramline:
assert_paramerrorlinesourcenumber*@retvalNone*/ voidassert_failed(uint8_t*file,uint32_tline){ /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,ex:
printf(\ /*Infiniteloop*/while
(1){}} #endif/***@}*//***@}*/ /*****ENDOFFILE****/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 11 串口 收发 实验