STM32F103ZET6 之 通用定时器单脉冲模式实验

由于前面买的核心板,供电老有问题,使得我现在的项目又改用了以前用的F103ZET6微控制器!


1、实验目的

1)产生脉宽任意可调的单脉冲(在允许的范围内)


2、硬件:通用定时器3、通用定时器4


3、单脉冲模式介绍

单脉冲模式允许计数器响应一个激励,并在一个程序可控的延时之后,产生一个脉宽可程序控制的脉冲。

可以通过从模式控制器启动计数器,在输出比较模式或者PWM模式下产生波形。设置TIMx_CR1 寄存器中的OPM 位将选择单脉冲模式,这样可以让计数器自动的产生下一个更新

事情UEV时停止。

仅当比较值与计数器的初始值不同时,才能产生一个脉冲。启动之前(当定时器正在等待触发),必须配置如下:

向上计数方式:CNT (计数器寄存器) < CCRx (比较寄存器)< ARR(自动装载寄存器)

向下计数方式:CNT > CCRx。

STM32F103ZET6 之 通用定时器单脉冲模式实验_第1张图片

需要在从TI2输入脚上检测到一个上升沿开始,延迟tDELAY 之后,在OC1上产生一个长度为tPULSE 的正脉冲。

具体的可以看参考手册。


4、软件设计

/**
  ******************************************************************************
  * @file    timonepulse.c 
  * @author  Cawen
  * @version V1.0
  * @date    2015-12-22
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include"timonepulse.h"

/* Private variables ---------------------------------------------------------*/
uint16_t PrescalerValue = 0;

/*
 * Function Name  : GPIO_Configuration
 * Description    : Configure the GPIOD Pins.
 * Input          : None
 * Output         : None
 * Return         : None
 * Attention		  : None
 */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* TIM4_CH1 pin (PB.06) configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* TIM4_CH2 pin (PB.07) configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/*
 * Function Name  : TIM4_Configuration
 * Description    : TIM4 configuration: One Pulse mode
									 The external signal is connected to TIM4_CH2 pin (PB.07), 
									 The Rising edge is used as active edge,
									 The One Pulse signal is output on TIM4_CH1 pin (PB.06)
									 The TIM_Pulse defines the delay value 
									 The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
									 TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz:
									 - Prescaler = (TIM2CLK / TIM2 counter clock) - 1
									 The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value 
									 to trigger the TIM4 input is 24000000/65535 = 300 Hz.

									 The TIM_Pulse defines the delay value, the delay value is fixed 
									 to 682.6 us:
									 delay =  CCR1/TIM4 counter clock = 682.6 us. 
									 The (TIM_Period - TIM_Pulse) defines the One Pulse value, 
									 the pulse value is fixed to 2.048 ms:
									 One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = 2.048 ms.
 * Input          : None
 * Output         : None
 * Return         : None
 * Attention		  : None
 */
void TIM4_Configuration(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_ICInitTypeDef  			 TIM_ICInitStructure;
	TIM_OCInitTypeDef  			 TIM_OCInitStructure;
	
	
	/* TIM4 and GPIOB clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	/* Compute the prescaler value */
  PrescalerValue = (uint16_t) (72000000 / 10000) - 1;
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 5999;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* TIM4 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 2000;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  /* TIM4 configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);//恢复缺省值

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//选择输入端IC2映射到TI2上
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//映射到TI2上
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//配置输入分频,不分频
  TIM_ICInitStructure.TIM_ICFilter = 0;//配置输入滤波,不滤波

  TIM_ICInit(TIM4, &TIM_ICInitStructure);

  /* 单脉冲模式选择 */
  TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);//生成单一的脉冲:计数器在下一个更新事件停止

  /* 选择 TIM 输入触发源 */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);//TIM 经滤波定时器输入2

  /* 选择 TIM 从模式 */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);	//计数器在触发的上升沿开始
}

/*
 * Function Name  : TIM3_PWM_Init(u16 arr,u16 psc)
 * Description    : TIM3 PWM部分初始化 
										PWM输出初始化
										arr:自动重装值
										psc:时钟预分频数
 * Input          : None
 * Output         : None
 * Return         : None
 * Attention		  : None
 */

void TIM3_PWM_Init(u16 arr,u16 psc)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);	//使能定时器3时钟
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //使能GPIO外设时钟
	
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断

	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_InitStruct中指定的参数初始化外设NVIC寄存器
	
   //设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形	GPIOA7
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //TIM_CH2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
 
   //初始化TIM3
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	
	//初始化TIM3 Channel2 PWM模式	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
	TIM_OCInitStructure.TIM_Pulse = 5000;
 	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM3 OC2

	TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能TIM3在CCR2上的预装载寄存器
	
	TIM_Cmd(TIM3, ENABLE);  //使能TIM3
}

//定时器3中断服务程序
void TIM3_IRQHandler(void)   //TIM3中断
{
	
		if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
		{
			TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx的中断待处理位:TIM 中断源 
		  TIM_Cmd(TIM3, DISABLE);  //失能TIM3				         
		}
}



你可能感兴趣的:(stm32,单脉冲模式,处理器【STM32】)