Saturday, June 18, 2011

PIC16F688 Timer 1 Code

After developing the code on the PIC16F877A for my tachometer, I decided to use the PIC16F688 since it was cheaper and smaller.  Below is the code to run Timer 1.


// PIC16F688
// Flash LED with Timer 1
// RoboSlave.Blogspot.com 6/2011


// -----------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------
#include "system.h" // Include general file


// -----------------------------------------------------------------------
// Configuration Bits
// -----------------------------------------------------------------------
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_OFF & _CPD_OFF & _HS_OSC & _CP_OFF
#pragma CLOCK_FREQ 20000600


// -----------------------------------------------------------------------
// Defines
// -----------------------------------------------------------------------
#define TimerLED portc.0


// -----------------------------------------------------------------------
// Interrupt
// -----------------------------------------------------------------------
void interrupt( void )
{
if( pir1 & (1<<TMR1IF) )   // Handle timer1 interrupt
{
TimerLED = !TimerLED; // Toggle Timer LED
pir1.TMR1IF = 0; // Reset interrupt flag
}
}


// -----------------------------------------------------------------------
// Main Program
// -----------------------------------------------------------------------
void main( void )
{
ansel = 0x00; //Configure portC as I/O
cmcon0 = 0xFF; //Configure portC as I/O


trisc = 0x00;   //Configure port C
portc = 0x00;   //Initialize port C


tmr1h = 0x00; //should be cleared before enabling interrupts
tmr1l = 0x00;


intcon.GIE = 1; //Global Interrupt Enable bit
intcon.PEIE = 1; //Peripheral Interrupt Enable bit
pie1.TMR1IE = 1; //Timer1 Overflow Interrupt Enable bit


t1con = 0b00010001;


     while (1)
    {
}
}

Friday, June 17, 2011

Tachometer Board and Programming

Since the last post, I have been working on getting version 1.0 tachometer board up and running.  It turns out that creating the schematic and laying out the board in one night is too fast for a novice like me.  I made a number of simple mistakes that doomed the board from the schematic level.

Bad Schematic (REV -)
The two areas boxed in red were incorrect. VDD is positive 5V and VSS is ground. Apparently the PIC16F688 does not work well when it is not grounded properly. However, it does show some life.  For whatever reason, I was still able to blink an led. The delay between blinks was 5X slower than it should have been, but it did work.  This played havoc with my programming because I was convinced the board was working properly.  Finally, after a bit of programming help from the Sourceboost C forum (my thread), I managed to get Timer 1 working properly. I also have fixed my current board, shown below.

Fixed Board
The schematic has been updated to fix the errors, as well as add holes to connect to the unused pins.  I wanted this in order to use the board for prototyping in the future.

Updated Schematic (Rev A)
Updated Layout