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.

Down convert 10 bit to 8 bit

Status
Not open for further replies.

zulkifliaziz

Junior Member level 1
Joined
Mar 27, 2008
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,409
Hi all,

I'm having a problem to understand how to convert 10 bit data to become 8 bit.

First of all, what i want to implement is to use an ADC from analog data (0-5V).

So, from my understanding,

10 bit data = 1024 (max)

in this case, if 5V=1024 and if 2.5V=512

But, to communicate with rs232, we have to use 8 bit = 1 byte each data being sent plus start bit and stop bit.

Code:
char temp_res;
char temp_text[7];

void main() 
{
  Uart1_Init(9600);
  ADCON1 = 0x80;
  TRISA  = 0xFF;
  TRISB = 0;
  PORTB = 0B00000000;
  for(;;)
     {
        temp_res = ADC_Read(1);
        if(temp_res>125)
           {
               ByteToStr(temp_res,temp_text);
               PORTB=0B00000001;
               Delay_ms(100);
               PORTB=0B00000000;
               Delay_ms(100);
               Uart1_Write(temp_text);
               Delay_ms(100);
           }
       }
 }

From the code above, do u see any wrong doing. pls help. What i want to know is that, why the number for this chattared keeps alway changing itself.
 

Hi,
Firstly 10 bit data = 1023 max (0-1023, that's 1024 numbers)
5v = 1023

In your code, you use the function ByteToStr, but temp_res is not a byte.
So, have another variable as "temp_res_byte" and convert "temp_res" to byte.
Try this:
Code:
char temp_res;
char temp_text[7];
unsigned char temp_res_byte;

void main() 
{
  Uart1_Init(9600);
  ADCON1 = 0x80;
  TRISA  = 0xFF;
  TRISB = 0;
  PORTB = 0B00000000;
  for(;;)
     {
        temp_res = ADC_Read(1) >> 2;
        temp_res_byte = temp_res;
        if(temp_res>125)     -------------------------------------->  //Why is this here?
           {
               ByteToStr(temp_res_byte,temp_text);
               PORTB=0B00000001;
               Delay_ms(100);
               PORTB=0B00000000;
               Delay_ms(100);
               Uart1_Write(temp_text);
               Delay_ms(100);
           }
       }
 }

To convert 10-bit to 8-bit, you need to divide by 4, ie >>2 (bitshift 2 to the right, which essentially means the same as divide by 4).

Hope this helps.
Tahmid.
 

it doesn't work. Infact, that the character doesn't change when i give different voltages for the adc. this is what i got from the usart terminal of the microC

 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top