Jump to content
McNerdius

Newbie To 18f Failing To Flash Led (18f26j11)

Recommended Posts

OK OK i feel like an idiot. I'm putting this here because it's so incredibly common when learning the ins and outs of a new product family to COMPLETELY AND TOTALLY miss something. I've googled to no avail in regards to some bits of code out there... I'm new to this family and have done very little with 18Fs in general. All i'm doing at this point is trying to flash an LED on portc - just to get my feet wet. Here is what i have (sorry it's so darn long... i'm including it ALL)

#include <system.h>

 

//////////////////////////////////////////////////////////////////

// Watchdog Timer:

#pragma config WDTEN = OFF // Disabled - Controlled by SWDTEN bit

 

//////////////////////////////////////////////////////////////////

// Stack Overflow/Underflow Reset :

#pragma config STVREN = ON // Enabled

 

//////////////////////////////////////////////////////////////////

// Extended Instruction Set:

#pragma config XINST = ON // Enabled

 

//////////////////////////////////////////////////////////////////

// Code Protect:

#pragma config CP0 = OFF // Program memory is not code-protected

 

//////////////////////////////////////////////////////////////////

// Oscillator:

#pragma config OSC = INTOSC // INTOSC

 

//////////////////////////////////////////////////////////////////

// T1OSCEN Enforcement:

#pragma config T1DIG = ON // Secondary Oscillator clock source may be selected

 

//////////////////////////////////////////////////////////////////

// Low-Power Timer1 Oscillator:

#pragma config LPT1OSC = ON // Low-power operation

 

//////////////////////////////////////////////////////////////////

// Fail-Safe Clock Monitor:

#pragma config FCMEN = ON // Enabled

 

//////////////////////////////////////////////////////////////////

// Internal External Oscillator Switch Over Mode:

#pragma config IESO = OFF // Disabled

 

//////////////////////////////////////////////////////////////////

// Watchdog Postscaler:

#pragma config WDTPS = 1 // 1:1

 

//////////////////////////////////////////////////////////////////

// DSWDT Clock Select:

#pragma config DSWDTOSC = INTOSCREF // DSWDT uses INTRC

 

//////////////////////////////////////////////////////////////////

// RTCC Clock Select :

#pragma config RTCOSC = INTOSCREF // RTCC uses INTRC

 

//////////////////////////////////////////////////////////////////

// Deep Sleep BOR :

#pragma config DSBOREN = OFF // Disabled

 

//////////////////////////////////////////////////////////////////

// Deep Sleep Watchdog Timer:

#pragma config DSWDTEN = OFF // Disabled

 

//////////////////////////////////////////////////////////////////

// Deep Sleep Watchdog Postscaler:

#pragma config DSWDTPS = 2 // 1:2 (2.1 ms)

 

//////////////////////////////////////////////////////////////////

// IOLOCK One-Way Set Enable bit:

#pragma config IOL1WAY = OFF // The IOLOCK bit (PPSCON<0>) can be set and cleared as needed

 

//////////////////////////////////////////////////////////////////

// MSSP address masking:

#pragma config MSSP7B_EN = MSK7 // 7 Bit address masking mode

 

//////////////////////////////////////////////////////////////////

// Write/Erase Protect Page Start/End Location:

#pragma config WPFP = PAGE_63 // Write Protect Program Flash Page 63

 

//////////////////////////////////////////////////////////////////

// Write/Erase Protect Region Select bit (valid when WPDIS = 0):

#pragma config WPEND = PAGE_0 // Page 0 to WPFP<5:0> erase/write-protected

 

//////////////////////////////////////////////////////////////////

// Write/Erase Protect Configuration Region :

#pragma config WPCFG = OFF // Configuration Words page not erase/write-protected

 

//////////////////////////////////////////////////////////////////

// Write Protect Disable bit:

#pragma config WPDIS = OFF // WPFP[5:0], WPEND, and WPCFG bits ignored

 

void main()

{

osccon = 0b01101100;

ancon0 = 255; ancon1 = 0b00011111; //disable analog crapola

trisa = 0; porta = 0;

trisb = 0; portb = 0;

latc = 0; trisc = 0; portc = 0;

 

while (true)

{

portc=255;//latc=255;

delay_ms(1);

portc=0;//latc=0;

delay_ms(1);

}

}

 

 

 

as you can see, i've set my tris values, my oscillator setup should be ok, i've disabled analog, disabled WDT, and everything else i've ever needed to do and everything i need to do as i understand it. NO WORKY. :) If anyone can be so kind as to give it a quick look-see and maybe even tell me what this LATC/LATB/etc stuff is (i mean, in practical language, not manual-speak) i would much appreciate it ! Thanks.

Share this post


Link to post
Share on other sites

I have not gone through the complete listing, but try putting the following to OFF instead:

 

// Extended Instruction Set:
#pragma config XINST = ON // Enabled

Share this post


Link to post
Share on other sites

You might want to increase your delay a bit.

 

It looks like you only have a 1ms delay. If so, you're won't see much on an LED.

 

delay_ms(1);

 

Better: delay_ms(500);

Share this post


