Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ADC to Digital conversion

Status
Not open for further replies.

salim alfarsi

New Member
Hi,

i have this code to convert the ADC value to PPM value and display it on LCD. now the problem is LCD is on but doesn't show any value!! any suggestion?


/*
* Project name:
LPG Project

* Copyright:
(c) 2012.

* Revision History:
Feb 2012 - V 1.2

* Description:
This program tests for the presence of combustible gas
using an LPG gas sensor.

* Test configuration:
MCU: PIC16F877A

Oscillator: 8.0000 MHz Crystal
Ext. Modules: None.

SW: mikroC PRO for PIC
https://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
*/

// Lcd pinout settings
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
char lcd_text[17];
char tmp_text[17];
// The ADC conversion value is 1023 ADC Levels/5V = 204.6 ADC Levels / Volt
#define ADC_CONVERSION_FACTOR 204.6

//The PPM conversion factor is10000PPM/5V = 2000 PPM per Volt
#define PPM_CONVERSION_FACTOR 2000

//The ADC LEVEL to PPM conversion factor is 10000PPM/1023ADC LEVELS = 9.775171
#define ADC_TO_PPM_CONVERSION_FACTOR 9.775171
//If the adc reads 512 the real voltage is 2.5 VDC and the PPM level is 5000PPM.


void main(){

unsigned int adc_value;
float ppm_value;

TRISE.B0=1; // RE5 as input

Lcd_Init();
ADC_Init();

while(1)
{
//--------------------------------
//Get the current ADC value
//--------------------------------

//Read the ADC for the latest value
adc_value = ADC_Read(5);

//Convert the ADC value to the PPM concentration
ppm_value = ADC_TO_PPM_CONVERSION_FACTOR*adc_value;

//--------------------------------
//Display the ADC value on the LCD
//--------------------------------
Lcd_Out(1,1,adc_value);
delay_ms(500);

//Convert the adc value to text
WordToStr(adc_value,tmp_text);

//Build the LCD display text
strcpy("Raw ADC:",lcd_text);
strcat(tmp_text,lcd_text);

//Display the RAW ADC Value
Lcd_Out(1,1,lcd_text);
delay_ms(500);

//--------------------------------
//Display the PPM value on the LCD
//--------------------------------


//Convert the ppm value to text
FloatToStr(ppm_value,tmp_text);

//Build the LCD display text
strcpy("PPM:", lcd_text);
strcat(tmp_text, lcd_text);

//Display the PPM Value
Lcd_Out(2,1,lcd_text);


Delay_ms(1000);
}
}
 
Is the display blank or just not showing what you expect? From what I remember about these displays, they are very fussy about the delay when it's required. Check you are really getting the delay you think you are.

You need to post the whole routines of things like LCD_Out() and ADC_Read();
 
the LCD show random numbers with characters. i think i need to convert to ASCII code for LCD to read the value. can some one help me how to add ascii code? i have uploaded picture of the lcd.
https://imageshack.us/photo/my-images/96/dsc02064c.jpg/

You send things twice to the first line, maybe there is junk in your variable messing up the display.
Try just sending some text to the display and see if it comes out correctly and work from there.

//--------------------------------
//Display the ADC value on the LCD
//--------------------------------
Lcd_Out(1,1,adc_value); <-----------------------------------
delay_ms(500);

//Convert the adc value to text
WordToStr(adc_value,tmp_text);

//Build the LCD display text
strcpy("Raw ADC:",lcd_text);
strcat(tmp_text,lcd_text);

//Display the RAW ADC Value
Lcd_Out(1,1,lcd_text); <---------------------------------------
delay_ms(500);
 
You can't use the LCD module like you have it Data has to be in the lower 4 bits or higher 4 bits
your data needs to be PORTB 4 to 7 or 0 to 3 You can't start at 2 to 5

See the library is shifting bits in 2 to 5 would take 6 bits thats why your getting junk.
 
Last edited:
You can't use the LCD module like you have it Data has to be in the lower 4 bits or higher 4 bits
your data needs to be PORTB 4 to 7 or 0 to 3 You can't start at 2 to 5

Yes you can, I often do this in order to keep B6/7 free for debugging.

Mike.
 
Not with MiKro C you can't it's not set up to handle it that way. You'll have to write your own Library.
 
The Op can use the lower for data 0 to 3 and then RS and E but not 2 to 5

The library only sends 4 bits lower or upper it will not set them in the middle

The OP can do it this way

Code:
// 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

Or this
Code:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top