First Attempt at PIC Programming

Status
Not open for further replies.

ormo

New Member
Hi,

I've just started with PIC programming and I've written the first basic code.

It's to be run on a PIC16F628A (with 2 x 8-bit ports). All it's meant to do is just turn the bits of each pin on in groups of 2. Have I gotten this right or am I barking up the wrong tree?

Code:
BSF 03h,5
MOVLW 00h
MOVWF 85h
MOVWF 86h
BCF 03h,5
START NOP
MOVLW b'00000011'
MOVWF 05h
CALL DELAY
MOVLW b'00001100'
MOVWF 05h
CALL DELAY
MOVLW b'00110000'
MOVWF 05h
CALL DELAY
MOVLW b'11000000'
MOVWF 05h
CALL DELAY
MOVLW b'00000000'
MOVWF 05h
CALL DELAY
MOVLW b'00000011'
MOVWF 06h
CALL DELAY
MOVLW b'00001100'
MOVWF 06h
CALL DELAY
MOVLW b'00110000'
MOVWF 06h
CALL DELAY
MOVLW b'11000000'
MOVWF 06h
CALL DELAY
MOVLW b'00000000'
MOVWF 06h
CALL DELAY
GOTO START
DELAY 20h EQU FFh
DL DECFSZ 20h,1
GOTO DL
RETURN
 
Just tried to build it - got told that it failed because it "Found opcode in column 1" on every line.

Can someone explain to me what this means? Thanks!
 
lol no trouble
tab all the lines
Code:
    BSF 03h,5
    MOVLW 00h
    MOVWF 85h
    MOVWF 86h
    BCF 03h,5
    START NOP
    MOVLW b'00000011'
    MOVWF 05h
    CALL DELAY
    MOVLW b'00001100'
    MOVWF 05h
    CALL DELAY
    MOVLW b'00110000'
    MOVWF 05h
    CALL DELAY
    MOVLW b'11000000'
    MOVWF 05h
    CALL DELAY
    MOVLW b'00000000'
    MOVWF 05h
    CALL DELAY
    MOVLW b'00000011'
    MOVWF 06h
    CALL DELAY
    MOVLW b'00001100'
    MOVWF 06h
    CALL DELAY
    MOVLW b'00110000'
    MOVWF 06h
    CALL DELAY
    MOVLW b'11000000'
    MOVWF 06h
    CALL DELAY
    MOVLW b'00000000'
    MOVWF 06h
    CALL DELAY
    GOTO START
    DELAY 20h EQU FFh
    DL DECFSZ 20h,1
    GOTO DL
    RETURN
 
Last edited:
It's to be run on a PIC16F628A (with 2 x 8-bit ports). All it's meant to do is just turn the bits of each pin on in groups of 2. Have I gotten this right or am I barking up the wrong tree?
Your code should look something more like this (assuming you're using MPLAB with the MPASM assembler):
Code:
	include "P16F628A.INC"
	__config _INTOSC_OSC_NOCLKOUT & _WDT_OFF & _LVP_OFF

	cblock	0x20		;start of general purpose registers
	d1
	endc

	org	0x000
	banksel	TRISA		;bank 1
	clrf	TRISA
	clrf	TRISB
	banksel	PORTA		;bank 0
START 	movlw	b'00000011'
	movwf	PORTA
	call	delay
	movlw	b'00001100'
	movwf	PORTA
	call	delay
	movlw	b'00110000'
	movwf	PORTA
	call	delay
	movlw	b'11000000'
	movwf	PORTA
	call	delay
	movlw	b'00000000'
	movwf	PORTA
	call	delay
	movlw	b'00000011'
	movwf	PORTB
	call	delay
	movlw	b'00001100'
	movwf	PORTB
	call	delay
	movlw	b'00110000'
	movwf	PORTB
	call	delay
	movlw	b'11000000'
	movwf	PORTB
	call	delay
	movlw	b'00000000'
	movwf	PORTB
	call	delay
	goto	START
	
delay	movlw	0xff
	movwf	d1
del	decfsz	d1,f
	goto	del
	return
	
	end
0. You need to include P16F628A.inc so you can use the proper names for registers instead of confusing numbers. Use PORTA instead of 05h. Much clearer that way. Who wants to constantly be looking up numbers to see what register they are? Assembler is hard enough to read without purposely making it even more cryptic.

1. The config line assumes that you're using the internal oscillator.

2. The cblock lets you set up a block of variables in RAM. It's much clearer to use a variable name than the actual address number of the RAM byte you're using.

3. I like to use the banksel directive to do my bank changes, rather than (again) using numbers that I have to look up and are error prone. Easier to read.

4. It's just personal preference, but I HATE all-uppercase code. Horribly unreadable.

5. Another personal preference thing is using (for instance) 0x3f instead of 3fh, as you you were doing. Using the h suffix is legal though, so do it if you prefer.

6. On lines like "del decfsz d1,f", use f/w instead of 0/1. Use f to store the result back in the register, and w to store the result in W. Once again, it's much clearer what you're doing. The equates for these are in the INC file.

7. You need an end statement at the end of the program. This tells the assembler to stop assembling there.
 
Last edited:
Save yourself some trouble and use C. It's a lot easier (however, basic understanding of registers and MCU commands and functionality is still required).
Precisely why (IMHO) all newb PIC programmers should start out with assembler, at least till they have a bit of a handle on it. Then move to C, and things will make a bunch more sense than if you just start out with C. C has enough of a learning curve without also having to learn how the PIC works at the same time. Actually, new C programmers would benefit from learning C on PC first, before moving to microcontrollers.

I love BoostC. Bought a full license a while back.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…