Simple RC5 code Send & receive

General discussion on mikroBasic.
Post Reply
Author
Message
juliovmd
Posts: 119
Joined: 02 Feb 2006 11:14
Location: Spain
Contact:

Simple RC5 code Send & receive

#1 Post by juliovmd » 13 Sep 2006 18:26

This code is a modification of the original one in www.picbasic.nl. I have used it and works perfect.

To adapt for the crystal that you need

4 Mhz: Glitch = 52 ; Ok = 50
8 Mhz: Glitch = 104 ; Ok = 100
12 Mhz: Glitch = 156 ; Ok = 150
16 Mhz: Glitch = 208 ; Ok = 200
20 Mhz: Glitch = 260 ; Ok = 250

Code to receive:

Code: Select all

'Programa receptor de codigos PHILIPS RC5***************************************
'Receptor de codigos IR con protocolo Philips RC5 para Mikrobasic 5.0.0.0
'Codigo original extraido de: http://www.picbasic.nl
'Modificado y verificado por: Julio Montagut
'Web: http://www.jvmbots.com
'
'-HARDWARE:
'    MCU: PIC 16F84A
'    Cristal: 4 Mhz
'    8- Diodos LED conectados a PORTB
'    1- Receptor IR TSOP1736 conectado a PORTA.0
'*******************************************************************************

const Glitch = 52
const Ok = 50

Dim BD1            as Byte
Dim Inicio_Bit_1   as Byte
Dim Inicio_Bit_2   as Byte
Dim Toggle_Bit     as Byte
Dim ComandoAnt     as Byte
Dim Comando        as Byte
Dim SistemaAnt     as Byte
Dim Sistema        as Byte

'Rutina de lectura del Receptor IR (PORTC.0)
Sub function Bit_IR as Byte
  result = PORTA.0
	BD1 = 0
	WHILE PORTA.0  = result  And (BD1 < Ok)
    Inc(BD1)
  WEND

	IF Ok = BD1 THEN
    GOSUB ErrorReceive
  End if
	Delay_us(1100)
  exit

ErrorReceive:
  Delay_ms(10)
  Comando = ComandoAnt        'Restauramos el Comando que habia antes del error
  Sistema = SistemaAnt        'Restauramos el Sistema que habia antes del error
End sub


Main:
  TRISA.0 = 1
  PORTA.0 = 0
  TRISB = 0
  PORTB = 0
  'Reset de variables
  Inicio_Bit_1=0
  Inicio_Bit_2=0
  Toggle_Bit=0
  Comando=0
  Sistema=0

