Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created November 18, 2015 03:23
Show Gist options
  • Save JHeld07/2507de7a4fe67f57a322 to your computer and use it in GitHub Desktop.
Save JHeld07/2507de7a4fe67f57a322 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;
#INT_AD
void int_ad_isr(){
Voltage = *Q * Vres;
}
//Frequency counter stetup
unsigned int OFCnt=0;
#INT_TIMER1 // Counter Overflows
void Int_timer1_isr(){
OFCnt++;
}
//Frequency counter
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;
}
BOOLEAN flag=0;
#INT_CCP1
void int_ccp1_isr(){
*PORTC ^= 0x01;
*CCPR1=*CCPR1+1250;
}
main(){
float time; // Establish time variable
lcd_init(); //inialize the LCD display
ADCON0->ADON = 1; // Enable A/D Converter module
ADCON0->CHSx = 0; // Channel 0
ADCON1->PCFGx = 8; // Setting up which pins are A vs D input.
*TRISA = 0x01; // A0 input
ADCON2->ADFM = 1; // RJ
// Interrupt Setup
PIE1->ADIE = 1; //
INTCON->GIE = 1; // Enable all unmasked interrupt
INTCON->PEIE = 1; // Enables all unmaked peripheral interrupts
// Setup Overflow
T1CON->TMR1ON=1; // Turn Timer1 on
T1CON->T1CKPSx=0; // Setting PS to 1
// Setup my Interrupts
PIE1->TMR1IE=1; // TMR1 overflow Interupt
PIE2->CCP2IE=1; // CCP2 interrupt bit
CCP2CON->CCPxMx=4; // Setup Capture falling edge CCP2
//PWM
*TRISC=0x02; // Port C output W/ Pin2 input.
PIE1->CCP1IE=1; // Enables CCP 1 interrupt bit
T1CON->RD16=1; // Enables Read/Write in one 16 bit timer
T1CON->T1RUN=1; // Device clock is from T1 timer
CCP1CON->CCPxMx=8; // Setup Compare CCP1
*CCPR1=0;
*CCPR2=0;
while(1){
time=dT*0.2e-6;
printf(lcd_putc,"\f %fV", Voltage);
printf(lcd_putc,"\n Freq=%f", 1/time);
ADCON0->GODONE = 1;
delay_ms(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment