打印

STM32F4定时器1外部时钟模式1

[复制链接]
6690|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
BSP.C板载外设的初始化
/**
  ******************************************************************************
  * @file    bsp.c
  * @author  wangfei
  * @date    13-April-2012
  * @e-mail  wfmjj@hotmail.com
  * @brief   Initialize peripherals.
  *****************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "bsp.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void Bsp_Init(void)
{
Bsp_GPIO_Config();
Bsp_NVIC_Config();
Bsp_TIM1_Config();
Bsp_TIM2_Config();
Bsp_USART3_Config();
Bsp_TIM3_Config();
}
/**
  * @brief   This function config gpio.
  * @param  None
  * @retval None
  */
void Bsp_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;  

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);   //打开外设GPIOE的时钟
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);     //turn on gpioc clock
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);    //turn on GPIOA clock
  

  GPIO_InitStructure.GPIO_Pin=(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);  //led口配置
   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOE, &GPIO_InitStructure);

  
   GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);  //Connect USART3 pins to AF7
   GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
  
   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10 | GPIO_Pin_11;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  //Connect TIM3 Pin to AF2
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM3);  //TIM3_Ch1
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM3);  //TIM3_Ch2
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM3);  //TIM3_Ch3
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_TIM3);  //TIM3_Ch4
// Config TIM3 Pin
GPIO_InitStructure.GPIO_Pin=(GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1); //PA9--TIM1_CH2

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**
  * @brief   This function config nvic.
  * @param  None
  * @retval None
  */
void Bsp_NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}
/**
  * @brief  This function config timer2.
  * @param  None
  * @retval None
  * @note   定时器2主要做1s的时间定时
  */
void Bsp_TIM2_Config(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

  TIM_TimeBaseStructure.TIM_Prescaler=1679;  
   TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
   TIM_TimeBaseStructure.TIM_Period=0xC350;  
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  
  TIM_ClearFlag(TIM2,TIM_FLAG_Update);        //初始化时必须将溢出中断清0必须在开溢出中断之前。
   //TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

}
/**
  * @brief   This function config timer1.
  * @param   None
  * @retval  None
  * @note    定时器1主要对其内部时钟计数
  */
void Bsp_TIM1_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);             //turn on timer1 clock
  
TIM_DeInit(TIM1);
  TIM_TimeBaseStructure.TIM_Prescaler=0;                       
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;      //定时器1向上计数
  TIM_TimeBaseStructure.TIM_Period=0xffff;                        
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

TIM1->CCMR1|=0X0100;  //配置定时器1为外部时钟模式1
TIM1->CCER&=0XFF5F;
TIM1->SMCR|=0X0067;

TIM_ClearFlag(TIM1,TIM_FLAG_Update);    //初始化时必须将溢出中断清0必须在开溢出中断之前。
}
/**
  * @brief   This function config USART3.
  * @param   None
  * @retval  None
  */
void Bsp_USART3_Config(void)
{
USART_InitTypeDef USART_InitStructure;
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //turn on usart3 clock
  
  USART_InitStructure.USART_BaudRate =115200 ;         //波特率设置
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART3, &USART_InitStructure);
  
  //USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
  USART_Cmd(USART3,ENABLE);
  USART_ClearFlag(USART3, USART_FLAG_TC);   //清除发送完成标志位
}
/**
  * @brief   This function config USART3.
  * @param   None
  * @retval  None
  */
void Bsp_TIM3_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);   //Open TIM3  Clock
/*******************************************************************************
TIM3 Config:generate 4 PWM Signals
TIM3CLK(CK_PSC)=2*PCLK1=2*SYSCLK/4=SYSCLK/2=84MHZ

To get TIM3 counter clock at 21 MHz, the prescaler is computed as follows:
TIM3CLK(CK_CNT)=Fclk_psc/(psc[15:0]+1);
psc[15:0]=(Fclk_psc/Fclk_cnt)-1;
prescaler=psc[15:0]=(Fclk_psc/Fclk_cnt)-1=(84/21)-1=3;

To get TIM3 output clock at 30 KHz, the period (ARR)) is computed as follows:
Fout_clk=Fclk_cnt/(ARR+1);
ARR=(Fclk_cnt/Fout_clk)-1=(21000000/30000)-1=699;

TIM3 Channelx duty cycle=(TIM3_CCRx/ TIM3_ARR)*100;
TIM3 Channel1 duty cycle=(TIM3_CCR1/ TIM3_ARR)*100=350/699=50%
TIM3 Channel2 duty cycle=(TIM3_CCR2/ TIM3_ARR)*100=200/699=28.6%
TIM3 Channel3 duty cycle=(TIM3_CCR3/ TIM3_ARR)*100=100/699=14.3%
TIM3 Channel4 duty cycle=(TIM3_CCR4/ TIM3_ARR)*100=500/699=71.5%
********************************************************************************/
  TIM_TimeBaseStructure.TIM_Prescaler=3;          //clk_cnt prescale
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;   //TIM3 Count mode
TIM_TimeBaseStructure.TIM_Period=699;         //Fout_clk=Fclk_cnt/(ARR+1)=21000000/700=30KHZ
TIM_TimeBaseStructure.TIM_ClockDivision=0;   

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

