User Tools

Site Tools


c_tips_and_tricks_for_pic_programming

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
c_tips_and_tricks_for_pic_programming [2007/12/18 10:07]
audiocommander added bitfields, cleaned up headline-levels
c_tips_and_tricks_for_pic_programming [2009/01/05 18:21]
Max Romantschuk Fixed broken structure
Line 11: Line 11:
 </​code>​ </​code>​
   * There'​s an excellent thread in the forum that discusses bitoperations:​ http://​www.midibox.org/​forum/​index.php?​topic=6981.0   * There'​s an excellent thread in the forum that discusses bitoperations:​ http://​www.midibox.org/​forum/​index.php?​topic=6981.0
 +  * The [[http://​www.midibox.org/​forum/​index.php/​topic,​9666.msg73781.html#​msg73781|Re:​ Scan Matrix extended : VOIRINOV]] thread has an explanation of what the declaration of the bitfield is all about.
   * 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**: ​+\\ 
 +\\
  
->> ​//if multiplicationsdivisionspointer operations, etcare used in the .c code, the linker may fail due to missing functionswhich are part of the libsdcc.lib library. The common library for pic16 derivatives ​is not compatible ​to MIOStherefore I've created a special one which can be downloaded from [[http://​www.ucapps.de/​mios/​mios_libsdcc_v2_5_0.zip|here]]. Read the README.txt file for further details. [[http://​www.ucapps.de/​mios_c.html|TK on the C-Page]] //+==== MIOS LIBSDCC Library ==== 
 + 
 +If that still is not enough or you have no time and a lot of processing power space available on your PICyou can include the **libsdcc library**. Using the new MIOS GPUtils structurethis will be included automatically as required. 
 + 
 +When using the librarysometimes ​the compiler will optimise multiplications ​to bitshifts (as demonstrated above) automatically. You can check the output files to see if this has occurredbut it is recommended ​to code the bitshifts manuallyto be sure.
  
  
 \\ \\
-====== Bitfields, Unions & Structs ​======+ 
 +==== Bitfields, Unions & Structs ====
  
   * Avoid using huge int- or char-arrays when you just need to store some ON/OFF values. Use a bitfield instead   * Avoid using huge int- or char-arrays when you just need to store some ON/OFF values. Use a bitfield instead
Line 45: Line 52:
 // get number // get number
 mynum = something.ALL;​ mynum = something.ALL;​
 +
 </​code>​ </​code>​
->> ​// Due to an SDCC restriction, bitfields are limited to 8 bits max //+ 
 +>> ​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 71: Line 79:
   * Adding the keyword '​[[http://​en.wikipedia.org/​wiki/​Volatile_variable|volatile]]'​ to a [[http://​en.wikipedia.org/​wiki/​Variable|variable]] is a good idea when this variable can be changed or altered outside the sourcefile that declared this variable.   * Adding the keyword '​[[http://​en.wikipedia.org/​wiki/​Volatile_variable|volatile]]'​ to a [[http://​en.wikipedia.org/​wiki/​Variable|variable]] is a good idea when this variable can be changed or altered outside the sourcefile that declared this variable.
  
-  * Always use '​unsigned'​ if you are sure you don't need negative values. ​As it's not clear how C treats a '​char'​ (signed: -128 to 127; unsigned: 0 to 255), it's better to be clear here. +  * Always use '​unsigned'​ if you are sure you don't need negative values. ​Although the default is an unsigned char, it's not always ​clear how C treats a '​char'​ (signed: -128 to 127; unsigned: 0 to 255), so it's better to be clear here. 
  
  
Line 96: Line 104:
 unsigned char value = MIDIValues[1];​ //explicit temp variable unsigned char value = MIDIValues[1];​ //explicit temp variable
 MIOS_MIDI_TxBufferPut(value);​ MIOS_MIDI_TxBufferPut(value);​
 +</​code>​
 +In most cases, adding parenthesis around your index variable has the same effect (see tip further down)
 +<code c>
 +MIOS_MIDI_TxBufferPut((MIDIValues[1]));​
 </​code>​ </​code>​
  
Line 101: Line 113:
  
 ==== 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];​ // will work unsigned char myArray[256];​ // will work
Line 108: Line 120:
 unsigned char myArray[64][4];​ // will work unsigned char myArray[64][4];​ // will work
 unsigned char myArray[64][5];​ // will not be linked! unsigned char myArray[64][5];​ // will not be linked!
 +
 +unsigned int myArray[128];​ // will work
 +unsigned int myArray[129];​ // will not be linked!
 </​code>​ </​code>​
  
c_tips_and_tricks_for_pic_programming.txt · Last modified: 2011/09/15 07:14 by ichaljhe