Connect with us

PIC16f877A Blinking an LED

Discussion in 'Microcontrollers, Programming and IoT' started by Luke Vassallo, Jan 9, 2016.

Scroll to continue with content
  1. Luke Vassallo

    Luke Vassallo

    33
    4
    Dec 10, 2014
    Hello,

    I am trying to blink an LED using the pic16F877A microcontroller and coding in assembly. The code looks fine to me but for some reason i am getting no output from the chip.

    Code:
     RESET_VECTOR    CODE   0x0000     ; processor reset vector
    
    INT_VECTOR    CODE    0x0004      ; interrupt vector location
       
    BSF 03h,5   ;go to bank 1
    MOVLW 00h   ;put 00000
    MOVLW 85h   ;move 00000 to TRISA
    BCF 03h,5   ;go back to bank 0
       
    Start   movlw 02h  ;00010
        movwf 05h
    
        Loop1 decfsz 20h,1
          goto Loop1
          decfsz 21h,1
          goto Loop1
       
        movlw 00h
        movwf 05h
       
        Loop2 decfsz 20h,1
          goto Loop2
          decfsz 21h,1
          goto Loop2
         
        goto Start
       
    END
    The above is the code i am using in MPLAB X. I build it and the .hex file is generated and then i just program the chip using a programmer.
    Any help ywould be greatly appreciated :)
     
  2. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    I have not used that pic, but most come with the output set for analogue.
    confirm with the manual. See ADCON1.
    Also setting port for I/O. in the manual.
    M.
     
  3. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    Also it is preferred to use the .INC file for your pic, then you won't need to enter a register address, just the name.
    M.
     
  4. Luke Vassallo

    Luke Vassallo

    33
    4
    Dec 10, 2014
    i noticed that that particular ouput i was using could also be an analouge input, however that being said i did configure that port as output. So i can change to a digital i/o port only RB1 and removed most of the code. just to perform a write. However nothing happend. the chip is programming successfully and also after programming i verified the data and it was correct. I don't understand, i also checked the status registor and i was selecting bank 1 to set the output pins.
     
  5. Luke Vassallo

    Luke Vassallo

    33
    4
    Dec 10, 2014
    Code:
    RESET_VECTOR    CODE   0x0000     ; processor reset vector
    
    INT_VECTOR    CODE    0x0004      ; interrupt vector location
    
    BCF 0x03,6
    BSF 0x03,5   ;go to bank 1
    MOVLW 0x00   ;put 00000
    MOVWF 0x86   ;move 00000 to TRISB
    BCF 0x03,5   ;go back to bank 0
       
       movlw 02h  ;00010
        movwf 06h
    
    END
    This is the new code
     
  6. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    It is much easier to follow when register names are used instead of addresses, when using the INC file.
    M.
     
  7. Colin Mitchell

    Colin Mitchell

    1,417
    312
    Aug 31, 2014
    At the beginning of the program you create a list of equates.

    Start with file(register) 20h call it del1 for delay 1 in your delay routine
    21h is del2

    now the set of equates becomes:

    del1 equ 20h
    del2 equ 21h

    portA equ 5h


    instead of

    movlw 00h
    movwf 05h

    you can use a single instruction:

    clrf portA

    and then decf portA will put all "1's" on the port and turn everything ON!!!!!!!


    Instead of writing the delay loop two times
    Loop1 decfsz 20h,1
    goto Loop1
    decfsz 21h,1
    goto Loop1


    use:


    Loop1 decfsz del1,1
    goto Loop1
    decfsz del2,1
    goto Loop1
    retlw 00


    and call Loop1 on two occasions.
     
  8. Luke Vassallo

    Luke Vassallo

    33
    4
    Dec 10, 2014
    Thank you for your replies. Where can i find the .INC file ? I will organize the code and try again but i doubt that this will change anything because i am not chaning anything.
     
  9. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    You can get it either from you Picmicro directory if you have downloaded the MPLAB IDE, or off the Picmicro site.
    When running an application in MPLAB it goes under Header Files.
    I am not saying it will change anything just makes it easier to program with register names instead of having to look up the address.
    M.
     
  10. Luke Vassallo

    Luke Vassallo

    33
    4
    Dec 10, 2014
    I am using MPLAX X IDE V3.20 but i can't find anything.Where is the pic micro directory ?
     
  11. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    Not tested but this is something to go on.
    Look up the tutorials on the Nigel Goodwin site, shows the code you need.
    Do a search of you program files dir for *.INC
    M.

    Code:
            CBLOCK 0x20 ; Start of General purpose registers
            STORE1
            STORE2
            endc
          
    
    RESET_VECTOR    CODE   0x0000     ; processor reset vector
            goto    Goto Start1
    INT_VECTOR    CODE    0x0004      ; interrupt vector location
    Start1:
            bcf  STATUS, RP0 ;
            bcf  STATUS, RP1 ; Bank0
            clrf PORTA ; Initialize PORTA by
            ; clearing output
            ; data latches
            bsf STATUS, RP0 ; Select Bank 1
            movlw  0x06 ; Configure all pins
            movwf  ADCON1 ; as digital
      
    ;
            bsf STATUS RP1   ;go to bank 1
            clrf TRISA   ;put 00000
            bcf STATUS, RP0   ;go back to bank 0
      
    Start:
            movlw 0x02  ;00010
            movwf PORTA
    
    Loop1:
         decfsz STORE1
         goto Loop1
          decfsz STORE2
          goto Loop1
      
        clrf PORTA
      
      
    Loop2:
             decfsz STORE1
              goto Loop2
              decfsz STORE2
               goto Loop2
        
        goto Start
      
       end
     
  12. HellasTechn

    HellasTechn

    1,542
    214
    Apr 14, 2013
    Could it just be a bad controller chip ?
     
  13. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    There is also running the Pickit 2/3 if that is what you are using in the debugger mode to step through the program.
    M.
     
  14. Luke Vassallo

    Luke Vassallo

    33
    4
    Dec 10, 2014
    Sorry for the late reply but currently i'm doing exams lol. I have tried another controller but i got the same problem again. I don't know if it's the programming board, i am using a K150, but i ordered the one that is designed by microchip. When i receive it i'll try again and let you know. Thanks for your help everyone.
     
  15. Arouse1973

    Arouse1973 Adam

    5,164
    1,081
    Dec 18, 2013
    Set up PORTA as M said and just use banksel command for switching banks if you need to. Use molw b'00010, dont use HEX it gets confusing unless you know it very well. Then set up a delay and use the call funtion.

    so:
    main
    bsf PORTA,1
    Call delay
    bcf PORTA,1
    call delay
    Return
     
  16. Minder

    Minder

    3,013
    640
    Apr 24, 2015
    Also in the OP you have used
    MOVLW 00h ;put 00000
    MOVLW 85h ;move 00000 to TRISA
    All you did was clear W then move 85h to W , it did nothing to PORTA.
    M.
     
  17. Arouse1973

    Arouse1973 Adam

    5,164
    1,081
    Dec 18, 2013
    Yes, he should of used movwf trisa. Well spotted M
    Adam
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-