// Tyler Grandahl
// 16F867A Tachometer
//
#include <htc.h>
#define _XTAL_FREQ 20000000 // define the clock freq
/* Program device configuration word
* Oscillator = HS
* Watchdog Timer = Off
* Power Up Timer = Off
* Brown Out Detect = On
* Low Voltage Program = Enabled
* Flash Program Write = Write Protection Off
* Background Debug = Disabled
* Data EE Read Protect = Off
* Code Protect = Off
*/
__CONFIG(HS & WDTDIS & PWRTDIS & BOREN & LVPEN & WRTEN & DEBUGDIS & DUNPROT & UNPROTECT);
// Global Variables
volatile char hall_count;
volatile char time_count;
volatile long time;
volatile short freq;
// Peripheral initialization function
void init(void)
{
/***** Common Code ****
// port b interrupts enabled
* Global interrupt disabled during initialization
*/
INTCON = 0b00001000;
/* Port directions: 1=input, 0=output
*/
TRISB = 0b11111111;
//Timer 1:enabled
//1:1 prescaller
//
T1CON = 0b00000001;
ei(); // Global interrupts enabled
}
/***************************************/
/*************** Main Function *********/
/***************************************/
void main()
{
hall_count = 0; // clear the counter just incase its not zero
time_count = 0; // clear the timer counter just incase
init(); // function call to initilization function to get things started
while(1)
{
if(hall_count == 3)
{
// the time calculation
time = 0;
time = (TMR1L+(256*TMR1H)); // calculate the elapsed time in uS and write to time register
time = (time + (64000*time_count));
freq = (5000000/(time/3));
T1CON = 0b00000000; //disable timer 1 to record the elapsed time
time_count = 0; // reset counter values
TMR1H = 0;
TMR1L = 0;
hall_count = 0;
T1CON = 0b00000001; // re enable timer 1 for another count
}
// timer overflow counter, polls value of TMR1H, when high the timer is stopped, zeroed, and time_count is increminted.
if (TMR1H >= 250)
{
T1CON = 0b00000000; //disable timer 1 to record the elapsed time
time_count ++;
TMR1H = 0;
T1CON = 0b00000001; // re enable timer 1 for another count
}
}
}
/*****************************/
/********* INTERRUPTS ********/
/*****************************/
void interrupt my_isr(void)
{
if((RB4 == 1))
{
//__delay_us(200);
//CLRWDT();
//if((RB4 == 1))
// {
hall_count ++;
if (hall_count == 3)
{
T1CON = 0b00000000; //disable timer 1 to record the elapsed time
//INTCON = 0b01000000;
}
// }
}
RBIF = 0;
}