Autor Tema: Emisor Sony IR problemas TIMERS  (Leído 2128 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Sepumail

  • PIC10
  • *
  • Mensajes: 2
Emisor Sony IR problemas TIMERS
« en: 28 de Abril de 2005, 00:30:00 »
Estoy intentando hacer un emisor y receptor de IR con la codificación de sony el receptor está logrado, pero el emisor me tiene loco. Soy nuevo en esto y eso de los timers se me vá de las manos. Tengo un codigo para el pic16f84 ke genera la señal IR de sony, pero como yo estoy usando el pic16f876, le cambio el modelo de pic y no manda los pulsos IR como debe de mandarlos ¿varian los tiempos de un modelo a otro? ¿cual sería el fallo? muchas gracias!!!

Codigo:

// sony.c
// Version 1.0
//
//
// This program is designed to run on an iRX 2.1 board.  It loops,
// waiting for incoming serial or IR data.  If the incoming data is
// serial, it reads three hexadecimal bytes and transmits those bytes
// using "Sony style" encoding.  If the incoming data is infrared,
// it reads a 12 bit string of IR data and, if valid, prints it to
// the serial output as a hexadecimal string.
//
// Sony IR Encoding
//
// Sony Remote Controls use a 40Khz modulated IR pulse stream.  Each
// 12 bit command starts with a header followed by 12 data bits, LSB
// first.
//
// Header: 2.4 mSec of pulses (96 pulses) then .6 mSec of darkness.
// 1 bit:  1.2 mSec of pulses (48 pulses) then .6 mSec of darkness.
// 0 bit:  0.6 mSec of pulses (24 pulses) then .6 mSec of darkness.
#include <16F876.H>
#fuses HS,NOWDT,NOPROTECT,PUT
#use DELAY(clock=10000000)


#use fast_io(A)
#use fast_io(B)
#use fast_io(C)

#include <lcd.c>

#define IZQ        PIN_A0
#define DER        PIN_A1
#define OK         PIN_A2
#define ARR        PIN_A3
#define ABJ        PIN_A4
#define IR_LED     PIN_C5


// Default TRIS bits: RS232_RCV and IR_SENSOR are inputs, all
// others are outputs
//
#define IRX_C_TRIS      0b00000000

// ===================================================================
// General definitions
//

#byte TMR0 = 0x01

struct {
  short int RBIF;
  short int INTF;
  short int T0IF;
  short int RBIE;
  short int INTE;
  short int T0IE;
  short int PEIE;
  short int GIE;
} INTCON;
#byte   INTCON   = 0x0B

// ===================================================================
// SONY IR definitions

// Sony uses 40Khz AM pulses.  One 40Khz period is
// 25 uSec, or 62 RTCC tics at 400 nSec per tic.
//
#define TICS_25US   62

// set the timer to roll over in given number of tics
//
#define SET_TMR0(tics)      
  TMR0 = 256+2-tics;      
  INTCON.T0IF = 0

// update the timer to roll over in the given number of tics
// (including compensation for tics already overshot)
//
#define UPDATE_TMR0(tics)   
  TMR0 += 256+2-tics;      
  INTCON.T0IF = 0

// Wait for RTCC to roll over, then set it to roll
// over at <tics> RTCC tics in the future.
//

#define WAIT_FOR_TMR0(tics)   
  while (!INTCON.T0IF) ;   
  UPDATE_TMR0(tics)

// buffer for data received or sent
//
long int gCmd;

// ===================================================================
// sending SONY IR
//
// These routines use RTCC (aka TMR0) via the SET_TMR0 and UPDATE_TMR0
// macros to maintain accurate 40Khz timing.

// Send npulses of 40Khz pulses
//
void send_pulse(int npulses) {
  do {
    WAIT_FOR_TMR0(TICS_25US);
    output_low(PIN_C5);
    delay_us(4);
    output_high(PIN_C5);
  } while (--npulses);
}

// Send 600 uSec of "silence"
//
void send_silence600() {
  int i;
  i=24;
  do {
    WAIT_FOR_TMR0(TICS_25US);
  } while (--i);
}

void send_command(long int c) {
  int i;

  SET_TMR0(TICS_25US);      /* prime the timer */


  i=12;            // 12 data bits
  send_pulse(96);      // send header: 96 pulses on, 16 off
  send_silence600();
  do {
    if (shift_right(&c, 2, 0)) {
      send_pulse(48);      // send one bit: 48 pulses on, 24 off
      send_silence600();
    } else {
      send_pulse(24);      // send zero bit: 24 pulses on, 24 off
      send_silence600();
    }
  } while (--i);

}


// ===================================================================
// main()
//

main() {
 
   setup_port_a(NO_ANALOGS);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_tris_a(0xFF); //Port_a Entradas//
   set_tris_b(0b00000001); //Port_b Salidas //
   set_tris_c(IRX_C_TRIS);
   setup_counters(RTCC_INTERNAL, WDT_18MS);   // 400 nSec per tic
   ext_int_edge(H_TO_L);      // watch for leading edge of IR pulse

  lcd_init();
 
  output_low(PIN_C5);
  delay_ms(2000);
  output_high(PIN_C5);
 


  while (1) {

     send_command(0b110101010101);   // send 12 bits via IR using Sony code
     delay_ms(1500);
     send_command(0b110110110110);   // send 12 bits via IR using Sony code
     delay_ms(1500);

    }
 
}