Microcontroller Programming, help please

Thread Starter


Joined Nov 18, 2010
Ok, it's been over a week and I am still not able to get my microcontroller to work. I am using a PIC18F2220 along with a crystal oscillator. I'm trying to get a 10 MHz square wave signal out of it. The problem I'm having is the programming. I'm using C along with PICkit 2 to program it.

My questions are:
- Are there any common mistakes people make with these chips?
- Does anyone have a pointers for getting this started?

I've programed with C++ and assembly language but never C. This is my first experience with a microcontroller.


John P

Joined Oct 14, 2008
This is the most miserable situation, where nothing at all happens and you don't know why.

First of all, does the programming device report that you have a processor at all? If not, maybe it's miswired, broken connection etc.

Second, try to find a "known good" program from somewhere else that will do something visible, like blink a light. If it loads properly, does it have the expected result? If it passes, then you can be sure that although correct code will run, yours doesn't, and is therefore not correct. From then on you can concentrate on software.


Joined May 11, 2009
What kind of C compiler do you use? Maybe you can "train" with the PICKIT debug express code. The most common n00b mistake is to forget about the config word(s) setting. Refer to section 23.0 SPECIAL FEATURES OF THE CPU. In the data sheet. A common n00b mistake is also to not be aware of that the many of the pins on your PIC have analog functions. After a reset the pins with analog functions default to to the analog function. Not as a common digital I/O. Again refer to the data sheet. Your pic also have an internal oscillator. For your first test code I recommend using this. As it is fail safe. And also disable the MCLR option. The pin used for MCLR will then be a standard input pin.
You may set the configuration(s) in MPLAB. But the best thing is to set them in the code. For documentation purpose. How this is done will vary among C compilers.
Last edited:


Joined Apr 24, 2011
First off, did you get a "clean" build, no errors, no warnings? If not, you need to fix that first. A clean build is quite possible with these things. A "warning" is usually the compiler saying "I could make up some code to do something but I do not think it means what you think it means."

Next, yeah, there are many many things to get right or nothing happens. Is the crystal oscillator a package device or a crystal with 2 caps? I've occasionally had oscillator problem when using crystals, usually just wiring. Your config setting have to be set correctly for the type of oscillator or again, nothing happens. That's one reason I try to pick devices with an internal oscillator, that can usually be poked to work.

The I/O pins all default to analog so you need to set them to digital by turning off the default on analog functions.

Even before you program the device you can simulate it inside MPLAB. That gets you past config problems.

Lastly, do learn how the in circuit debugger works with your PicKit. It can be your best friend at times like this.
Be sure the WDT and LVP is off in config also.

Once the blinky thing happens, then on to get a 10Mhz square wave:
1) A 10Mhz crystal or resonator
2) HSPLL enabled in configuration 1H register
3) Set PR2 to 0
4) Very few bits of the duty cycle register will be available at 10 Mhz, start by setting the DC1B0 and/or DC1B1 bits in CCP1CON
5) Make CPP1 pin an output, i.e. bcf TRISC,2
6) Set TMR2 prescale to 1:1, and turn it on
7) Enable CCP1CON in standard pwm mode
8) Verify output at CCP1 pin


Thread Starter


Joined Nov 18, 2010
Thanks guys for the help, I really really appreciate your time and help.

We are using CCS compiler and we followed all the steps that nickelflipper suggested, although we are having trouble setting PR2 to zero. Also, could I get more clarification on step 4? That is setting DC1B0/B1 bits in CCP1CON.
The 18f2220 data sheet is your friend, looking up ccp1con, you will see that the DC1B0 and DC1B1 are the two lsb's of the 10 bit PWM register. To understand how many bits you have to play with for the PWM duty cycle, again the data sheet gives a formula for that. Look at table 15.4 for the general trend on how many bits of duty cycle resolution are available for a given PWM frequency.

If the CCS compiler and or library is giving problems, it is simple enough to do with assembler. The steps I stated previously are directly from, guess where? ......that's right. Will not be around to discuss further, best of luck.

The ccp1con setup for a 18f26k22 (16mips) in assembler would look something like this:
Rich (BB code):
    bcf    TRISC,2,ACCESS        ;CCP1 default
;******** CCP1 *****************
    movlw    12        ;std PWM mode
    movwf    CCP1CON,ACCESS
    movlw    0        ;16 MHz (PRx + 1)*1/16000000*TMRx prescale
    movwf    PR2,ACCESS
    movlw    0        ;D.C. 50% (CCPRxL:CCPxCON<5:4>)/4(PRx+1)
    movwf    CCPR1L,ACCESS
    bsf        CCP1CON,DC1B1,ACCESS
    bcf        CCP1CON,DC1B0,ACCESS
    bsf        T2CON,TMR2ON,ACCESS        ;no prescale