LEDs - 03
In this section we are going to: - Visit GPIO port functionality and Registers - Programming the port address and control functions together - Configure Port B for red LED PB7 |
GPIO Ports: - Each port contains control and data registers - Each port can be input or output and each port pin is individually programmable - Input: can generate an interrupt to the MCU - Output: 2mA (normal, push-pull) or 20mA (open-drain mode, pull-up resistor mode, etc.) - Can support alternate function when programmed to do so, e.g., UART, Timer, ADC
GPIO Registers: The ez8.h file maps all of the addresses for the four Registers (listed below) used by ports A through J (F6482 series supports up to 67 port pins). The complete Register Map can be found in PS0294 starting on page 27. For Port A, we have: - PAADDR at address FD0. It selects subregister values. See PS0294, page 86. - PACTL at address FD1. Provides access to all PAADDR subregisters. PAADDR and PACTL work together. See PS0294, page 87. - PAIN at address FD2. Input data register, used to examine the input values to port A. See PS0294, page 94. - PAOUT at address FD3. Output data register. See PS0294, page 95.
PxADDR and PxCTL are programmed together: PxADDR selects which of the sub-registers PxCTL is mapped to. The aforementioned four registers will be modified to perform our task(s). In our Tutorial01.c example, we'll work with the red LED at pin 7 of Port B: PB7. Considerations for any port's pin are: 1) Input or output port? 2) GPIO port or special function? 3) If output, normal operation or open-drain? Standard or high-output drive? 4) If input, wakeup from stop mode or on a change?
Configure Port B for red LED PB7. The following map to the code in the screenshot below: An LED will draw more than 2mA of current so we need to select higher than normal current; we'll use PBADDR subregister 04 High Drive Enable. Next we'll choose the affected pin(s); in our case it's pin 7. In binary that would be 1000 0000. In hex, it's 0x80. We wish the PB7 pin to be an output so we write 01 to PBADDR to signify the direction category. Now we program PBCTL for direction with 0 for output and 1 for input. Note that we set the drive type before direction. Lastly we lockdown PBADDR against any accidental changes to PBCTL by using subregister 00. This is what our code should look like:
As you have done before, use Build | Build and Debug | Go to compile and download your code to the ZCOG. The output should simply be the red LED illuminating and that's all. Repower your board. Notice how the red LED is no longer lit? That's because the IDE monitor for the board has taken over and is awaiting download, etc., from the host PC. In the next section, we'll put our code into a function, program all 3 LEDs, and call the 3 functions from main. |