I feel silly, but....

Status
Not open for further replies.

Vizier87

Active Member
Hi guys,
Sorry to ask such a simple question.
I'm using MikroC now (since I've been using Assembly for simple tasks) and couldn't get my PIC18F4455 to turn on an LED. I read the initialization on the PORTB part on the datasheet, which states:


Now here's my C code, trying to turn on all the PORTB pins,

C:
void main() {

     PORTB=0X00;
     LATB=0X00;
     ADCON1=0X0F;
PORTB= 0b11111111;
while (1){
}
}

Sometimes the PIC turned on well, but then it turns off again when I interrupt its supply i.e it doesn't seem to return to its original state. I suspect it might have to do with other configurations, but I suppose MikroC covers such details when I used the Project Wizard.

Any help would be highly appreciated.

Vizier87
 
Last edited by a moderator:
I can't help you with C, but you don't appear to be setting TRIS to make the pins outputs?.
 
yeah, I missed out the TRISB while pasting the code, but still, the results are the same. I'm not sure if I missed out anything else. It's just turning a pin on....
 
ANSELH has to be cleared to use most of portb they are all on by default..
 
okay, so I tried it like this:
C:
void main() {
     ANSELH=0X00
     PORTB=0X00;
     LATB=0X00;
     ADCON1=0X0F;
     TRISB=0X00;
PORTB= 0b11111111;
while (1){
}
}

But still no avail.
 
Last edited:
It sounds like you are getting a brown out. Try turning on the brownout reset in the config.

Mike.
 
My bad (thats the newer processors) set ADCON1 to anything above 0x8 (0xA if you want porta analogue) Sorry about that to many variations now..
 
It sounds like you are getting a brown out. Try turning on the brownout reset in the config.

Mike.
Hi Mike, I'm using MikroC and that's where I'm a bit at loss. During the days I'm using templates of ASM, the config part is shown properly.

However, how do I go about configuring the Config part through MikroC? Is it done as part of the code itself?

Thanks.
Vizier87
 
My bad (thats the newer processors) set ADCON1 to anything above 0x8 (0xA if you want porta analogue) Sorry about that to many variations now..

Hi Ian, I've set the ADCON1 to 0x0F. That's with accordance to the setting that I want so all PORTB pins are output.
 

I'm afraid I don't use MikroC so not sure how you set the config. Guess you'll have to check the manual.

Mike.
 
You set the config under the top menu; Project -> Edit_Project
then check the config boxes as desired.

You should have a good read through the PIC datasheet under "Special features of the CPU" chapter which described the effect of all the config options.
 

hi Mr RB,
yeah thanks for your info. I'll try to get a look into it.
 
Hi guys,
Still no avail. I changed the Brown Out to "Disabled in Hardware, SBOREN Disabled". Anything else which needs to be changed?BTW, my crystal osc is 20 MHz, just to inform you guys.
Here's a screenshot:
**broken link removed**

Vizier87
 
Also, if you have a 20MHz crystal then the top box (prescaler) needs to be 5.

Mike.
 
Set the "Brown Out Detect" to on.

Mike.

Hi, thanks for the input. The Brownout options are:
**broken link removed**
Which one should it be?

Also here's my code, please check it out:
C:
void main() {

PORTB=0x00;
LATB=0X00;
ADCON1=0X0F;
TRISB=0X00;
}
PORTB= 0b11111111;
while (1){
}

Also, how about the CPU system clock Post Scaler?
thanks.
 
Last edited:
okay, something's definitely happening, but it's pretty strange. my PORTC pins turned on!
the pins: C0,C1,C2,C6 and C7
 
With a 20MHz crystal you need the prescaler set to 5 so the PLL gets 4MHz. The output will be 96MHz and so you need to divide it by something like 4 to get a usable 24MHz. So postscaler = 4. The oscillator type should be HSPLL (High speed - pll enabled).

The brownout reset should be enabled in hardware.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…