Difference between revisions of "Index of sample code pages"

From OpenCircuits
Jump to navigation Jump to search
Line 93: Line 93:
  
 
it will only compile if the target is a 16f877A.  This way one file can contain code for a bunch od different processors.
 
it will only compile if the target is a 16f877A.  This way one file can contain code for a bunch od different processors.
 +
 +
 +
If you want to include more than one processor you can use code like:
 +
 +
   
 +
    #if ( defined ( _PIC16F877A_H_ ) || defined ( _PIC16F877_H_ ) )
 +
          TRISA = 0b00011111;
 +
    #endif
  
 
== Using #define for Magic Numbers ==
 
== Using #define for Magic Numbers ==

Revision as of 19:13, 19 May 2009

This page is more of an idea than a finished product, we need help to fill it out.


These are short sections of code for particular tasks. The first goal is to get code that works. Later goals include:

  • Easily modified code..
  • Robust: failure resistant even with bad input.
  • Fast
  • Plays well when imbeded with other routines.
  • Minimum resource use.

Because of the different goals we may have more than one sample in each category.


The perfect second

Timing routines, showing how seconds can be counted. Want high accuracy even with odd clock rates.


Using I2C Memory

Using I2C Memory

Output to A Shift Register

Often used for port expansion.

This seems to work for 8 bits to a 74HC/HCT164

    void shiftOutByte( unsigned char aData ) {
    
    	unsigned char ix;
    
    	for( ix = 0; ix < 7; ix++ )   {
    	
    		if ( aData.1 ) {
    			set_bit( SRPort, SRDataBit );
    		} else {
         		clear_bit( SRPort, SRDataBit );
    		}
    		
    		clear_bit( SRPort, SRClockBit );   // edge triggered low to high 
    		aData  >>= 1; // put here as delay 
    		set_bit( SRPort, SRClockBit );
    	
       }
    	return;
    }

Software Circular Buffer

A place to store and retrieve data, say bytes. First in First out. Methods to add and remove data. Code still needed.

read before write

Solving the issue of PIC16's pin state not being saved internally. Code still needed.

Good Main Program Header

A C Program Header Example A good header is a help to anyone who wants to use your code.

Using #define to Configure Hardware

Suppose you are lighting an LED on PORTB.3. If you use the following defines:

   #define   LED_PORT PORTB
   #define   LED_BIT  3

then

    set_bit( LED_PORT, LED_BIT );

can be used instead of

    set_bit( PORTB, 3 );

There are a couple of advantages to this:

  • The code is easier to read and write.
  • Reconfiguring the hardware can be done simply by just changing the #defines.
  • Reading the #defines ( normally all at the top of the program ) often gives a good overview of the hardware assignment.

Using #define to Support Multiple CPU Targets

The compiler ( actually I think the preprocessor ) always defines a symbol that contains the name of the target processor, for example _PIC16F877A_H_. This means you can surround code with the following preprocessor commands:

    #ifdef  _PIC16F877A_H_
         TRISA = 0b00011111;
    #endif

it will only compile if the target is a 16f877A. This way one file can contain code for a bunch od different processors.


If you want to include more than one processor you can use code like:


    #if ( defined ( _PIC16F877A_H_ ) || defined ( _PIC16F877_H_ ) )
         TRISA = 0b00011111;
    #endif

Using #define for Magic Numbers

A magic number is a number, that for some reason, has a special meaning. For example the maximum number for an 8 bit number is 255, but a reader of the program might not recognize this significance. If you:

    #define MAX_8BIT = 255

then a test like

    if ( a == MAX_8BIT ) ....

might be more meaningfull. ( note this is not a great example, best I could come up with right now ) You could consider that port addresses are aqctually numbers ( and different for diffent processors ) and that symbols like PORTB are actually magic numbers defined differently for different processors.

Counting Down with Unsigned Numbers

You are counting down and want to know when an unsigned number goes negative ( never ). You could declare it signed, slowing everything down. Instead check against FF. This assumes you do not use FF on the positive side. It also assumes you are counting down by one, else you could skip over FF.

  if ( 0xFF == ix ) ..

Access Bytes Inside Another Type

Example use a union:

    typedef union {
         unsigned int ui;
         unsigned char b[2];
    } noportable_int;
    
    //main program starts here.    
    void main()   {
    
         noportable_int n;
    
         n.ui        = 0x1234;
    
         n.b[0]      = tmr1l;
         n.b[1]      = tmr1h;
    
    }

idea from: [1]