Inicio:
  While true
    BD1 = 0
    WHILE PORTA.0 = 1
      Comando=0
      Sistema=0
    WEND

    WHILE (PORTA.0=0) And (BD1 <Glitch> Glitch THEN
      Delay_ms(100)
      GOTO Inicio
    End if

    ComandoAnt = Comando        'Guarda los valores anteriores por si hay algun error
    SistemaAnt = Sistema

    Delay_us(200)               '200us de respiro para el procesamiento del primer dato

    Inicio_Bit_1.0=Bit_IR       'Bits de inicio
    Inicio_Bit_2.0=Bit_IR
    Sistema.4=Bit_IR            'Bits de Sistema
    Sistema.3=Bit_IR
    Sistema.2=Bit_IR
    Sistema.1=Bit_IR
    Sistema.0=Bit_IR
    Comando.5=Bit_IR            'Bits de comando
    Comando.4=Bit_IR
    Comando.3=Bit_IR
    Comando.2=Bit_IR
    Comando.1=Bit_IR
    Comando.0=Bit_IR

    'Bit de Toggle
    Toggle_Bit = Toggle_Bit xor 1

    'Presentamos la informacion de comando recibida
    PORTB=Comando

    Delay_ms(10)
  Wend
End.

juliovmd
Posts: 119
Joined: 02 Feb 2006 11:14
Location: Spain
Contact:

Send RC5 code

#2 Post by juliovmd » 13 Sep 2006 18:31

It sends the code RC5 that corresponds to the 13. It modifies the parameters Command_Z and System_Z for your application.

Code: Select all

program Envio_RC5

'Programa Emisor de codigos PHILIPS RC5*****************************************
'Emisor de codigos IR con protocolo Philips RC5 para Mikrobasic 5.0.0.0
'Codigo original extraido de: http://www.picbasic.nl
'Modificado y verificado por: Julio Montagut
'Web: http://www.jvmbots.com
'
'-HARDWARE:
'    MCU: PIC 16F877A
'    Cristal: 4 Mhz
'    1- Diodo LED-IR conectado a PORTC.1
'*******************************************************************************

Dim Command_Z as byte
Dim Systeem_Z as byte
Dim ToggBit_Z as byte
Dim Cycl_1 as byte
Dim Cycl_2 as byte

Main:
  ADCON1=6
  TRISC=%00000001
  TRISD=0

  'Reset de variables
  Cycl_1 = 0
  Cycl_2 = 0

  'Seteo del envio (Comando 16 = + Volumen)
  Command_Z=16
  Systeem_Z=0
  ToggBit_Z=0

  While true
    Gosub RC5_Code                         'Envio del codigo RC5
    Delay_ms(1000)                         'Delay de un segundo
  Wend

RC5_Code:                                  'Envio completo del codigo RC5
  Asm
     call    TxEenRC5                      'Cabecera

     btfss   _Command_Z,6       'Bit de comando invertido
     call    TxEenRC5
     btfsc   _Command_Z,6
     call    TxNulRC5

     'Togglebit
     btfsc   _ToggBit_Z,0
     call    TxEenRC5
     btfss   _ToggBit_Z,0
     call    TxNulRC5

     'Datos de los bits de Sistema (4,3,2,1,0)
     btfsc   _Systeem_Z,4
     call    TxEenRC5
     btfss   _Systeem_Z,4
     call    TxNulRC5

     btfsc   _Systeem_Z,3
     call    TxEenRC5
     btfss   _Systeem_Z,3
     call    TxNulRC5

     btfsc   _Systeem_Z,2
     call    TxEenRC5
     btfss   _Systeem_Z,2
     call    TxNulRC5

     btfsc   _Systeem_Z,1
     call    TxEenRC5
     btfss   _Systeem_Z,1
     call    TxNulRC5

     btfsc   _Systeem_Z,0
     call    TxEenRC5
     btfss   _Systeem_Z,0
     call    TxNulRC5

     'Datos del bit de Comando (5,4,3,2,1,0)
     btfsc   _Command_Z,5
     call    TxEenRC5
     btfss   _Command_Z,5
     call    TxNulRC5

     btfsc   _Command_Z,4
     call    TxEenRC5
     btfss   _Command_Z,4
     call    TxNulRC5

     btfsc   _Command_Z,3
     call    TxEenRC5
     btfss   _Command_Z,3
     call    TxNulRC5

     btfsc   _Command_Z,2
     call    TxEenRC5
     btfss   _Command_Z,2
     call    TxNulRC5

     btfsc   _Command_Z,1
     call    TxEenRC5
     btfss   _Command_Z,1
     call    TxNulRC5

     btfsc   _Command_Z,0
     call    TxEenRC5
     btfss   _Command_Z,0
     call    TxNulRC5
  return

'Envio de un 0 (on-off)
TxNulRC5:
  call    LichtAanRC5
  call    LichtUitRC5
return

'Envio de un 1 (off-on)
TxEenRC5:
  call    LichtUitRC5
  call    LichtAanRC5
return

'LED-IR Apagado (OFF)
LichtUitRC5:
  movlw   102                           '102 ciclos de delay (Xtal:4Mhz)
  movwf   _Cycl_2
LichtUit2RC5:
  nop
  nop
  nop
  nop
  nop
  nop
  decfsz  _Cycl_2,f
  goto    LichtUit2RC5
return

'LED-IR encendido (On-Pulso)
LichtAanRC5:
  movlw   32                            '32 ciclos rapidos del LED-IR para un 1 sobre el codigo RC5
  movwf   _Cycl_1
'32 Ciclos de pausa y pulso
LichtAan1RC5:
  'Pausa
  nop
  movlw   6                            'Tiempo (para Xtal:4MHz) de pausa entre pulsos del LED-IR
  movwf   _Cycl_2
LichtAan2RC5:
  decfsz  _Cycl_2,f
  goto    LichtAan2RC5
  'Pulso
  bsf     PORTC,1                      'LED-IR encendido
  movlw   2                            'Tiempo encendido (Xtal:4MHz)
  movwf   _Cycl_2
LichtAan3RC5:
  decfsz  _Cycl_2,f
  goto    LichtAan3RC5
  bcf     PORTC,1                      'LED-IR Apagado
  decfsz  _Cycl_1,f
  goto    LichtAan1RC5
  return
End asm
End.
I hope that he is useful :wink:

Regards

Naveen Tyagi
Posts: 229
Joined: 16 Dec 2005 11:44
Location: India
Contact:

#3 Post by Naveen Tyagi » 14 Sep 2006 05:32

Thanks
[color=red]Tyagi[/color]

mpavlica
Posts: 161
Joined: 02 Jan 2006 19:18

#4 Post by mpavlica » 15 Sep 2006 09:11

Hello!
Could you please comment source on english (for receiver(?
I am not sure that i undestand it well...
Milan Pavlica YU7XW

Post Reply

Return to “mikroBasic General”