Can a PIC having a 10-bit ADC be made to perform as an 8-bit ADC?

Status
Not open for further replies.

Kunal2

Junior Member level 3
Joined
Jun 21, 2011
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,512
Hello

I have a PIC 16F877A with a 10 bit ADC. I want to use USART for communication but the data to be sent can be of 8 bits only. So how can make a PIC wth a 10-bit ADC give me an ADC result of 8-bit i.e for 10 bit ADC, 5V = 1024; i want it to be 8 bit ADC with 5V represented by 256.

However it would be appreciated if someone could tell be whether with 10 bit ADC, it is possible to transmit data via USART specifying that 8 bits of data is to be used.

Thanks.

Kind regards

Kunal
 


Are you using Assembly or C Language? If C Language, what compiler are you using?

The result of the ADC conversion is stored in the ADRESH:ADRESL register pair with the unused bits padded with zeros. One of the simplest ways to utilize only MSBs 8-bits of the 10-bit results of the conversion, is to left justify the result by clearing the A/D Result Format Select bit (ADFM) found in the ADCON1<7> register.

This will move the MSBs to left into ADRESH, give you 8-bits of resolution, with Vdd = 255 (5V if Vdd=5V) and 0v = 0, give you approximately 0.020V/bit.

The range of the value in ADRESH will be (255,0), so one way to send the data via USART is first convert the data into a character string before transmitting it over the serial connection. I usually combine the data character string with a token, to allow for parsing at the receiving end, for example "$V:" followed by the character string representing the voltage.

Reference PIC16F87XA Data Sheet, pg132, Section 11.4.1 A/D Result Registers:


Does this answer your question?

BigDog
 
Thanks for the post Bigdog.

So if we will then be truncating the lower 2 bits of the 10 bit result corresponding to a 0.015V loss in data which may not be a problem.

I will re-write my program and test it with your proposed solution.

Thanks once again.

Kind regards

Kunal
 

Actually by using the eight most significant bits (MSBs) of the 10-bit, the resolution is reduced from approximately 0.0049V/bit to 0.020V/bit.

In other words the smallest voltage increment would be 0.020V versus 0.0049V.

Does that clear things up?

BigDog
 
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
This is my program which i wrote for the adc conversion:

program ADC_on_LEDs

dim Adc_Result as integer


main:
TRISA = $FF ' PORTA is input
TRISC = $00
TRISD = $00 ' PORTD is output

PORTC = 0
PORTD = 0

ADCON1 = $00 'configuring

while true
Adc_Result = ADC_read(0) 'Read from channel 0

PORTD = hi(Adc_Result)
PORTC = Lo(Adc_Result) ' Display lower byte of result on PORTD
Delay_us(10) ' 500 ms pause
wend ' Repeat all

end.

Can you help me modify it to make one of the ports get the 8 bits MSB?

Kind regards

Kunal
 

To be honest, I've never used BASIC for PICs.

However, here is a short C program which should show you the initial setup requirements:

Code:
void main(void)
{
  unsigned char adcValue;

  
  TRISA = 0x03;        // Input for RA0 and RA1
  TRISD = 0x00;        // Set All on PORTC as Output

  ANSEL = 0b00000001;  // Set PORT AN0 to analog input AN1 to AN7 digital I/O
  ANSELH = 0;              // Set PORT AN8 to AN11 as Digital I/O

  /* Init ADC */
  ADCON0=0b10000000;   // Set A/D  Conversion Clock to Fosc/32
  ADCON1=0b00111110;   // select Left Justify result. ADC port channel 0
  ADON=1;	       // turn on the A2D conversion module


  while(1) 
  { 
    delay_ms(1);                // increase delay to decrease samples per second
    GODONE=1;	             // initiate conversion on the channel 0

    while(GODONE) continue;  // Wait conversion done

    adcValue=ADRESL;           // Dummy load may not be necessary
    adcValue=ADRESH;          // Get the 8 bit MSBs result

    PORTD=adcValue;
  }
}

You'll need to set the A/D Conversion Clock to an appropriate setting based on your PIC's clock frequency.

Does this help get you started?

BigDog
 
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
You can easily do Adc_Result=Adc_Result/4 to get 8bit resolution.
or
To maintain 10bit resolution, you can send Adc_Result through UART in 2 byte.
ex.
uart_send(Adc_Result>>8);
uart_send(Adc_Result);

See my example data send by UART (RF Module)
 

Thanks for all the replies (Bigdog and engsharul)

I will go through the programs and test it and update you.

Kind regards

Kunal
 

Quick question Kunal2,

What are you transmitting the ADC values to via UART? PC or another MCU?

BigDog
 

I am transmitting ADC values via USART to a pc to be enterred in labview.....Besides i have the NI DAQ 6024 E card plus connector block CB 68 LP with its cable to connect to the PCI.....

However upon browsing through the MAX, I saw the option VISA. So is it possible to send the ADC values via USART to my pc and then through serial port enter the values in labview by the VISA function.

Thanks for helping.

Kind regards

Kunal
 

I am transmitting ADC values via USART to a pc to be enterred in labview.....Besides i have the NI DAQ 6024 E card plus connector block CB 68 LP with its cable to connect to the PCI.....

Yes, both are a viable option.

However upon browsing through the MAX, I saw the option VISA. So is it possible to send the ADC values via USART to my pc and then through serial port enter the values in labview by the VISA function.

This is the reason I posed the question, if you are sending data serially to PC or other devices which expects ASCII character streams, the method engshahrul proposed above will not have the expect results. You will need to convert the data into ASCII character streams.

However, as in engshahrul's project, if you are sending data serially to another MCU or other devices which processes the transmitted bytes as raw data and then you do not have to convert the values into ASCII character format.

Reference:

For example if you connected your PIC's USART to the PC's serial port and opened a terminal emulator like Hyperterminal or Putty and the PIC sent the value of 0x07, you would not see any character or value being display, but instead hear a beep. This is due to the fact the printable character set is only a portion of the entire ASCII character set, there are many control characters as well, 0x07 being that of "Bell" from the teletype days.


Does this info help?

BigDog
 
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…