欢迎来到天天文库
浏览记录
ID:38235479
大小:135.81 KB
页数:3页
时间:2019-05-27
《STM32定时器输出比较模式完全解读》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include"stm32f10x.h"__IOuint16_tCCR1_Val=60000;//捕获比较追赶值为+60000/***函数声明***/voidRCC_Configuration(void);voidGPIO_Configuration(void);voidNVIC_Configuration(void);voidTIM_Configuration(void);/***主函数***/intmain(void){RCC_Configuration();//时钟配置NVIC_Configuration();/
2、/中断和优先级配置GPIO_Configuration();//GPIO口配置TIM_Configuration();//定时器配置while(1);//死循环}voidRCC_Configuration(void){RCC_PCLK1Config(RCC_HCLK_Div4);/*PCLK1=HCLK/4,即PCLK1=18M,定时器TIMx的时钟将乘2变为36M*/RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//TIM2时钟使能RCC_APB2PeriphCl
3、ockCmd(RCC_APB2Periph_GPIOD,ENABLE);//GPIOD时钟使能}voidGPIO_Configuration(void){//配置GPIOD.6GPIO_InitTypeDefGPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GP
4、IO_Init(GPIOD,&GPIO_InitStructure);}voidNVIC_Configuration(void){NVIC_InitTypeDefNVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;/*使能TIM2总中断*/NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NV
5、IC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);}voidTIM_Configuration(void){/*------------------------------------------------------------------TIM2配置:输出比较时间模式(设置CCMR1寄存器中的OC1M[2:0],这样比较后对OCxREF不起作用)TIM2计数器时钟为72÷4×2÷6=6MHz(APB1四分频所以乘以2,
6、计数器预分频6)CC1更新速率=计数器时钟/CCR1_Val=6M/60000=100Hz,即10ms产生一次比较-------------------------------------------------------------------*/#definePrescalerValue5//预分频值为6-1TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//定义时基初始化结构体变量TIM_OCInitTypeDefTIM_OCInitStructure;//定义输出比较初
7、始化结构体变量/*时基配置*/TIM_TimeBaseStructure.TIM_Period=65535;//设置自动重装载值TIM_TimeBaseStructure.TIM_Prescaler=0;//暂不分频,下面再分频TIM_TimeBaseStructure.TIM_ClockDivision=0;//时钟分割,无视之!TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式TIM_TimeBaseInit(TIM2,&TIM_Time
8、BaseStructure);/*预分频配置*/TIM_PrescalerConfig(TIM2,PrescalerValue,TIM_PSCReloadMode_Immediate);/*输出比较时间模式配置:通道1*/TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Timing
此文档下载收益归作者所有