/* PWM1 Mode configuration: TIM3_Ch1 */
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;               //select PWM1 mode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //config oc1 as output
TIM_OCInitStructure.TIM_Pulse=350;                            //config TIM3_CCR1 vaule
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    //config oc1 high level avaliable
TIM_OC1Init(TIM3, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);         // turn on oc1 preload

/* PWM1 Mode configuration: TIM3_Ch2 */
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;               //select PWM1 mode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //config oc2 as output
TIM_OCInitStructure.TIM_Pulse=200;                            //config TIM3_CCR2 vaule
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    //config oc2 high level avaliable
TIM_OC2Init(TIM3, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);         // turn on oc2 preload

/* PWM1 Mode configuration: TIM3_CH3 */
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;               //select PWM1 mode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //config oc3 as output
TIM_OCInitStructure.TIM_Pulse=100;                            //config TIM3_CCR1 vaule
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    //config oc3 high level avaliable
TIM_OC3Init(TIM3, &TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);         // turn on oc3 preload

/* PWM1 Mode configuration: TIM3_CH4 */
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;               //select PWM1 mode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //config oc4 as output
TIM_OCInitStructure.TIM_Pulse=500;                            //config TIM3_CCR1 vaule
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    //config oc4 high level avaliable
TIM_OC4Init(TIM3, &TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);         // turn on oc4 preload

TIM_ARRPreloadConfig(TIM3, ENABLE);
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

沙发
wfmartin28|  楼主 | 2013-2-1 08:47 | 只看该作者
APP.C为主函数
/**
  ******************************************************************************
  * @file    app.c
  * @author  wangfei
  * @date    13-April-2012
  * @e-mail  wfmjj@hotmail.com
  * @brief   Initialize peripherals.
  *****************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "app.h"
#include "bsp.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern uint8_t TimeFlag;
extern uint16_t n_Counter;
uint8_t Data[4];
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
int main(void)
{
Bsp_Init();
GPIO_ResetBits(GPIOE,(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3));
  while(1)
  {
  GPIO_SetBits(GPIOE,(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3));
  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
  TIM_Cmd(TIM2,ENABLE);
   TIM_Cmd(TIM1,ENABLE);
  while(TimeFlag==0);     //等待1s定时时间的到来
  TIM2->CR1&=0XFFFE;      //关闭定时器2
  TIM1->DIER&=0XFFFE;     //禁止定时器2溢出中断
  GPIO_ResetBits(GPIOE,(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3));
  //TIM1->EGR|=0X01;
  //TIM2->EGR|=0X01;
  TimeFlag=0;
  Data[0]=n_Counter>>8;
  Data[1]=n_Counter;
  USART_SendData(USART3,Data[0]);
    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  USART_SendData(USART3,Data[1]);
    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  Delay(10000000);
   }
}
/**
  * @brief  Delay Function.
  * @param  nCount:specifies the Delay time length.
  * @retval None
  */
void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}

使用特权

评论回复
板凳
wfmartin28|  楼主 | 2013-2-1 08:49 | 只看该作者
定时器2的中断处理函数
void TIM2_IRQHandler(void)
{
  if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
  {
  n_Counter=TIM1->CNT;   //读出计数器1的数值
  TIM1->CR1&=0XFFFE;     //失能计数器1
  TIM2->CR1&=0XFFFE;     //失能定时器2
  TIM1->CNT&=0X0000;    //计数器1的计数寄存器清0
   TimeFlag=1;           //1s时间标志位置1
  TIM2->SR&=0XFFFE;     //退出中断前清楚定时器2的溢出标志位
}
}

使用特权

评论回复
地板
wfmartin28|  楼主 | 2013-2-1 08:51 | 只看该作者

使用特权

评论回复
5
1713664847| | 2015-12-4 09:12 | 只看该作者
学习下

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

19

主题

144

帖子

0

粉丝