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.

comparing voltage with giving look up table value in microcontroller

Status
Not open for further replies.

anurag16

Junior Member level 3
Junior Member level 3
Joined
May 14, 2012
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,490
I am trying to build project where voltage will able detect from the source and able to compare with look up table value which is save pic micro controller.
But I am struck at that part where voltage is not comparing with value which i save in look up table in pic microcontroller
how it will automatic compare with look up table....
when i giving manually value ......... value is comparing with look up table but when voltage is coming from source its not comparing
so what i have to do?
please help
 

Post your code.
rom float V[9]={3.31,3.32,3.33,3.35,3.34,3.36,3.38, 3.37,3.39};
rom float I[8]={0,5,6,16.69,3.94,3,3, 32.44};
rom float R[9]={0,1.1,1.21,1.37,1.32,1.50,1.35,1.6,1};
xlow = ADRESL; //assign the lower 8 bits to a variable
xhigh = ADRESH; //assign the higher 2 bits to another variable
xchange = xhigh<<8; //shift left by 8 bits
hold = xlow|xchange; // OR the final bytes.
printf("hold = %u \n",hold); //just to check the output, is 0.
volt = (5*hold/1024)+48; //Vcc=5V, Dout*stepsize = Vin,catch the first digit
volt2 = (50*hold/1024)%10+48; //catch the first decimal
volt3 = (500*hold/1024)%10+48;
x=volt*100;
//x=(int)x;
//lcddata(x+48);
y=volt2*10;
//y=(float)y;
//y = y * 10;
//lcddata(y+48);
z=volt3;

//z= z*100;
//z=(float)z;
//lcddata(z+48);
//V1=(float)2.00;

V1=(x+y+z)-527;
//V1=(float)336;
//x1 = V1-527;
//V1=(float)x1 ;
v2=(float)V1/100;
v3=(int)v2;
for(m=0;m<=10;m++)
{

if( v2==V[m]){

p=(float)R[m] ;
q= (int)p ;
v2=(float)p/10;
lcddata(p+0X30);
v3=(int)v2;
break;
}}
the loop is working for manually but not for voltage supply
 

What is xhigh? 8bit or 16 bit? You left shift it 8 times. It should be 16 bits wide.

This should be

