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.

Data Convertion

Status
Not open for further replies.

Spideys

New Member
Hi there ppl.

working with a P18F4331 and doing A/D convertion. I want to display the result on PORTB but as you know P18f4331 is a 10bit A/D converter and PortB only has 8Bits

If A/D is justified right

char x,y;
int z;

x = ADRESL; //low byte
y = ADRESH; // high byte

and i have the following "nested if " ( deviding 10bit value into the 8bits of portB)

if(z <= 128)
PORTB = 0x01;
else if(z <= 256)
PORTB = 0X03;
else if(z <= 384)
PORTB = 0x07;
else if(z <= 512)
PORTB = 0x0f;
else if(z <= 640)
PORTB = 0x1f;
else if(z <= 768)
PORTB = 0x3f;
else if(z <= 896)
PORTB = 0x7f;
else if(z < 896)
PORTB = 0xff;

how do i define z where z = Highbyte,lowbyte combind. Basically how do i make a 10 bit value form 2 8bit values x & y
 
Justify left and just copy ADRESH to PORTB.

Edit, if you still want to combine into an int then do Result=ADRESH*256+ADRESL;.

Mike.
 
Last edited:
Thanks Again for responding Mike...

That was what i did but saw strange results (just testing with a POT conected to AN0) as i turn the POT light connected to Portb do light up as resistance decrease, but sometimes lights turn off. i thought that there might be an C-instruction, that could combine the the two byte values into one int or long int.

z = x + (256 * y); // this was my initial idea.

otherwise it is just the POT that is screwy :)

X + Y = Z
11110000 + 00000011 = 1111110000
 
Thanks Again for responding Mike...

That was what i did but saw strange results (just testing with a POT conected to AN0) as i turn the POT light connected to Portb do light up as resistance decrease, but sometimes lights turn off. i thought that there might be an C-instruction, that could combine the the two byte values into one int or long int.

z = x + (256 * y); // this was my initial idea.

otherwise it is just the POT that is screwy :)

X + Y = Z
11110000 + 00000011 = 1111110000

If its sometimes showing blank perhaps you are polling the ADC before it is ready again (the internal capacitor is discharged).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top