pic18F4580_ADC

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

pic18F4580_ADC

Postby fazlul76 » Thu Feb 18, 2016 11:56 am

Hi,

I am using pic18F4580 to acquire samples from 8 adc channels. For AN0 I connected to +5V and AN0 connected to +0V. I make a simple code to acquire from all channels and when conversions completed at all channels the coded will send a termination charater 'A'.
CODE: SELECT_ALL_CODE
#include <18F4580.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define button (PIN_B0)

void main()
{
   
     
   setup_adc_ports(ALL_ANALOG); 
   setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_8);
   
   const int8 channel1[]=0,1,2,3,4,5,6,7;
   int16 ADC_value[sizeof(channel1)];
   int8 value;
   int z;
   char char1;
   
   SETUP_ADC_PORTS(ALL_ANALOG);                                //all analogue
   output_low(PIN_B6);
   output_low(PIN_B7);
   
   
while(TRUE)                                                    //loop forever

  {
      while (TRUE)
      {

         
          if(input(button) == 0)                                  //RB0 when pressed o/p will be 0
         
              break;                                               
          else
             
              output_high(PIN_B6);
           

      }
 
//get here when 'S' is pressed
for (z=0;z<1;z++)                                             //1x=8 samples data for 1 frame image
 {
      output_low(PIN_B6);
                           
      for (value=0;value<sizeof(channel1);value++) 
     
// read from 9 adc channels
      {
         output_low(PIN_B7);
         set_adc_channel(channel1[value]);
         ADC_value[value]=read_adc();                       //ADC readings
         printf("%4.3w\r\n", ADC_value[value]);               //4 digit adc value and new line between value
         //putc(read_adc());
         output_high(PIN_B7);
      }             
         
         printf("A\r\n");
   }   
}
}



From the hyper terminal I am getting this value:
CODE: SELECT_ALL_CODE
0.255
0.232
0.114
0.099
0.050
0.031
0.035
0.000
A
0.255
0.226
0.134
0.083
0.043
0.056
0.021
0.000
A



My questions are:
a) I am getting value of 255 (8-bit) instead of 5 from AN0. How to change the samples to real floating point value?
b) Instead of sending each data from each channels when conversion is completed, how to send one-time data from all channels when all samples are available?

Thank you.
fazlul76
Greenhorn
 
Posts: 3
Joined: Wed Feb 03, 2016 12:03 pm

Re: pic18F4580_ADC

Postby Idris » Thu Feb 18, 2016 3:09 pm

Hi fazlul76,

a) Data that you obtained from ADC is a raw data either 0 - 255 (for 8-bit) or 0 - 1023 (for 10-bit). In order to convert a raw ADC data to voltage, you need to do a calculation.

b) Refer to your code, you already store all ADC data in 'ADC_value' variable, so you just need to remove the 'printf' in current 'for' loop, and add another 'for' loop to display them all.

*I'm not using CCS compiler, so I'm not sure this modified code it is working or not.
CODE: SELECT_ALL_CODE
#include <18F4580.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define button (PIN_B0)

void main()
{
  setup_adc_ports(ALL_ANALOG); 
  setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_8);
   
  const int8 channel[] = 0, 1, 2, 3, 4, 5, 6, 7;
  int16 ADC_value[sizeof(channel)];
  int8 value;
  int16 voltage[sizeof(channel)];
  int z;
  char char1;
 
  SETUP_ADC_PORTS(ALL_ANALOG);
  output_low(PIN_B6);
  output_low(PIN_B7);

  while(TRUE)
  {
    while(TRUE)
    {
      if(input(button) == 0) {
        break;
      }
      else {
        output_high(PIN_B6);
      }
    }

    for(z = 0; z < 1; z++)
    {
      output_low(PIN_B6);

      // Collect all ADC data
      for(value = 0; value < sizeof(channel); value++) // Read from 8 adc channels
      {
        output_low(PIN_B7);
        set_adc_channel(channel[value]);
        ADC_value[value] = read_adc(); // ADC readings
        voltage[value] = ADC_value[value] * (5000 / 255); // Convert ADC data to mV
        output_high(PIN_B7);
      }

      // Print all ADC data
      for(value = 0; value < sizeof(channel); value++) // Display 8 voltage values
      {
        printf("%4.3w\r\n", voltage[value]);
      }

      printf("A\r\n");
    }
  }
}
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 13 guests

cron