Infineon MCU XC866-2FR infrared receiving program (state machine)

Publisher:iota19Latest update time:2012-08-31 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This is the first project I participated in outside. I was mainly responsible for the infrared sending and receiving parts. This program is the receiving part and is suitable for the Infineon microcontroller XC866-2FR chip. I share it with you and it can be used by you with a little deletion.
#include "MAIN.H"

#define State_Free 0 //Five defined states
#define State_Start 1
#define State_Data 2
#define State_Stop 3
#define State_Error 4
#define Rx_Lg 13 // Define the maximum receive data


uword T2_Counter=0; // Count the number of interrupts that enter timer T2
ubyte Bit_Counter=0; //Receive 8bit variable
ubyte State=0; //State variable
ubyte Date_Rx=0; //Data receiving variable
ubyte Table_Cnt; // variable for receiving data number
ubyte Send_Flag=0; //Send flag
ubyte Free_Error=0; //Error flag in idle state
ubyte Stop_Error=0; //Error flag in stop state

ubyte Get_Cnt=0; //The number of data sent through the serial port
uword Data_Ch1=0,Data_Ch2=0;

ubyte Table_Rx[14]={0}; //Receive array


 

//************************************************ ******************************
// @Controller Infineon XC866-2FR
// @Compiler Keil
// @Function T2_vInit(void)
// @Description Send data through the serial port
// @Input None
// @Output None

// @Auth0r KEVIN
// @Date 2010-11-25
// @Version 1.0
//************************************************ ******************************
void T2_vInit(void)
{
        T2_RC2L = 0x20; // load timer 2 reload/capture register,   
        T2_RC2H = 0xEF; // load timer 2 reload/capture register,   
        T2_T2MOD = 0x10; // load timer 2 mode register

        ET2 = 1; // Enable interrupt

        TR2 = 1; // start timer

}
//************************************************ ******************************
// @Controller Infineon XC866-2FR
// @Compiler Keil
// @Function Send_Data()
// @Description Send data through the serial port
// @Input None
// @Output None

// @Auth0r KEVIN

// @Date 2010-11-25
// @Version 1.0
//************************************************ ******************************
void Send_Data()
{
    Data_Ch1=(Table_Rx[1]-0x30)+(Table_Rx[3]-0x30)*16+(Table_Rx[2]-0x30)

                                 +(Table_Rx[7]-0x30)*4096+(Table_Rx[6]-0x30)*256

                                 +(Table_Rx[5]-0x30)*16+(Table_Rx[4]-0x30);
             Data_Ch2=(Table_Rx[11]-0x30)*4096+(Table_Rx[10]-0x30)*256

                                 +(Table_Rx[9]-0x30)*16+(Table_Rx[8]-0x30);
  
             if(Data_Ch1!=Data_Ch2)
             {
                      Table_Rx[13]=1;
             }

             for(Get_Cnt=0;Get_Cnt<14;Get_Cnt++) //Send the 16 data through the serial port
             {
                          UART_vSendData8(Table_Rx[Get_Cnt]); //Send buffer data
                          while(!TI); //Wait for sending to complete
                          TI=0; //Sending completed, clear flag
            }
           Table_Rx[13]=0;
}

//************************************************ ******************************
// @Controller Infineon XC866-2FR
// @Compiler Keil
// @Function T2_viTmr(void)
// @Description Timer T2 interrupt function, receiving data sent by infrared
// @Input None
// @Output None

// @Auth0r KEVIN
// @Date 2010-11-25
// @Version 1.0
//************************************************ ******************************
void T2_viTmr(void) interrupt T2INT
{
         if (TF2)
        {
        TF2 = 0;
        T2_Counter++;
        if(T2_Counter>400) //Prevent variable overflow
        {
          T2_Counter=400;
       }
                 switch(State) //state machine
                 {
          case State_Free: if((HWRx==0)&&(Bit_Counter==0))
               {
                if(T2_Counter>15) //Judge whether the time is greater than 164*16us
                {
                    T2_Counter=0; //Yes, clear  
                    State=State_Start; //Change the state to the start state
                }
                else //Otherwise
                {
                   T2_Counter=0; //clear
                   Free_Error=1;
                   State=State_Error; //Change the state to error state
                }
              }
               break; //End
         case State_Start: if((T2_Counter>=2)&&(HWRx==0)

                &&(Bit_Counter==0)) //Judge whether the start state is met
                         {
                            T2_Counter=0; //Satisfied, clear
                         Bit_Counter=8; //Assign initial value
                         State=State_Data; //Convert state to data

                                                                                                        Receiving status
                          }
                      break; //End

         case State_Data: if((T2_Counter>=5)&&((Bit_Counter>0)

                 &&(Bit_Counter<=8))) //Judge whether data reception is complete
                     {
                        T2_Counter=0; //No, clear the count variable
                        Date_Rx>>=1; //Move left one bit
                        if(HWRx==1) //Judge whether a high level is reached
                        {
                           Date_Rx|=0x80; //Yes, set the corresponding position to 1
                        }
                        Bit_Counter--; //Decrement by one
                        if(Bit_Counter==0) //Judge whether the eight-bit data is

                                                                                                         Receiving completed
                        {
                           State=State_Stop;
                        }
                      }
                                                     break;

         case State_Stop: if((T2_Counter>=5)&&(Bit_Counter==0)) //Judge whether all 8 bits of the data have been received and the stop bit has been detected
                    {
                                                              T2_Counter=0; //clear
                                                              if(HWRx==1) //Judge whether the data is received

                                                                                             Is HWRx pulled high?                
                           Table_Rx[Table_Cnt]=Date_Rx;  

                                                                                    // Temporarily save the data   
                                                                     Date_Rx=0; // clear to prevent influence

                                                                                                  The following data
                                                                    Table_Cnt++; //Number of data received

                                                                                                  Add 1
                                                                    if(Table_Rx[0]!=0x40) //Judge the first

                                                                         Is the received data 0x40? If not, discard it.
                                                                    {
                                                                            Table_Cnt=0;
                                                                     }
                                                                     State=State_Free; //Change the state to

                                                                                                             Idle state          
                                                                    if(Table_Cnt>=Rx_Lg) //Judge 16

                                                                                                Is data reception completed?
                                                                   {
                                                                           Table_Cnt=0; //Yes, clear to zero
                                                                           Send_Flag=1; //Set the send flag
                                                                   }
                                                           }
                                                          else //Otherwise, it will enter an error state
                                                          {
                                                                    State=State_Error;
                                                                    Stop_Error=1;
                                                          }
        
                                               }
                                              break; //End

                  case State_Error: if(Free_Error==1)       

                                                {
                                                        if(T2_Counter>50)
                                                         {
                                                                  Free_Error=0;
                                                                  State=State_Free;
                                                         }   
                                               }
                                               if(Stop_Error==1)
                                              {
                                                      if(T2_Counter>18)
                                                      {
                                                              Stop_Error=0;
                                                              State=State_Free;
                                                       }
                                               } //If yes, change the state to idle state
                                               break;

                            default: State=State_Free; //Other situations will be turned into idle state
                                                break;       
                }
      }

}
Keywords:MCU Reference address:Infineon MCU XC866-2FR infrared receiving program (state machine)

Previous article:DIY 1602 clock with adjustable display
Next article:Infrared sending program based on single chip microcomputer

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

PIC microcontroller CONFIG description
CONFIG is also called fuse bit. You can configure the internal functions of the microcontroller by configuring the fuse bit, such as crystal clock, watchdog, JTAG, power-off detection and code protection. If you don't understand these settings, it is recommended to copy and paste them directly. In PIC microcontrolle
[Microcontroller]
PIC microcontroller CONFIG description
Summary of PIC16F616 MCU learning experience
    1. Preface   In order to summarize the learning experience of PIC16F616 microcontroller some time ago and facilitate communication with everyone, I wrote this article about the learning experience of PIC microcontroller, which is organized in my own language after reading the manual and programming and debugging.
[Microcontroller]
Microcontroller C language programming example
1 51 MCU C language programming example    Basic knowledge: 51 MCU programming basics MCU external structure: 1. DIP40 dual in-line socket;  2. P0, P1, P2, P3 four 8-bit quasi-bidirectional I/O pins; (when used as I/O input, high level must be output first)  3. Power supply VCC (PIN40) and ground GND (PIN20); 
[Microcontroller]
STC12C5A60S2 single-chip dual serial port simultaneous communication complete version of the modbus program
The circuit schematic is as follows: /******************************** Program function: modbus RTU mode setting reads the status of sixteen relays and the settings of the internal holding registers  Hardware test environment: single-chip stc89C52RC sixteen relays 485 interface control board   communication protocol
[Microcontroller]
STC12C5A60S2 single-chip dual serial port simultaneous communication complete version of the modbus program
Single chip microcomputer timer duty cycle button adjustment Proteus simulation program
The simulation schematic is as follows   The microcontroller source program is as follows: #include "reg52.h"  #define uchar unsigned char  #define uint unsigned int  float COUNT100,PWMCOUNT;  /*LCD port definition*/  sbit RS=P3^7;  sbit RW=P3^6;  sbit E=P3^5;  sbit PWM=P1^0;  sbit K1=P3^0; //add 1 sbit K2=P3^1; /
[Microcontroller]
Single chip microcomputer timer duty cycle button adjustment Proteus simulation program
Minimum Deviation Circular Interpolation Algorithm Based on Single Chip Microcomputer Platform
Among the G codes of CNC machine tools, the most common ones are G0, G1, G2, and G3 codes, which represent linear and circular interpolation respectively. Linear interpolation is relatively easy to implement for a single-chip microcomputer. It only needs to convert the displacement increment into a pulse increment and
[Microcontroller]
Volume Control Circuit and Program Design Based on 51 Single Chip Microcomputer
1 Introduction The volume control IC introduced in this article is M62429. There are many similar products on the market, such as FM62429, CD62429, CSC62429, etc. In fact, if you master the programming methods of one or two ICs, it is easy to transplant them to other products with a slight change.
[Microcontroller]
Volume Control Circuit and Program Design Based on 51 Single Chip Microcomputer
59-Non-contact wireless infrared temperature measurement based on 51 single chip microcomputer
Specific implementation functions Specific functions: (1) Use infrared temperature sensor to measure temperature and use LCD1602 to display; (2) The buttons are start button, save button and display data button, which can realize the control of temperature value; (3) The temperature can be sent to the mobile pho
[Microcontroller]
59-Non-contact wireless infrared temperature measurement based on 51 single chip microcomputer
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号