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)
    {
}
}

1 comment:

  1. I find your code sexy. I want to kiss you always.

    -Robot Alex

    ReplyDelete