// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
//Global Variables
double volt, amp, power, rload, FP_NUM;
char string[8];
bit b_mode1, b_mode2;
const char character[] = {0,14,17,17,10,27,0,0};
void CustomChar(char pos_row, char pos_char) {
char i;
Lcd_Cmd(64);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
Lcd_Chr(pos_row, pos_char, 0);
}
//Function Prototypes
void floattostring(double FP_NUM);
//Sub Function
void floattostring(double FP_NUM) {
double fpnumber;
long int befdec, aftdec;
fpnumber = FP_NUM;
befdec = fpnumber; // Fractional part is truncated
// 12.163456 becomes 12
aftdec = fpnumber * 100; // 12.163456 becomes 1216
aftdec = aftdec - (befdec * 100); // 1216 - 1200 = 16
if (fpnumber < 1) {
string[0] = '0';
string[1] = '.';
string[2] = (aftdec/10) + 48;
string[3] = (befdec/1)%10 + 48;
string[4] = ' ';
string[5] = ' ';
string[6] = ' ';
string[7] = '\0';
}
else if ((fpnumber >= 1) && (fpnumber < 10)) {
string[0] = (befdec/1)%10 + 48;
string[1] = '.';
string[2] = (aftdec/10) + 48;
string[3] = (befdec/1)%10 + 48;
string[4] = ' ';
string[5] = ' ';
string[6] = ' ';
string[7] = '\0';
}
else if ((fpnumber >= 10) && (fpnumber < 100)) {
string[0] = (befdec/10) + 48;
string[1] = (befdec/1)%10 + 48;
string[2] = '.';
string[3] = (aftdec/10) + 48;
string[4] = (befdec/1)%10 + 48;
string[5] = ' ';
string[6] = ' ';
string[7] = '\0';
}
else if ((fpnumber >= 100) && (fpnumber < 1000)) {
string[0] = (befdec/100) + 48;
string[1] = (befdec/10)%10 + 48;
string[2] = (befdec/1)%10 + 48;
string[3] = '.';
string[4] = (aftdec/10) + 48;
string[5] = ' ';
string[6] = ' ';
string[7] = '\0';
}
else if ((fpnumber >= 1000) && (fpnumber < 10000)) {
string[0] = (befdec/1000) + 48;
string[1] = (befdec/100)%10 + 48;
string[2] = (befdec/10)%10 + 48;
string[3] = (befdec/1)%10 + 48;
string[4] = '.';
string[5] = (aftdec/10) + 48;
string[6] = ' ';
string[7] = '\0';
}
else if ((fpnumber >= 10000) && (fpnumber < 100000)) {
string[0] = (befdec/10000) + 48;
string[1] = (befdec/1000)%10 + 48;
string[2] = (befdec/100)%10 + 48;
string[3] = (befdec/10)%10 + 48;
string[4] = (befdec/1)%10 + 48;
string[5] = '.';
string[6] = (aftdec/10) + 48;
string[7] = '\0';
}
}
void main() {
TRISA = 0b00000011;
PORTA = 0x00;
TRISB = 0x00;
PORTB = 0x00;
TRISC = 0b00001110;
PORTC = 0x00;
ADCON1 = 0b10000010;
CMCON.CM2 = 0;
CMCON.CM1 = 0;
CMCON.CM0 = 0;
LCD_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"Welcome Vlad");
Delay_ms(3000);
Lcd_Cmd(_LCD_CLEAR);
while(1) {
volt = ADC_Read(0);
amp = ADC_Read(1);
/*
if ((PORTC.F1 == 1) || (b_mode1 == 1) && (PORTC.F2 == 0)) {
Delay_ms(20);
if ((PORTC.F1 == 1) || (b_mode1 == 1) && (PORTC.F2 == 0)) {
volt = volt * 0.0725388601036269;
amp = amp * 0.0103626943005181;
b_mode1 = 1;
b_mode2 = 0;
}
}
else if ((PORTC.F2 == 1) || (b_mode2 == 1) && (PORTC.F1 == 0)) {
Delay_ms(20);
if ((PORTC.F2 == 1) || (b_mode2 == 1) && (PORTC.F1 == 0)) {
volt = volt * 0.5181347150259067;
amp = amp * 0.05181347150259067;
b_mode2 = 1;
b_mode1 = 0;
}
}
*/
volt = volt * 0.0725388601036269;
amp = amp * 0.0310880829015544;
power = volt * amp;
rload = volt / amp;
floattostring(volt);
Lcd_Out(1,1,string);
Lcd_Out(1,8,"V");
floattostring(amp);
Lcd_Out(1,9,string);
Lcd_Out(1,16,"A");
floattostring(power);
Lcd_Out(2,1,string);
Lcd_Out(2,8,"W");
floattostring(rload);
Lcd_Out(2,9,string);
CustomChar(2,16);
}
}