Using STM8's PWM input to capture and measure infrared code program

Publisher:电子设计探索者Latest update time:2020-09-01 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Example description:
1. This example uses the PWM input capture mode of the STM8 microcontroller timer TIM1 to measure the infrared remote control code. 

2. The infrared remote control code format is NEC infrared code.
3. The infrared receiving signal input is connected to PC1 (TIM1 input channel 1).
4. Use external crystal oscillator 16M.
5. Use IAR FOR STM8 development environment.
6. The program code is as follows:
//--------------------------------------------------------------------
#include


#define uchar unsigned char
#define uint unsigned int


uchar IrRecStep = 0; //Receiving step
uchar IrIndex = 0; //Receiving bit number
uchar IrRecFlag = 0; //Receiving completed flag
uchar IrRecBuff[4]; //Receiving buffer
uchar IrRecAddr1; //Infrared address code
uchar IrRecAddr2;
uchar IrRecData1; //Infrared data code
uchar IrRecData2;
uint IrCycle = 0; //Infrared cycle
uint IrHigh = 0; //Duty cycle
const uchar IrCode[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};


//--------Boot code low level range------------------
#define IrHeadLow_Max 2300 //2300*4 = 9200us
#define IrHeadLow_Min 2100 //2100*4 = 8400us
//--------Boot code high level range------------------
#define IrHeadHigh_Max 1250 //1250*4 = 5000us
#define IrHeadHigh_Min 1050 //1050*4 = 4200us
//--------Data 0 period range-------------------
#define IrData00_Max 300 //300*4 = 1200us
#define IrData00_Min 260 //260*4 = 1040us
//--------Data 1 period range-------------------
#define IrData10_Max 580 //580*4 = 2320us
#define IrData1_Min 400 //400*4 = 1600us
//--------Data 1 duty cycle (high level) range---------
#define IrData1_Max 50 //50*4 = 1760us
#define IrData1_Min 200 //200*4 = 1600us
//--------Data 2 duty cycle (high level) range---------
#define IrData1_Max 440 //440*4 = 1760us
#define IrData1_Min 300 //300*4 = 1600us
//----------------------------------------------------------
#define REMOTE_CLK_DIV 63 //63+1=64----16Mh/64=4us
#define REMOTE_ICF_FILTER 0x30


//Delay program
void Delay_ms( uint ms )

  uint i,j;
  for( j=0; j { 
    for( i=0; i<1000; i++ )
    {;} 
  }
}


//Initialize infrared receiving port
void Ir_Init(void)
{
  PC_DDR_DDR1 = 0; //PC1 is input
  PC_CR1_C11 = 1;
  PC_CR2_C21 = 0;
}


//Use TIM1's PWM input capture
void Ir_PWM_Init(void)

  TIM1_PSCRH = 0;
  TIM1_PSCRL = REMOTE_CLK_DIV;


  TIM1_CCER1 &= 0xee; //Disable input capture 1, 2
  TIM1_CCMR1 |= (REMOTE_ICF_FILTER + 0x01); // ch1 to ti1fp1
  TIM1_CCER1 &= 0xec; //ch1 rising edge trigger
  TIM1_CCMR2 |= (REMOTE_ICF_FILTER + 0x02); // ch2 selects ti1fp2
  TIM1_CCER1 |= 0x20; //ch1 falling edge triggers ic2
  TIM1_SMCR |= 0x54; //Select source trigger source and trigger mode reset
  TIM1_CCER1 |= 0x11; //Enable input capture 1, 2
  TIM1_CR1 |= 0x05;
}


//Infrared receiving program
void Ir_Receive(void)
{
  switch(IrRecStep)
  {
  case 0:
          IrIndex = 0;
          if ((TIM1_SR1_CC1IF == 1)&&(TIM1_SR1_CC2IF == 1))
          {
            IrHigh = (uint)(TIM1_CCR2H); 
            IrHigh = (IrHigh << 8) + TIM1_CCR2L; //IrHigh duty cycle
            IrCycle = (uint)(TIM1_CCR1H);
            IrCycle = (IrCycle << 8) + TIM1_CCR1L; //IrCycle period
            //Is it within the low level range of the boot code
            if (((IrCycle - IrHigh) < IrHeadLow_Max)&&((IrCycle - IrHigh) > IrHeadLow_Min))
            {
              IrRecStep = 1;
              TIM1_SR1_CC1IF = 0;
              TIM1_SR1_CC2IF = 0;
              TIM1_SR2_CC1OF = 0;
              TIM1_SR2_CC2OF = 0; TIM1_SR1_UIF =
              0;
              
              IrRecBuff[0] = 0;
              IrRecBuff[1] = 0;
              IrRecBuff[2] = 0;
              IrRecBuff[3] = 0;
            }
          }
          break;
  case 1:
          if ((TIM1_SR2_CC1OF==1) || (TIM1_SR2_CC2OF==1) || (TIM1_SR1_UIF==1))
          { 
            IrRecStep = 0;
          }
          else
          { 
            if ((TIM1_SR1_CC1IF == 1)&&(TIM1_SR1_CC2IF == 1))
            { 
              IrHigh = (uint)(TIM1_CCR2H); 
              IrHigh = (IrHigh << 8) + TIM1_CCR2L;
              IrCycle = (uint)(TIM1_CCR1H); 
              IrCycle = (IrCycle << 8) + TIM1_CCR1L;
              //Is it within the boot code high level range
              if ((IrHigh < IrHeadHigh_Max)&&(IrHigh > IrHeadHigh_Min))
              {
                IrRecStep = 2;
              }
            }
          }
          break;
  case 2:
          if ((TIM1_SR2_CC1OF==1) || (TIM1_SR2_CC2OF==1) || (TIM1_SR1_UIF==1))
          { 
            IrRecStep = 0;
          }
          else
          { 
            if ((TIM1_SR1_CC1IF == 1)&&(TIM1_SR1_CC2IF == 1))
            { 
              IrHigh = (uint)(TIM1_CCR2H); 
              IrHigh = (IrHigh << 8) + TIM1_CCR2L;
              IrCycle = (uint)(TIM1_CCR1H); 
              IrCycle = (IrCycle << 8) + TIM1_CCR1L;
              //Is the cycle within the range of data 0
              if ((IrCycle > IrData00_Min)&&(IrCycle < IrData00_Max))
              {
                //Is the high level within the range of data 0
                if ((IrHigh > IrData0_Min)&&(IrHigh < IrData0_Max))
                {
                  IrIndex++;
                }
                else
                {
                  IrRecStep = 0;
                }
              }
              //Is the cycle within the range of data 1
              else if ((IrCycle > IrData10_Min)&&(IrCycle < IrData10_Max))
              {
                //Is the high level within the range of data 1
                if ((IrHigh > IrData1_Min)&&(IrHigh < IrData1_Max))
                {
                  IrRecBuff[IrIndex >> 3] |= IrCode[IrIndex & 0x07];
                  IrIndex++;
                }
                else
                {
                  IrRecStep = 0;
                }
              }
              else
              {
                IrRecStep = 0;
              }
              if(IrIndex >= 32)
              {
                IrRecStep = 0;
                IrRecFlag = 1;
              }
            }
          }
          break;
  }
}
      
//Clock initialization
void CLK_Init(void)
{
  CLK_ECKR=0x03; //External clock register external clock is ready, external clock is on
  CLK_SWCR=0x02; //Switch control register enables switching mechanism
  CLK_SWR=0xB4; //Master clock switch register selects HSE as master clock source*/


  while (!(CLK_SWCR & 0x08));  
  CLK_CSSR=0x01; //Clock security system register    
}



//Initialization
void Devices_Init(void)
{
  Delay_ms(200);
  CLK_Init(); 
  Ir_Init();
  Ir_PWM_Init();


}


//Main program
void main( void )
{
  Devices_Init();
  while(1)
  {
    Ir_Receive();
    if(IrRecFlag == 1)
    {
      IrRecAddr1 = IrRecBuff[0];
      IrRecAddr2 = IrRecBuff[1];
      IrRecData1 = IrRecBuff[2];
      IrRecData2 = IrRecBuff[3];
      IrRecFlag = 0;
    }
  }
}

Keywords:STM8 Reference address:Using STM8's PWM input to capture and measure infrared code program

Previous article:stm8s development (nine) use of EEPROM: use EEPROM to store data!
Next article:LED intelligent lighting control system based on IOT

Recommended ReadingLatest update time:2024-11-16 16:19

Things to note when changing different chips in the STM8 program
1. Open the program with AVR software, select options, and modify the microcontroller model 2. If there is a model in C++, modify the corresponding model (do not skip this step) 3. DEBUGGER selects the corresponding emulator 4. If the header file is inconsistent, modify it 5. In the H file stm8l15x.h, select t
[Microcontroller]
Things to note when changing different chips in the STM8 program
STM32F4 advanced timer generates six complementary PWM waves
1. Use advanced timer TIM8; 2. Use pins: CH1-PC6, CH1N-PA5, CH2-PC7, CH2N-PB0, CH3-PC8, CH3N-PB1; 3. Registers used: TIMx_CCMRx: Select PWM mode                                TIMx_ARR: PWM period                                TIMx_CCRx: PWM duty cycle 4. Structure: GPIO_InitTypeDef: GPIO port multiplexing
[Microcontroller]
51 MCU outputs 3-way PWM to drive RGB LED source program
Use the microcontroller interrupt to process PWM, control the ordinary three-color LED, output seven different colors, and change with breathing. It does not take up the program running time.    The microcontroller source program is as follows:                          }                                         else
[Microcontroller]
51 MCU outputs 3-way PWM to drive RGB LED source program
STM8 reset register address map
Reset Status Register (RST_SR) Please refer to the corresponding data sheet for base address information. (Table 7: STM8 reset register address mapping)
[Microcontroller]
STM8 reset register address map
STM32 PWM output experiment
First, some necessary declarations #include stm32f10x.h #include "pwm.h"u32 Sys_Clk=1000000;u16 pwm1_2_Freqz;//Frequency of pwm wave 1, 2 output port u16 pwm3_4_Freqz;//Frequency of pwm wave 3, 4 output port u16 TIM2_PERIOD;//Number of timer jump cycles u16 TIM4_PERIOD;u16 CCR_VAL1;//Value of the timer comparison reg
[Microcontroller]
STM32 PWM output experiment
STM32F103+HAL+PWM+DMA+WS2812
As the title says: Use the HAL library to control WS2812 through the DMA mode of stm32f103r8's PWM The hardware uses TIM3's CHANNEL1. The hardware configuration is as follows: The clock is configured as a normal clock. The arr required for timer 3 to generate a 1.25us cycle is 89 (calculation omitted)
[Microcontroller]
STM32F103+HAL+PWM+DMA+WS2812
STM8 main clock source
The following 4 clock sources can be used as the master clock: 1-24MHz High Speed ​​External Crystal Oscillator (HSE) Maximum 24MHz high-speed external clock signal (HSE user-ext) 16MHz High Speed ​​Internal RC Oscillator (HSI) 128KHz low speed internal RC (LSI) Each clock source can be turned on or off indep
[Microcontroller]
STM8 main clock source
Characteristics and Application of DMPPT and PFM/PWM Hybrid Modulation Circuit
1 Introduction The research and development of power management integrated circuits for portable products has the design technology of pulse frequency modulation (PFM), pulse width modulation (PWM) and digital power management chips, and has a series of advanced boost chip designs and products. In parti
[Power Management]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号