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.

Extreme latency/delay issue with PIC18F4550

diogenes500

New Member
Hello,

I'm beginning to learn about microcontrollers and have run into a perplexing issue for which I can't find the answer to on any of the forums. Using the PIC18F4550, programming in c, with the standard MPLAB software and hardware (pickkit3), I can successfully compile, program, and run the code below (turn an led on/off) but there is a very long wait time between my command in software to run the program and it actually executing, around 50 seconds! Is this related to sleep mode, or turning off the watchdog? I turned the watchdog back on to no avail. I've programmed the same program in asm and have the same problem.


For completeness, here is the code.
------------------------------------

#include <p18f4550.h>
#include <delays.h>

#pragma config FOSC = INTOSCIO_EC //Internal oscillator
#pragma config WDT = OFF

#define LEDP LATDbits.LATD1
#define LEDT TRISDbits.TRISD1

void main()
{
LEDT = 0;
LEDP = 1;

while(1)
{
LEDP = ~LEDP;
Delay10KTCYx(10);
}

}
 
The MCLR of the pic, pin 1, is connected to pin 1 of the programmer (also MCLR,vpp,etc). I don't have acces to the board until tonight or I would attempt to do something with that pin now.
 
You have to set the OSCCON register to 0x73 and define the osc speed

Or you'll be in slow speed and some chips have no default which means like the 12f683 it will run a 4mhz with no setting but the 18f4550 will run in khz less then 500

Look at page 34 of the data sheet

bit 7 IDLEN: Idle Enable bit
1 = Device enters Idle mode on SLEEP instruction
0 = Device enters Sleep mode on SLEEP instruction
bit 6-4 IRCF2:IRCF0: Internal Oscillator Frequency Select bits
111 = 8 MHz (INTOSC drives clock directly)
110 = 4 MHz
101 = 2 MHz
100 = 1 MHz(3)
011 = 500 kHz
010 = 250 kHz
001 = 125 kHz
000 = 31 kHz (from either INTOSC/256 or INTRC directly)(2)
bit 3 OSTS: Oscillator Start-up Time-out Status bit(1)
1 = Oscillator Start-up Timer time-out has expired; primary oscillator is running
0 = Oscillator Start-up Timer time-out is running; primary oscillator is not ready
bit 2 IOFS: INTOSC Frequency Stable bit
1 = INTOSC frequency is stable
0 = INTOSC frequency is not stable
bit 1-0 SCS1:SCS0: System Clock Select bits
1x = Internal oscillator
01 = Timer1 oscillator
00 = Primary oscillator
Note 1: Depends on the state of the IESO Configuration bit.
2: Source selected by the INTSRC bit (OSCTUNE<7>), see text.
3: Default output frequency of INTOSC on Reset.
 
Last edited:
Thanks for the advice, Ian. Turning MCLRE Off did the trick! Chip executes as soon as I release from reset. Unfortunately, it brings up a different "problem", maybe by design. Whereas before I could halt execution in MPLAB via "hold in reset," now I cannot. Anyway to regain this kind of functionality?

Burt, I can't figure out how to set OSCCON bits. every code example I find on the internet gives a build fail. Also, I thought setting
pragma config FOSC = INTOSCIO_EC
sets the chip to use the internal clock at 1 MHz, no?
 
Yep the 18F4550 should be running with a 1MHz internal clock with your settings.

You should just be able to set the value of OSCCON directly.

e.g.

OSCCON = 0b01110010;

Should set up internal oscillator at 8MHz.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top