Link to post
Share on other sites
//////////////////////////////////////////////////////////////////

// Extended Instruction Set:

#pragma config XINST = ON // Enabled

Turn this off, BoostC doesn't support the extended instruction set.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Will do on the XINST (when i get home).

 

I meant for that to be a delay_s(1) ... odd... my fingers are betraying me. However, i've recently changed the loop to:

 

while (true)

{

 

latc = 255;

}

 

still no luck.

Share this post


Link to post
Share on other sites

From the data sheet in section 10.4 there is a little code fragment to help out new users. If you have not read the data sheet before posting your question then shame on you. If you have then sorry to scold.

 

Also, check out section 21.0 and have a look at ANCON0 and ANCON1. Now that I see the port defs of ANCON0 and ANCON1 I see that the code in the data sheet just gives you a hint. If you look at these bit definitions in sections 21.3 and 21.4 you will have your answer. I'm being a jerk and not giving it to you so that you get the data sheet and learn how to read it.

 

go to http://www.microchip.com or here to get the data sheet:

http://ww1.microchip.com/downloads/en/DeviceDoc/39932D.pdf

 

 

CLRF LATC; Initialize PORTC by  
			; clearing output
			; data latches
MOVLW 0x3F; Value used to	   
			  ; initialize data
			  ; direction
MOVWF TRISC; Set RC<5:0> as inputs 
				 ; RC<7:6> as outputs
MOVLB 0x0F; ANCON register is not in Access Bank
BSF ANCON1,PCFG11
			  ;Configure RC2/AN11 as digital input

Share this post


Link to post
Share on other sites
From the data sheet in section 10.4 there is a little code fragment to help out new users. If you have not read the data sheet before posting your question then shame on you. If you have then sorry to scold.

 

Also, check out section 21.0 and have a look at ANCON0 and ANCON1. Now that I see the port defs of ANCON0 and ANCON1 I see that the code in the data sheet just gives you a hint. If you look at these bit definitions in sections 21.3 and 21.4 you will have your answer. I'm being a jerk and not giving it to you so that you get the data sheet and learn how to read it.

 

 

void main()

{

osccon = 0b01101100;

ancon0 = 255; ancon1 = 0b00011111; //disable analog crapola

trisa = 0; porta = 0;

trisb = 0; portb = 0;

latc = 0; trisc = 0; portc = 0;

 

while (true)

{

portc=255;//latc=255;

delay_ms(1);

portc=0;//latc=0;

delay_ms(1);

}

}

 

It's all there in my post man.

 

you'll note that i set bit six of adcon1 as 0, just as page 353 of the manual instructs. I've read the manual, and as a side note - the topic of my post is "Newbie to 18F failing to flash Led." I know it's common for newbies to post expecting a solution without having googled, read the manual, or experimented. That's not the case here. If the code i posted in my original post looks flawed, let me know.

Share this post


Link to post
Share on other sites

Just as a sanity check. Is all the hardware working correctly? I've placed an LED in backwards once, actually quite a few times. If you're convinced the code is correct, then it must be and the problem lies elsewhere.

Share this post


Link to post
Share on other sites

Pics of hardware attached. I had been assembling it on a SBB, then said screw it and put it on a perfboard.

 

I'm hoping at this point all that i need to do is use a higher-quality capacitor. It's always something stupid.

post-3676-1304035502_thumb.jpg

post-3676-1304035521_thumb.jpg

Share this post


Link to post
Share on other sites

Have you turned off extended instruction set? SourceBoost compilers don't support it and code that they produce won't run correctly if extended instruction set is enabled.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
Have you turned off extended instruction set? SourceBoost compilers don't support it and code that they produce won't run correctly if extended instruction set is enabled.

 

Regards,

Pavel

 

Yep. Good news too... well, news anyway. It works via debug mode. I'd personally never used debug on anything, so it took a bit of fiddling to figure it out this evening, but sure enough - things run nicely via debug. As i'm not familiar with In-Circuit-Debugging, i can only make guesses as to what this means about my project. The one and only thing that comes to mind is clocking. My configuration bits and osccon are correct, therefore i assume my problem is the aluminum electrolytic capacitor i am using for vddcore. Then again... That's just an uneducated guess.

Edited by McNerdius

Share this post


Link to post
Share on other sites
Pics of hardware attached. I had been assembling it on a SBB, then said screw it and put it on a perfboard.

 

I'm hoping at this point all that i need to do is use a higher-quality capacitor. It's always something stupid.

 

Looking at your pics. It might be prudent to add a current limiting resistor between the I/O pin and your LED. I'm no expert on the 18FPICS, but the mid-range pics limit the I/O pin source/sink capabilities to +/- 20mA. I typically use 1.5V for an LED forward voltage. The formula to calculate the correct resistance:

 

R[ohms] = (power supply voltage[VDD]- LED forward voltage[Vf]) / LED operating current.

 

Example:

 

R = (5[VDD] - 1.5[Vf]) / 10mA

 

R = 350 ohms, use a standard 330 ohm resistor.

 

Other than that, I would say your circuit is O.K. The electrolytic capacitor looks fine, just make sure the polarity is correct.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...