Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problems turning an LED on-PIC16F54

Status
Not open for further replies.

zanes

New Member
Ok, I've got my PICkit2 and a PIC16F54 hooked together, and, as my first program, I'm trying to get a LED to turn on. Nothing fancy, but I'm having problems. Ok, here's my code, with some comments;
Code:
;*****
;Alex E-S
;4/4/2008
;Version 1
;for PIC 16f54
;Clock 2.4576MHz
; Function: Turns LED on

	list P = 16F54
	include "c:\PIC\P16F5X.inc"
	_config _RC_OSC & _WDT_OFF & _CP_OFF
;==========

;Declarations:

porta 	equ 05h

	org 1FFh
	goto Start
	org 0
	
	
;==========

; Subroutines:

Init	clrf porta ;Resets
;	clrf portb ;I/O ports

	movlw	b'0000' ;Sets porta
	tris porta 	; as all outputs
;	movlw 	b'00000000' ;sets portb
;	tris portb	    ;as all outputs
	retlw 0		    ;resets working reg to 0

;==========
;Program Start
Start call Init
Main bsf porta,0 ;sets RA0 to 1 (+5v)
	goto Main  ;loops back to main
	END

Right, the PIC is on breadboard. Pin 4 is hooked to pin 1 on my PICkit 2 (the one with the arrow above it).
Pin 5 is hooked to ground, which is hooked to pin 3 on the PICkit.
Pin 12 is hooked to pin 5 on the PICkit.
Pin 13 is hooked to pin 4 on the pickit.
Pin 14 is hooked to +5V, which is pin 2 on the pickit.
Pin 15 is hooked to one side of a 2.4576 crystal, Pin 16 is hooked to the other. Each side of the crystal is hooked to ground (pin 3 on pickit) via a 22pF capacitor.
Pin 17 (RA0) is hooked to a LED, which is connected to ground via a 470 ohm resistor.
VDD is set to +5.0V in the PICkit software, the box next to it is checked
The/MCLR box in the PICkit software is unchecked.
The pickit has the Power and Target LEDs lit constantly, busy flashes while writing.
The code was assembled using MPLAB, then the pickit software was used to write the hex file to the PIC.
I'm leaning towards a hardware fault, as it is my first time using crystals, and I don't get error messages whilst assembling/writing the PIC. Thanks in advance.
 
zanes said:
Pin 15 is hooked to one side of a 2.4576 crystal, Pin 16 is hooked to the other. Each side of the crystal is hooked to ground (pin 3 on pickit) via a 22pF capacitor.
Since you're using a crystal, configure the PIC accordingly.

__CONFIG _XT_OSC & _WDT_OFF_ & _CP_OFF

Also, the Vpp pin on the PIC (4) is used as a /MCLR pin and it should be connected to Vdd via a resistor (I usually include a diode in series too).


 
Last edited:
Ok, changed that. The only bit of code I pinch from the sample program in my book, and it's not right! (the book uses an RC oscillator for this example.) But there's still no change. Writing to the pic finishes, and the pic does nothing.
 
zanes said:
Ok, changed that. The only bit of code I pinch from the sample program in my book, and it's not right! (the book uses an RC oscillator for this example.) But there's still no change. Writing to the pic finishes, and the pic does nothing.

What book is that?
 
You'll love this. I looked at my code close up, and note that the config only has one "_" before it. Changing it to __config fixed the problem! Thanks to all!!
 
Oh blueroom, the one I talked about in my adder thread,The PIC Microcontroller, Your Personal Introductory Course, by John Morton.
 
you mention that this is your first time using a crystal, so I'll ask an obvious question, you have remembered to take a 20-33pF ceramic cap between each end of the crystal and ground/vss havent you ? and have sufficient supply decoupling caps on the breadboard ?

I once got caught out with exactly this, although I didn't forget the caps, just a wee wire on the breadboard taking the line back to gnd had come out the breadboard and was hanging in the air just above where it should have been, but it kept me going for hours because stray capacitance caused all sorts of startup problems, sometimes it would start, other times it would start fine then crash during execution etc etc...it all worked fine to begin with...must be buggy code I was thinking...mistake :>) check and double check the circuit is exactly how you expect it should be.

Keep your initial code as simple as possible until you verify the breadboard implementation works as you expect. no sub routines or paging, just an org, a goto, a pin state change, and a bsf etc.

something along the lines of

Code:
list P=16F54
include "c:\PIC\P16F5X.inc"

__config _HS_OSC & _WDT_OFF & _CP_OFF ;<edited this daft mistake, just in case someone copies this and falls into the same trap !

equ porta=h'05'
equ myled=h'00'


org h'00'      ;set start of memory
goto start    ;start the main loop

start:                 ;main loop starts here

clrf porta            ;clear junk on port
movlw h'fe'          ;set pin state mask ready for change
tris porta            ;change porta bit 0 to an output
bsf porta,myled    ;turn on my led

loop:                  ;main loop starts here
goto loop            ;go back and loop again

end

I hope this helps you out a bit :>)
 
Last edited:
ahh...that's what I get for copy/pasting ;)
I copied your initial code and edited it...duh...glad you got it working :)
 
zanes said:
I'm confused, hasn't that program set the indirect address register to "My LED"?

No, it's set every occurance of "myled" in the program to be replaced by the number zero - in this case the zero refers to the bit number of a port, which can be from 0 to 7.

All the EQU command does (and the include file does) is a simple text substitution during the first pass of the assembler.

Incidently, it's pointless adding an EQU for porta, as it's already done in the include file.
 
hehe...just force of habit Nigel :)

I come from the days when everything had to be explicitly defined within your source...

I rely on my equ statements, and use the header as a fall back for the things I have forgotten about :)

rgds
 
quote" __config _HS_OSC & _WDT_OFF & _CP_OFF"

whether i write HS,XT,LP or RC mplab returns an error saying "Illegal Opcode"
can anyone please tell me why?? why should the VDD Pickit2 ON box be ticked if you are not using any external supply? thanks
 
quote" __config _HS_OSC & _WDT_OFF & _CP_OFF"

whether i write HS,XT,LP or RC mplab returns an error saying "Illegal Opcode"
can anyone please tell me why??

If you post your code pehaps we can tell you?, make sure to use the 'code' tags to format it correctly.

why should the VDD Pickit2 ON box be ticked if you are not using any external supply? thanks

Because, fairly obviously, your target processor needs power to work.
 
hi, i solved the problem fortunately by now. still thanks a lot for your help....

another problem still arose.. pickit2 doesn't seem to erase, it says that it did erase, but if i press read, or blank check, both say that the program memory didn't really erase... what should i do? should i reload the os to the programmer? please help i am really desperate :'(
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top