This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
gm_arts_tb-12_v1 [2016/10/21 06:07] gm_arts [Code Improvements] |
gm_arts_tb-12_v1 [2017/06/28 00:07] (current) |
||
|---|---|---|---|
| Line 2: | Line 2: | ||
| versions 1.1, 1.5, 2.1 and 2.2:\\ | versions 1.1, 1.5, 2.1 and 2.2:\\ | ||
| - | {{gm_arts_tb-12_r03.zip|download here}} | + | {{gm_arts_tb-12_r03a.zip|download here}} |
| ==== Overview ==== | ==== Overview ==== | ||
| Line 446: | Line 446: | ||
| \\ | \\ | ||
| - | ==== Code Improvements ==== | ||
| - | I hadn't coded in C for about 20 years before working on firmware for the TB-12, so some of the code is as bit rough. A couple of obvious improvements are: | ||
| - | |||
| - | (1) The function to set bit values is better with pointers, so instead of: | ||
| - | |||
| - | <code> | ||
| - | uint16_t SetBit16(uint16_t var, uint8_t newstate, uint8_t bitnum) | ||
| - | { | ||
| - | if(newstate) | ||
| - | return var | (1 << bitnum); | ||
| - | else | ||
| - | return var & ~(1 << bitnum); | ||
| - | } | ||
| - | |||
| - | // called with: | ||
| - | SomeVar = SetBit16(SomeVar, 1, 5) | ||
| - | </code> | ||
| - | |||
| - | This is better: | ||
| - | |||
| - | <code> | ||
| - | void SetBit16(uint16_t *var, uint8_t newstate, uint8_t bitnum) | ||
| - | { | ||
| - | if(newstate) | ||
| - | *var |= (1 << bitnum); | ||
| - | else | ||
| - | *var &= ~(1 << bitnum); | ||
| - | } | ||
| - | |||
| - | // called with: | ||
| - | SetBit16(&SomeVar, 1, 5); | ||
| - | </code> | ||
| - | |||
| - | (2) Calling functions within a timer event is not good practice, even though it works fine in the example due to the relatively slow speed of the timer. So instead of: | ||
| - | |||
| - | <code> | ||
| - | ISR(TIMER1_COMPA_vect) | ||
| - | { | ||
| - | // update counters | ||
| - | // call functions | ||
| - | } | ||
| - | </code> | ||
| - | |||
| - | There are several ways around this, I just use a simple flag: | ||
| - | |||
| - | <code> | ||
| - | // at the start of the program in declarations: | ||
| - | uint8_t volatile bFiredTimer1 = false; | ||
| - | |||
| - | ISR(TIMER1_COMPA_vect) | ||
| - | { | ||
| - | // update counters | ||
| - | bFiredTimer1 = true; | ||
| - | } | ||
| - | |||
| - | // and in the main loop | ||
| - | int main(void) | ||
| - | { | ||
| - | // initialisations (including setting up timer 1) | ||
| - | while(1) | ||
| - | { | ||
| - | // check footswitches | ||
| - | // check pedals | ||
| - | // check timer | ||
| - | if(bFiredTimer1) { | ||
| - | bFiredTimer1 = false0; | ||
| - | // call functions | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </code> | ||