160 likes | 297 Views
Examples 1 - 4. Lecture L2.2. Example 1a. // Example 1a: Turn on every other segment on 7-seg display # include <hidef.h> /* common defines and macros */ # include <mc9s12dp256.h> /* derivative information */ # include "main_asm.h" /* interface to the assembly module */
E N D
Examples 1 - 4 Lecture L2.2
Example 1a // Example 1a: Turn on every other segment on 7-seg display #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { PLL_init(); // set system clock frequency to 24 MHz DDRH = 0xff; // Port H is output PTH = 0x55; // switch on every other segment for(;;) {} /* wait forever */ }
Example 1b // Example 1b: 7-Segment Display #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display seg7_on(0x55); // switch on every other segment for(;;) {} /* wait forever */ }
Example 2a // Example 2a: Blinking 7-Segment Display #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void delay(void); void main(void) { seg7_enable(); // enable 7-segment display while(1){ seg7_on(0xFF); // switch on all segments delay(); seg7_on(0x00); // switch off all segments delay(); } } void delay() { int i,j; for(i = 0; i < 500; i++) { for(j = 0; j < 5999; j++) { } } }
Example 2b // Example 2b: Blinking 7-Segment Display - ms_delay(ms) #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display while(1){ seg7_on(0xFF); // switch on all segments ms_delay(500); // half-second delay seg7_on(0x00); // switch off all segments ms_delay(500); // half-second delay } }
Example 3a // Example 3a: 7-Segment Decoder - C version #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void delay(void); void main(void) { constchar seg7tbl[] = { 0x3F,0x06,0x5B,0x4f, 0x66,0x6D,0x7D,0x07, 0x7F,0x6F,0x77,0x7C, 0x39,0x5E,0x79,0x71 }; int i;
Example 3a (cont.) PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display while(1){ for(i = 0; i < 16; i++) { PTH = seg7tbl[i]; delay(); } } } void delay() { int i,j; for(i = 0; i < 500; i++) { for(j = 0; j < 6248; j++) { } } }
Example 3b // Example 3b: 7-Segment Decoder - seg7dec(i) #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { int i; PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display while(1){ for(i = 0; i < 16; i++) { seg7dec(i); ms_delay(500); } } }
Example 4a // Example 4a: Single segments in 7-Segment Display #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void delay(void); void main(void) { PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display
Example 4a (cont.) while(1){ PTH |= 0x01; // turn on segment a delay(); PTH |= 0x40; // turn on segment g delay(); PTH |= 0x08; // turn on segment d delay(); PTH &= 0xFE; // turn off segment a delay(); PTH &= 0xBF; // turn off segment g delay(); PTH &= 0xF7; // turn off segment d delay(); } } void delay() { int i,j; for(i = 0; i < 500; i++) { for(j = 0; j < 6248; j++) { } } }
Example 4b // Example 4b: Single segments in 7-Segment Display #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { /* put your own code here */ seg7_enable(); // enable 7-segment display
Example 4b (cont.) while(1){ PTH_HI(0); // turn on segment a ms_delay(500); // half-second delay PTH_HI(6); // turn on segment g ms_delay(500); // half-second delay PTH_HI(3); // turn on segment d ms_delay(500); // half-second delay PTH_LO(0); // turn off segment a ms_delay(500); // half-second delay PTH_LO(6); // turn off segment g ms_delay(500); // half-second delay PTH_LO(3); // turn off segment d ms_delay(500); // half-second delay } }