View Single Post
Old 04-07-2009, 09:28 AM   #780 (permalink)
MazdaMatt
Master EcoModder
 
Join Date: Jun 2008
Location: London, Ontario
Posts: 1,096

2k2Prot5 - '02 Mazda Protege5
90 day: 33.82 mpg (US)
Thanks: 0
Thanked 17 Times in 14 Posts
On that topic, i have a suggestion for your code. It is wise to create a HAL (hardware abstraction layer). It is as simple as creating a ".h" file with a list of all of the ports that you use, defined with a logical name.

This is not a code snippet from this or any project, just an example.

before:
(source.c)
PORTB &= 0x04; //this turns on the overvolt LED

after:
(hal.h)
#define OVERVOLT_LED_PORT PORTB
#define OVERVOLT_LED_PIN 0x04
(source.c)
OVERVOLT_LED_PORT &= OVERVOLT_LED_PIN; //this turns on the overvolt LED

The benefit of doing this is extreme portability. You can move from atmega to atmega by simply changing the HAL.h file. You can also re-layout the board using differnt pins/ports and you only need to change code in ONE place for it all to work. Additionally, I could then take your source code, lay out my own board using a Freescale Coldfire chip and re-write the HAL for my own purposes. All of your code stays the same.
You can also abstract the HAL! so HAL.h only has:
#include <paulATM80.1HAL.h>
and all the rest of your files include the HAL. Then as people create new board layouts, they just create their own. titleChipVerHAL.h file and they can switch between them by changing a single line of code.
  Reply With Quote