View Single Post
Old 09-12-2010, 02:33 AM   #10
toan86
Nhập môn đệ tử
 
Tham gia ngày: Nov 2010
Bài gửi: 4
:
#include <16F877A.h>
#include <def_877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)



/* TIMER0 configuration */
#define TIMER1_CONFIG T1_INTERNAL | T1_DIV_BY_1

/* Interrupt rate: */
/* 4/20000000*65536*1 = 13.1 ms */
/* */
/* Start: 3.0 ms (ignored) */
/* "1": 1.8 ms (9000) */
/* "0": 1.2 ms (6000) */

#define ONE_MIN 8000
#define ONE_MAX 10000
#define ZERO_MIN 5000
#define ZERO_MAX 7000

//#include "lcd.c"


/* irframes[0] (start) will be garbage, ignore it... */
int16 irframes[13];
int8 ircount = 0;
int1 irdone = FALSE;



#int_ext
void ext_isr() {output_bit(PIN_A5, 0);
if (irdone) return;
irframes[ircount++] = get_timer0();
if (ircount >= 13)
irdone = TRUE;
set_timer0(0);
enable_interrupts(INT_TIMER1);
}


#int_timer1
void timer_isr() {
disable_interrupts(INT_TIMER1);
}


#separate
int1 decode_ir(int8 &addr, int8 &cmd) {
int8 i;
int8 mask;
int8 bits[13];

addr = 0;
cmd = 0;

for (i=1; i<=12; i++) {
if ((ONE_MIN <= irframes[i]) && (irframes[i] <= ONE_MAX))
bits[i] = 0x01;
else
if ((ZERO_MIN <= irframes[i]) && (irframes[i] <= ZERO_MAX))
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(0x00);
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);

output_bit(PIN_A5, 1);
//lcd_init();
//output_bit(PIN_C2, 0);
delay_ms(100);

//lcd_putc("\fWaiting...");


setup_timer_1(TIMER1_CONFIG);
setup_timer_2(T2_DISABLED, 255, 1);
ext_int_edge(0, L_TO_H);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
delay_ms(100);
start_ir();
while(TRUE) {

if (irdone) {
ok = decode_ir(addr, cmd);
printf("%u", cmd);
printf("%u", addr);
if (!ok){
printf("1111");}
else
output_bit(PIN_A5, 0);
delay_ms(50);
output_bit(PIN_A5, 1);
start_ir();
}
}
}
cao thủ nào cho mình hỏi là code này thì port nào nhận tín hiệu vào và mình muốn xuất ra port thí phải so sánh với giá trị
Mã lệnh điều khiển TV SONY
Phím Mã lệnh
1 0H
2 1H
3 2H
4 3H
5 4H
6 5H
7 6H
8 7H
9 8H
10 9H
và xuất ra điều khiển relay
xin cảm ơn!
toan86 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn