1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

JuneBug SIRC RX - Complete

Discussion in 'Microcontrollers' started by UTMonkey, Jan 14, 2008.

  1. UTMonkey

    UTMonkey New Member

    Joined:
    Oct 16, 2007
    Messages:
    450
    Likes:
    3
    Location:
    Chesterfield, Derbyshire - UK
    Hi All,

    Thanks to the great advice given from this site I utilised the IR module of my JuneBug to detect SIRC codes. I have included the completed the code below - Thanks to all who helped!

    How it works
    The IR receiver has its output put into RB0, I configured INT0 on this port to trigger on the falling edge - or the beginning of a pulse.

    When this happens the code clears TMR0L and enables the timer, the INT0 interrupt is then altered to fire upon the rising edge.

    When this occurs TIMER0 is disabled and a reading is taken from TMR0L.

    Taking TMR0L readings from as far away as 10 meters, gives me.
    0x12-0x14 = for a StartBit
    0x9 - 0xB = for a "one" bit
    0x4 - 0x6 = for a "zero" Bit

    What the code doesnt do
    1. Nigel Goodwin's tutorials indicate the possibility of "extended" SIRC codes, these have not been accomodated into this code (but could be).

    2. does not have any output routines, sorry I just wanted to check the IR functionality and so I havent patched this into any LCD code etc.

    How to get it working
    Best to run from MPLAB IDE in debug mode, when first run LED1 will light indicating it is ready to receive a code, when you fire a SIRC code at it LED2 should light indicating it has received a valid code.

    At this stage, stop the debugger and inspect variables CommandCode and DeviceCode you should see the relevant 7 and 5 bit values.

    To start again, run the debugger and press button 2 on the JuneBug to begin again.



    Code (text):

        list    p=18F1320
        include <p18F1320.inc>
    ;   include <Macros.inc>  - Dont need
        CONFIG  OSC = INTIO1, WDT = OFF, LVP = OFF, DEBUG = ON

    btDone  equ 0x0
    btStart equ 0x1
    btCommand   equ 0x2


        cblock  0x00
            Time2
            BitCount
            DeviceCode
            CommandCode
            IRStatus
            Bit
            ScratchCode

        endc       
        org 0x0000
        goto    Init

    ;****************************
    ;* Interrupt Service        *
    ;****************************

        org 0x0008

    ISR
        bcf LATA, RA0
        bcf LATA, RA6
        btfsc   IRStatus, btDone    ;Has a code already been read?
        goto    ISR_Done
        btfsc   INTCON2, INTEDG0    ;Check if falling or rising signal
        goto    Rise
        btg INTCON2, INTEDG0

        clrf    TMR0L       ;Reset Timer

        movlw   b'11000111' ;Enable timer 0, use 8bit counter, use 256 prescaler
        movwf   T0CON       ;Do it
       

        goto    ISR_Done
    Rise

        clrf    T0CON       ;Disable timer 0
        movff   TMR0L, Time2    ;Put timer 0 value into storage
        btg INTCON2, INTEDG0

        nop
    GetPulseWidth
        movlw   0x11        ;Check for start pulse,
                    ;should be between 0x12 and 0x14
        cpfsgt  Time2       ;Greater than 0x11?
        goto    CheckZero
        movlw   0x15
        cpfslt  Time2       ;Less than 0x15?
        goto    IRError     ;Start Bit exceeds tolerance
       
    StartBit                ;We have a Start Bit
        btfsc   IRStatus, btStart   ;Check if already set (ERROR)
        goto    IRError
        bsf IRStatus, btStart   ;Flag IRStatus

        movlw   0x7
        movwf   BitCount
        goto    ISR_Done
    CheckZero
        movlw   0x3     ;Check for zero pulse
                    ;Should be between 0x4 and 0x6
        cpfsgt  Time2       ;Greater than 0x3?
        goto    IRError
        movlw   0x7
        cpfslt  Time2       ;Less than 0x7?
        goto    CheckOne
    ZeroBit             ;We have a zero bit
        btfss   IRStatus, btStart   ;Check if we have previously had Start Bit
        goto    IRError
        movlw   0x00
        movwf   Bit
        call    ProcessBit
        goto    ISR_Done
    CheckOne
        movlw   0x8     ;Check for One Pulse
                    ;Should be between 0x9 and 0xB
        cpfsgt  Time2       ;Greater than 0x8?
        goto    IRError
        movlw   0xC
        cpfslt  Time2       ;Less than 0xC
        goto    IRError

    OneBit              ;We have a One bit
        btfss   IRStatus, btStart   ;Check if we have previously had Start Bit
        goto    IRError
        movlw   0x1
        movwf   Bit
        call    ProcessBit
        goto    ISR_Done


    IRError
        call ClearVariables
        bcf INTCON2, INTEDG0    ; Interrupt on falling edge

    ISR_Done   
        clrf    LATA
        bcf INTCON, INT0IF

        retfie  FAST   


    ;****************************
    ;* End of Interrupt Service *
    ;****************************

    ;*******************
    ;* Routines        *
    ;*******************
    ProcessBit
        btfss   Bit,0       ;Check if bit set
        goto    WriteZero
    WriteOne
        bsf ScratchCode, 0x7
        goto    RotateRight
    WriteZero
        bcf ScratchCode, 0x7
    RotateRight
        rrncf   ScratchCode
        decfsz  BitCount
        return
    WhichCode
        btfss   IRStatus, btCommand
        goto    CommandCodeDone
    DeviceCodeDone
        rrncf   ScratchCode ;a couple of more rotations
        rrncf   ScratchCode
        movff   ScratchCode, DeviceCode
        bsf IRStatus, btDone
        return
    CommandCodeDone
        movff   ScratchCode, CommandCode
        bsf IRStatus, btCommand
        movlw   0x5
        movwf   BitCount
        clrf    ScratchCode
        return



    ClearVariables
        clrf    IRStatus
        clrf    DeviceCode
        clrf    CommandCode
        clrf    ScratchCode
        return


    ;****************************
    ;* End of Routines          *
    ;****************************



    Init   
        call    ClearVariables
        bsf OSCCON, IRCF2   ;Set Internal 8MHz
        bsf OSCCON, IRCF1
        bsf OSCCON, IRCF0

        bcf ADCON1, PCFG0   ; Set RA0 as digital
           
        bsf TRISB, RB0  ; Make RB0 Input
        bsf TRISB, RB2  ; Make RB5 Input
        bcf INTCON2, RBPU

        bsf ADCON1, PCFG4   ; Make RB0 digital

       
        movlw   b'10111110'
        movwf   TRISA

        bcf LATA, RA0
        bcf LATA, RA6
       
        clrf    T0CON
       
    InitISR
        movlw   0x13
        sublw   0x13


        bcf INTCON2, INTEDG0    ; Interrupt on falling edge

        bsf INTCON, INT0IE  ; Enable INT0 interrupt
        bcf RCON, IPEN  ; Disable priority interrupts
        bcf INTCON, INT0IF  ; Clear INT0 flag - This stumped me at first
        bcf INTCON, PEIE    ; Disable peripheral interrupts

        bsf INTCON, GIE ; Enable global Interrupt
       
       
    Main
        bsf LATA, RA0       ;Light LED 1
        bcf LATA, RA6
        btfss   IRStatus, btDone
        goto    Main

        bcf LATA, RA0       ;Light LED 2
        BSF LATA, RA6
       
    WaitForRB2


        btfsc   PORTB, RB2
        goto    WaitForRB2
        call    ClearVariables
        goto    Main
        end
     
     
    Last edited: Jan 15, 2008
    • Like Like x 1
  2. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Wow, you beat me to it UTMonkey. Nice coding.

    Where does one get "Macros.inc"?
     
    Last edited: Jan 14, 2008
  3. UTMonkey

    UTMonkey New Member

    Joined:
    Oct 16, 2007
    Messages:
    450
    Likes:
    3
    Location:
    Chesterfield, Derbyshire - UK
    Hi Bill,

    You can remove that line, it links to the charlie plexing macros (you wrote actually) but I didnt get to use them.

    Regards

    Mark
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada

    May I make a couple of observations
    1. Change INTIO1 to INTIO2
    2. Use "bra" instead of "goto" (short 1K branch)

    Nice use of the compare instructions, so handy to have.
    I noticed you had fun with 18F interrupts too, they can be a little confusing at first. "retfie fast" is a sneaky one for anyone used to 16F PICs.
     
    Last edited: Jan 15, 2008
  6. UTMonkey

    UTMonkey New Member

    Joined:
    Oct 16, 2007
    Messages:
    450
    Likes:
    3
    Location:
    Chesterfield, Derbyshire - UK
    Okeydoke, thanks.

    Its been an enjoyable learning curve.
     
  7. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    I'm learning the 18F too, always something new to learn. A real nice PIC family to work with.
     
  8. ex-navy

    ex-navy New Member

    Joined:
    Jan 28, 2008
    Messages:
    29
    Likes:
    0
    Location:
    Toronto Canada
    Would it work here

    Hi UTMonkey,

    I have also purchased a Junebug. Great little Tutor/Programmer.

    I bought it for the exact same purpose. To receive SIRC codes (or very near SIRC code).

    I have a Neuros OSD digital recorder that uses what looks like Sony SIRC protocol. See here, pages 5 & 6 of the link below:

    http://www.electro-tech-online.com/custompdfs/2008/01/OSD20REMOTE20060711.pdf

    The IR is fairly close to Sony Sirc.

    I wonder if your code would work?
     
  9. ex-navy

    ex-navy New Member

    Joined:
    Jan 28, 2008
    Messages:
    29
    Likes:
    0
    Location:
    Toronto Canada
  10. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    That sure looks like it'll works. There is tonnes of code for SIRCs and PICs on the net. The Sony transmits at 40kHz but the 38kHz detector on the Junebug will see it fine (digikey sells the 40kHz Vishay IR detector). UTMonkeys code should work fine, do you know how to use the MPLAB debugger?
    And thanks for the compliment.
     
  11. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Looks like fun. Guess I need to get a Digikey order together and get myself a TSOP34838 for my Junebug. :)
     
  12. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
  13. UTMonkey

    UTMonkey New Member

    Joined:
    Oct 16, 2007
    Messages:
    450
    Likes:
    3
    Location:
    Chesterfield, Derbyshire - UK
    Hi Ex-Navy,

    I agree with Bill, it is SIRC.

    If the start bit takes 2.4ms then a "one" bit is half that (1.2ms) and then "zero" is 0.6.

    It may be a bit reckless but I was thinking of altering my code to assume the codes it would receive would always be SIRC. That way the first bit could be "measured" and the 1 and 0 bits could be deduced from that, rather than expecting a particular timing.

    Good Luck

    Mark
     
  14. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,331
    Likes:
    653
    Location:
    Derbyshire, UK
    I don't really see the point?, if it's SIRC the timing is already known (bearing in mind the width of the pulses isn't accurately transferred by the IR link), and if it's not SIRC then it's completely different anyway.
     
  15. UTMonkey

    UTMonkey New Member

    Joined:
    Oct 16, 2007
    Messages:
    450
    Likes:
    3
    Location:
    Chesterfield, Derbyshire - UK
    There probably is'nt, and I have moved on since I developed that code.

    This is only for development, so the only "point" would be for me to enhance my knowledge, and lets face it - the journey involved in enhancing my knowledge is bound to lead me up some blind alleys. but that will have taught me something as well! ;)

    All the very best!
     
  16. ex-navy

    ex-navy New Member

    Joined:
    Jan 28, 2008
    Messages:
    29
    Likes:
    0
    Location:
    Toronto Canada
    Learning MPLAB, Sirc Rx and Junebug

    Hi guys,

    My forehead hurts as I have been reading reading and more reading.

    I have managed to learn how to create a project in MPLAB 8.0, select
    the 18f1320, select the programmer Pickit 2 for the Junebug, etc

    I added the lkr file 18f1320.lkr

    Added the sirc rx.asm file to the project, saved the project and selected build all.

    Here is the output:

    Clean: Deleting intermediary and output files.
    Clean: Deleted file "C:\my projects\sony ir.o".
    Clean Warning: File "C:\my projects\C:\Documents and Settings\Darren\Desktop\sony ir.lst" doesn't exist.
    Clean: Done.
    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p18F1320 "C:\Documents and Settings\Darren\Desktop\sony ir.asm" /l"sony ir.lst" /e"sony ir.err" /o"sony ir.o"
    Warning[205] C:\DOCUMENTS AND SETTINGS\DARREN\DESKTOP\SONY IR.ASM 1 : Found directive in column 1. (list)
    Executing: "C:\Program Files\Microchip\MPASM Suite\MPLink.exe" "C:\Program Files\Microchip\MPASM Suite\LKR\18f1320.lkr" "C:\my projects\sony ir.o" /o"18f1320proj.cof" /M"18f1320proj.map" /W
    MPLINK 4.14, Linker
    Copyright (c) 2007 Microchip Technology Inc.
    Error - section '.org_1' can not fit the absolute section. Section '.org_1' start=0x00000008, length=0x0000011e
    Errors : 1

    Link step failed.
    BUILD FAILED: Thu Jan 31 22:56:49 2008

    Probably something simple as my forehead pain is hurting too much.

    Any suggestions, kicks.....................

    Thanks
     
  17. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    It's not relocatable code so don't use the linker. Run the wizard and include only the .asm source file. It'll assemble fine.
     
  18. ex-navy

    ex-navy New Member

    Joined:
    Jan 28, 2008
    Messages:
    29
    Likes:
    0
    Location:
    Toronto Canada
    Thanks Bill!!!
    I will have to learn what is meant by relocatable code.

    Now to learn how to run the debugger.
     
  19. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Relocateable code does not have fixed addresses, slightly different syntax.
     
  20. ex-navy

    ex-navy New Member

    Joined:
    Jan 28, 2008
    Messages:
    29
    Likes:
    0
    Location:
    Toronto Canada
    Programmed and how to test JUnebuy sirc ir.asm properly

    I ran the program on the Junebug, got the LED1 and LED2 to illuminate when I fired a SIRC signal at it.

    Am I correct in assuming iaw a debugger that I select debugger>select tool>MPLAB SIM

    How do I read the contents of DEVICECODE & COMMANDCODE?
     
    Last edited: Feb 1, 2008
  21. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    On reset LED1 lights and on a valid code LED2 will light. Open a View/Watch window and add symbols CommandCode and DeviceCode.
    Vaild 12bit codes will show there.

    An enhancement to the program would be to send the codes out the EUSART and you could view them with the UART tool.
     

    Attached Files:

Share This Page