I got my first PIC chips in the mail the other day, and I’ve been working with them a bit. They’re surprisingly complex for such tiny, cheap things. One obstacle you’ll encounter when first starting is configuring the clock source. At first, you write a small program that sets the TRISB register and twiddles the PORTB bits and hope to see and LED light up.
So, first roadblock. Nothing happens at all when you do this on the chips I got (PIC18F14K50). You have to enable the internal clock source with the correct fuse bits. Luckily, you don’t have to know which bits directly, Microchips C compiler lets you configure these things via pragmas with mnemonics. And, since each chip has its own set of values with different names and bits in the configuration memory, there’s a GUI in the mplabx IDE that lets you simply select which options you want.
So you enable the internal oscillator and get things to work, but how fast is it running? It’s pretty easy to tell with an oscilloscope. Write a program that sets PORTB to output, then alternatively to 0xff, 0×00, 0xff, 0×00, etc. Do this a few times without a loop, and then put all that into an infinite loop. Build the project and look at the disassembly to confirm that it’s really only taking one instruction every time it sets PORTB, and it is. Look at one of the PORTB pins on your oscilloscope and you’ll see something like this.
What I’ve done here is enabled the 16MHz oscillator, configured it to use the /2 divider and the 4x PLL. So it should be oscillating at 32MHz. What you see on the scope is a single pulse from the port. The beginning of the pulse rise to the beginning of the pulse fall is one instruction. It’s taking 2 and a half time divs with each div being 0.05 microseconds, or a 0.125 microsecond pulse. 1 / 0.000000125 is only 8MHz, what gives? Each instruction takes 4 clock cycles on the PIC, it isn’t pipelined. This is exactly what we hoped to see here.
I’m glad I have s scope here, because confirming that you really are setting the oscillator bits correctly is difficult without it. My next task is to get the 12MHz crystal oscillator hooked up and working, and then dive into the USB library. I feel that I’m most of the way there already.






