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

stdlib.h problem?

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



Joined: 21 Jan 2011
Posts: 5

View user's profile Send private message

stdlib.h problem?
PostPosted: Fri Jan 21, 2011 7:33 pm     Reply with quote

Hi, I'm new in c programming of pic mcu's and a new member of this forum, but in the process of c learning, I think that I discovered a problem with the "stdlib.h" library when is used with "#device ADC=10" directive.
I can't compile my code when I try to include "stdlib.h" library for use "itoa()" function when I have "#device ADC=10" directive and the error messages are:

*** Error 23 "led_test.c" Line 3(8,9): Can not change device type this far into the code
*** Error 48 "led_test.c" Line 3(9,12): Expecting a (
*** Error 43 "led_test.c" Line 3(13,15): Expecting a declaration
3 Errors, 0 Warnings.

The device which I use is PIC16F887.
What is the problem? Thank You.

Code:

#include <16F887.H>
//#include <stdlib.h>  // problem!? I can't use this *       
#device ADC=10         // ? *
#fuses INTRC_IO, NOMCLR, NOWDT
#use delay(clock=8M)
#use rs232(uart1, baud=9600, parity=N)

void main()
{  setup_oscillator(OSC_8MHZ);
   set_tris_d(0x00);
   int16 r,var;
   setup_adc_ports(sAN0);     
   setup_adc(ADC_CLOCK_DIV_8);       
   set_adc_channel(0);   
   
   while(1)
   {
      r =read_adc();   
     output_d(r);     
      printf("AD value is  %4LD \r\n", r);
      delay_ms(1000);   
   }
}
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Jan 21, 2011 7:42 pm     Reply with quote

Swap the second and third lines. The device declarations must always come first
_________________
Google and Forum Search are some of your best tools!!!!
dorinjj



Joined: 21 Jan 2011
Posts: 5

View user's profile Send private message

PostPosted: Fri Jan 21, 2011 8:03 pm     Reply with quote

Thank You for reply. Smile
Problem solved.
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Sat Jan 22, 2011 3:16 am     Reply with quote

I'd actually say, to avoid future problems, put the include down after _all_ the initialisation stuff. So:

1) Processor define/include
2) Any extra 'device' statements
3) Fuses and clock statements
4) device configuration (#use RS232/I2C etc.)

5) Include other stuff & defines
6) main code.

Problem is that if any included file uses RS232, or timings, it really needs to come after the clock setups, and RS232 declarations. Think of 1 to 4, as the 'header', that must be there before other code is loaded.

You can twiddle the order a little, _but_ in general, this is the safest layout to use, and is quite logical.

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