STM32单片机学习(5) 定时器中断实验
时间:11-19
来源:互联网
点击:
/*
* * 软件功能: 定时器中断实验* */#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
STM32单片机学习定时器中断实 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)