This web site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more.

ADC - Volt conversion on LCD

Last update: 31/07/2015

convertitore adc volt

 

Here follows an example program to convert two channels of ADC values read on Porta in Volt using a four lines LCD. The program should be reaally clear and can be easily modified for any need.

 

 

Very often there is the need to show a voltage value using one or more analog converters on PORTA. Such a conversion can be done quite easily using an high level language as Mikroc.

 


// Define LCD - to be modified according to the schematics
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB2_bit;
sbit LCD_D6 at RB1_bit;
sbit LCD_D7 at RB0_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB0_bit;

 

unsigned int adcvalue,value;
unsigned char car,x,y;
char *voltage = "00.00";
long temp;

 

void ShowADC(int x, int y, unsigned int adcvalue)      // Routine to show the value of the ADC_read
{
car = adcvalue / 1000;
LCD_Chr(x,y,48+car);
adcvalue=adcvalue-1000*car;
car = (adcvalue / 100);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-100*car;
car = (adcvalue / 10);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-10*car;
car = adcvalue;
LCD_Chr_CP(48+car);
delay_ms(30);
}

 

void ShowVoltage (int x,int y, unsigned int value)     // Routine to show the ADC_read conversion in Volt
{
temp = (long)value* 5000;                      
temp = temp / 1023;
voltage[0] = temp/10000 + 48;
voltage[1] = (temp/1000)%10 + 48;
voltage[3] = (temp/100)%10 + 48;
voltage[4] = (temp/10)%10 + 48;
Lcd_Out (x,y,voltage);
delay_ms(30);
}

 

void main() {

 

ADCON1 = 0x80;                          // All analogs - right justify
TRISA = 0xFF;                               // PORTA Input
TRISB = 0;                                    // PORTB Output
TRISD = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, " ADC Voltage");
do {
value = ADC_Read(1);                    // channel 1 (RA1)
adcvalue=value;
Lcd_Out(3,1,"Ch1:");
ShowADC (3,5,adcvalue);
ShowVoltage(3,11,value);
value = ADC_Read(2);                    // channel 2 (RA2)
adcvalue=value;
Lcd_Out(4,1,"Ch2:");
ShowADC (4,5,adcvalue);
ShowVoltage(4,11,value);
} while(1);
}

 

Naturally if we have to measure different levels of voltage we have to use a resistor partitioner to reduce such voltages value to an acceptable value for the picmicro and modify the multiplier value (5000) with a right one.

 

Diritti riservati Cuteminds.com 2008-2015. Tutti i marchi riportati sono detenuti dai legittimi proprietari. Tutto il materiale e` liberamente utilizzabile ma non per scopi commerciali. Non si assume alcuna responsabilita` sul materiale pubblicato per eventuali danni diretti o indiretti che possano derivare dall`utilizzo di schemi, progetti o altro materiale presente nel sito. Inviare una mail a: remove-admin@cuteminds.com (contenuti) remove-webmaster@cuteminds.com (webserver) per domande e/o suggerimenti.