This is the second article dealing with my experiments with the PIC 16F628 and the Arduino programmer. The first thing I tried was the usual blinking LED. I then tried sending data to the Raspberry Pi using the serial port. The datasheet for the 628 mentions several different ways of controlling the clock speed, including the internal oscillator, an external crystal, an external RC timer and an external 'logic level' clock signal.

PIC and slow clock

I recently bought some CD4047 for just that purpose: unlike the bog-standard 555 timers, they can work at 3.3 or 5v so the same circuit could work with the Raspberry Pi or a 5v TTL circuit. I wired up the chip according to the datasheet (the one I found was from Fairchild). Pins 4,5,6 & 14 are connected to the 5v power. Pins 7,8,9 & 12 are connected to ground. The resistor goes between pins 2 & 3, with the capacitor between 1 & 3. I used a 500MΩ potentiometer and 1μF capacitor. One output went to the OSC1 pin of the 628, the other went

4047 pulse generator

Tthrough a resistor and LED so I had a visual indication of the frequency.

The next step was to modify the blink code to use the external clock signal. After a bit of trial and error, I found that I needed the  FOSC = ECIO setting.

 The final C code looked like

#pragma config FOSC = ECIO    // External clock on OSC1, OSC2 = RA6 as IO
#pragma config LVP = OFF      // Low-Voltage programming off
#pragma config PWRTE = OFF    // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = ON     // MCLR Pin Function Select->MCLR/VPP pin function is MCLR
#pragma config CP = OFF       // Flash Program Memory Code Protection
#pragma config BOREN = OFF    // Brown-out Reset Enable->Disabled
#pragma config WDTE = OFF     // Watchdog Timer Enable bit (WDT disabled) 

#include <xc.h>
#include <pic16f628.h>

void main(void) {
    TRISB0 = 0;
    while(1) {
        RB0 = 1;
        RB0 = 0;
    }
}

The PIC takes 4 clock cycles to execute each instruction, so the LED attached to the microcontroller should flash at less than 1/4 the speed of the one attached to the 4047. It should be off for twice as long as it is on, since the loop instruction will also take time.