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

PIC24F and ADC expression

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



Joined: 11 Oct 2007
Posts: 12

View user's profile Send private message

PIC24F and ADC expression
PostPosted: Sun Nov 28, 2010 8:39 am     Reply with quote

Please,
I'd like a help with this code below...
The ad_c value shows the value 63 when the ADC value is 1023.
The expression "ad_c = (ad_c * 3300)/1023;" return a wrong value.


Code:
#include <24FJ128GA010.h>
#device ADC = 10           
#fuses HS,PR,NOWDT,NOPROTECT
#use delay(clock=8000000)

#include "LCD8B.c" 

int32 ad_c;

void main()         {         

lcd_ini();         
setup_adc_ports(sAN0, VSS_VDD);
setup_adc(ADC_CLOCK_DIV_128 | ADC_TAD_MUL_31);
SET_ADC_CHANNEL(0);               

while(true)              {
     ad_c = READ_ADC();
     ad_c = (ad_c * 3300)/1023;       
     printf(lcd_escreve,"\fADC = %ld",ad_c);     
     delay_ms(1000);   
    }

}


Thanks


Last edited by sandrini on Mon Nov 29, 2010 4:56 am; edited 1 time in total
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sun Nov 28, 2010 10:36 am     Reply with quote

Couple of things come to mind here
- there needs to be a delay after you set the channel before you can read and
- I would consider modifying the print routine to display both the raw count from the ADC and the result.
You indicate you get the wrong result for a given count, but you don't say how you determined that really was the count. I may have missed something (have not had coffee yet), but it doesn't look like an overflow since you are using int32.

Also, you don't indicate what you are using for the "temp" reading, but most of the linear temp sensors have an offset you need to subtract out as well (500mv=0degC for example). You might also want to consider using signed so if the resulting temp is negative it doesn't blow up Very Happy

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
temtronic



Joined: 01 Jul 2010
Posts: 9161
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Nov 28, 2010 11:32 am     Reply with quote

also..

if the 3300 and 1023 are 'constants', have the compiler do the work !

have a line like

const int midterm= 3300 / 1023

it might save some time as division takes a lot of time compared to mutlply
sandrini



Joined: 11 Oct 2007
Posts: 12

View user's profile Send private message

PostPosted: Sun Nov 28, 2010 12:00 pm     Reply with quote

gpsmikey temtronic and I would like to thank ...

In my code, the correct is "printf (lcd_escreve," \ fADC =% ld ", ad_c);"

I do not know what is happening, but when I use the expressin "ad_c = (ad_c * 3300) / 1023" ... The result is completely wrong.
I use this expression with the PIC16 and PIC18 and have no problems.

I decided to use the suggestion of Temtronic, so I'm dividing 'ad_c' by '3 .22 (3300/1023), in which case the result is correct.

But still many thanks for the tips.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Nov 28, 2010 1:13 pm     Reply with quote

Quote:
const int midterm= 3300 / 1023

it might save some time as division takes a lot of time compared to mutlply

Results in midterm value of 3, so it's only a roughly correct result.

Actually, there's no plausible reason why the original code shouldn't work, except you're using a buggy PCD version. Which version has been used?
sandrini



Joined: 11 Oct 2007
Posts: 12

View user's profile Send private message

PostPosted: Mon Nov 29, 2010 4:56 am     Reply with quote

I'm using version 4.108 and I also believe it is a bug in the compiler.

I have no problems with the PIC 16F and / or 18F, only now I started working with the PIC 24F, I noticed these problems.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Nov 29, 2010 5:08 am     Reply with quote

You said, it's the calculation not the ADC built in function. But I checked the calculation to be performed correctly in V4.107 and also recent PCD versions (don't have 4.108 installed at present). I would trace the program execution in debugger to find, where it fails.
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