1st step to PIC programing

Thread Starter

chamara

Joined Oct 22, 2011
21
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;

Rich (BB code):
org 0x00

status equ 0x03
trisb equ 0x86
count1 equ 0x20
portb equ 0x06
count2 equ 0x25

bsf status,5    ;go to bank 1
clrf trisb   ;configure portb as output
bcf status,5    ;go back to bank 0

movlw 0xFF    ;define the output/load w-reg
movwf portb    ;put w-reg value to portb

movlw 0xff
movwf count1
movlw 0x05
movwf count2

loop1
decfsz count1
goto loop1
decfsz count2
goto loop1

movlw 0x00
movwf portb

end
 
Last edited by a moderator:

nerdegutta

Joined Dec 15, 2009
2,684
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. :)
 

nerdegutta

Joined Dec 15, 2009
2,684
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.
 

AlexR

Joined Jan 16, 2008
732
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.
Rich (BB code):
 org 0x00

status equ 0x03
trisb equ 0x86
count1 equ 0x20
portb equ 0x06
count2 equ 0x25

 bsf status,5 ;go to bank 1
 clrf trisb ;configure portb as output
 bcf status,5 ;go back to bank 0

 movlw 0xFF ;define the output/load w-reg
 movwf portb ;put w-reg value to portb

main_loop    ;continuos loop to flash the LED
 movlw 0xff
 movwf count1
 movlw 0x05
 movwf count2

loop1
 decfsz count1
 goto loop1
 decfsz count2
 goto loop1

 movlw 0x00
 movwf portb
 goto main_loop

 end
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
#include <P16F628A.INC>
    __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _LVP_OFF ) ;Note the order is of no consequence
The setup should be correct for your PIC using the internal oscillator. And input on the MCLR pin.

Rich (BB code):
_LP_OSC              EQU  H'3FEC'    ; LP oscillator: Low-power crystal on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
_XT_OSC              EQU  H'3FED'    ; XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
_HS_OSC              EQU  H'3FEE'    ; HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
_EXTCLK_OSC          EQU  H'3FEF'    ; EC: I/O function on RA6/OSC2/CLKOUT pin, CLKIN on RA7/OSC1/CLKIN
_INTOSC_OSC_NOCLKOUT EQU  H'3FFC'    ; INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
_INTRC_OSC_NOCLKOUT  EQU  H'3FFC'    ; INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
_INTOSC_OSC_CLKOUT   EQU  H'3FFD'    ; INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
_INTRC_OSC_CLKOUT    EQU  H'3FFD'    ; INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
_RC_OSC_NOCLKOUT     EQU  H'3FFE'    ; RC oscillator: I/O function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
_ER_OSC_NOCLKOUT     EQU  H'3FFE'    ; RC oscillator: I/O function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
_RC_OSC_CLKOUT       EQU  H'3FFF'    ; RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
_ER_OSC_CLKOUT       EQU  H'3FFF'    ; RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
_WDT_OFF             EQU  H'3FFB'    ; WDT disabled
_WDT_ON              EQU  H'3FFF'    ; WDT enabled
_PWRTE_ON            EQU  H'3FF7'    ; PWRT enabled
_PWRTE_OFF           EQU  H'3FFF'    ; PWRT disabled
_MCLRE_OFF           EQU  H'3FDF'    ; RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD
_MCLRE_ON            EQU  H'3FFF'    ; RA5/MCLR/VPP pin function is MCLR
_BODEN_OFF           EQU  H'3FBF'    ; BOD disabled
_BOREN_OFF           EQU  H'3FBF'    ; BOD disabled
_BODEN_ON            EQU  H'3FFF'    ; BOD enabled
_BOREN_ON            EQU  H'3FFF'    ; BOD enabled
_LVP_OFF             EQU  H'3F7F'    ; RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming
_LVP_ON              EQU  H'3FFF'    ; RB4/PGM pin has PGM function, low-voltage programming enabled
DATA_CP_ON           EQU  H'3EFF'    ; Data memory code-protected
DATA_CP_OFF          EQU  H'3FFF'    ; Data memory code protection off
_CP_ON               EQU  H'1FFF'    ; 0000h to 07FFh code-protected
_CP_OFF              EQU  H'3FFF'    ; Code protection off
 

AlexR

Joined Jan 16, 2008
732
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.
Rich (BB code):
#include <P16F628A.INC>    ;file containing register and config bit definitions
 __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _LVP_OFF ) ;Note the order is of no consequence

status equ 0x03
trisb equ 0x86
count1 equ 0x20
portb equ 0x06
count2 equ 0x25

 org 0x00
 
 bsf status,5 ;go to bank 1
 clrf trisb ;configure portb as output
 bcf status,5 ;go back to bank 0
 movlw 0xFF ;define the output/load w-reg
 movwf portb ;put w-reg value to portb
main_loop    ;continuos loop to flash the LED
 movlw 0x00
 movwf portb
 call led_delay
 movlw 0xff
 movwf portb
 call led_delay
 goto main_loop

led_delay    ;delay subroutine
 movlw 0xff
 movwf count1
 movlw 0x05
 movwf count2
loop1
 decfsz count1
 goto loop1
 decfsz count2
 goto loop1
 return

 end
 

Thread Starter

chamara

Joined Oct 22, 2011
21
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
 

AlexR

Joined Jan 16, 2008
732
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.
 

Thread Starter

chamara

Joined Oct 22, 2011
21
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
------------------------------------------------------------------------------
 

AlexR

Joined Jan 16, 2008
732
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.
Rich (BB code):
#include <P16F628A.INC>    ;file containing register and config bit definitions
 ERRORLEVEL -302            ; get rid of idiotic message 302

 __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _LVP_OFF ) ;Note the order is of no consequence


status equ 0x03
trisb equ 0x86
count1 equ 0x20
portb equ 0x06
count2 equ 0x25

 org 0x00
 
 bsf status,5 ;go to bank 1
 clrf trisb ;configure portb as output
 bcf status,5 ;go back to bank 0
 movlw 0xFF ;define the output/load w-reg
 movwf portb ;put w-reg value to portb
main_loop    ;continuos loop to flash the LED
 movlw 0x00
 movwf portb
 call led_delay
 movlw 0xff
 movwf portb
 call led_delay
 goto main_loop

led_delay    ;delay subroutine
 movlw 0xff
 movwf count1
 movlw 0x05
 movwf count2
loop1
 decfsz count1,f
 goto loop1
 decfsz count2,f
 goto loop1
 return

 end
 
Top