《stm32实验程序代码.docx》由会员分享,可在线阅读,更多相关《stm32实验程序代码.docx(13页珍藏版)》请在第壹文秘上搜索。
1、实验1GPIO#include#incIude*Input:None*Output:None*Return:None*intmain(void)*Configurethesystemclocks*/*Input:None*Output:None*Return:None*voidNVIC_Configuration(void)NVIC_IniUypeDefNVIC_Ini!Structure;*Configureonebitforpreemptionpriority*/NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);*EnabletheEXT19_5
2、Interrupt*/* Input:None* Output:None* Return:None*/voidGP10_Configuration(void)(GP10_InitTypeDefGPI0_lni!Structure;*EnableGPIOA,GPIOFandAFIOclocks*/RCC_APB2PeriphCIockCmd(RCC_APB2Periph_GPIOARCC_APB2Periph_GPIOFRCC_APB2Periph_AFI0,ENABLE);*ConfigurePF.asOutputpush-pulI*/GPI0_Pin_3|GPI0_Pin_4|GPI0_Pi
3、n_5|GPI0_Pin_6|GPI0_Pin_7|GPI0_Pin_8|GPI0_Pin_9|GPIO_Pin_1O;=GPI0_Speed_50MHz;=GPIO_Mode_Out_PP;GPIO_lnit(GPIOF,&GPIOJnitStructure);*ConfigurePA9asinputfloating(EXTILine9)*/=GPI0_Pin_9;* Input:None* Output:None* Return:None/intmain(void)*配置LED灯使用的GPlO管脚模式*/RCC_APB2PeriphCIockCmd(RCC_GPIO_LED,ENABLE)
4、;*使能LED灯使用的GPIO时钟*/=LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN|LD5_PIN|LD6_PIN|LD7_PIN|LD8_PIN;=GPI0_Mode_0ut_PP;=GPI0_Speed_50MHz;GPIO_lnit(GPIO_LED,&GP10_lni!Structure);*使用的LED灯相关的GPlo初始化*/GPI0_ResetBits(GPI0_LED,LD1_PIN|LD2_PINLD3,PINLD4_PINLD5_PINLD6,PINLD7_PIN|LD8_PIN);/*关闭所有的LED指示灯*/whiIe(1)GP10_ResetBit
5、s(GP10_LED,LD1_PINLD2_PINLD3_PINLD4_PINLD5_PINLD6_PINLD7_PIN|LD8_PIN);*关闭所有的LED指示灯*/Turn_0n_LED(count%8);*Input:nCont:specifiesthedeIaytimelength.* Output:None* Return:None*/voidDelay(vu32nCount)for(;nCount!=0;nCount);实验11串口收发#include#include*paramNone*retvaINone*/voidRCC_Configuration(void)(*使能串口1和
6、串口2使用的GPIo时钟*/RCC_APB2PeriphCIockCmd(USART1_GPIO_CLKUSART2_GPIO_CLK,ENABLE);*EnableUSART1Clock*/*使能串口1时钟*/RCC_APB2PeriphCIockCmd(USART1_CLK,ENABLE);*使能串口2时钟*/RCC_APB1PeriphCIockCmd(USART2_CLK,ENABLE);/*使能LED灯使用的GPIO时钟*/RCC_APB2PeriphCIockCmd(RCC_GPI0_LED,ENABLE);)* briefConfiguresthedifferentGPIOpor
7、ts.* paramNone*retvaINone*/voidGP10_Configuration(void)(GP10_InitTypeDefGP10_Ini!Structure;*串口1RX管脚配置*/*ConfigureUSART1Rxasinputfloating*/=USART1_RxPin;=GPIO_Mode_IN_FLOATING;GPI0_lnit(USART1_GPI0,&GPI0_lni!Structure);*串口2RX管脚配置*/*ConfigureUSART2Rxasinputfloating*/=USART2_RxPin;GPI0_lnit(USART2_GPI0
8、,&GP10_Ini!Structure);*串口1TX管脚配置*/* Configure USART1 Txasalternatefunctionpush-puII*/=USART1_TxPin;=GPI0_Speed_50MHz;=GPIO_Mode_AF_PP:GPI0_lnit(USART1_GPI0,&GPIO_lni!Structure);*串口2TX管脚配置*/* Configure USART2 Txasalternatefunctionpush-pulI*/=USART2_TxPin;GPI0_lnit(USART2_GPI0,&GPIO_lnItStructure);*配置
9、LED灯使用的GPlO管脚模式*/=LD1_PIN|LD2_PINLD3_PINLD4_PIN;=GPIO_Mode_Out_PP;=GPI0_Speed_50MHz;GPI0.lnit(GPIO_LED,&GPI0_lni!Structure);)* briefConfiguresthenestedvectoredinterruptcontroller.* paramNone*retvaINone*/voidNVIC_Configuration(void)(NVIC_InitTypeDefNVIC_Ini!Structure;*ConfiguretheNVICPreemptionPriori
10、tyBits*/NVIC_PrioriIyGroupConfig(NVIC_PriorityGroup_0);*EnabletheUSART1Interrupt*/=USART1_IRQn;=0;=ENABLE;NVICInit(&NVICInitStructure);*EnabletheUSART2Interrupt*/=USART2_IRQn;=1;=ENABLE:NVIC_Init(&NVIC_Ini!Structure);)* briefComparestwobuffers.* parampBuffer1,pBuffer2:bufferstobecompared.* paramBuff
11、erLength:buffersIength* retvaIPASSED:pBuffer1identicaltopBuffer2* FAILED:pBuffer1differsfrompBuffer2*/TestStatusBUfferCmP(U8*pBuffer1,u8*pBuffer2,u16BufferLength)whiIe(BufferLength)if(*pBuffer1!=*pBuffer2)returnFAILED;)pBuffer1+;pBuffer2+;)returnPASSED;)* briefThisfunctionhandIesUSART1gIobaIinterrup
12、trequest.* paramNone*retvaINone*/voidUSARTI-IRQHandIer(VOid)if(USARTGetITStatus(USART1,USARTITRXNE)!=RESET)*Readonebytefromthereceivedataregister*/RxBufferIRxCounterI+=USART_ReceiveData(USART1);if(RxCounterI=NbrOfDataToReadD(*DisabletheUSART1Receiveinterrupt*/USART_ITConfig(USART1,USART_IT_RXNE,DISA
13、BLE);)if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)(*Writeonebytetothetransmitdataregister*/USART_SendData(USART1,TxBufferITxCounterI+);if(TxCounterI=NbrOfDataToTransferD(*DisabletheUSART1Transmitinterrupt*/USART_ITConfig(USART1,USART_IT_TXE,DISABLE);)* briefThisfunctionhandIesUSART2gIobaIinterruptrequest.* paramNone*retvaINone*/voidUSART2_IRQHandler(void)(if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)(*Readonebytefromthereceivedataregister*/RxBuffer2RxCounter2+=USART_ReceiveData(USART2);if(RxCounter2=NbrOfDataToReadD(*DisabletheUSART2Re