Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 16th April 2008, 03:33 AM   #16
Default

You might accuse me of being a purist - but to reduce errors i'd suggest you let the compiler keep track of miniscule stuff like the size of an array - so instead of:

Code:
#define TABLE_SIZE 100
const rom unsigned char roots[TABLE_SIZE] =
{ 0, 1, 1, 1, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };


Code:
const rom unsigned char roots[] =
{ 0, 1, 1, 1, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };

#define TABLE_SIZE  sizeof(roots)
sizeof() always returns the size in bytes - so if it's an array of bytes you are good to go.

if it was an array of integers (16 bit value in the case of a PIC i think?) you would say:

Code:
#define TABLE_SIZE  sizeof(roots)/sizeof(int)
Just one less Constant you have to manually update is all - in the case of your example it comes back with the correct answer either way.
David / W9GF is offline  
Old 16th April 2008, 04:04 AM   #17
Default

Oops! I didn't realize we were talking about two different C compilers either.

Thank you for all the clarifications guys. I'm going to try and change the RAM array to a constant ROM array in that old DayOfWeek() routine...
Mike, K8LH is offline  
Reply

Tags
c18, data, define, table

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
usart with pic neelam29 Micro Controllers 19 18th July 2009 09:39 AM
Motor controller Totally blank Electronic Projects Design/Ideas/Reviews 9 16th January 2008 02:12 PM
18F4550 - PORTD inputs are shaky, even with internal pull up enabled toodles Micro Controllers 0 29th April 2007 03:05 AM
help me to describe this code indie Micro Controllers 0 18th June 2004 02:11 PM
An error in pic16f84a, why? Zener_Diode Micro Controllers 6 11th April 2004 03:55 AM



All times are GMT. The time now is 08:27 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker