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

pic programming.................. what next?

Discussion in 'Microcontrollers' started by tama182, Jan 4, 2006.

  1. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    hey nigel i just noticed, howcome the main board in your tutorial hasn't got anything to clock the pic, theres not crystal or anything
     
  2. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    Try reading the datasheet (or even my tutorials?), one of the reasons for choosing the 628 is that it has an internal 4MHz oscillator - this gives you 16 I/O pins on an 18 pin chip.
     
  3. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,679
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    Hiya Tama,
    Just one question mate since your using the ICD2 have you read the datasheet for for the ICD2?. I ran into similar problems using my ICD2 and a good read of the datasheet explained it all. Oh and just one note NEVER use pullup resistors on the pgc,pgm and use the correct setup for the mlcr pin or you will blow 3 transistors, I'm saying this as I've already been there and done it. But again read the datasheet for the ICD2 and understand it or your new expensive programmer-debuuger could be an expensive paper weight.

    Hope this Helps

    Cheers Bryan :D
     
  4. dave

    Dave New Member

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


     
  5. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0

    Thanks Bryan, i will read the manual PROPERLY ! before i do anything, unfortunatly im the type that rushes into things and then asks loads of questions before ive even properly read the instructions, LOL :lol:
     
  6. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    Can anyone see whats wrong with this code, it seems to run fine, until it gets to goto _7SegDisp, where it does goto to the table but then after it has got the binary code, its seems to jump back up to the very first command movlw 0x07
    Code (text):

        LIST    p=16F628        ;tell assembler what chip we are using
        include "P16F628.inc"       ;include the defaults for the chip
        __config 0x3D18         ;sets the configuration settings (oscillator type etc.)

        cblock  0x20            ;start of general purpose registers
            count1          ;used in delay routine
            counta          ;used in delay routine
            countb          ;used in delay routine
            count
        endc
       
        org 0x0000          ;org sets the origin, 0x0000 for the 16F628,
                        ;this is where the program starts running  
        movlw   0x07
        movwf   CMCON           ;turn comparators off (make it like a 16F84)

        bsf     STATUS,     RP0 ;select bank 1
        movlw   b'00000000'     ;set PortB all outputs
        movwf   TRISB
        bcf     STATUS,     RP0 ;select bank 0
        clrf    PORTB



    start   clrf    count
    read    movfw   count
            goto    _7SegDisp
            movwf   PORTB
            goto    delay
            incf    count,  w
            xorlw   d'14'           ;check for last (14th) entry
            btfsc   STATUS, Z
            goto    start           ;if start from beginning
            incf    count,  f       ;else do next
            goto    read











    _7SegDisp   addwf       PCL     ; skips a certain number of instructions
            retlw       b'11111110' ; code for 0
            retlw       b'01100000' ; code for 1
            retlw       b'11011010' ; code for 2
            retlw       b'11110010' ; code for 3
            retlw       b'01100110' ; code for 4
            retlw       b'10110110' ; code for 5
            retlw       b'10111110' ; code for 6
            retlw       b'11100000' ; code for 7
            retlw       b'11111110' ; code for 8
            retlw       b'11110110' ; code for 9
            retlw       b'11101110' ; code for A
            retlw       b'00111110' ; code for b
            retlw       b'10011100' ; code for C
            retlw       b'01111010' ; code for d
            retlw       b'10011110' ; code for E
            retlw       b'10001110' ; code for F


    delay   movlw   d'250'          ;delay 250 ms (4 MHz clock)
        movwf   count1
    d1  movlw   0xC7
        movwf   counta
        movlw   0x01
        movwf   countb
    Delay_0
        decfsz  counta, f
        goto    $+2
        decfsz  countb, f
        goto    Delay_0

        decfsz  count1  ,f
        goto    d1
        retlw   0x00

        end      
     
     
  7. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    The program is doing EXACTLY what it's being told to - the reason it doesn't work is because you are using 'goto' instead of 'call' to call subroutines. You do this on both subroutine calls!.
     
  8. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    oh, so you cant return from a goto instruction, it only remembers its place from a call instruction, is that right,

    but if i use a call to go somwhere, and then in that place i use a further goto, to go somwhere else, and then from that place i use the return instruction will i go backl to where i started from, only the next instruction after call?
     
  9. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    When you execute a 'call' the return address is pushed on to the stack, when you you execute a 'return' or 'retlw' the address is popped back off the stack. Goto instructions don't affect this in any way - by executing a 'return' without the previous 'call' there isn't a return address to recover, so the stack will 'under-flow' and return an unknown value.

    The PIC stack is also extremely small, so you need to be careful about how many nested subroutines you call, and make sure that you always return from them correctly - this applies to ANY processor, but is particularly important with such a small stack.
     
  10. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    yes i have read all about stack, when using the pic16f54 i only had two, and i know that its first on, last off, if you have more than two say, so i need to make sure that if i add one on top, i must remove it if i want the next one,

    im sorry but i didn't know that you cant retuen from a goto, but now it makes sense why it didn't work,

    at the moment i am trying out 7 segs as you may have seen, i thought that it might be a nice tutorial for you to add to your list, such as strobing, which i haven't done yet
     
  11. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    Yes. But, be aware that PICs can only remember 8 places to return to. This does not mean that you can only use 8 calls but that if you call code after a call but before it returns, it then has to remember 2 return address'.

    EG
    Code (text):

            call Sub1
            more code

    Sub1  call  sub2
             return

    Sub2 return
     
    The above code has to remember 2 address'.

    As you write more code you will realise that this is quite a big limitation. Your code might call ShowMenu which calls PrintString which calls PrintChar which calls SendBit etc. And, during all this interrupts come along and they have to remember where to return to as well.

    Phew

    Mike.
     
  12. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    Which is an excellent reason NEVER to use subroutines called from interrupt routines! - it just adds more chances to over-flow.
     
  13. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    Wow, 2 replies in less time than it took to write mine.

    I didn't notice that the OP was using a 16f54 and therefore the stack is even smaller - it can only remember 2 locations to return to.

    For learning purposes, it might be better to switch to something like a 16F628A as this will be more capable as you learn more.

    Mike.
     
  14. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    no, im using 16f628 now, its just that the first book i read used 16f54 :D
     
  15. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    hey nigel, what do you think of this, i messed round with on of you circuits so that it only takes on switch to change the tables
    Code (text):
    LIST    p=16F628        ;tell assembler what chip we are using
        include "P16F628.inc"       ;include the defaults for the chip
        __config 0x3D18         ;sets the configuration settings (oscillator type etc.)

        cblock  0x20            ;start of general purpose registers
            count1          ;used in delay routine
            counta          ;used in delay routine
            countb          ;used in delay routine
            count           ;MAIN 14 COUNT
        endc
       
    LEDPORT Equ PORTB           ;set constant LEDPORT = 'PORTB'
    LEDTRIS Equ TRISB           ;set constant for TRIS register
    SWPORT  Equ PORTA
    SWTRIS  Equ TRISA
    SW1 Equ 4           ;set constants for the switches

    LED1    Equ 3           ;and for the LED's
    LED2    Equ 2
    LED3    Equ 1
    LED4    Equ 0

       
        org 0x0000          ;org sets the origin, 0x0000 for the 16F628,
                        ;this is where the program starts running  
        movlw   0x07
        movwf   CMCON           ;turn comparators off (make it like a 16F84)

        bsf     STATUS,     RP0 ;select bank 1
        movlw   b'00000000'     ;set PortB all outputs
        movwf   LEDTRIS
        movlw   b'11110000'     ;set PortA 4 inputs, 4 outputs
        movwf   SWTRIS
        bcf STATUS,     RP0 ;select bank 0
        clrf    LEDPORT         ;set all outputs low
        clrf    SWPORT          ;make sure all LED's are off
        BSF     SWPORT, LED1

    start1  CLRF    count
            MOVFW   count


    Start   movfw   count
            btfsc   SWPORT, LED1        ;check which LED is lit
            call    Table1          ;and read the associated table
            btfsc   SWPORT, LED2
            call    Table2
            btfsc   SWPORT, LED3
            call    Table3
            btfsc   SWPORT, LED4
            call    Table4
            movwf   LEDPORT
            incf    count,  w
            xorlw   d'14'           ;check for last (14th) entry
            btfsc   STATUS, Z
            goto    start1          ;if start from beginning
            incf    count,  f
            CALL    DELAY
            GOTO    Start


    Table1  ADDWF   PCL, f          ;data table for bit pattern
        retlw   b'10000000'
            retlw   b'01000000'
            retlw   b'00100000'
            retlw   b'00010000'
            retlw   b'00001000'
            retlw   b'00000100'
            retlw   b'00000010'
            retlw   b'00000001'
            retlw   b'00000010'
            retlw   b'00000100'
            retlw   b'00001000'
            retlw   b'00010000'
            retlw   b'00100000'
            retlw   b'01000000'

    Table2  ADDWF   PCL, f          ;data table for bit pattern
        retlw   b'11000000'
            retlw   b'01100000'
            retlw   b'00110000'
            retlw   b'00011000'
            retlw   b'00001100'
            retlw   b'00000110'
            retlw   b'00000011'
            retlw   b'00000011'
            retlw   b'00000110'
            retlw   b'00001100'
            retlw   b'00011000'
            retlw   b'00110000'
            retlw   b'01100000'
            retlw   b'11000000'

    Table3  ADDWF   PCL, f          ;data table for bit pattern
        retlw   b'01111111'
            retlw   b'10111111'
            retlw   b'11011111'
            retlw   b'11101111'
            retlw   b'11110111'
            retlw   b'11111011'
            retlw   b'11111101'
            retlw   b'11111110'
            retlw   b'11111101'
            retlw   b'11111011'
            retlw   b'11110111'
            retlw   b'11101111'
            retlw   b'11011111'
            retlw   b'10111111'

    Table4  ADDWF   PCL, f          ;data table for bit pattern
        retlw   b'00111111'
            retlw   b'10011111'
            retlw   b'11001111'
            retlw   b'11100111'
            retlw   b'11110011'
            retlw   b'11111001'
            retlw   b'11111100'
            retlw   b'11111100'
            retlw   b'11111001'
            retlw   b'11110011'
            retlw   b'11100111'
            retlw   b'11001111'
            retlw   b'10011111'
            retlw   b'00111111'



    Switch1 BTFSC   SWPORT, LED1
            GOTO    JUMP1
            BTFSC   SWPORT, LED2
            GOTO    JUMP2
            BTFSC   SWPORT, LED3
            GOTO    JUMP3
            BTFSC   SWPORT, LED4
            GOTO    JUMP4
            retlw   0x00

    JUMP1   BCF SWPORT, LED1
            BSF SWPORT, LED2
            BCF SWPORT, LED3
            BCF SWPORT, LED4
            GOTO    BACK

    JUMP2   BCF SWPORT, LED1
            BCF SWPORT, LED2
            BSF SWPORT, LED3
            BCF SWPORT, LED4
            GOTO    BACK

    JUMP3   BCF SWPORT, LED1
            BCF SWPORT, LED2
            BCF SWPORT, LED3
            BSF SWPORT, LED4
            GOTO    BACK

    JUMP4   BSF SWPORT, LED1
            BCF SWPORT, LED2
            BCF SWPORT, LED3
            BCF SWPORT, LED4
            GOTO    BACK



    DELAY   movlw   d'250'          ;delay 250 ms (4 MHz clock)
        movwf   count1
    d1  call    ChkKeys         ;check the keys
        movlw   0xC7            ;delay 1mS
        movwf   counta
        movlw   0x01
        movwf   countb
    Delay_0
        decfsz  counta, f
        goto    $+2
        decfsz  countb, f
        goto    Delay_0

        decfsz  count1  ,f
        goto    d1
        retlw   0x00

    ChkKeys btfss   SWPORT, SW1
        call    Switch1
    BACK

        retlw   0x00


        end      




     
    [/quote]
     
  16. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    That's what the tutorials are there for, tested working examples that you can alter around to do what you want :)
     
  17. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    How come that all the circuits and icd2 manual have a pull up resister for MCLR, i know that its logic 0 activated, but i thought that the pins on IC's go high if there not connected,

    so why is a pull up needed?

    also you cant use pull ups on PGC/PGD, again doesn't the same apply here,

    so does this mean that you basicly cant use R6 and R7 as logic 0 activated switches, because you couldn't have them usually pulled up when not pressed,

    but you could have them as logic 1 activated switches, where they would usually be pulled down, when not pressed.
     
  18. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    Why did you think that?, it's not true.

    With CMOS inputs (like in a PIC) it's important to have the input pins connected to 'something', otherwise their high impedance 'may' allow the pin to drift high, increasing supply current many times, and possibly even triggering the input.

    To pull the reset pin high.

    You should pull them high or low, with resistors, to ensure the default state, then switch to the other state. PortB on most PIC's has internal pull-ups, which can be enabled by a bit in a register.
     
  19. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    Well im just following the ICD2 manual, but when it says that you can't use pull ups on the pgc and pgd inputs, this is just for the programming right?

    so if i was to have pull ups on on RB6 and RB7, i could put them on after the programming, is that right?
     

    Attached Files:

    • icd.jpg
      icd.jpg
      File size:
      28.1 KB
      Views:
      617
  20. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    Yes, assuming that was what you wanted to do - but designing for ICSP always places constraints on the target design.
     
  21. tama182

    tama182 New Member

    Joined:
    Jan 4, 2006
    Messages:
    57
    Likes:
    0
    ok, im just beginning the LCD tutorials now, and im reading through it, and im looking at the table from the pdf of that electronics site that you've got on the first tutorial,

    right i understand that your sending the data in nibbles to save pins, but where does the data get saved, because at first i thought that the circuit was strobing, but then i noticed the program only gets run once and then repeats that last last forever,

    so you sending the bits in 4, in the table a capital H is 0100 upper and 1000 lower,

    so first you send the 0100 to lcd port, pins 0,1,2,3 which on the lcd is data lines 4-7,

    but then you send the lower 1000 number, which then over writes 0100, because you used the mov command,

    does the 0100 get saved somwhere?

    is there memmory or somthing in the lcd, because each nibble you add overwrites the last nibble, so how does each letter get saved, i mean it has to be saved because the program only runs once

    (kinda whshing you didn't write the tutorial now ..huh? :roll: )
     

Share This Page