Code C - [expand]
1
2
3
4
for(m=0;m<=10;m++)
{
 
if( v2==V[m]){



like this

Code C - [expand]
1
2
3
4
for(m=0;m<=9;m++)
{
 
if( v2==V[m]){



v[m] can have these values {3.31,3.32,3.33,3.35,3.34,3.36,3.38, 3.37,3.39}.

What if your v2 never match these values. what if you get 3.312, 3.325, ... like that? Are you rounding your v2 to two decimal digits?

Didide by 1024 in adc calculation is wrong. It should be (adc_val * 5 / 1023)

What Compiler are you using? What is your MCU? What is your Fosc?

This is wrong...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
xhigh = ADRESH; //assign the higher 2 bits to another variable
xchange = xhigh<<8; //shift left by 8 bits
hold = xlow|xchange; // OR the final bytes
[syntax]
Try this...
[syntax=c]
unsigned char lbyte, hbyte;
unsigned int word;
 
lbyte = ADRESL;
hbyte = ADRESH;
word = hbyte;
word = word << 8 | lbyte;

 
Last edited:

i try to catch only 2 decimal number after point
so u mean that there some value too after 2 decimal point which cant be read from array look up table .....
i am using Fosc/16

- - - Updated - - -

TRISB=0x00; //PORT B as output
TRISD=0x00; //PORT D as output
OSCCON = 0x70;
ADCON0 = 0x01; //0000 0001,channel 0(AN 0),A/D is ON.
ADCON1 = 0x0E; //0000 1110,AN0 is analog i/p, use Vdd & Vss as refs.
ADCON2 = 0x9D; //Right justified, 6Tad acquisition time, Fosc/16.
PORTA = 0x00; //Clear PORT A
PORTD = 0x00; //clear PORT D
en = 0;

- - - Updated - - -

This is wrong...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
xhigh = ADRESH; //assign the higher 2 bits to another variable
xchange = xhigh<<8; //shift left by 8 bits
hold = xlow|xchange; // OR the final bytes
[syntax]
Try this...
[syntax=c]
unsigned char lbyte, hbyte;
unsigned int word;
 
lbyte = ADRESL;
hbyte = ADRESH;
word = hbyte;
word = word << 8 | lbyte;

[/QUOTE]



This is wrong...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
xhigh = ADRESH; //assign the higher 2 bits to another variable
xchange = xhigh<<8; //shift left by 8 bits
hold = xlow|xchange; // OR the final bytes
[syntax]
Try this...
[syntax=c]
unsigned char lbyte, hbyte;
unsigned int word;
 
lbyte = ADRESL;
hbyte = ADRESH;
word = hbyte;
word = word << 8 | lbyte;

[/QUOTE]


the code which you have provide is same thing i think only variable name change
 

Post your full code. I need to see your ADC read routine.

I don't know what typpe is your volt, volt1, and volt2 is but you are storing character values like '1', '2', '3',... in them.

volt = '3' = 0x33 = d51
volt1 = '3'
volt2 = '1' = 0x31 = d49

x = volt*100 = 51*100 = 5100
y = volt2*10 = 51*10 = 510
z = volt3 = 49

V1 = 5100+510+49-527 = 5659-527 = 5132
V2 = 5132.0/100 = 51.32
V3 = 51

for loop 1st round

m = 0

51.32 == 3.31
 
Last edited:

rom float V[9]={3.31,3.32,3.33,3.35,3.34,3.36,3.38, 3.37,3.39};
rom float I[8]={0,5,6,16.69,3.94,3,3, 32.44};
rom float R[9]={0,1.1,1.21,1.37,1.32,1.50,1.35,1.6,1};

void main (void)
{
int volt,volt2,volt3, volt4;
unsigned int hold, xlow,xhigh,xchange, word;
signed float y,z,V1,v2,v4,x1,a,v10;
signed int m,n,ones,V2,tenth,twos,b,v3,v5,V3,q,t,x;
signed float I1,T,V6,p,voltf;
TRISB=0x00; //PORT B as output
TRISD=0x00; //PORT D as output
OSCCON = 0x70;
ADCON0 = 0x01; //0000 0001,channel 0(AN 0),A/D is ON.
ADCON1 = 0x0E; //0000 1110,AN0 is analog i/p, use Vdd & Vss as refs.
ADCON2 = 0x9D; //Right justified, 6Tad acquisition time, Fosc/16.
PORTA = 0x00; //Clear PORT A
PORTD = 0x00; //clear PORT D
en = 0;
MSDelay(100);
lcdcmd(0x38); //initiate LCD 2 lines and 5x7 matrix
while(1)
{
lcdcmd(0x0E); // display and cursor ON.Writing 0x0F makes the cursor blink
MSDelay(15); //delay
lcdcmd(0x01); // this is to clear LCD
MSDelay(15);
lcdcmd(0x06); // shift the cursor to right
MSDelay(15);
lcdcmd(0x80); //line 1, position 0
MSDelay(1);
ADCON0bits.GO = 1; //start conversion
while(ADCON0bits.DONE == 1); //wait for DONE bit to go low
xlow = ADRESL; //assign the lower 8 bits to a variable
xhigh = ADRESH;//assign the higher 2 bits to another variable

xchange = xhigh<<8; //shift left by 8 bits
hold = xchange|xlow; // OR the final bytes.
printf("hold = %u \n",hold); //just to check the output, is 0.
volt = (5*hold/1023)+48; //Vcc=5V, Dout*stepsize = Vin,catch the first digit
volt2 = (50*hold/1023)%10+48; //catch the first decimal
volt3 = (500*hold/1023)%10+48; //20/41 =~ 500/1024, catch the second decimal
x=volt*100;
//x=(int)x;
//lcddata(x+48);
y=volt2*10;
//y=(float)y;
//y = y * 10;
//lcddata(y+48);
z=volt3;

//z= z*100;
//z=(float)z;
//lcddata(z+48);
//V1=(float)2.00;

V1=(x+y+z)-527;
//V1=(float)336;
//x1 = V1-527;
//V1=(float)x1 ;
v2=(float)V1/100;
v3=(int)v2;

for(m=0;m<=10;m++)
{

if( v2==V[m]){

p=(float)R[m] ;
q= (int)p ;
v2=(float)p/10;
lcddata(p+0X30);
break
}
 

These are integer variables...

int volt,volt2,volt3, volt4;

and you are assigning characters to it...

volt = (5*hold/1023)+48; //Vcc=5V, Dout*stepsize = Vin,catch the first digit
volt2 = (50*hold/1023)%10+48; //catch the first decimal
volt3 = (500*hold/1023)%10+48; //20/41 =~ 500/1024, catch the second decimal

these are float and int variables...

signed float y,z,V1,v2,v4,x1,a,v10;
signed int m,n,ones,V2,tenth,twos,b,v3,v5,V3,q,t,x;

again chracter is assigned...

x=volt*100;
y=volt2*10;
z=volt3;

if volt = '1' = 0x31 or d49

then

x = 49 * 100 = 490
 

please help.....i understood what u r saying......
give some idea how to do that

- - - Updated - - -

ok i am using mplab x as compiler
Vref is 5 volt
Fosc/16

- - - Updated - - -

Vref= 0-5v

- - - Updated - - -

what is MCU?
 

MCU = microcontroller unit or just MC. Fosc = Crystal frequency. What is Fosc? MPLAB X is IDE not Compiler. What is your Compiler? C18, Hi-Tech C, CCS C, etc...?


Edit: You are using AN0 i.e., RA0 pin and there is no TRISA setting?
 
Last edited:

MCU is is 14f4580
why is taking character value becoz i am assign as integer value
C18 is compiler
 
Last edited:

I will write a code tomorrow. It is 2:00 AM here. See my code here. It has float to string conversion. Get adc value and convert it to string and display it on LCD but retain the original float adc value and use it to compare with the table. Try to write a code, I will post the code tomorrow.
 

Thank u so much ..........
i will wait tomorrow.............
it will be very helpful
 

its default .......16 mhz

whole code is this
#include <p18f4580.h>
#pragma config WDT = OFF
#pragma config OSC= IRCIO67
#pragma config PWRT=ON
#pragma config LVP=OFF
#define ldata PORTD
#define rs PORTBbits.RB0 //define reset bit
#define rw PORTBbits.RB1 //define read/write bit
#define en PORTBbits.RB2 //define enable bit
void lcdcmd (char value);
void lcddata(char value);
void MSDelay(int itime);
rom float V[9]={3.31,3.32,3.33,3.35,3.34,3.36,3.38, 3.37,3.39};
rom float I[8]={0,5,6,16.69,3.94,3,3, 32.44};
rom float R[9]={0,1.1,1.21,1.37,1.32,1.50,1.35,1.6,1};

void main (void)
{
int volt,volt2,volt3, volt4;
unsigned int hold, xlow,xhigh,xchange, word;
signed float y,z,V1,v2,v4,x1,a,v10;
signed int m,n,ones,V2,tenth,twos,b,v3,v5,V3,q,t,x;
signed float I1,T,V6,p,voltf;
TRISB=0x00; //PORT B as output
TRISD=0x00; //PORT D as output
OSCCON = 0x70;
ADCON0 = 0x01; //0000 0001,channel 0(AN 0),A/D is ON.
ADCON1 = 0x0E; //0000 1110,AN0 is analog i/p, use Vdd & Vss as refs.
ADCON2 = 0x9D; //Right justified, 6Tad acquisition time, Fosc/16.
PORTA = 0x00; //Clear PORT A
PORTD = 0x00; //clear PORT D
en = 0;
MSDelay(100);
lcdcmd(0x38); //initiate LCD 2 lines and 5x7 matrix
while(1)
{
lcdcmd(0x0E); // display and cursor ON.Writing 0x0F makes the cursor blink
MSDelay(15); //delay
lcdcmd(0x01); // this is to clear LCD
MSDelay(15);
lcdcmd(0x06); // shift the cursor to right
MSDelay(15);
lcdcmd(0x80); //line 1, position 0
MSDelay(1);
ADCON0bits.GO = 1; //start conversion
while(ADCON0bits.DONE == 1); //wait for DONE bit to go low
xlow = ADRESL; //assign the lower 8 bits to a variable
xhigh = ADRESH;//assign the higher 2 bits to another variable

xchange = xhigh<<8; //shift left by 8 bits
hold = xchange|xlow; // OR the final bytes.
printf("hold = %u \n",hold); //just to check the output, is 0.
volt = (5*hold/1023)+48; //Vcc=5V, Dout*stepsize = Vin,catch the first digit
volt2 = (50*hold/1023)%10+48; //catch the first decimal
volt3 = (500*hold/1023)%10+48; //20/41 =~ 500/1024, catch the second decimal

/////////PRINT THE COLLECTED DATA ON LCD///////////////////
lcddata(volt);
MSDelay(15);
lcddata('.');
MSDelay(15);
lcddata(volt2);
MSDelay(15);
lcddata(volt3);
MSDelay(15);

lcddata(86);
MSDelay(500);
lcdcmd(0x89);
x=volt*100;
//x=(int)x;
//lcddata(x+48);
y=volt2*10;
//y=(float)y;
//y = y * 10;
//lcddata(y+48);
z=volt3;

//z= z*100;
//z=(float)z;
//lcddata(z+48);
//V1=(float)2.00;

V1=(x+y+z)-527;
//V1=(float)336;
//x1 = V1-527;
//V1=(float)x1 ;
v2=(float)V1/100;
v3=(int)v2;

lcddata(v3);
MSDelay(15);
lcddata('.');
V6=(float)(v2-v3);
V6 = V6 * 10;
//V6 = V6-2;

lcddata(V6+0X30);
MSDelay(15);

v4=(int)(V6);
V6 = (float)(V6-v4);
v5 =V6 * 10;
v5=(int)(v5);

//v5=v5 + 48;
lcddata(v5+0X30);

lcdcmd(0xC0);
MSDelay(15);


for(m=0;m<=10;m++)
{

if( v2==V[m]){

p=(float)R[m] ;
q= (int)p ;
v2=(float)p/10;
lcddata(p+0X30);
v3=(int)v2;

v2 -= v3;
v2 *= 10.0;
b=(int)(v2);
a= b + 48;
lcddata(a);
MSDelay(15);
break;
MSDelay(15);
}
}
MSDelay(10000);

}

}
void lcdcmd(char value)
{
ldata = value; // put the value on the pins of PORTD
rs = 0;
rw = 0;
en = 1; // strobe enable pin
MSDelay(1);
en = 0;
}
void lcddata (char value)
{
ldata = value; //put the value on the pins of PORTD
rs = 1;
rw = 0;
en = 1; // strobe enable pin
MSDelay(1);
en = 0;
}
void MSDelay(int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top