Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created December 10, 2015 18:46
Show Gist options
  • Save JHeld07/686bde7312acdc390967 to your computer and use it in GitHub Desktop.
Save JHeld07/686bde7312acdc390967 to your computer and use it in GitHub Desktop.
#include <18f4520.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOLVP
#include "../../Library/18f4520ptr.h"
#include "../../Library/modifiedlcd.h"
float Vres=5.0/1023.0;
float Voltage;
float Voltage1;
float Vmin=0;
//Frequency counter
unsigned int OFCnt=0;
#INT_TIMER1 // Counter Overflows
void Int_timer1_isr(){
OFCnt++;
}
unsigned int32 tstart, tstop, dT;
#INT_CCP2
void int_ccp2_isr(){
tstop = *CCPR2; //Get the time off the system...
dT = (OFCnt*0x10000)-tstart+tstop;
tstart = tstop;
OFCnt = 0;
}
#INT_AD
void int_ad_isr(){
Voltage=*Q*Vres-Vmin;
}
main(){
float RPM, time;
int16 press1, press2;
lcd_init();
//PWM
*TRISC=0x02;
CCP1CON->CCPxMx=0xC;
T2CON->TMR2ON=1;
*PR2=100; //PR2 is the period driver.
//Frequeny counter
T1CON->TMR1ON=1;
T1CON->TMR1CS=0;
T1CON->T1CKPSx=0; // Setting PS to 1
CCP2CON->CCPxMx=4; // Capture Falling edge
PIE1->TMR1IE=1; // Overflow Interupt
PIE2->CCP2IE=1;
INTCON->GIE=1;
INTCON->PEIE=1;
//A/D Conversion
ADCON0->ADON = 1;
ADCON0->CHSx = 0;
ADCON1->PCFGx = 8;
*TRISA = 0x01; // A0 input
ADCON2->ADFM = 1; // RJ
// Interrupt Setup
PIE1->ADIE = 1;
INTCON->GIE = 1;
INTCON->PEIE = 1;
INTCON2->RBPU=0; // PortB Pull up resistors are enabled
while(1){
Voltage1=((Voltage/5)*100);
time=dT*0.2e-6; // PS 1 and Fosc 20m
printf(lcd_putc,"\fRPM=%f", (1/((161*time)/60)) ); // Prints and calculates RPM speed.
printf(lcd_putc,"\n%2.2FV Percent=%2.0f", Voltage, Voltage1);
*CCPR1=Voltage1; // Controling the Duty Cycle
delay_ms(300);
ADCON0->GODONE=1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment