下载

0下载券

加入VIP
  • 专属下载券
  • 上传内容扩展
  • 资料优先审核
  • 免费资料无限下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 STM32——基本定时一秒闪烁LED

STM32——基本定时一秒闪烁LED.doc

STM32——基本定时一秒闪烁LED

瑾存了de依赖
2019-04-21 0人阅读 举报 0 0 0 暂无简介

简介:本文档为《STM32——基本定时一秒闪烁LEDdoc》,可适用于高等教育领域

STM通用定时器的基本定时器功能实现灯闪烁*Includes*#include"stmfxlibh"ErrorStatusHSEStARTUpStatusvoidRCCConfiguration(void)voidUSARTConfiguration(void)voidGPIOConfiguration(void)voidTIMConfiguration(void)voidNVICConfiguration(void)voiddelay(void)intmain(void){#ifdefDEBUGdebug()#endifRCCConfiguration()GPIOConfiguration()TIMConfiguration()USARTConfiguration()NVICConfiguration()*Infiniteloop*while(){if(TIMGetITStatus(TIM,TIMITUpdate)!=RESET){}}}********************************************************************************FunctionName:assertfailed*Description:Reportsthenameofthesourcefileandthesourcelinenumber*wheretheassertparamerrorhasoccurred*Input:file:pointertothesourcefilename*line:assertparamerrorlinesourcenumber*Output:None*Return:None*******************************************************************************voidRCCConfiguration(void){*将外设RCC寄存器重设为缺省值*RCCDeInit()*设置外部高速晶振(HSE)*RCCHSEConfig(RCCHSEON)RCCHSEONHSE晶振打开(ON)*等待HSE起振*HSEStARTUpStatus=RCCWaitForHSEStARTUp()if(HSEStartUpStatus==SUCCESS)SUCCESS:HSE晶振稳定且就绪{*设置AHB时钟(HCLK)*RCCHCLKConfig(RCCSYSCLKDiv)RCCSYSCLKDivAHB时钟=系统时钟*设置高速AHB时钟(PCLK)*RCCPCLKConfig(RCCHCLKDiv)RCCHCLKDivAPB时钟=HCLK*设置低速AHB时钟(PCLK)*RCCPCLKConfig(RCCHCLKDiv)RCCHCLKDivAPB时钟=HCLK*设置FLASH存储器延时时钟周期数*FLASHSetLatency(FLASHLatency)FLASHLatency延时周期*选择FLASH预取指缓存的模式*FLASHPrefetchBufferCmd(FLASHPrefetchBufferEnable)预取指缓存使能*设置PLL时钟源及倍频系数*RCCPLLConfig(RCCPLLSourceHSEDiv,RCCPLLMul)PLL的输入时钟=HSE时钟频率RCCPLLMulPLL输入时钟x*使能PLL*RCCPLLCmd(ENABLE)*检查指定的RCC标志位(PLL准备好标志)设置与否*while(RCCGetFlagStatus(RCCFLAGPLLRDY)==RESET){}*设置系统时钟(SYSCLK)*RCCSYSCLKConfig(RCCSYSCLKSourcePLLCLK)RCCSYSCLKSourcePLLCLK选择PLL作为系统时钟*PLL返回用作系统时钟的时钟源*while(RCCGetSYSCLKSource()!=x)x:PLL作为系统时钟{}}*使能TIM的APB外设时钟*RCCAPBPeriphClockCmd(RCCAPBPeriphTIM,ENABLE)*使能或者失能APB外设时钟*RCCAPBPeriphClockCmd(RCCAPBPeriphGPIOA|RCCAPBPeriphGPIOB|RCCAPBPeriphGPIOC|RCCAPBPeriphGPIOD|RCCAPBPeriphGPIOF,ENABLE)RCCAPBPeriphGPIOAGPIOA时钟RCCAPBPeriphGPIOBGPIOB时钟RCCAPBPeriphGPIOCGPIOC时钟RCCAPBPeriphGPIODGPIOD时钟}*voidUSARTConfiguration(void)串口初始化函数{USARTInitTypeDefUSARTInitStructure串口设置恢复默认参数USARTInitStructureUSARTBaudRate=设置波特率=USARTInitStructureUSARTWordLength=USARTWordLengthb字长位USARTInitStructureUSARTStopBits=USARTStopBits位停止字节USARTInitStructureUSARTParity=USARTParityNo无奇偶校验USARTInitStructureUSARTHardwareFlowControl=USARTHardwareFlowControlNone无流控制USARTITConfig(USART,USARTITRXNE,ENABLE)接收中断使能USARTITConfig(USART,USARTITTXE,ENABLE)发送中断使能USARTInitStructureUSARTMode=USARTModeRx|USARTModeTx打开Rx接收和Tx发送功能USARTInit(USART,USARTInitStructure)初始化串口USARTCmd(USART,ENABLE)启动串口}*voidGPIOConfiguration(void){GPIOInitTypeDefGPIOInitStructureGPIOInitStructureGPIOPin=GPIOPin|GPIOPin|GPIOPin|GPIOPin|GPIOPinGPIOInitStructureGPIOMode=GPIOModeOutPP推挽输出GPIOInitStructureGPIOSpeed=GPIOSpeedMHzGPIOInit(GPIOA,GPIOInitStructure)GPIOInitStructureGPIOPin=GPIOPinGPIOInitStructureGPIOMode=GPIOModeOutPP推挽输出GPIOInit(GPIOD,GPIOInitStructure)}voidTIMConfiguration(void){*每秒发生一次更新事件(进入中断服务程序)。RCCConfiguration()的SystemInit()的RCC>CFGR|=(uintt)RCCCFGRPPREDIV表明TIMCLK为MHz。因此,每次进入中断服务程序间隔时间为((TIMPrescaler)M)*(TIMPeriod)=(()M)*()=秒*TIMTimeBaseInitTypeDefTIMTimeBaseStructureTIMDeInit(TIM)TIMTimeBaseStructureTIMPeriod=自动重装载寄存器周期的值(定时时间)累计xFFFF个频率后产生个更新或者中断(也是说定时时间到)TIMTimeBaseStructureTIMPrescaler=时钟预分频数例如:时钟频率=(时钟预分频)TIMTimeBaseStructureTIMClockDivision=xTIMTimeBaseStructureTIMCounterMode=TIMCounterModeUpTIMTimeBaseInit(TIM,TIMTimeBaseStructure)*此函数的语句"TIMx>EGR=TIMPSCReloadModeImmediate"以软件方式产生更新事件(注:当发生一个更新事件时,所有的寄存器都被更新,硬件同时(依据URS位)设置更新标志位(TIMxSR寄存器中的UIF位)。*TIMTimeBaseStructureTIMRepetitionCounter=TIMSetAutoreload(TIM,)TIMClearFlag(TIM,TIMFLAGUpdate)ClearTIMupdatependingflag清除TIM溢出中断标志TIMITConfig(TIM,TIMITUpdate,ENABLE)EnableTIMUpdateinterruptTIM溢出中断允许TIMCmd(TIM,ENABLE)TIMenablecounter允许tim计数}*voidEXTIConfiguration(void){连接EXTILine到PAGPIOEXTILineConfig(GPIOPortSourceGPIOA,GPIOPinSource)配置EXTILine产生一个下降沿中断EXTIInitStructureEXTILine=EXTILineEXTIInitStructureEXTIMode=EXTIModeInterruptEXTIInitStructureEXTITrigger=EXTITriggerFallingEXTIInitStructureEXTILineCmd=ENABLEEXTIInit(EXTIInitStructure)连接EXTILine到PBGPIOEXTILineConfig(GPIOPortSourceGPIOB,GPIOPinSource)配置EXTILine产生一个下降沿中断EXTIInitStructureEXTILine=EXTILineEXTIInit(EXTIInitStructure)}*voidNVICConfiguration(void){NVICInitTypeDefNVICInitStructure#ifdefVECTTABRAM*SettheVectorTablebaselocationatx矢量表设置在x基地位置*NVICSetVectorTable(NVICVectTabRAM,x)#else*VECTTABFLASH**SettheVectorTablebaselocationatx矢量表设置在x基地位置*NVICSetVectorTable(NVICVectTabFLASH,x)#endifNVICPriorityGroupConfig(NVICPriorityGroup)*EnabletheTIMglobalInterrupt全局中断使能TIM*NVICInitStructureNVICIRQChannel=TIMIRQChannelNVICInitStructureNVICIRQChannelPreemptionPriority=NVICInitStructureNVICIRQChannelSubPriority=NVICInitStructureNVICIRQChannelCmd=ENABLENVICInit(NVICInitStructure)}voiddelay(void){ui,jFor(i=i<xfffffi){j}}#ifdefDEBUGvoidassertfailed(u*file,uline){*用户可以添加自己的执行情况报告的文件名和行号,例如:printf(“请值错误的参数:文件的行d的RN”的,文件,行)**Infiniteloop*while(){}}#endif*******************(C)COPYRIGHTSTMicroelectronics*****ENDOFFILE****中断函数#include"stmfxith"TIMTimeBaseInitTypeDefTIMTimeBaseStructurevoidTIMIRQHandler(void){TIMCmd(TIM,DISABLE)if(TIMGetITStatus(TIM,TIMITUpdate)!=RESET){TIMClearITPendingBit(TIM,TIMITUpdate)GPIOWriteBit(GPIOA,GPIOPin,(BitAction)(GPIOReadOutputDataBit(GPIOA,GPIOPin)))取反GPIOWriteBit(GPIOD,GPIOPin,(BitAction)(GPIOReadOutputDataBit(GPIOD,GPIOPin)))取反GPIOSetBits(GPIOC,GPIOPin)GPIOSetBits(GPIOA,GPIOPin)GPIOSetBits(GPIOF,GPIOPin)TIMTimeBaseStructureTIMRepetitionCounter=TIMConfiguration()}}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

评分:

/14

VIP

意见
反馈

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利