1 / 8

ECE 447 - Lecture 22

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

zora
Download Presentation

ECE 447 - Lecture 22

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 447 - Lecture 22 Common Errors & Good Programming Style

  2. 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;

  3. 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) ;

  4. 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

  5. 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; }

  6. 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

  7. 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

  8. 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

More Related