PDA

View Full Version : I’m Running Out of CodeSpace, What Can I Do?



Melanie
- 12th July 2004, 20:25
I’m Running Out of CodeSpace, What can I do?
My progam is too slow - What can I do?

Apart from the obvious and move to a faster PIC with more program memory, there’s a few little known tricks you can try.


Bank0

No, that's not a reference to how much money you've got left at the end of each month. It refers to locating all your most commonly used variables in Bank0. Why? It takes a PIC fewer machine instructions to access Bank0 RAM than it does to access RAM located further up the chain. Fewer instructions means RAM is accessed faster - You may even find a performance improvement in seriously intensive processing applications.

You can force your variables into Bank0 by simply specifying Bank0 after your variables definition… here is a snip from a typical program…



ADCValue var Byte Bank0 ' Final value from ADC
BeepTone var Word ' Variable for Beep Period (half-cycle)
BeepLength var Word ' Number of Repitition Cycles to execute
CounterA var Byte Bank0 ' General Purpose Byte Counter
CounterB var Byte Bank0 ' General Purpose Byte Counter
Counterc var Word Bank0 ' General Purpose Word Counter
DataA var Byte Bank0 ' General Purpose Byte Variable
DataB var Byte Bank0 ' General Purpose Byte Variable
DataC var Word Bank0 ' General Purpose Word variable
DataP var Byte [45] ' Preset Data loaded from EEPROM
DataW var Word [28] ' Word Counters/Timers
Display var Byte ' Display Status Counter
ErrorStatus var Word Bank0 ' System Error Status
I2CAddress var Byte ' I2C Bus Hardware Address
IPRX var WORD ' Status Byte Received from AUX IP CPU
IPTX var WORD ' Command Byte for Transmission to AUX IP CPU
KeyBut var Byte Bank0 ' Keyboard Status
MenuStatus var Byte Bank0 ' Current Set-Up Menu Field being Referenced
Message var Byte Bank0 ' Message Reference for Display


How much can I save?

Well, this very much depends on your style of programming and your use of variables, but, typically on an 8K PIC (say 16F876, 16F877), creative use of Bank0 can free up between 1K and 2K… that’s up to 25% extra codespace for FREE. Next time you’re running out of space or end up plagued by ‘feature-creep’ try it.

Melanie