c_tips_and_tricks_for_pic_programming
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
c_tips_and_tricks_for_pic_programming [2007/08/30 11:38] – Added stack expansion stryd_one | c_tips_and_tricks_for_pic_programming [2008/04/05 21:03] – Added info for the gpasm toolchain stryd_one | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | \\ | ||
- | |||
====== Arithmetic Calculations ====== | ====== Arithmetic Calculations ====== | ||
Line 13: | Line 11: | ||
</ | </ | ||
* There' | * There' | ||
+ | * The [[http:// | ||
* If this is not enough, you could search for ASM optimized custom functions. You'll find some in code examples of TK, the [[ACSensorizer]] and a lot of PIC-Specialized Webpages – or of course the forum. | * If this is not enough, you could search for ASM optimized custom functions. You'll find some in code examples of TK, the [[ACSensorizer]] and a lot of PIC-Specialized Webpages – or of course the forum. | ||
- | * If that still is not enough or you have no time and a lot of processing power / space available on your PIC, you can include the **libsdcc library**: | + | \\ |
+ | \\ | ||
+ | ==== MIOS LIBSDCC Library ==== | ||
- | >> | + | If that still is not enough or you have no time and a lot of processing power / space available on your PIC, you can include the **libsdcc library**: |
+ | \\ | ||
+ | If multiplications, | ||
+ | You Should specify your custom linker script in the makefile if using the new gpasm based mios toolchain. | ||
+ | \\ | ||
+ | When using the library, sometimes the compiler will optimise multiplications to bitshifts (as demonstrated above) automatically. You can check the output files to see if this has occurred, but it is recommended to code the bitshifts manually, to be sure. | ||
\\ | \\ | ||
+ | ====== Bitfields, Unions & Structs ====== | ||
+ | |||
+ | * Avoid using huge int- or char-arrays when you just need to store some ON/OFF values. Use a bitfield instead | ||
+ | <code c> | ||
+ | // define the bitfield | ||
+ | typedef union { | ||
+ | struct { | ||
+ | unsigned ALL: | ||
+ | }; | ||
+ | struct { | ||
+ | unsigned led1: | ||
+ | unsigned led2:1; | ||
+ | unsigned led3:1; | ||
+ | unsigned led4:1; | ||
+ | unsigned free:4; | ||
+ | }; | ||
+ | } something_t; | ||
+ | |||
+ | // declare var | ||
+ | something_t something; | ||
+ | |||
+ | // set bits | ||
+ | something.led1 = 1; | ||
+ | something.led3 = 0; | ||
+ | |||
+ | // get number | ||
+ | mynum = something.ALL; | ||
+ | </ | ||
+ | >> // It has been confirmed with recent versions of SDCC, that bitfields are not limited to 8bits as was previously expected. | ||
+ | |||
+ | |||
+ | \\ | ||
====== C Functions ====== | ====== C Functions ====== | ||
Line 42: | Line 80: | ||
* Adding the keyword ' | * Adding the keyword ' | ||
- | * Always use ' | + | * Always use ' |
- | \\ | ||
+ | |||
+ | |||
+ | \\ | ||
====== SDCC Bugs/ | ====== SDCC Bugs/ | ||
Some of these bugs have first been described in a [[http:// | Some of these bugs have first been described in a [[http:// | ||
Line 52: | Line 92: | ||
- | ===== Array Access | + | ==== Array Access ==== |
Sometimes the transfer of an array between modules does not work properly, e.g. file 1: | Sometimes the transfer of an array between modules does not work properly, e.g. file 1: | ||
<code c> | <code c> | ||
Line 65: | Line 105: | ||
unsigned char value = MIDIValues[1]; | unsigned char value = MIDIValues[1]; | ||
MIOS_MIDI_TxBufferPut(value); | MIOS_MIDI_TxBufferPut(value); | ||
+ | </ | ||
+ | In most cases, adding parenthesis around your index variable has the same effect (see tip further down) | ||
+ | <code c> | ||
+ | MIOS_MIDI_TxBufferPut((MIDIValues[1])); | ||
</ | </ | ||
\\ | \\ | ||
- | ===== Large Arrays | + | ==== Large Arrays ==== |
- | Arrays with more than 256 elements will produce compile (in fact linker) errors: | + | Arrays with more than 256 bytes of elements will produce compile (in fact linker) errors: |
<code c> | <code c> | ||
unsigned char myArray[256]; | unsigned char myArray[256]; | ||
Line 77: | Line 121: | ||
unsigned char myArray[64][4]; | unsigned char myArray[64][4]; | ||
unsigned char myArray[64][5]; | unsigned char myArray[64][5]; | ||
+ | |||
+ | unsigned int myArray[128]; | ||
+ | unsigned int myArray[129]; | ||
</ | </ | ||
Line 84: | Line 131: | ||
Thanks to Thomas for [[http:// | Thanks to Thomas for [[http:// | ||
- | \\ | + | |
\\ | \\ | ||
- | + | ==== Bit Copy Operations ==== | |
- | ===== Bit Copy Operations | + | |
There is potential trouble with bit copy operations (See [[http:// | There is potential trouble with bit copy operations (See [[http:// | ||
<code c> | <code c> | ||
Line 102: | Line 148: | ||
</ | </ | ||
It is less elegant, but it works safely. | It is less elegant, but it works safely. | ||
+ | |||
\\ | \\ | ||
- | ===== Parenthesis | + | ==== Parenthesis ==== |
Always use parenthesis around expressions like <code c> | Always use parenthesis around expressions like <code c> | ||
\\ | \\ | ||
- | ===== Preprocessor #ifs ===== | + | ==== Preprocessor #ifs ==== |
Avoid #ifdef and #if preprocessor-statements wrapped around declarations and function prototypes. Even if the preprocessor' | Avoid #ifdef and #if preprocessor-statements wrapped around declarations and function prototypes. Even if the preprocessor' | ||
#define TEST 1 | #define TEST 1 | ||
Line 121: | Line 168: | ||
</ | </ | ||
- | \\ | ||
- | ===== Zero Compare | + | \\ |
- | Avoid comparisons of '' | + | ==== Zero Compare ==== |
+ | Avoid comparisons of '' | ||
+ | <code c> | ||
unsigned char i; | unsigned char i; | ||
for (i = 0; i < 0; i+ü) { | for (i = 0; i < 0; i+ü) { | ||
- | //body | + | // body |
- | | + | } |
- | + | ||
- | \\ | + | |
</ | </ | ||
+ | '' | ||
- | ===== Stack Size ===== | + | \\ |
+ | ==== Stack Size ==== | ||
[[http:// | [[http:// |
c_tips_and_tricks_for_pic_programming.txt · Last modified: 2011/09/15 07:14 by ichaljhe