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.

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:

CLRF PORTB ; Initialize PORTB by
; clearing output
; data latches
CLRF LATB ; Alternate method
; to clear output
; data latches
MOVLW 0Eh ; Set RB<4:0> as
MOVWF ADCON1 ; digital I/O pins
; (required if config bit
; PBADEN is set)
MOVLW 0CFh ; Value used to
; initialize data
; direction
MOVWF TRISB ; Set RB<3:0> as inputs
; RB<5:4> as outputs
; RB<7:6> as inputs

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:
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.... :(
 
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.
 
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
 
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

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. :)
 
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:
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.
Back
Top