Timer capture driver for smart learning air conditioning project based on STM32

Publisher:beup001Latest update time:2019-03-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*************************************************************************************************************************

* Function: TIM3.C

* Function: Infrared signal capture and transmission

* Parameters: None

* Return: None

* Dependency: underlying read and write functions

*************************************************************************************************************************/ 


voidIRDA_GPIO_Init(void)

{

 

 GPIO_InitTypeDef  GPIO_InitStructure;

 

//LED1 LED2

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //Enable PA, PD port clock

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //Enable PA, PD port clock


 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12; //

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output

 GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //IO port speed is 50MHz

 GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA according to the set parameters.8

 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PA6 clears the previous settings  

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);


 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PA6 clears the previous settings  

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 GPIO_SetBits(GPIOA,GPIO_Pin_5); //PA.8 output high

 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //PA6 clears previous settings  

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOB, &GPIO_InitStructure);

 

//Watchdog SP706 input pin PA7

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PA7 clears the previous settings  

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

}


void TIM3_Int_Init(u16 arr,u16 psc)

{

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_ICInitTypeDef  TIM_ICInitStructure;

NVIC_InitTypeDef NVIC_InitStructure;



RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Clock enable



TIM_TimeBaseStructure.TIM_Period = arr; //Set the value of the auto-reload register period to count to 5000 for 500ms before the next update event

TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value used as the TIMx clock frequency divisor 10Khz counting frequency  

TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Set clock division: TDTS = Tck_tim

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //Initialize the time base unit of TIMx according to the parameters specified in TIM_TimeBaseInitStruct

 

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;

  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_ICInit(TIM3, &TIM_ICInitStructure);


NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //Preempt priority level 0

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled

NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct



TIM_ITConfig(TIM3,TIM_IT_CC1,DISABLE); //Do not allow CC1IE to capture interrupts

TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);//Update interrupt is not allowed



TIM_Cmd(TIM3,DISABLE); //Enable timer 3

  

}





void TIM_Polarityset(u8 Polarity)

{

 if(Polarity == 0) TIM3->CCER &= ~(1<<1); //Rise

 else TIM3->CCER |= 1<<1; //Decline

}






void TIM3_IRQHandler(void)   //TIM3中断

{

 if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //Check whether the specified TIM interrupt occurs: TIM interrupt source 

 {

TIM_ClearITPendingBit(TIM3,TIM_IT_Update ); // Clear TIMx interrupt pending bit: TIM interrupt source 

  switch(Timer3)

  {

   case 0:

   {

    Timer3++;

    if(u8Lev == 0x0) GPIO_SetBits(GPIOA,GPIO_Pin_5);

    else GPIO_ResetBits(GPIOA,GPIO_Pin_5);

    break;

   }

   case 1:

   {

    Timer3++;

    GPIO_SetBits(GPIOA,GPIO_Pin_5);

    break;

   }

   case 2:

   {

    Timer3 = 0;

    Timer_PWM++;

    if(Timer_PWM >= Capture)

    {

     Capture =  u16P[u16Pcn++];

     if(Capture == 0xffff) // Output ends

     {

      TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);

      TIM_Cmd(TIM3,DISABLE);

      GPIO_SetBits(GPIOA,GPIO_Pin_5);

irad_launch = 1;

      u8FSMirda = IRDA_FSM_DILE;

     }

     else

     {

      u8Lev = ~u8Lev;

      Timer_PWM = 0;

     } 

    }

    break;

   }

   default:

   {

    Timer3 = 0;

   } 

  }

  

 }


 if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET) 

  {

    //TIM_ICInitTypeDef  TIM_ICInitStructure;

    /* Clear TIM3 Capture compare interrupt pending bit */

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

    u8Timerirda = 0;

    if(CaptureNumber == 0)

    {

      /* Get the Input Capture value */

      IC3ReadValue1 = TIM_GetCapture1(TIM3);

      CaptureNumber = 1;

      u8Lev = 0xff;

    }

    else if(CaptureNumber == 1)

    {

      /* Get the Input Capture value */

      IC3ReadValue2 = TIM_GetCapture1(TIM3); 

      

      /* Capture computation */

      if (IC3ReadValue2 > IC3ReadValue1)

      {

        Capture = (IC3ReadValue2 - IC3ReadValue1); 

      }

      else

      {

        Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); 

      }

      u16P[u16Pcn++] = Capture;

      IC3ReadValue1 = IC3ReadValue2;

      

      /* Frequency computation */ 

      //TIM3Freq = (uint32_t) SystemCoreClock / Capture;

      //CaptureNumber = 0;

    }

  u8Lev = ~u8Lev;  

  TIM_Polarityset(u8Lev);  

  }

}


Keywords:STM32 Reference address:Timer capture driver for smart learning air conditioning project based on STM32

Previous article:DS18B20 driver based on STM32
Next article:Linux kernel transplantation based on S3C2440 and yaffs2 file system production--starting system

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号