80 likes | 146 Views
ECE 447 - Lecture 22. Common Errors & Good Programming Style. Do not use magic constants!. Bad style. Good style. #include <hc11e9.h>. #include <hc11e9.h>. #define PIOC_STAI 0x40 #define PIOC_EGA 0x20 #define TFLG2_TOF 0x80. PIOC |= 0x40; PIOC &= 0xBF;. // set EGA
E N D
ECE 447 - Lecture 22 Common Errors & Good Programming Style
Do not use magic constants! Bad style Good style #include <hc11e9.h> #include <hc11e9.h> #define PIOC_STAI 0x40 #define PIOC_EGA 0x20 #define TFLG2_TOF 0x80 PIOC |= 0x40; PIOC &= 0xBF; // set EGA PIOC |= PIOC_EGA; // clear STAI PIOC &= ~PIOC_STAI;
Do not use magic constants! Bad style Good style #include <hc11e9.h> #include <hc11e9.h> // do nothing while TOF equal to 0 while (!(TFLG2 & 0x80)) ; while (!(TFLG2 & TFLG2_TOF)) ; while ((TFLG2 & TFLG2_TOF) == 0) ;
Do not use magic constants! Good style Bad style PIOC EQU $1002 PIOC_STAI EQU $40 PIOC_EGA EQU $20 // set EGA LDAA PIOC ORAA #PIOC_EGA STAA PIOC // clear STAI LDAA PIOC ANDA #~PIOC_STAI STAA PIOC LDAA $1022 ORAA #$02 STAA $1022 LDAA $1022 ANDA #$BF STAA $1022
Initializing global variables Bad practice Good practice int counter; int counter = 0; char lookup[3]; const char lookup[] = {0xE6, 0xE4, 0xEA}; const char string1 = “Menu:”; char string1[5]= {‘M’,’e’,’n’,’u’,’:’}; void main(void) { counter = 0; } main() { lookup[0] = 0xE6; lookup[1] = 0xE4; lookup[2] = 0xEA; }
Common errors regarding interrupts C • No __attribute__((interrupt)) in the declaration/prototype • of an interrupt service routine • Using local auto variables to store information • between interrupts • Calling an interrupt service routine in your program • (allowed only for testing after removing __attribute__(()) modifier) ASM • Using RTS instead of RTI C & ASM • No CLI • No setting of the local interrupt enable flag • Waiting for a flag to be set INSIDE of an interrupt
Common errors regarding polling C • Using if instead of while to wait for a flag to be set • No clearing the flag after it was set • No using of special procedure to clear the flag • No setting of an interrupt enable mask • Checking the wrong flag ASM Using too complicated procedure to check the flag Bad style Good style LOOP LDAA PIOC ANDA #PIOC_STAF CMPA #0 BEQ LOOP LOOP TST PIOC BPL LOOP
Use but do not abuse comments! Useless and repetitive LDAA #INIT ; load accumulator A with the constant INIT STAA COUNTER ; store accumulator A to the variable COUNTER Effective ; initialize COUNTER to INIT LDAA #INIT STAA COUNTER