You are on page 1of 2

Mikroc: Adc to Volt conversion using a 16F877

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 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) { 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) { 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;

// Routine to show the value of the ADC_read

// Routine to show the ADC_read conversion in Volt

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

// channel 1 (RA1)

// channel 2 (RA2)

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.

You might also like