Professional Documents
Culture Documents
Author Message
Joined: 03 Oct 2005 I've seen some requests for infrared receivers on this forum, so here is my code. It works flawlessly with a remote from a SONY KV-21T3K television
Posts: 73
and cheap cloned remotes. The range is very good.
Code:
/******************************************************************/
/* */
/* Infrared Receiver for SONY 12-bit protocol (SIRC) */
/* */
/* Compiler: CCS PCH 3.242 */
/* Processor: PIC18F452 @ 32MHz */
/* IR Sensor: TSOP1738 */
/* Circuit: Standard from TSOP1738 Datasheet, OUT to RB0 */
/* Requires LCD on port D, LED+resistor on RC2 */
/* LED blinks when a command is recognised. */
/* Author: Aurelian Nichita nicksubzero@yahoo.com */
/* Based on: http://www.xs4all.nl/~sbp/knowledge/ir/sirc.htm */
/* */
/******************************************************************/
#zero_ram
/* TIMER0 configuration */
#define TIMER0_CONFIG RTCC_INTERNAL | RTCC_DIV_1
/* Interrupt rate: */
/* 4/32000000*65536*1 = 8.192 ms */
/* */
/* Start: 3.0 ms (ignored) */
/* "1": 1.8 ms (14400) */
/* "0": 1.2 ms (9600) */
#include "lcd.c"
#int_ext
void ext_isr() {
if (irdone) return;
irframes[ircount++] = get_timer0();
if (ircount >= 13)
irdone = TRUE;
set_timer0(0);
enable_interrupts(INT_TIMER0);
}
#int_timer0
void timer0_isr() {
disable_interrupts(INT_TIMER0);
}
#separate
int1 decode_ir(int8 &addr, int8 &cmd) {
int8 i;
int8 mask;
int8 bits[13];
addr = 0;
cmd = 0;
1 от 2 01.3.2007 г. 11:14
CCS :: View topic - Infrared receiver for SONY remotes http://ccsinfo.com/forum/viewtopic.php?t=26226&highlight=inte...
bits[i] = 0x00;
else // Error
return FALSE;
}
mask = 0x01;
for (i=1; i<=7; i++) {
if (bits[i])
cmd = cmd | mask;
mask <<= 1;
}
mask = 0x01;
for (i=8; i<=12; i++) {
if (bits[i])
addr = addr | mask;
mask <<= 1;
}
return TRUE;
}
void start_ir() {
memset(irframes, 0x00, sizeof(irframes));
ircount = 0;
irdone = FALSE;
}
void main() {
int8 addr, cmd;
int1 ok;
delay_ms(100);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
set_tris_a(0b11111111);
set_tris_b(0b11111111);
set_tris_c(0b11111011); // PIN_C2 used for the LED
set_tris_d(0b00000000); // LCD
set_tris_e(0b11111111);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
lcd_init();
output_bit(PIN_C2, 0);
delay_ms(100);
lcd_putc("\fWaiting...");
setup_timer_0(TIMER0_CONFIG);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED, 255, 1);
setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
ext_int_edge(0, L_TO_H);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
start_ir();
#ignore_warnings 203
while(TRUE) {
#ignore_warnings NONE
if (irdone) {
ok = decode_ir(addr, cmd);
printf(lcd_putc, "\fCmd %3u\nAddr %3u", cmd, addr);
if (!ok)
lcd_putc(" ERROR");
else
output_bit(PIN_C2, 1);
delay_ms(50);
output_bit(PIN_C2, 0);
start_ir();
}
}
}
Page 1 of 1
2 от 2 01.3.2007 г. 11:14