Why Is My PIC18F45J10 Not Lighting Up an LED Despite Correct Code and Circuit?

  • Thread starter El Moriana
  • Start date
  • Tags
    Output
In summary, the problem lies in the configuration and wiring of the PIC18F45J10 for lighting up an LED. The LED-res-ground should be connected to RC7 (pin 26) and the code should be set to configure PORTC pins as output and then set the RC7 bit. The issue may be due to a lack of understanding of latches, which are important for the physical pin status. The code provided shows that the LED is not lighting up and the output of TRISC, PORTC, and RC7 are all 0x00. The solution may involve explicitly configuring all the peripheral hardware, ensuring proper connections for VSS and VDD, and using the correct oscillator and resistor values. The discrepancy between using MPL
  • #1
El Moriana
33
0
Hi,
Here goes:

Problem
I'm having trouble getting my PIC18F45J10 to light up an LED.

I have a LED-res-ground connected to RC7 (pin 26).
My code is set to configure PORTC pins to be output and then set the RC7 bit.
Upon debugging, the following is shown:
TRISC=0x00
PORTC=0x80
RC7=0x00

It can be seen here that, though the RC7 bit in PORTC is set (shown by the '10000000' in PORTC), RC7 is not... my physical circuit is also devoid of any activity and my LED stays dark.

Quick Analysis of Problem
This is purely a code problem; the circuit has been checked and is working.

A major part of the problem is certainly because I am new to programming PIC18Fxxxx's. Also, I have never dealt with latches before. My guess is that my problem has to do with latches and how they affect the physical pin status. I have only a vague inkling of what the latches actually do.. as the I/O section of the datasheet mentions only that they are "useful" and that the most relevant threads on my problem I have found mention them and assume I already know all about them.

I was about to venture into the realm of vague google searching but I might as well also ask here: what are latches and why are they useful? (the latter is not always obvious)

I have provided my code below:

Code
Code:
;Imports
	LIST P=18F45J10		;directive to define processor
	#include <P18F45J10.INC>	;processor specific variable definitions

	;Config Bits
    CONFIG	FOSC = HS	;select HS oscillator

	;Start
		ORG	0x0000	;reset
		goto	Init	;go to initialisation

	;Initialisation
Init:
		clrf	PORTC	;clear register
		clrf	TRISC	;set all pins to output
		clrf	LATC	;clear data latch
		goto 	Main	
	;Main Code
Main:

		BSF		LATC, LATC7 ;run pin high
	
Loop:		
		goto 	Loop ;Infinite Loop

		END

I have also tried setting RC7 using
Code:
BSF		PORTC, RC7
but read somewhere I should always write to latches and never the actual port.. in anycase both gave me the same result in the debugger.
 
Engineering news on Phys.org
  • #2
Make sure you configure all the peripheral hardware explicitly. Don't rely on the reset and subsequent defaults. For example disable all the comparators and a/d converters etc.

Also to debug it, change all the outputs not just the one you want.

Finally try turning the ports both on and off. Maybe you've flipped the sense of something.
 
  • #3
As Antiphon suggests, you may want to have toggle your output (say, with BTG) rather than just set and enter an infinite loop (i.e. incorporate the toggle into the loop). You should be able to generate a sqaure wave that you can then scope (assuming you have a scope here). If you don't have a scope, you can make it blink using delay statements (e.g. if your clock is 1 MHz, and it takes 4 clock cycles to execute an instruction, 32000 comparison with a NOP--two instructions, IIRC--will get you around a quarter of a second).

Back to your hardware, do you have both VSS and VDD lines hooked up to the appropriate rails, [itex]\overline{MCLR}[/itex] hooked up to your VSS through a 1k or higher pull-up resistor, and your oscillator / crystal properly attached?

EDIT: According to the datasheet, toggle is one instruction cycle, branch / comparison is two if the branch is taken, one if not.
 
Last edited:
  • #4
The 18F45J10 family parts are only 3.6V (max) VDD parts - what voltage are you running your processor at? Are you trying to sink or source current for the LED? What is the forward voltage of your LED and what resistor value are you using?

Have you tried disconnecting your circuit and measuring the pin level with a meter or scope?
 
  • #5
Managed to make it work! But it poses a new problem of sorts. This is what I found:

