Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created November 7, 2015 16:31
Show Gist options
  • Save JHeld07/e139b054098191624191 to your computer and use it in GitHub Desktop.
Save JHeld07/e139b054098191624191 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/KeypadC1.h"
#include "../Library/modifiedlcd.h"
float Vres=5.0/1023.0;
float Voltage, Voltage1, VoltageP;
float Vmin=0;
#INT_AD
void int_ad_isr(){
Voltage=*Q*Vres-Vmin;
}
//Compare
#INT_CCP1
void int_ccp1_isr(){
*PORTC^= 0x01;
*PORTC^=0X02;
if (*PORTC & 0x01){
*CCPR1=*CCPR1+(VoltageP*100);
}
else{
*CCPR1= *CCPR1+((100-VoltageP)*100);
}
}
main(){
lcd_init();
*TRISC=0x00; // Port C output.
T1CON->TMR1ON = 1;
T1CON->TMR1CS=0;
T1CON->T1CKPSx=0; // Setting PS to 1
CCP1CON->CCPxMx=8; // Setup my Compare
PIE1->CCP1IE=1; // Setup my Interrupts
INTCON->GIE=1;
INTCON->PEIE=1;
*CCPR1=0;
ADCON0->ADON=1; //Turn on A/D conversion
ADCON0->CHSx=0; //Turn Channel 0 A/D on
ADCON1->PCFGx=0x0F; //Set port A all channels to Digital
*TRISA=0x0F; // Port A, 4 LSB input
*TRISB=0xF0; // Port B, 4 LSB output, 4 MSB input.
ADCON2->ADFM=1;
// Enable References
ADCON1->VCFG0=1; // Enables -Voltage reference, used in A/D conversion. Allows usage of voltages lower then zero.
ADCON1->VCFG1=1; // Enables +Voltage reference, used in A/D Conversion.
//A/D Interupet setup.
PIE1->ADIE=1; // Enables A/D external interupt.
INTCON->GIE=1; // Because RCON->IPEN=0 INTCON->GIE=1 enables all unmasked intrrupts.
INTCON->PEIE=1; // Enables all unmasked peripherial interrupts.
INTCON2->RBPU=0; // PortB Pull up resistors are enabled
while(1){
VoltageP=Voltage1;
Voltage1=((Voltage/5)*100); // Converts voltage from pot to a % value.
printf(lcd_putc,"\f %2.0f %1.4f", Voltage1, Voltage);
printf(lcd_putc,"\n %f", VoltageP); //Print Pot voltage, print keypad value.
ADCON0->GODONE=1; //Go preform A/D conversion
delay_ms(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment