#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; } } }
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
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Embedded Linux Zero-Based Introductory Learning Materials Limited Time Free Credit Download
- ESP32-S2-Kaluga-1 Espressif's new multimedia development board
- Get a more intuitive understanding of 5G. Watch the keynote speech by Rohde & Schwarz and win prizes!
- msp432 record 2-uart and display usage
- [AB32VG1 Development Board Review] RTC Electronic Clock
- Live broadcast at 10 am today [In-depth study of TI's industrial processor chip AM57X with machine learning accelerator]
- Protecting Your IP Cores - Part 1 Soft IP, Section 1: Encryption of HDL Code
- EEWorld 2020 annual summary: useful articles, popular reviews, download top list, and well-received courses, all here!
- Hey guys, this is my first time using Cadence's Pspice simulation and I encountered this problem
- How to use Bluetooth 4.2 to implement the Internet of Things