Usually
I code and build my files in the Microchip MPLAB IDE. Then I import the .hex to my PICKit 2 desktop app (separate from MPLAB). In the app I then write to the chip and usually click the 'On' checkbox to power the chip and start the program. This method did not work with this chip, however worked perfectly on a PIC16F690 i was playing with before.

This Time
I used the "Programmer>Select Programmer", "Programmer>Release from Reset" options in MPLAB. This method worked!

And I have no idea why ='(

Have any of you come across this before/know what is happening?

PS: I assume (due to the correct LED finally lighting up) that both my circuit and code are fine. I have checked this result across multiple PORTB and PORTC pins (changing the code each time accordingly and checking the non-LED pins with a meter) to ensure that it wasnt merely a fluke success.
 
  • #6
I think I found the reason as to why the two programs were behaving differently.
I have no oscillator connected and expected the PIC to run automatically from its internal oscillator.

I think that the "Release from Reset" option in MPLAB forces the PIC into using internal oscillation for debugging purposes. The PICKit 2 programmer app however, runs the PIC directly, with whatever settings have been coded.

Does this seem plausible or have I got it way wrong?

I have tried setting the internal oscillator manually (setting the WDTEN and FOSC2 config bits as mentioned by the data sheet, p.30). It isn't working, so either my theory was wrong or that's not the way to set the internal oscillator. Help?
 
  • #7
Progress! Managed to figure it out.
The discrepancy between MPLAB and PICKit 2 express was not due to a mysterious debug mode in MPLAB (as was my original theory above). It was, in fact, due to a combination of the MPLAB release-from-reset conditions and bad wiring on my part.

The full explanation
When one writes to a PIC from MPLAB and thereafter selects "Release from Reset" one can notice that in the PICKit 2 window it reports "setting MCLR Vdd". i.e. pin1 of the PICKit 2 is set high.

My circuit was such that my PIC MCLR pin was always and exclusively (therein lies the problem) connected to my PICKit 2 pin 1. Hence, whenever MPLAB set MCLR to Vdd, my PIC would run and happiness ensued.

The PICKit 2 express app only contains Vdd-On checkbox. Ticking it does exactly what the name implies. It sets the Vdd Pin of the PICKit 2 (pin 2) high. This does not affect the MCLR pin whatsoever. Due to my MCLR pin not being connected to Vdd in any way, it is now plain to see that the voltage-starved PIC would not turn on and run.

I have now modified my circuit accordingly and lo-and-behold, happiness ensues in both MPLAB and PICKit 2 express.

I hope I have not been too annoyingly ignorant for all those that replied earlier. Thank you for your suggestions =). Hopefully someone may profit from my mistakes, be they unfortunate enough to fall into the same ignorance-trap.

E Moriana
 

Related to Why Is My PIC18F45J10 Not Lighting Up an LED Despite Correct Code and Circuit?

1. What is a PIC18F45J10 microcontroller?

A PIC18F45J10 is a type of microcontroller, a small computer on a single integrated circuit, that is commonly used in electronic devices for controlling various functions.

2. What is a basic PIC18F45J10 output problem?

A basic PIC18F45J10 output problem refers to an issue with the output pins of the microcontroller, which are responsible for sending signals to other electronic components. This can include problems with incorrect signals, no output at all, or inconsistent output.

3. How can I troubleshoot a basic PIC18F45J10 output problem?

The first step in troubleshooting a basic PIC18F45J10 output problem is to check the connections between the microcontroller and other components. Next, you can use a multimeter to test the output pins for proper voltage levels. You can also try reprogramming the microcontroller or replacing it altogether if necessary.

4. What are common causes of basic PIC18F45J10 output problems?

Some common causes of basic PIC18F45J10 output problems include faulty connections, incorrect programming, damaged components, and power supply issues. It is also possible that the microcontroller itself may be damaged or defective.

5. How can I prevent basic PIC18F45J10 output problems?

To prevent basic PIC18F45J10 output problems, it is important to carefully follow the datasheet and instructions for programming and connecting the microcontroller. Regularly checking for loose connections and using proper power supply and grounding techniques can also help prevent issues. Additionally, using a reliable programmer and properly handling the microcontroller can help prevent damage.

Similar threads

  • Electrical Engineering
Replies
10
Views
3K
  • Electrical Engineering
Replies
9
Views
2K
  • Electrical Engineering
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Electrical Engineering
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • General Engineering
Replies
2
Views
3K
Back
Top