Created
October 7, 2015 19:19
-
-
Save JHeld07/e687061aa01c677d8662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <18F4520.h> | |
#use delay (clock = 20000000) | |
#fuses HS, NOWDT, NOLVP | |
#include "../4520_B.h" | |
#include "../modifiedlcd_B.h" | |
#define COL0 0x6 | |
#define COL1 0x5 | |
#define COL2 0x3 | |
#define ROW0 0xE | |
#define ROW1 0xD | |
#define ROW2 0xB | |
#define ROW3 0x7 | |
unsigned int16 *AD=0xFC3; //right justified | |
float Vres = 5.0/1023.0; | |
float voltage; | |
#INT_AD | |
void int_ad_isr(){ | |
voltage = (Vres *(float)(*AD))/5*100; | |
} | |
struct _KeyPad{ | |
int columns:3; | |
int unused:1; | |
int rows:4;}; | |
struct _KeyPad *KEYPAD = 0xF81; | |
char KeyPadPress(void); | |
int twoDigitNumber(currentValue); | |
int16 currentValue; | |
main(){ | |
lcd_init(); | |
*TRISA = 0x01; //A0 input | |
*ADCON0 = (*ADCON0 | 0x01) & 0x03; //ch0, AD-ON | |
*ADCON1 =0x0E; //from modifiedlcd.h | |
*ADCON2 |= 0x80; //right justified, 16 TAD, FOSC/2 | |
*TRISB = 0xF0; //B7-B4 input | |
INTCON2->RBPU = 0; //pullups ON | |
*PIE1 |= 0x40; //A/D int enable | |
while(1){ | |
*ADCON2 = 0x02; | |
currentValue = twoDigitNumber(currentValue); | |
printf(lcd_putc,"\f %lu",currentValue); | |
printf(lcd_putc, "\n Vin =%f", (Voltage)); | |
delay_ms(200); | |
} | |
} | |
//function declaration | |
char KeyPadPress(void){ | |
char x='_'; //default | |
KEYPAD->columns = COL0; | |
switch(KEYPAD->rows){ | |
case ROW0: | |
x ='1'; | |
break; | |
case ROW1: | |
x='4'; | |
break; | |
case ROW2: | |
x='7'; | |
break; | |
case ROW3: | |
x='*'; | |
break; | |
default: | |
} | |
KEYPAD->columns = COL1; | |
switch(KEYPAD->rows){ | |
case ROW0: | |
x ='2'; | |
break; | |
case ROW1: | |
x='5'; | |
break; | |
case ROW2: | |
x='8'; | |
break; | |
case ROW3: | |
x='0'; | |
break; | |
default: | |
} | |
KEYPAD->columns = COL2; | |
switch(KEYPAD->rows){ | |
case ROW0: | |
x ='3'; | |
break; | |
case ROW1: | |
x='6'; | |
break; | |
case ROW2: | |
x='9'; | |
break; | |
case ROW3: | |
x='#'; | |
break; | |
default: | |
} | |
return(x); | |
} | |
int twoDigitNumber(int currentValue){ | |
int numericValue; | |
numericValue = (int)keyPadPress()-0x30; | |
if(numericValue<=9 && numericValue>=0){ | |
if( currentValue>10 && currentValue < 100){ | |
currentValue=0; | |
} | |
currentValue = currentValue*10+numericValue; | |
} | |
return(currentValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment