Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created October 9, 2015 02:13
Show Gist options
  • Save JHeld07/2f4f6c6bc2772aadc4c8 to your computer and use it in GitHub Desktop.
Save JHeld07/2f4f6c6bc2772aadc4c8 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;
float Vmin=0;
#INT_AD
void int_ad_isr(){
Voltage=*Q*Vres-Vmin;
}
main(){
int16 press1, press2, VoltagePer;
lcd_init();
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.
*TRISC=0x00; // Port C output.
ADCON2->ADFM=1; //RJ
//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.
//Interupt 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){
press1=(int16)KeyPress()-0x30; //Takes the ASCII value from the keypress and removes the hex value.
Voltage1=((Voltage/5)*100); // Converts voltage from pot to a % value.
if(press1<=9 && press1>=0){ // Filters out any value other than 0-9
press2=((press2*10)+press1); // Takes the first press, saves it for use when adding press1 to press2
if(press2>100){ // if the press2 value is greater than 100 reset press2 to 0 value.
press2=0;
}
}
printf(lcd_putc,"\f %2.0f", (Voltage1-press2)); // print the difference of %voltage and press2
printf(lcd_putc,"\n %1.2fV %lu", Voltage, press2); //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