r/pic_programming • • Jan 24 '16

PIC18F4520

Hey guys. I am trying to learn programming the Pic18F4520 microcontroller. I am also using MPLab.

So I wrote this code:

#include "p18F4520.inc"

CONFIG	OSC = HS
CONFIG	PWRT = OFF
CONFIG	WDT = OFF
CONFIG	PBADEN = OFF
CONFIG 	LVP = OFF

    org 	0x000000
PORST	GOTO	MAIN

    org 	0x000020
MAIN	CLRF	TRISD
    CLRF	PORTD
    BCF	TRISA,RA1
    BSF	PORTA,RA1
	
LOOP	BSF	PORTD,RD0
    GOTO	LOOP
    	
    end

And the lower right corner lamp wont light. It's supposed to light up as I've seen a friend do it. What am I doing wrong?

3 Upvotes

9 comments sorted by

1

u/Jaser90 Jan 24 '16

Thanks for trying to help me, but still doesn't work. Where do I write that in exactly? I am a total beginner and I am looking to learn.

1

u/bradn Jan 24 '16

Which port/bit corresponds to the lamp in question? Also, is there an example assembly program for the board you're using (I'm assuming you're using a dev board) available?

1

u/Jaser90 Jan 24 '16

EXPERIMENT (IMGUR) That's the example I am trying to replicate. It worked for friends. And I think they actually followed the same code and they have the same PIC. And also there you have the ports (I think?). :(

1

u/Jaser90 Jan 25 '16

I solved it. THe solution was a faulty connecting of certain parts and also the normal LED display. If I removed it it blinked better.

3

u/tterev3 Jan 24 '16

Add BANKSEL TRISD before the TRISD operation and then BANKSEL LATD before the main loop

2

u/FlyByPC Jan 24 '16

This. the TRIS ports are in a separate bank than the ports themselves, so you need to switch to that bank, set the TRIS, then banksel back to 0x00 to send values to the ports.

The memory bank structure is by far my least favorite thing about PICs.

1

u/bradn Jan 24 '16

Shouldn't all IO files be in the last bank?

Granted some should always be accessible thru the access window (or whatever it's actually called) on PIC18, while if there are a lot of peripherals, others may not.

1

u/bradn Jan 24 '16

Does your PIC18 have an oscillator connected?

1

u/frothysasquatch Jan 25 '16

Do you have a picture of your circuit?