// timerTest.c - simple test program for the Microchip PICDEM mechatronics board
//
// interrupts 0.1mSec blinks a LED
// connect terminal emulator program to COMM port at 57600 baud
#include <htc.h>
#include "uart.h"
// Setup configuration bits
__CONFIG (EC & WDTDIS & PWRTDIS & UNPROTECT & MCLREN & BORDIS & FCMEN & IESOEN);
volatile int tenthMillSecCounter=0; // incremented every 10mSec
// Timer 0 is programmed to interrupt every 0.1mSeconds
// T0 clock is FOSC/4 = 200000
// with 1:2 prescaler decrements 2000000/2 times / second
// to get an interrupt every 0.1 mSec the calculation for TMR0 overflow from 0xFF to 0 is
// count = 255 - 1000000/10000 = 255 - 100
// i.e. for 0.1mSec counter is
#define tenthMillSec 155
static void interrupt isr(void)
{
if (T0IF) // A TMR0 interupt occurred
{
TMR0=tenthMillSec; // increment counters every .1 mSec
tenthMillSecCounter++;
}
T0IF=0; // clear TMR0 interrupt flag
}
// delay for count tenths of a second
void tenthMilliSecondDelay(int count)
{
tenthMillSecCounter=0; // zero counter
while( tenthMillSecCounter < count) ; // wait
}
// initalise clock, set port A for digital input, set up timer 0, etc
void systemInitialise(void)
{
OSCCON=0x70; // Fosc use oscillator 8MHz
ANSEL=0; // set digital inputs
OPTION=0x80; // TMR0 RATE 1:2
TMR0=tenthMillSec; // initialise timer 0 counter
T0IE = 1; // enable interrupt on TMR0 overflow from 0xFF to 0
GIE = 1; // Global interrupt enable
}
//extern int tenMillSecCounter, second, tenthsSecond;
// test program
void main(void)
{
int i=0, t, adc;
char ch='0';
systemInitialise();
UARTInitialise(); // initalise UART for rS232 serial output
TRISA1=1; // RA1 is switch input
TRISD6=0; // RD6 is LED output
TRISD5=0; // RD5 is LED output
RD6=1; // set LED
putString("\n\rTest program for Microchp PICDEM mechatronics board\n\r\n\r");
putString("\n\r");
while(1)
{
putchar(ch);
if(ch++ == '9') ch='0';
RD6=!RD6; // invert LED to blink it
tenthMilliSecondDelay(10000);
}
}