










Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!











Overview
▪ Basic Concepts ▪ Port Circuitry ▪ Alternate Functions ▪ Peripheral Access In C
▪ Inputs ▪ Outputs
GPIO Alternative Functions ▪ Pins may have different features ▪ To enable an alternative function, set up the appropriate register ▪ May also have analogue paths for ADC / DAC etc. ▪ Advantages: ▪ Saves space on the package ▪ Improves flexibility Pin Function Select … Memory Mapped I/O Alternate Function 1 Alternate Function 2 Alternate Function n
Pull-Up & Pull-Down Resistors ▪ Ensure a known value on the output if a pin is left floating ▪ In our example, we want the switch SW1 to pull the pin to ground, so we enable the pull-up ▪ The pin value is: ▪ High when SW1 is not pressed ▪ Low when SW1 is pressed Pull-down Pull-up
Code Structure ▪ Main code talks to the drivers, producing easy to read and understand code ▪ gpio_set_mode(P2_5, Output) ▪ Drivers utilise CMSIS library and group relevant actions ▪ port_struct->direction_reg = output ▪ CMSIS transforms memory mapped registers into C structs ▪ #define PORT0 ((struct PORT*)0x2000030) ▪ Registers directly control hardware ▪ Hardware drives IO pins physically main.c drivers CMSIS Registers Hardware High Level Low Level Pin direction_reg Input Output
Drivers Layer: How It Works void gpio_set(Pin pin, int value)
C Interface: GPIO Configuration /*! This enum describes the directional setup of a GPIO pin. / typedef enum { Reset, //!< Resets the pin-mode to the default value. Input, //!< Sets the pin as an input with no pull-up or pull-down. Output, //!< Sets the pin as a low impedance output. PullUp, //!< Enables the internal pull-up resistor and sets as input. PullDown //!< Enables the internal pull-down resistor and sets as input. } PinMode; /! \brief Configures the output mode of a GPIO pin.
C Interface: Reading and Writing /*! \brief Sets a pin to the specified logic level.
C Code gpio_set_mode(P_LED1, Output); // Set LED pins to outputs gpio_set_mode(P_LED2, Output); gpio_set_mode(P_SW, Pullup); // Switch pin to resistive pull-up while (1) { if (gpio_get(P_SW)) { // Switch is not pressed (active low), turn LED1 off and LED2 on. gpio_set(P_LED1, 0); gpio_set(P_LED2, 1); } else { // Switch is pressed, turn LED2 off and LED1 on. gpio_set(P_LED2, 0); gpio_set(P_LED1, 1); } }
INTERFACING Inputs and Outputs, Ones and Zeros,Voltages and Currents
Outputs: What’s a One? A Zero? ▪ Nominal output voltages ▪ 1:VDD-0.5 V to VDD ▪ 0: 0 to 0.5 V ▪ Note: Output voltage depends on current drawn by load on pin ▪ Need to consider source-to-drain resistance in the transistor ▪ Above values only specified when current < 5 mA (18 mA for high-drive pads) and VDD > 2.7 V Iout V out Logic 1 out Logic 0 out
Output Example: Driving LEDs ▪ Need to limit current to a value which is safe for both LED and MCU port driver ▪ Use current-limiting resistor ▪ R = (VDD – VLED)/ILED ▪ Set ILED = 4 mA ▪ VLED depends on type of LED (mainly color) ▪ Red: ~1.8V ▪ Blue: ~2.7 V ▪ Solve for R given VDD = ~3.0 V ▪ Red: 300 W ▪ Blue: 75 W ▪ Demonstration code in Basic Light Switching Example