Difference between revisions of "Index of sample code pages"
Russ hensel (talk | contribs) |
Russ hensel (talk | contribs) |
||
Line 30: | Line 30: | ||
[[A C Program Header Example]] A good header is a help to anyone who wants to use your code. | [[A C Program Header Example]] A good header is a help to anyone who wants to use your code. | ||
+ | == Using #define to Configure Hardware == | ||
+ | |||
+ | |||
+ | |||
+ | == Using #define to Support Multiple CPU Targets == | ||
+ | |||
+ | |||
+ | == Using #define for Magic Numbers == | ||
== Counting Down with Unsigned Numbers == | == Counting Down with Unsigned Numbers == |
Revision as of 16:38, 27 March 2009
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.
Contents
- 1 The perfect second
- 2 Output to A Shift Register
- 3 Software Circular Buffer
- 4 read before write
- 5 Good Main Program Header
- 6 Using #define to Configure Hardware
- 7 Using #define to Support Multiple CPU Targets
- 8 Using #define for Magic Numbers
- 9 Counting Down with Unsigned Numbers
- 10 Access Bytes Inside Another Type
The perfect second
Timing routines, showing how seconds can be counted. Want high accuracy even with odd clock rates.
Output to A Shift Register
Often used for port expansion. Code still needed.
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
Using #define to Support Multiple CPU Targets
Using #define for Magic Numbers
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]