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

Working with Shift Registers and a PIC help

Discussion in 'Microcontrollers' started by Darkstar64, Sep 4, 2008.

  1. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    wow ive spent all day on this how hard can it be to make this work :mad: here is my code. I want it to read the 8 bit string and then one digit after the other send it out on pin GP0 and then after each digit it needs to clock on GP1 the one im using needs a low to high to clock it please help ive been at this for days I don't know if the engine works or not ether please haha I only want the first 4 LED's to light up on the shift hence the '11110000'


    Code (text):
        list      p=12F509            ; list directive to define processor
        #include <p12F509.inc>        ; processor specific variable definitions

        __CONFIG   _MCLRE_ON & _CP_OFF & _WDT_OFF & _IntRC_OSC

    ; '__CONFIG' directive is used to embed configuration word within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.



        cblock  0x07
    ;***** VARIABLE DEFINITIONS
    count,temp
    D
    CP
    delay
        endc




    ;**********************************************************************
    RESET_VECTOR    CODE   0x3FF      ; processor reset vector

    ; Internal RC calibration value is placed at location 0x3FF by Microchip
    ; as a movlw k, where the k is a literal value.
       
    MAIN    CODE    0x000
        movwf   OSCCAL            ; update register with factory cal value


    start
            movlw   b'11110000'
            movlw   temp
            call    shift
            goto    start
    ;********Subroutines********
    shift
    shift5  movlw   0x08        ;set loop counter
            movwf   count
            movf    INDF,w      ;get a byte
            movwf   temp
    shift1          rrf temp,f      ;rotate ms-bit into carry
            btfsc   STATUS,C    ;is it 0?
            goto    shift2      ;no, shift out a 1
            goto    shift3      ;yes, shift out a 0
    shift2          bsf GPIO,1
            nop
            bsf GPIO,1      ;blip clock
            nop
            bcf GPIO,1
            goto    shift4
    shift3          bcf GPIO,1
            nop
            bsf GPIO,1      ;blip clock
            nop
            bcf GPIO,1
    shift4          decfsz  count,f     ;done 8-bits?
            goto    shift1      ;no, go again
                   retlw 0
           
            END                 ; End of program !!
    [/CODE]
     
    Last edited: Oct 3, 2008
  2. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    I see two things right off the bat:

    1. You haven't set your pins to outputs with TRIS. By default, PIC pins are inputs on reset. To make them outputs you must set them yourself.

    2. You're using the same pin for both data and clock. That won't work.

    Here's your code neatened up and decluttered (so I could read it comfortably). I did not make the fixes listed above. You can do that.

    You don't need to mess around with that reset vector thing or the osc calibration value. I removed that stuff.

    Code (text):
        #include <P12F509.inc>
        __CONFIG _MCLRE_ON & _WDT_OFF & _IntRC_OSC

        cblock  0x07
        count,temp
        endc

        org 0x000
    main    movlw   b'11110000'
        movlw   temp
        call    shift5
        goto    main

    shift5  movlw   0x08        ;set loop counter
        movwf   count
        movf    INDF,w      ;get a byte
        movwf   temp
    shift1  rrf temp,f      ;rotate ms-bit into carry
        btfsc   STATUS,C    ;is it 0?
        goto    shift2      ;no, shift out a 1
        goto    shift3      ;yes, shift out a 0
    shift2  bsf GPIO,1
        nop
        bsf GPIO,1      ;blip clock
        nop
        bcf GPIO,1
        goto    shift4
    shift3  bcf GPIO,1
        nop
        bsf GPIO,1      ;blip clock
        nop
        bcf GPIO,1
    shift4  decfsz  count,f     ;done 8-bits?
        goto    shift1      ;no, go again
        retlw 0
           
        end
     
    Last edited: Oct 3, 2008
  3. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    ok so it would look something like this then o and about the tris I had that done in another part I deleted just forgot to put it back in I guess . I changed the pins is this correct and I added the tris part its just not shown is that everything?

    Code (text):
        #include <P12F509.inc>
        __CONFIG _MCLRE_ON & _WDT_OFF & _IntRC_OSC

        cblock  0x07
        count,temp
        endc

        org 0x000
    main    movlw   b'11110000'
        movlw   temp
        call    shift5
        goto    main

    shift5  movlw   0x08        ;set loop counter
        movwf   count
        movf    INDF,w      ;get a byte
        movwf   temp
    shift1  rrf temp,f      ;rotate ms-bit into carry
        btfsc   STATUS,C    ;is it 0?
        goto    shift2      ;no, shift out a 1
        goto    shift3      ;yes, shift out a 0
    shift2  bsf GPIO,1
        nop
        bsf GPIO,2      ;blip clock
        nop
        bcf GPIO,2
        goto    shift4
    shift3  bcf GPIO,1
        nop
        bsf GPIO,2      ;blip clock
        nop
        bcf GPIO,2
    shift4  decfsz  count,f     ;done 8-bits?
        goto    shift1      ;no, go again
        retlw 0
           
        end
     
    Last edited: Oct 3, 2008
  4. dave

    Dave New Member

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


     
  5. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.

    If you're going to use GPIO2 as an output then you must clear T0CS in the OPTION register (I put the tris line in too) or it just won't work:
    Code (text):
        movlw   b'11001000' ;clear T0CS for output on GPIO2
        option
        movlw   b'00001000' ;set pins to all outs
        tris    GPIO
    Alternately, you could probably use
    Code (text):
        bcf OPTION,T0CS
    I haven't tested that line though. Would have to have a look in the datasheet to see if I had some reason for doing all 8 bits there.
     
    Last edited: Oct 3, 2008
  6. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Cube page on my site (link below) has been updated. There's a YouTube video of it working too. :p
     
  7. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    Ok well thanks alot all give that a try now when I write this code it should only lite up the first 4 LED's right and they will stay on just making sure bc its been doing some weird stuff with some of code ive givin it thanks alot for the help all let you know how it goes
     
  8. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    Ok it just doesn't want to work no matter what I try I am hooking up the Data and Shift right into the PIC on pins GP1 and GP2 can you post a schmatic I could try with my MC74HC595A shift register and some assembler just to test to make sure its working I have tried everything I can shift it manually but when I hook it into the pic nothing happens or something happens just not what I told it to do here is the code I have programmed in it would help if you could give me a small simple test schmatic that I could use I have one but its for manually shifting the register. I don't think ive ever had so much problems with one thing it just won't work I don't get it
     
    Last edited: Oct 4, 2008
  9. skyhawk

    skyhawk New Member

    Joined:
    Feb 9, 2007
    Messages:
    235
    Likes:
    7
    Location:
    Morgantown, WV
    I don't have time to study your code in detail, but are you latching your output?
     
  10. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Then you're doing it wrong. :D

    My schematic wouldn't help you much. I'm using totally different parts.

    Show me your schematic and your code! Then I (and/or others) can probably spot the problem. A photo is sometimes helpful too. Camera set on macro with a steady hand and LOTS of light.

    I could maybe even breadboard up the identical thing for better troubleshooting.
     
  11. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    No im not but its doing it by itself for some unknown reason I don't even have a connection hooked up to it so all try addding a wire to GP4 to latch it it says a low to high will latch it so all test it out fast and tell you what happens ok so my final code is here with the latch at the very bottom is this correct ? and when I post the code into here it comes out weird its not like that in MPLAB


    Code (text):
        #include <P12F509.inc>
        __CONFIG _MCLRE_ON & _WDT_OFF & _IntRC_OSC

        cblock  0x07
        count,temp
        endc

            org 0x000
    main    movlw   b'11001000' ;clear T0CS for output on GPIO2
            option
            movlw   b'00001000' ;set pins to all outs
            tris    GPIO
            movlw   b'11110000'
            movlw   temp
            call    shift5
            goto    main

    shift5  movlw   0x08        ;set loop counter
            movwf   count
            movf    INDF,w      ;get a byte
            movwf   temp
    shift1  rrf temp,f      ;rotate ms-bit into carry
            btfsc   STATUS,C    ;is it 0?
            goto    shift2      ;no, shift out a 1
            goto    shift3      ;yes, shift out a 0
    shift2  bsf GPIO,1
            nop
            bsf GPIO,2      ;blip clock
            nop
            bcf GPIO,2
            goto    shift4
    shift3  bcf GPIO,1
            nop
            bsf GPIO,2      ;blip clock
            nop
            bcf GPIO,2
    shift4  decfsz  count,f     ;done 8-bits?
            goto    shift1      ;no, go again
            bcf     GPIO,4
            bsf     GPIO,4
            retlw 0
           
        end
     
    Last edited: Oct 4, 2008
  12. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    That sounds right. Darkstar's code was NOT latching.

    Darkstar, the reason my code has no latching is because the shift register I use auto-latches. The leading 1 is pushed into the latch by the 36th bit shifted. A 74HC595 must be manually latched.
     
  13. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    It may be floating and just kinda sometimes latching by itself.

    Put a NOP between those two, or you may have a problem.
    Code (text):
        bcf     GPIO,4
        bsf     GPIO,4
        retlw 0
           
        end
     
  14. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    Ya at the bottom of my code is the latching now just before retlw 0 but it still doesn't want to work haha
     
  15. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    Ok it seems to be working but the LED's are wrong for some reason here is what it is happening '10011000' instead of the '11110000'
     
  16. skyhawk

    skyhawk New Member

    Joined:
    Feb 9, 2007
    Messages:
    235
    Likes:
    7
    Location:
    Morgantown, WV
    Back to basics. Why not put a 1 on the output pin, clock it, latch it, and test whether the first output pin on the shift register is high.

    edit: Disregard. I see it's working.
     
    Last edited: Oct 4, 2008
  17. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    ok all put up a schmatic if I can draw one that is I don't know what you guys use for it though can you point me to the program ?
     
  18. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    Here it is I didn't put anything on QA to QH but there's a resistor then a LED
     

    Attached Files:

    Last edited: Oct 4, 2008
  19. skyhawk

    skyhawk New Member

    Joined:
    Feb 9, 2007
    Messages:
    235
    Likes:
    7
    Location:
    Morgantown, WV
    What are you doing with these two lines? You haven't initialized FSR. It looks like you are clobbering the contents of temp.

    Code (text):
            movf    INDF,w      ;get a byte
            movwf   temp
     
    Last edited: Oct 4, 2008
  20. Darkstar64

    Darkstar64 New Member

    Joined:
    Apr 3, 2008
    Messages:
    112
    Likes:
    0
    I still don't think the code is working right because everytime I try to sim it it seems to only want to go to shift3 even if its a 1 in the carry so im not sure whats going on I gave the schmatic for you guys of what I have setup I don't know if its hardware or software maby you guys can help with that anyways I won't be home till 9pm eastern so maby when I get back if there is anything new all try it out like new assembler or a new schmatic im desperate to get this working right haha I think thats the problem or one of them is that it will never go to shift2 it will always go to shift3 even if its '11111111' as the string I don't think the btfsc is working right if I change it to btfss it goes to shift2 and no matter what will never go to shift3 and its the same reversed as I said before
     
    Last edited: Oct 4, 2008
  21. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    OH!!! I didn't spot that! Darkstar, you need to learn about indirect addressing with FSR and INDF. You can't use INDF without first setting your pointer with FSR. Well, you can, but it'll just puke things or do nothing. Look at section 4.9 on Page 26 of the datasheet.

    If you've never done pointers/indirect addressing before then you might be a bit baffled for a while. It's not terribly difficult to understand though, and it's important to know to do anything beyond the basics. PICs do it poorly, but good enough for what they are. Motorola/Freescale chips do it superbly. But I digress. :p

    In the above line you're grabbing a random byte (no FSR set) and shifting it out. The shifting is probably working, but with the pointer pointing to a random memory location the byte you shift out could be anything.
     
    Last edited: Oct 4, 2008

Share This Page