Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] help with PIC16f677 ADC convertion method

Status
Not open for further replies.

romel_emperado

Advanced Member level 2
Joined
Jul 23, 2009
Messages
606
Helped
45
Reputation
132
Reaction score
65
Trophy points
1,318
Location
philippines
Activity points
6,061
guys. I need help how to display exactly what is the input in the ADC to LCD.

what i have now is the adc binary value is directly displayed in the LCD.

my ADC input is 5v and I want to display ov to 5v in the LCd.. but i dont know how to write a code for that..

protues file and hex file attached..

my compiler is mikroC

pls check my code




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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;
 
 
 
 
void main()
{
 
  unsigned int temp_ADCresult,volt;
  char txt[8];
 
 
  TRISC = 0x00; // set direction to be output
  PORTC = 0x00;
 
  //LCD config
  Lcd_Init(); // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 
  do
  {
   temp_ADCresult = ADC_Read(2);// Get 10-bit results of AD conversion
   PORTC = temp_ADCresult;//result from ADC
   volt = temp_ADCresult;
   IntToStr(volt, txt);
   Lcd_Out(1,2, txt);
 
  } while(1); // Endless loop
 
}

 

Attachments

  • adc-lcd.rar
    13.8 KB · Views: 109
Last edited:

thanks malik.. I will check this and post update after.. thanks

---------- Post added at 13:59 ---------- Previous post was at 13:29 ----------

hi malik.. I check the blog and we have almost the same code..

the only lacking in my code is conversion... I want to display exactly what is in the input of ADC.. the max is 5v input and the ouput of LCD is 1023 which is wrong..
 

// 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;




void main()
{

unsigned int temp_ADCresult,volt;
char txt[8];


TRISC = 0x00; // set direction to be output
PORTC = 0x00;

//LCD config
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

do
{
temp_ADCresult = ADC_Read(2);// Get 10-bit results of AD conversion
PORTC = temp_ADCresult;//result from ADC <--- you can omit this.
volt = temp_ADCresult; <---- in here, the value is 0-1024.
IntToStr(volt, txt); <--- you just converted that value to str for it to display.
Lcd_Out(1,2, txt);

} while(1); // Endless loop

}[/syntax]

So I guess what you need is a formula. Try doing the steps I will provide:

Set volt as a float then,

volt=(float)(((long)temp_ADCresult*5)/1023);

Then use your FloatToStr syntax.

Give a feedback if it works.

Regards,
Gello Mark C. Vito
 
So I guess what you need is a formula. Try doing the steps I will provide:

Set volt as a float then,

volt=(float)(((long)temp_ADCresult*5)/1023);

Then use your FloatToStr syntax.

Give a feedback if it works.

Regards,
Gello Mark C. Vito


wow!!! exelent!! it works.. thank you so much..

Im thinking this formula of mine but your formula is better and awesome!! thanks so much..
float x=1024;
x= x/10;
x=x*58;
x=x/1000;
x=x%1000;


---------- Post added at 14:37 ---------- Previous post was at 14:29 ----------

how about if I want to display floating points like this 1.2??

---------- Post added at 14:41 ---------- Previous post was at 14:37 ----------

and I have question if I use voltage divider to measure higher voltage ex. 20v... is this formula will work accordingdly?? sorry for the noob question.

it is alsomy first try of microcontrollers..
 

What do you mean? Do you just want to display 1.2?

1. x=1.2
2. Do a FloatToStr
3. LCD_Out

You can display any float you want using MikroC's FloatToStr.

For your question:

volt=(float)(((long)temp_ADCresult*5)/1023);

That is for 5V. Now for 20V, use this:

volt=(float)(((long)temp_ADCresult*20)/1023);

Did you get the logic?


Regards,
Gello Mark C. Vito
 
okay I get is now..but wait

PIC is only capable of 5v input. right?


so Im thinking of this

Vmax =15v
---
|
\
/ R1
\
/
|___ Vin =5v (to PIC input)
|
\
/ R2
\
/
|
|
V
GND

Vin = 5v
Vmax = 15v

find R1?

R1= ((R2* 15v)/5v) - R2

R1 = 200k


Find R2?

R1 = 2 or 200k
R2= 2 / ((15v/5v) -1)
R2 = 1 or 100k

I doubt if the formula is still the same.. :sad::sad:
 

yes the formula is good.. what i mean is this formula

volt=(float)(((long)temp_ADCresult*20)/1023);

I applied this formula using protues with voltage devider and do actual simulation but it always displays 20v fix..
 

As you have said recently, the PIC is only capable of having inputs up to 5V ideally. So you still have an input maximum of 5V right? Still you have to use this:

volt=(float)(((long)temp_ADCresult*5)/1023);

But at this time, you multiply the equation by 4. Why? Because 4*5=20. If you have ADC value of 4, 4*4=16. If 2, 2*4=8 and so forth.

Also try varying your Vmax value.
 
excellent!! but i need to adjust my resistor in the network.. still the display output is less than more or less 3v..

thanks so much.. i will update soon when this is done... thanks so much for your help
 

thanks guys this thread is now solved..

here is my final code:


Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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;
 
 
 
 
 
 
 
void main()
{
 
  unsigned int temp_ADCresult;
  char i, y=6, x=12;
  char *intro[] = {"W","e","l","c","o","m","e"};
  float volt;
  char txt[14];
 
 
  TRISA = 0xff;
  TRISC = 0x00;          // set direction to be output
  PORTC = 0x00;
 
 
  //LCD config
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);           // Cursor off
  
 
  for(i=0; i<=6; i++)
  {
    Lcd_Out(1,x, intro[y]);
    y--;
    x--;
    Delay_ms(400);
  }
  Lcd_Out(1,13, "!");
  Delay_ms(1200);
  Lcd_Cmd(_LCD_CLEAR);
 
 
 
  while(1)
  {
 
   temp_ADCresult = ADC_Read(2);
   PORTC = temp_ADCresult;
   volt = temp_ADCresult;
   volt = (temp_ADCresult*4.8/1023) * 4.2;
   if(temp_ADCresult <= 60)
   {
 
     Lcd_Out(1,8,"0");
     Lcd_Out(1,9,"V");
   }
   else
   {
    FloatToStr(volt, txt);
    Lcd_Out(1,7, txt);
    Lcd_Out(1,16,"V");
   }
 
    Lcd_Out(1,1,"ToTAL:");
 
 }
 
 
 
 
}





and I used this very nice voltage divider value calvulator..



but before this thread to be closed. do you have PIC programmer circuit? so that I can now test this in actual hardware...

i search manay programmer in google and I found many available programmer but I doubt if those will work :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top