1st step to PIC programing

Discussion in 'Embedded Systems and Microcontrollers' started by chamara, Oct 22, 2011.

  1. chamara

    Thread Starter New Member

    Oct 22, 2011
    21
    0
    Hi All!

    I wrote my very 1st code with PIC16F628A,but it gave BUILD FAIL msg when im testing it.Can u plz help me....

    Here is my code;

    Code ( (Unknown Language)):
    1.  
    2. org 0x00
    3.  
    4. status equ 0x03
    5. trisb equ 0x86
    6. count1 equ 0x20
    7. portb equ 0x06
    8. count2 equ 0x25
    9.  
    10. bsf status,5    ;go to bank 1
    11. clrf trisb   ;configure portb as output
    12. bcf status,5    ;go back to bank 0
    13.  
    14. movlw 0xFF    ;define the output/load w-reg
    15. movwf portb    ;put w-reg value to portb
    16.  
    17. movlw 0xff
    18. movwf count1
    19. movlw 0x05
    20. movwf count2
    21.  
    22. loop1
    23. decfsz count1
    24. goto loop1
    25. decfsz count2
    26. goto loop1
    27.  
    28. movlw 0x00
    29. movwf portb
    30.  
    31. end
    32.  
    33.  
     
    Last edited by a moderator: Oct 22, 2011
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,514
    784
    Why not use the Hi-Tech C compiler?

    What do you want this code to do?

    And, please, use the #-symbol on the menubar on source code. The readability is like night and day. Try it, you'll see. :)
     
    chamara likes this.
  3. chamara

    Thread Starter New Member

    Oct 22, 2011
    21
    0
    i wanted to flash 8 LEDs using assembly language
    but still i got the same error msg :/
     
  4. nerdegutta

    Moderator

    Dec 15, 2009
    2,514
    784
    It would be easier to the members on the forum to help you if you copy/paste the entire error message, instead of just saying you got BUILD FAIL.
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Once you add the leading spaces for all of the command lines and tell MPLAB which device you are using it builds without any errors. You do however need to add a loop statement to the end of your code to loop back and keep the LED flashing. The way it is now it turn on the LED, delays, turns it off and then thunders off into infinity. All microcontroller programs should finish with some sort of endless loop.
    Code ( (Unknown Language)):
    1.  
    2.  org 0x00
    3.  
    4. status equ 0x03
    5. trisb equ 0x86
    6. count1 equ 0x20
    7. portb equ 0x06
    8. count2 equ 0x25
    9.  
    10.  bsf status,5 ;go to bank 1
    11.  clrf trisb ;configure portb as output
    12.  bcf status,5 ;go back to bank 0
    13.  
    14.  movlw 0xFF ;define the output/load w-reg
    15.  movwf portb ;put w-reg value to portb
    16.  
    17. main_loop    ;continuos loop to flash the LED
    18.  movlw 0xff
    19.  movwf count1
    20.  movlw 0x05
    21.  movwf count2
    22.  
    23. loop1
    24.  decfsz count1
    25.  goto loop1
    26.  decfsz count2
    27.  goto loop1
    28.  
    29.  movlw 0x00
    30.  movwf portb
    31.  goto main_loop
    32.  
    33.  end  
    34.  
     
    chamara likes this.
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You do not set the configuration word. As far as I can see. You have to do this in order for your PIC to work. Read the "SPECIAL FEATURES OF THE CPU" section in the data sheet. Then set the bits so they match your setup.
    A common beginner mistake is also to not be aware of that the many of the pins on your PIC have analog functions. After a reset the pins with analog functions default to to the analog function. Not as a common digital I/O. Again refer to the data sheet and the PORTx sections. Your pic also have an internal oscillator. For your first test code I recommend using this. As it is fail safe. And also disable the MCLR option. The pin used for MCLR will then be a standard input pin.
    You will find the configuration word options at the end of your PIC include file (P16F628A.INC in the \MPASM Suite folder). But I include them here for you also. At the top of your asm file you put this
    Code ( (Unknown Language)):
    1. #include <P16F628A.INC>
    2.     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _LVP_OFF ) ;Note the order is of no consequence
    3.  
    The setup should be correct for your PIC using the internal oscillator. And input on the MCLR pin.

    Code ( (Unknown Language)):
    1.  
    2. _LP_OSC              EQU  H'3FEC'    ; LP oscillator: Low-power crystal on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
    3. _XT_OSC              EQU  H'3FED'    ; XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
    4. _HS_OSC              EQU  H'3FEE'    ; HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
    5. _EXTCLK_OSC          EQU  H'3FEF'    ; EC: I/O function on RA6/OSC2/CLKOUT pin, CLKIN on RA7/OSC1/CLKIN
    6. _INTOSC_OSC_NOCLKOUT EQU  H'3FFC'    ; INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    7. _INTRC_OSC_NOCLKOUT  EQU  H'3FFC'    ; INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    8. _INTOSC_OSC_CLKOUT   EQU  H'3FFD'    ; INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    9. _INTRC_OSC_CLKOUT    EQU  H'3FFD'    ; INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    10. _RC_OSC_NOCLKOUT     EQU  H'3FFE'    ; RC oscillator: I/O function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
    11. _ER_OSC_NOCLKOUT     EQU  H'3FFE'    ; RC oscillator: I/O function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
    12. _RC_OSC_CLKOUT       EQU  H'3FFF'    ; RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
    13. _ER_OSC_CLKOUT       EQU  H'3FFF'    ; RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
    14. _WDT_OFF             EQU  H'3FFB'    ; WDT disabled
    15. _WDT_ON              EQU  H'3FFF'    ; WDT enabled
    16. _PWRTE_ON            EQU  H'3FF7'    ; PWRT enabled
    17. _PWRTE_OFF           EQU  H'3FFF'    ; PWRT disabled
    18. _MCLRE_OFF           EQU  H'3FDF'    ; RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD
    19. _MCLRE_ON            EQU  H'3FFF'    ; RA5/MCLR/VPP pin function is MCLR
    20. _BODEN_OFF           EQU  H'3FBF'    ; BOD disabled
    21. _BOREN_OFF           EQU  H'3FBF'    ; BOD disabled
    22. _BODEN_ON            EQU  H'3FFF'    ; BOD enabled
    23. _BOREN_ON            EQU  H'3FFF'    ; BOD enabled
    24. _LVP_OFF             EQU  H'3F7F'    ; RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming
    25. _LVP_ON              EQU  H'3FFF'    ; RB4/PGM pin has PGM function, low-voltage programming enabled
    26. DATA_CP_ON           EQU  H'3EFF'    ; Data memory code-protected
    27. DATA_CP_OFF          EQU  H'3FFF'    ; Data memory code protection off
    28. _CP_ON               EQU  H'1FFF'    ; 0000h to 07FFh code-protected
    29. _CP_OFF              EQU  H'3FFF'    ; Code protection off
    30.  
     
    chamara likes this.
  7. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    One further point, you will need another delay routine after you change portb to 0x00, you can either copy the existing delay code and paste it just in front of the goto main_loop instruction or a better option would be to turn the delay code into a sub-routine to be called when needed.
    So the full code including the config code suggested by t06afre should look as below.
    Code ( (Unknown Language)):
    1.  
    2. #include <P16F628A.INC>    ;file containing register and config bit definitions
    3.  __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _LVP_OFF ) ;Note the order is of no consequence
    4.  
    5. status equ 0x03
    6. trisb equ 0x86
    7. count1 equ 0x20
    8. portb equ 0x06
    9. count2 equ 0x25
    10.  
    11.  org 0x00
    12.  
    13.  bsf status,5 ;go to bank 1
    14.  clrf trisb ;configure portb as output
    15.  bcf status,5 ;go back to bank 0
    16.  movlw 0xFF ;define the output/load w-reg
    17.  movwf portb ;put w-reg value to portb
    18. main_loop    ;continuos loop to flash the LED
    19.  movlw 0x00
    20.  movwf portb
    21.  call led_delay
    22.  movlw 0xff
    23.  movwf portb
    24.  call led_delay
    25.  goto main_loop
    26.  
    27. led_delay    ;delay subroutine
    28.  movlw 0xff
    29.  movwf count1
    30.  movlw 0x05
    31.  movwf count2
    32. loop1
    33.  decfsz count1
    34.  goto loop1
    35.  decfsz count2
    36.  goto loop1
    37.  return
    38.  
    39.  end
    40.  
     
    chamara likes this.
  8. chamara

    Thread Starter New Member

    Oct 22, 2011
    21
    0
    This is the error msg i got,i tried lot of time with different code but in every time i got this kind of error msg.plz help....


    Clean: Deleting intermediary and output files.
    Clean: Deleted file "C:\Documents and Settings\Chamara\Desktop\Semester 05\EE 320\Test\Test.mcs".
    Clean: Done.
    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F84 "Test.asm" /l"Test.lst" /e"Test.err"
    Message[301] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\P16F628A.INC 35 : MESSAGE: (Processor-header file mismatch. Verify selected processor.)
    Message[302] C:\DOCUMENTS AND SETTINGS\CHAMARA\DESKTOP\SEMESTER 05\EE 320\TEST\TEST.ASM 13 : Register in operand not in bank 0. Ensure that bank bits are correct.
    Message[305] C:\DOCUMENTS AND SETTINGS\CHAMARA\DESKTOP\SEMESTER 05\EE 320\TEST\TEST.ASM 32 : Using default destination of 1 (file).
    Message[305] C:\DOCUMENTS AND SETTINGS\CHAMARA\DESKTOP\SEMESTER 05\EE 320\TEST\TEST.ASM 34 : Using default destination of 1 (file).
    Error[173] C:\DOCUMENTS AND SETTINGS\CHAMARA\DESKTOP\SEMESTER 05\EE 320\TEST\TEST.ASM 39 : Source file path exceeds 62 characters (C:\DOCUMENTS AND SETTINGS\CHAMARA\DESKTOP\SEMESTER 05\EE 320\TEST\TEST.ASM)
    Halting build on first failure as requested.
    BUILD FAILED: Sat Oct 22 18:54:44 2011
     
  9. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    You have a couple of problems there.
    First you have not set the device type in MPLAB. Go to the menu bar, click on "Configure" and then on "Select Device", click on the device drop-down menu and scroll down to and click on "PIC16F628A".
    The second (and more serious) fault is with your source file path. As error[173] state the path to your source files (the .asm file) is too long for MPLAB to handle so you may have to store your project in a directory with a shorter path and/or shorter file names.
     
    chamara likes this.
  10. chamara

    Thread Starter New Member

    Oct 22, 2011
    21
    0
    Thank you very much!!!

    but still i got this kind of msg,can u describe it for me ?

    ------------------------------------------------------
    Clean: Deleting intermediary and output files.
    Clean: Deleted file "C:\Program Files\Microchip\Test\Test.o".
    Clean: Deleted file "C:\Program Files\Microchip\Test\Test.lst".
    Clean: Deleted file "C:\Program Files\Microchip\Test\test1.lst".
    Clean: Done.
    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F628A "Test.asm" /l"Test.lst" /e"Test.err"
    Message[302] C:\PROGRAM FILES\MICROCHIP\TEST\TEST.ASM 13 : Register in operand not in bank 0. Ensure that bank bits are correct.
    Message[305] C:\PROGRAM FILES\MICROCHIP\TEST\TEST.ASM 32 : Using default destination of 1 (file).
    Message[305] C:\PROGRAM FILES\MICROCHIP\TEST\TEST.ASM 34 : Using default destination of 1 (file).
    Loaded C:\Program Files\Microchip\Test\Test.cod.
    BUILD SUCCEEDED: Sat Oct 22 20:51:55 2011
    ------------------------------------------------------------------------------
     
  11. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Message[302] is normal, it is just a reminder to make sure that you are using the correct bank and it comes up irrespective of whether you are in the correct bank or not.

    Many people like to suppress message[302] by inserting "ERRORLEVEL -302" near the top of the .asm file.

    Message[305] means that you have not provided a destination for the decfsz commands. When you decrement a file the result can either get stored back into the file or in the W register. If no destination is supplied it defaults to storing the result back to the file and issues the Message[305] warning.

    The file below has both these issues fixed.
    Code ( (Unknown Language)):
    1.  
    2. #include <P16F628A.INC>    ;file containing register and config bit definitions
    3.  ERRORLEVEL -302            ; get rid of idiotic message 302
    4.  
    5.  __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _LVP_OFF ) ;Note the order is of no consequence
    6.  
    7.  
    8. status equ 0x03
    9. trisb equ 0x86
    10. count1 equ 0x20
    11. portb equ 0x06
    12. count2 equ 0x25
    13.  
    14.  org 0x00
    15.  
    16.  bsf status,5 ;go to bank 1
    17.  clrf trisb ;configure portb as output
    18.  bcf status,5 ;go back to bank 0
    19.  movlw 0xFF ;define the output/load w-reg
    20.  movwf portb ;put w-reg value to portb
    21. main_loop    ;continuos loop to flash the LED
    22.  movlw 0x00
    23.  movwf portb
    24.  call led_delay
    25.  movlw 0xff
    26.  movwf portb
    27.  call led_delay
    28.  goto main_loop
    29.  
    30. led_delay    ;delay subroutine
    31.  movlw 0xff
    32.  movwf count1
    33.  movlw 0x05
    34.  movwf count2
    35. loop1
    36.  decfsz count1,f
    37.  goto loop1
    38.  decfsz count2,f
    39.  goto loop1
    40.  return
    41.  
    42.  end
    43.  
     
    chamara likes this.
  12. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    305 means you havent set the destination for the code should be ,w for w reg or ,f for file reg.
     
    chamara likes this.
Loading...