您的位置 首页 应用

STM32单片机学习(5) 定时器中止试验

/***软件功能:定时器中断实验**/#includestm32f10x.h#includedelay.hvoidRCC_Configuration(void);voidGPIO_Configurat

/*

*	* 软件功用:	 定时器中止试验* */#include "stm32f10x.h"#include "delay.h"void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM3_Configuration(u16 arr,u16 psc);/*函数: int main(void)功用: main主函数参数: 无回来: 无/int main(void){RCC_Configuration();GPIO_Configuration();NVIC_Configuration();TIM3_Configuration(4999,7199);//10Khz的计数频率,计数到5000为500ms  //delay_init(72);GPIO_ResetBits(GPIOB,GPIO_Pin_0); while(1);   }/*函数: void RCC_Configuration(void)功用: 复位和时钟操控 装备参数: 无回来: 无/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus;                    //界说外部高速晶体发动状况枚举变量RCC_DeInit();                                    //复位RCC外部设备寄存器到默认值RCC_HSEConfig(RCC_HSE_ON);                       //翻开外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp();      //等候外部高速时钟准备好if(HSEStartUpStatus == SUCCESS)                  //外部高速时钟现已准别好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //舱位FLASH预读缓冲功用,加快FLASH的读取。一切程序中有必要的用法.方位:RCC初始化子函数晒干,时钟起振之后FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延时RCC_HCLKConfig(RCC_SYSCLK_Div1);               //装备AHB(HCLK)时钟等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1);                //装备APB2(PCLK2)钟==AHB时钟RCC_PCLK1Config(RCC_HCLK_Div2);                //装备APB1(PCLK1)钟==AHB1/2时钟RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //装备PLL时钟 == 外部高速晶体时钟 * 9 = 72MHzRCC_PLLCmd(ENABLE);                                   //使能PLL时钟while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等候PLL时钟安排妥当{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //装备体系时钟 = PLL时钟while(RCC_GetSYSCLKSource() != 0x08)                  //查看PLL时钟是否作为体系时钟{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //答应 GPIOB、AFIO时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能}/*函数: void GPIO_Configuration(void)功用: GPIO装备参数: 无回来: 无/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure;        //界说GPIO初始化结构体GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); 	   //PB用于输出操控LED灯}void NVIC_Configuration(void)	 //中止分组和优先级装备	 详见《STM32的函数阐明(中文).pdf》P165{NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中止NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器}void TIM3_Configuration(u16 arr,u16 psc)	  //TIM3定时器装备   {TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_TimeBaseStructure.TIM_Period = arr; //设置鄙人一个更新事情装入活动的主动重装载寄存器周期的值	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟切割:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //依据指定的参数初始化TIMx的时刻基数单位/*((1+7199[TIM_Prescaler] )/72M)*(1+4999[TIM_Period] )=500,000us=500ms */ TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中止,答应更新中止TIM_Cmd(TIM3, ENABLE);  //使能TIMx}//定时器3中止服务程序void TIM3_IRQHandler(void)   //TIM3中止{if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //查看TIM3更新中止产生与否{TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //铲除TIMx更新中止标志 if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) 	GPIO_SetBits(GPIOB,GPIO_Pin_0); else   GPIO_ResetBits(GPIOB,GPIO_Pin_0); }}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/265255.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部