/**************************************************************************************************************** ELE3EMP Laboratory 3 - Introduction to ZiLOG Z8Encore Development Environment - Task 1 - Blink all four LEDs on the Development Board. Written by Daniel Stephenson, 17/2/2006 Modified by Geoffrey Tobin, 7, 15, 22 March 2007 ****************************************************************************************************************/ #include // A byte is an unsigned 8-bit quantity. typedef unsigned char byte; #include // Function Prototypes void delay (void); void init_IO (void); void LED_bit_pattern (byte); void turn_off_LEDs (void); void turn_on_LEDs (void); /****************************************************************************************************************/ /* Main program begins here */ /* This program blinks ALL 4 LEDs continually */ /****************************************************************************************************************/ void main (void) { init_IO(); // Initializes PortG[0..3] to drive LEDs turn_off_LEDs(); // Make sure the LEDs are off to begin the program delay(); // Keep them off for one "unit" of delay for(;;) // Infinite loop { turn_on_LEDs (); // keep LEDs on for three "units" of delay delay(); delay(); delay(); turn_off_LEDs (); // keep LEDs off for three "units" of delay delay(); delay(); delay(); } // end of infinite loop } // end of main() /****************************************************************************************************************/ // function to intiialise PORTG which has the LEDs attached to the lower nibble bits PortG[0..1] // requires no input, returns nothing /****************************************************************************************************************/ void init_IO (void) { // LEDs and Switches PGADDR = 0x02; // Select Alternate Function Register PGCTL = 0x00; // Select GPIO function PGADDR = 0x01; // select the DDR register PGCTL = 0xF0; // set DDR reg to set bits 0 to 3 as output, bits 4 to 7 as input // note that PGOUT is set to 0x00 by CPU upon reset => outputs will be LOW after the previous line executes PGADDR = 0x00; // Set ADDR register back to 0x00 to prevent further changes } /****************************************************************************************************************/ // Turn ON all LEDs // communicates only with the LEDs // requires no input, returns nothing /****************************************************************************************************************/ void turn_on_LEDs (void) { LED_bit_pattern (~0); } // end of turn_off_leds() /****************************************************************************************************************/ // Turn OFF all LEDs /****************************************************************************************************************/ void turn_off_LEDs (void) { LED_bit_pattern (0); } // end of turn_off_leds() /****************************************************************************************************************/ // Place any desired bit pattern on the LEDs: 1 = on, 0 = off // requires byte input, returns nothing /****************************************************************************************************************/ void LED_bit_pattern (byte bit_pattern) { // LED anodes are at the positive supply voltage; // LED cathodes are connected as outputs to Port G bits 0 to 3: // this makes the LEDs are active low on the board. // 0x0F = 0000 1111 in binary. // So this bit mask selects the LEDS and only the LEDs. enum { LED_bits = 0x0F }; // The long way: a step-by-step process: // Invert bit_pattern to compensate for active LOW controls // which occur because PGOUT is connected to the LEDs' cathodes. // byte led_pattern = ~ bit_pattern; // LED-connected bits (LED_bits) of the pattern must be set. // byte change = led_pattern & LED_bits; // Keep other bits (~LED_bits) of Port G data unchanged. // byte keep = PGIN & ~LED_bits; // Finally, combine the kept bits and the changed bits. // PGOUT = keep | change; // All of the above, in one line: PGOUT = (PGIN & ~LED_bits) | (~bit_pattern & LED_bits); } // end of LED_bit_pattern() /****************************************************************************************************************/ // A fixed, blocking, software delay routine using a nested 'for' loop. // For a precise delay, use an internal Z8 TIMER. (TIMERs are in a later lab.) // requires no input, returns nothing. /****************************************************************************************************************/ void delay (void) { int i, j; j = 0; // waste some CPU time => results in delay for (i=0; i < 32000; i++) { j = j++; j = j--; } // waste some more time for (i=0 ; i < 15000; i++) { } } // end of delay() //-------------------------------------------------------------------ADD NEW FUNCTIONS SOUTH OF THIS LINE-------