View Single Post
Old 09-03-2009, 05:06 AM   #2
vietanh
Đệ tử 1 túi
 
Tham gia ngày: Dec 2005
Bài gửi: 20
:
đây là đoan mã ma tui kiếm được trên diễn đàn của ccs của thằng người nhật nichika lasuko ADINOMOTO dùng thử coi, dịnh chế lại làm dk robot bằng tay chắc cũng ok
Code:
/******************************************************************/ 
/*               EM LA NGUOI NHAT CHUA VO   HAI CON   MUON YEU 3 CO VIET NAM                                             */ 
/*      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 */ 
/*                                                                */ 
/******************************************************************/ 
 
#define CLKSPEED 32000000 
#include <18F452.h> 
#fuses H4,NOPROTECT,NOOSCSEN,BROWNOUT,BORV45,NOWDT,WDT128,PUT,NOSTVREN,NODEBUG,NOLVP,WRT,NOCPB,WRTB,WRTC,NOCPD,NOWRTD,NOEBTR,NOEBTRB 
#use delay(clock=CLKSPEED) 
#use fast_io(A) 
#use fast_io(B) 
#use fast_io(C) 
#use fast_io(D) 
#use fast_io(E) 
#ignore_warnings none 
 
#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)           */ 
 
#define ONE_MIN  13400 
#define ONE_MAX  15400 
#define ZERO_MIN 8600 
#define ZERO_MAX 10600 
 
#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() { 
  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; 
 
  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(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(); 
    } 
  } 
}

thay đổi nội dung bởi: namqn, 09-03-2009 lúc 05:57 AM. Lý do: định dạng code
vietanh vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn