PIC 100 Hz generator

This program uses the internal RC oscillator of a PIC12F509 to create a 100 Hz square wave signal on GP5 (pin 2), as a solution for a problem posted on EEVblog. A nice feature of the PIC12F509 is the +/-1 % factory calibrated internal 4 MHz RC oscillator (and relatively stable over a wide range of supply voltage and temperature), so this 100 Hz generator needs just the PIC and no other parts (but a 100 nF decoupling capacitor is recommended). The Oscillogram for this program, at room temperature and 5V supply voltage:

This was my first test of MPLAB X and the IDE looks nice, more modern than the old MPLAB IDE. But sometimes it needs a second until a configuration dialog is opened. The user interface of the old MPLAB IDE was faster.

Simulation of the project in the IDE:

The Logic Analyzer shows the number of instruction cycles. One instruction cycle needs four clocks, so 10,000 instruction cycles results in 100 Hz for the 4 MHz internal oscillator. For fine tuning the number of cycles you could count the instruction cycles manually (which is not difficult, because most instructions need one cycle, except the call/retlw/goto instructions, which need two cycles). I set a breakpoint in the code and used the "Stopwatch" tool.

The project files with source code and hex file in PIC100.X\dist\default\production: pic12f509.zip

Slightly modified for the PIC12F675: pic12f675.zip

PIC pitfalls

When reading the GPIO register, it reads the voltage levels on the pins, even if the pin is configured as output. This is the reason I use an extra register in my code to implement the toggle function, and toggle the bits in this register, then moving the value to the GPIO register, because if an external device pulls the pin to ground, a wrong value could be read. Same if you read immediatly after a write and if there is a large capacitive load connected to the pin.

The PIC12F509 and PIC12F675 has a factory calibrated oscillator value, encoded as a movlw instruction at 0x3ff. If you erase the whole device, it is gone and you have to calibrate it manually. Use "Window->PIC Memory Views->Program Memory" for new PICs and make a note of the instruction at 0x3ff to restore it, if it gets deleted.

5. Januar 2014, Frank Buß