主程序如下:
/************************************************************
* Program: AVRGCC - RC5 Test
* Comments: RC5 decoder
* target mcu: 8515
***********************************************************/
#include
#include
#include
#include "lcd.h"
unsigned int heartbeat=0;
// RC5 variables & definitions
#define PRSC1_SELECT 2 // timer CLK/8
#define TRUE 1
#define FALSE 0
#define IRDet bit_is_set(PIND,PD2)
volatile unsigned char RC5RxAddress, RC5RxCommand;
volatile unsigned char RC5Avail=0, RC5Repeat, RC5PhaseErr;
void delay(unsigned int p)
{
unsigned int i;
unsigned char j;
for(i=0;i for (j=0;j<100;j++);
}
/* signal handler for timer1 overflow interrupt */
SIGNAL(SIG_OVERFLOW1)
{
static unsigned char RC5BitCnt=0;
static unsigned int RC5Data;
static unsigned char OldValue;
static unsigned char ControlBit;
// Load Timer again...
outp(0xFC, TCNT1H); // set TCNT1 // 889us
outp(0x87, TCNT1L); // set TCNT1
if (RC5BitCnt==0) {
RC5Data=0;
OldValue=IRDet;
RC5PhaseErr=FALSE;
}
if (RC5BitCnt<24) { // sample 24 bits
if (RC5BitCnt & 0x01) {
if (OldValue!=IRDet) { // check for Biphase error
RC5Data <<= 1; // next bit;
if (IRDet) RC5Data &= 0xFFFE; else RC5Data |= 0x0001;
} else RC5PhaseErr=TRUE; // biphase error
}
RC5BitCnt++;
OldValue=IRDet; // remember signal level
} else { // if >24 then finish
outp(0 , TCCR1B); // stop timer 1
RC5BitCnt=0; // prepare for next time
if (!RC5PhaseErr && !RC5Avail) {
RC5Repeat=ControlBit==((RC5Data & 0x0800)==0x0800); // if new controlbit=old then RC5Repeat=1
ControlBit=((RC5Data & 0x0800)==0x0800); // new controlbit
RC5RxAddress=(unsigned char)((RC5Data & 0x07C0)>>6);
RC5RxCommand=(unsigned char)(RC5Data & 0x003F);
RC5Avail=TRUE; // RC5 code is vailable...
}
RC5PhaseErr=FALSE;
outp(0x40, GIMSK);
}
}
/* signal handler for external interrupt int0 */
SIGNAL(SIG_INTERRUPT0)
{
outp(0x00, GIMSK); // disable external int0
outp(0xF3, TCNT1H); // set TCNT1 // 3111 us
outp(0xD9, TCNT1L); // set TCNT1
outp(PRSC1_SELECT, TCCR1B); // start timer1
}
/* initialize RC5 decoder */
void RC5_init(void)
{
cbi(DDRD,PD2);
sbi(PORTD,PD2);
outp((1<
outp(0x40, GIMSK); // enable external int0
outp(0x80, TIMSK); // enable timer1 Overflow interrupts
outp(0x00, TCCR1A); // disable PWM and stuff
outp(0x00, TCCR1B);
outp(0xF3, TCNT1H); // set TCNT1 // 3111 us
outp(0xD9, TCNT1L); // set TCNT1
}
/* main routine for testing */
int main(void)
{
sei(); // enable interrupts
RC5_init();
lcd_init();
lcd_control(1,0,0);
lcd_putstr("RC5 decoder");
lcd_goto(0x49);
lcd_putstr("PErr=");
while(1)
{
lcd_goto(0x0E);
printhex(heartbeat++); // heart beat :)
if (RC5Avail) { // display new RC5 data
lcd_goto(0x40);
printhex(RC5Repeat);
lcd_putch(' ');
printhex(RC5RxAddress);
lcd_putch(' ');
printhex(RC5RxCommand);
RC5Avail=FALSE; // ready to get new one
}
lcd_goto(0x4E);
printhex(RC5PhaseErr); // error check
delay(1000);
}
}
Reference address:Infrared remote control decoding program RC5 decoding
/************************************************************
* Program: AVRGCC - RC5 Test
* Comments: RC5 decoder
* target mcu: 8515
***********************************************************/
#include
#include
#include
#include "lcd.h"
unsigned int heartbeat=0;
// RC5 variables & definitions
#define PRSC1_SELECT 2 // timer CLK/8
#define TRUE 1
#define FALSE 0
#define IRDet bit_is_set(PIND,PD2)
volatile unsigned char RC5RxAddress, RC5RxCommand;
volatile unsigned char RC5Avail=0, RC5Repeat, RC5PhaseErr;
void delay(unsigned int p)
{
unsigned int i;
unsigned char j;
for(i=0;i for (j=0;j<100;j++);
}
/* signal handler for timer1 overflow interrupt */
SIGNAL(SIG_OVERFLOW1)
{
static unsigned char RC5BitCnt=0;
static unsigned int RC5Data;
static unsigned char OldValue;
static unsigned char ControlBit;
// Load Timer again...
outp(0xFC, TCNT1H); // set TCNT1 // 889us
outp(0x87, TCNT1L); // set TCNT1
if (RC5BitCnt==0) {
RC5Data=0;
OldValue=IRDet;
RC5PhaseErr=FALSE;
}
if (RC5BitCnt<24) { // sample 24 bits
if (RC5BitCnt & 0x01) {
if (OldValue!=IRDet) { // check for Biphase error
RC5Data <<= 1; // next bit;
if (IRDet) RC5Data &= 0xFFFE; else RC5Data |= 0x0001;
} else RC5PhaseErr=TRUE; // biphase error
}
RC5BitCnt++;
OldValue=IRDet; // remember signal level
} else { // if >24 then finish
outp(0 , TCCR1B); // stop timer 1
RC5BitCnt=0; // prepare for next time
if (!RC5PhaseErr && !RC5Avail) {
RC5Repeat=ControlBit==((RC5Data & 0x0800)==0x0800); // if new controlbit=old then RC5Repeat=1
ControlBit=((RC5Data & 0x0800)==0x0800); // new controlbit
RC5RxAddress=(unsigned char)((RC5Data & 0x07C0)>>6);
RC5RxCommand=(unsigned char)(RC5Data & 0x003F);
RC5Avail=TRUE; // RC5 code is vailable...
}
RC5PhaseErr=FALSE;
outp(0x40, GIMSK);
}
}
/* signal handler for external interrupt int0 */
SIGNAL(SIG_INTERRUPT0)
{
outp(0x00, GIMSK); // disable external int0
outp(0xF3, TCNT1H); // set TCNT1 // 3111 us
outp(0xD9, TCNT1L); // set TCNT1
outp(PRSC1_SELECT, TCCR1B); // start timer1
}
/* initialize RC5 decoder */
void RC5_init(void)
{
cbi(DDRD,PD2);
sbi(PORTD,PD2);
outp((1<
outp(0x80, TIMSK); // enable timer1 Overflow interrupts
outp(0x00, TCCR1A); // disable PWM and stuff
outp(0x00, TCCR1B);
outp(0xF3, TCNT1H); // set TCNT1 // 3111 us
outp(0xD9, TCNT1L); // set TCNT1
}
/* main routine for testing */
int main(void)
{
sei(); // enable interrupts
RC5_init();
lcd_init();
lcd_control(1,0,0);
lcd_putstr("RC5 decoder");
lcd_goto(0x49);
lcd_putstr("PErr=");
while(1)
{
lcd_goto(0x0E);
printhex(heartbeat++); // heart beat :)
if (RC5Avail) { // display new RC5 data
lcd_goto(0x40);
printhex(RC5Repeat);
lcd_putch(' ');
printhex(RC5RxAddress);
lcd_putch(' ');
printhex(RC5RxCommand);
RC5Avail=FALSE; // ready to get new one
}
lcd_goto(0x4E);
printhex(RC5PhaseErr); // error check
delay(1000);
}
}
Previous article:AVR drive floppy drive motor program
Next article:Temperature sensor 18B20 program under M8, ICCAVR
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- Zhuhai is looking for embedded engineers
- Baud rate adaptation of TI DSP chip SCI module
- Shenzhen Industry Exhibition Recruitment: Product Engineer, Technical Support Engineer
- Thank you for being there + looking forward to EEWORLD in 2020
- Playing with Zynq Serial 38——[ex57] Zynq AXI HP bus bandwidth test
- Discussion about littleFS in Micropython official forum
- [GD32E231 DIY Contest] (Part 3): TIMER2_CH1 PWM output
- Application of LoRa technology and Bluetooth technology in electronic display boards
- SensorTile.boxr's personal experience
- Verilog Tutorial Syntax Details 2004