CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

pic18F26K22 ADC 10-bit resolution!?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RoMar



Joined: 22 May 2020
Posts: 6

View user's profile Send private message

pic18F26K22 ADC 10-bit resolution!?
PostPosted: Sun Aug 16, 2020 1:23 pm     Reply with quote

Hi,

I'm using ccs (5.008 version) to program a pic18F26K22.
I need to use an ADC. In datasheet I can see this PIC has a ADC with 10-bit.
With this ADC, I want to read voltage's values from 1 to 5Volts with voltage divider circuit.
The resolution is 0-1024 (Full-Scale Range).

But with the code below I just have values from 1 to 255, this means just 8-bit wide!!?
At value's ADC 255 corresponds to 1,23 Volts. And when the voltage is greater than 1.23V,
the ADC gives again a lower values, ( lower than 255 ) something is missing here
(some values are in the table below).

I think I'm doing right. I tried many ways to get 10-bit resolution, but I just have 8-bits??!
The CCS code is below. I can't understand what is missing here, please I need some help.


    __U___|__ADC(bit)_|_ U_adc__ [adc U_float]__multimeter
    0.494V__|____ 104___|_ 49___|___0.4929___|__0.50V
    0.494V__|____ 104___|_ 49___|___0.4929___|__0.50V
    0.494V__|____ 104___|_ 49___|___0.4929___|__0.50V
    0.618V__|____ 130___|_ 61___|___0.6162___|__0.63V
    0.782V__|____ 166___|_ 78___|___0.7868___|__0.80V
    1.146V__|____ 242___|_ 114 _|___1.1470___|__1.16V
    1.206V__|____ 255___|_ 120__|___1.2087___|__1.23V
    0.120V__|____ 26____|_ 12___|___0.1232___|__1.38V ---->X
    0.120V__|____ 27____|_ 12___|___0.1279___|__1.38V ---->X
    0.120V__|____ 27____|_ 12___|___0.1279___|__1.38V ---->X
    0.284V__|____ 61____|_ 28___|___0.2891___|__1.54V ---->X
    0.284V__|____ 61____|_ 28___|___0.2891___|__1.54V ---->X
    0.284V__|____ 61____|_ 28___|___0.2891___|__1.54V ---->X


Code:

#include <18F26K22.h>

#fuses  HSH, PLLEN, NOLVP, NOPROTECT, NOWDT, NOXINST
#device ADC=10

#use delay(crystal=12MHz, clock=48MHZ) //cristal de 12MHz
#use rs232(stream=UART_1,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include "lcd.c"

void tabela(void);

int main(){
      
   unsigned int8 value_ADC, adcU;
   float adcUFloat;
   char valorU[6]="     V";
   
   lcd_init();
   
   delay_ms(500);
   setup_adc_ports( sAN0 | VSS_VDD );
   setup_adc(ADC_CLOCK_DIV_16);
   
   delay_ms(5000);
   tabela();
   
   for(;;){
         delay_ms(500);
         
         set_adc_channel(0);
         delay_ms(1);
         value_ADC = read_adc();
         delay_ms(500);
         
         adcU = value_ADC*0.474;
         adcUFloat = value_ADC*0.00474;
   
         valorU[0] = adcU/100%10+48;
         valorU[1] = ".";
         valorU[2] = adcU/10%10+48;
         valorU[3] = adcU%10+48;
         valorU[4] = adcU*10%10+48;
         
         printf("\t %c",valorU[0]);
         printf("%c",valorU[1]);
         printf("%c",valorU[2]);
         printf("%c",valorU[3]);
         printf("%c",valorU[4]);
         printf("%c",valorU[5]);
         
         printf("\t |\t %u ", value_ADC);
         printf("\t |\t %u ", adcU);
         printf("\t |\t %0.4f \r\n", adcUFloat);
         
         lcd_gotoxy( 5, 1);
         printf(lcd_putc, valorU);
         lcd_gotoxy( 11, 1);
         printf(lcd_putc, " .....");
         delay_ms(5000);
   }
   return 0;
}

void tabela(){
   
   lcd_gotoxy(1, 1);      
   printf(lcd_putc, "                ");
   lcd_gotoxy(2, 1);
   printf(lcd_putc, "U=      ");
   
   printf("\r\n");
   printf("\t      U");
   printf("\t          ADC(bit)   ");
   printf("\t           U_adc    ");
   printf("\t         adc U_float  \r\n");
}


Best Regards[/list]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: pic18F26K22 ADC 10-bit resolution!?
PostPosted: Sun Aug 16, 2020 1:34 pm     Reply with quote

Look closely at this and you'll see what's wrong:
RoMar wrote:

unsigned int8 value_ADC, adcU;

value_ADC = read_adc();
RoMar



Joined: 22 May 2020
Posts: 6

View user's profile Send private message

PostPosted: Sun Aug 16, 2020 2:10 pm     Reply with quote

In first place, thanks for your time and help,
Of course,

int8 -> 0 - 255

I change to int16 -> 0 to 65535
and
printf("\t |\t %Lu ", value_ADC);
printf("\t |\t %Lu ", adcU);

and everything is working,
well, I'm new here, thus mistakes are very close to me yet.
Thanks a lot.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group