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 CCS Technical Support

round function for a double

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 18, 2008 4:22 pm     Reply with quote

One thing I want to check: What is your crystal frequency ?
Guest








PostPosted: Tue Mar 18, 2008 4:31 pm     Reply with quote

Hi PCM,
It is a crystal at 20Mhz.

Regards

Marken
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 18, 2008 5:00 pm     Reply with quote

Quote:
#include <18F4550.h>
#device adc=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)

It is a crystal at 20Mhz.

Your PIC is really running at 48 MHz.


If I take the following test program and use your #fuses settings, and
change the #use delay() statement to 48 MHz, the LED on pin B0 blinks
at a 1 second interval when timed with a stop watch.
Code:
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

//===============================
void main()
{

while(1)
  {
   output_high(PIN_B0);
   delay_ms(100);
   output_low(PIN_B0);
   delay_ms(900);
  }

}


You need to change the A/D divisor to 64 now, so it will work with
a 48 MHz oscillator frequency. This could be the source of most
of your A/D problems. Also change the #use delay() statement
to 48 MHz as shown above. Example:
Code:
#use delay(clock=48000000)

and
Code:
setup_adc(ADC_CLOCK_DIV_64);
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Mar 18, 2008 5:44 pm     Reply with quote

Quote:
I do not understand why CCS does not support round function it is a standard C function
Round is a relative new C function, it was not available in the ISO C89 standard. Reason for this might be that round is not so universal as you think it is, for example: how to handle rounding in negative numbers? What to do if the rounded value goes outside the range of the return type? That's why C99 defines multiple new rounding functions besides ceil and floor: trunc, round, nearbyint / rint and lrint.
It would be nice for CCS to add these functions (send them an email) but it's easy enough to roll them yourself.

Quote:
Then trough a potential divider I read the output voltage and feed it to a 10 bit analogue input on the PIC 18f4550.
What are the resistor values in your voltage divider? For a reliable reading the impedance as seen by the PIC must be relative low, check the datasheet.
Ttelmah
Guest







PostPosted: Wed Mar 19, 2008 8:55 am     Reply with quote

Yes.
Ckielstra's comment here, is very much a likely start to the problems. Once the code is working (timing the ADC right, which otherwise could be part of the problem), when dealing with a voltage like this, there are likely to be a number of difficulties. First, the DAC itself, is likely to exhibit some lag, or overshoot (or even both), on the output voltage generated. Then the voltage fed back to the PIC, from the output, will take time to respond (depending on the capacitance, and resistances present in the potential divider). To design a DAC/ADC combination, with gives fast response, without oversoot, is quite a complex task....
Look at the Microchip application notes. Select 'signal conditioning and measurement', then ADC.
Though a lot of the notes are for older chips (like the 16C71), the details a the same for all the Microchip converters. You may find out why you might want to use 1024, rather than 1023 as the divider for Microchip ADC's - the most accurate conversion, is actually:

float_val = ((read_adc()*2)+1)/2048;

Best Wishes
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