View previous topic :: View next topic |
Author |
Message |
sambyte
Joined: 29 Dec 2008 Posts: 12
|
Just a simple question |
Posted: Mon Dec 29, 2008 4:07 am |
|
|
I finally decided to use CCS C. This is mainly because of this comparison.
http://www.ccsinfo.com/content.php?page=newcompilercomp
I don't know if this is a fair comparison since it is coming from CCS itself.
I got myself a copy and have a go on it. I got a question...
example...
#byte PORTB = 6 //Just an example, check the <--- WHY?
#define ALL_OUT 0 //DATA sheet for the correct
#define ALL_IN 0xff //address for your chip
main() {
int i;
set_tris_b(ALL_OUT);
...
Q. Why PORTA (or PORTB etc) is not predefined in the device header .h file? Why do we have to refer to the device datasheet for the correct address every time, when it can be set as standard?
Thanks
Sam |
|
|
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
|
Posted: Mon Dec 29, 2008 4:17 am |
|
|
Is it just me, but when I buy a new compiler, the first thing I do is read the manual to find out what functions are available in the libraries.
If you read the manual, especially the Discrete I/O section you will see numerous functions for dealing with ports without having to know their addresses.
Andy |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Dec 29, 2008 4:34 am |
|
|
The CCS concept may be unexpected at first sight, cause other compilers typically are accessing SFRs to perform basic I/O. In the example, no PORTB SFR definition is needed when all IO is done by CCS built-in functions. They form a kind of hardware-abstraction layer. Direct SFR access is an additional option, that may be meaningful in special cases. |
|
|
sambyte
Joined: 29 Dec 2008 Posts: 12
|
|
Posted: Mon Dec 29, 2008 5:20 am |
|
|
The code fragment was actually taken from the CCS C manual on page 307 (actual page, 319)
...
#byte PORTB = 6 //Just an example, check the
#define ALL_OUT 0 //DATA sheet for the correct
#define ALL_IN 0xff //address for your chip
main() {
int i;
set_tris_b(ALL_OUT);
PORTB = 0;// Set all pins low
for(i=0;i<=127;++i) // Quickly count from 0 to 127
PORTB = i; // on the I/O port pin
set_tris_b(ALL_IN);
i = PORTB; // i now contains the portb value.
}
...
As a newbie to CCS C, example is the first part I'd look first for a quick start. To me, CCS should have only used built-in functions to avoid confusion in their examples. The code above should have read like this...
main() {
int i;
set_tris_b(ALL_OUT);
output_b(0);// Set all pins low
for(i=0;i<=127;++i) // Quickly count from 0 to 127
output_b(i); // on the I/O port pin
set_tris_b(ALL_IN);
i = input_b(); // i now contains the portb value.
} |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Dec 29, 2008 5:32 am |
|
|
A special point with built-in functions is the automatic control of TRISx registers in default mode. It's generally convenient but may be unwanted in some cases. Direct access to SFR is one means to handle these cases. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Dec 29, 2008 8:41 am |
|
|
The header files for all the chips I use have the I/O pins defined. I very rarely use a whole port for any one function so I hardly ever have to define a port. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Dec 29, 2008 8:41 am |
|
|
sambyte wrote: | As a newbie to CCS C, example is the first part I'd look first for a quick start. To me, CCS should have only used built-in functions to avoid confusion in their examples. | The referenced example program shows how to map a variable onto an I/O port, this is an advanced topic and is not meant as a general I/O example program. This example was given to show an alternative method for accessing the Special Function Registers when the inbuilt CCS functions do not suffice. Replacing the code by inbuilt functions reverses the purpose of this example. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Just a simple question |
Posted: Mon Dec 29, 2008 4:49 pm |
|
|
sambyte wrote: |
#byte PORTB = 6 //Just an example, check the <--- WHY?
Q. Why PORTA (or PORTB etc) is not predefined in the device header .h file? |
This is in my opinion a real shortcoming of the CCS compilers. They assume you are going to use their built-in functions to access all SFRs. I, for one, never use CCS library I/O functions, even though I use the CCS compiler. For each new processor I use, I have to go through the datasheet and make up my own extension to the header file, defining the SFRs and bits within them using the #byte and #define directives. There is nothing so complicated about the I/O ports that I would need a library function to make an interface between me and the PIC. Now things may be different for something like a USB stack. That is complicated enough to justify a library function, and I would hate to have to write my own. But for general purpose I/O, SPI, UART, CCP, I always talk directly to the registers. It is absolutely necessary for some performance-critical code, and not that hard for all code. Just my opinion... _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
sambyte
Joined: 29 Dec 2008 Posts: 12
|
|
Posted: Mon Dec 29, 2008 5:09 pm |
|
|
Hi Robert,
I agree with you. That's exactly what I'm going to do. For now, I just use #asm to access them directly. Like you've said, nothing so complicated about the PIC I/O...
I am just so used with MChip assembly SFR names...HiTECH C has no problem with them. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Dec 30, 2008 1:16 am |
|
|
If you use C statements for direct SFR accesses instead of assembler, the generated code is exactly the same, with writing less text. You need to define the SFRs once, of course. |
|
|
sambyte
Joined: 29 Dec 2008 Posts: 12
|
|
Posted: Tue Dec 30, 2008 2:48 am |
|
|
BTW Robert, this is just an idea. Do you mind sharing with me those header files? It'll save a lot of times. I was thinking of adding portion of HTC device header files to existing CCS device header files, with a bit of modification, of course.
Thanks
Sam |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 30, 2008 3:21 am |
|
|
How about using the forum search?.
There are two 'standard' includes posted here. One for 16 chips, and one for 18 chips. Use "register name defines", and set to search for 'all terms'. You will find PCM programmer has done links to the normally used defines.
Now, in general:
I'd avoid asm. In the early CCS days, it was necessary, but now, there is little you can do with assembler, that you can't do just as well with the compiler.
For 90% of applications, the CCS default approach works well. Look for example at the recent posting by PCM, on how to access half a port for read, and half for write. works just as well as an assembler solution.
Using the standard defines then allows those 'other' operations, stepping beyond those easy to do with the CCS functions. The latter compilers, have an ability to generate such a define file for you, but it would have been simpler, and tidier, for CCS just to include default files, with the compiler. A case of an 'overcomplex' solution really....
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Dec 30, 2008 3:22 am |
|
|
BTW, did you notice the PCW IDE's Device Editor tool and it's option, to generate SFR include files? |
|
|
sambyte
Joined: 29 Dec 2008 Posts: 12
|
|
Posted: Tue Dec 30, 2008 3:48 am |
|
|
Thanks guys..I'll try that... |
|
|
sambyte
Joined: 29 Dec 2008 Posts: 12
|
|
Posted: Tue Dec 30, 2008 6:58 am |
|
|
Thanks FvM. I've just found out about how to create the file. I'm beginning to like the CCS C more...
Thanks again... |
|
|
|