1 of 31

E - Interfacciamento dei microcontrollori

ES06 - Libreria Pulsanti

FB - 05.2021 - v1.1

2 of 31

Libreria pulsanti

Scrittura del codice in linguaggio C per la gestione dei pulsanti, da poter riutilizzare nei prossimi progetti.

2

© FB - 05.2021

3 of 31

Indice

3

© FB - 09.2019

4 of 31

Obiettivi didattici

4

5 of 31

Obiettivi didattici

  • Configurazione dei pin GPIO
  • Utilizzo delle maschere per le operazioni di set, reset
  • Utilizzo delle costanti nel codice invece dei “magic numbers”
  • Collaudo del programma scritto

5

© FB - 05.2021

6 of 31

Introduzione teorica

6

7 of 31

Prerequisito

7

© FB - 04.2022

8 of 31

Prerequisito

8

© FB - 04.2022

9 of 31

GPIO input: joystick

9

© FB - 04.2022

10 of 31

GPIO input: Joystick

10

© FB - 04.2019

11 of 31

Schema HW

11

© FB - 05.2021

12 of 31

Registers

12

© FB - 05.2021

13 of 31

Board Pin connections

13

© FB - 05.2021

14 of 31

GPIO pin configuration

Diversi registri del microcontrollore STM32L476 ci permettono la configurazione e l’utilizzo dei pin GPIO, ecco i principali:

  • RCC_AHB2ENR per abilitare le porte GPIO
  • GPIO port mode register (GPIOx_MODER)
  • GPIO port pull-up/pull-down register (GPIOx_PUPDR)
  • GPIO port input data register (GPIOx _IDR)
  • GPIO port output data register (GPIOx_ODR)

where the 'x' in each register name acronym represents the port i.e. the GPIOx_MODER associated with port A is called GPIOA_MODER

14

© FB - 05.2021

15 of 31

AHB peripheral clock enable register

  • Ogni periferica per poter essere utilizzata deve avere il proprio clock abilitato, altrimenti rimane disabilitata risparmiando energia.
  • Per abilitare la periferica basta settare il bit corrispondente in uno dei registri RCC_AHB2ENR, __________
  • Es.: To enable the clock to reach the GPIOC peripheral, we need to set the "GPIOCEN" bit (bit 2) in the RCC_AHB2ENR register�RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;

15

© FB - 05.2021

16 of 31

GPIO port mode register (GPIOx_MODER)

16

© FB - 05.2021

This is a 32-bit register where each set of two consecutive bits represent the mode of a single I/O pin. For example bits 0 and 1 of the MODER register associated with GPIOC (GPIOC_MODER), represent the mode of GPIO pin PC0 and bits 26 and 27 of the same register represent the mode of GPIO pin PC13.

Alternate function mode which allow the GPIO pins to be used by peripherals such as the UART, SPI e.t.c. It is important to note that if a pin's MODE is set to alternate function, any GPIO settings for that pin in the GPIO registers will be overridden by the peripheral. I will be addressing Alternate function mode in more detail in a future entry

17 of 31

GPIO port mode register (GPIOx_MODER)

17

© FB - 05.2021

18 of 31

GPIO port pull-up/pull-down register (GPIOx_PUPDR)

18

© FB - 04.2019

19 of 31

GPIO port pull-up/pull-down register (GPIOx_PUPDR)

19

© FB - 05.2021

20 of 31

GPIO port pull-up/pull-down register (GPIOx_PUPDR)

The GPIOx_PUPDR registers configures the internal pull-ups and pull-down resistors on each I/O pin. The internal pull-up/down resistors can be configured on GPIO pins set as input or output (though I'd imagine they'd be more popular on input pins). The Pullup/down resistors have a typical value of 40KOhms but can range from 30-50Kohms.

Again each two consecutive bits represent the internal pull-up/down resistor setting for each pin within a single port.

20

© FB - 04.2019

21 of 31

GPIO port pull-up/pull-down register (GPIOx_PUPDR)

21

© FB - 05.2021

22 of 31

22

GPIO port input data register (GPIOx _IDR)

This is a 16-bit read-only register. Each bit represents the input value on a corresponding pin. Reading a '0' in bit 8 of this GPIOC _IDR register indicates that the voltage on PC8 is 0V (GND). While reading a '1' in bit 8 of this GPIOC _IDR register indicates that the voltage on PC8 is 3.3V (VDD)

23 of 31

23

GPIO Configuration Modesbefore

  • Input floating – safe and default configuration after the reset

24 of 31

Esercitazione

24

25 of 31

Completare il codice

  • Creare le seguenti routine (in assembler restituire in R0 il risultato della funzione:
    • void SwInit(void);
    • int SwCxPress(void);
    • int SwSxPress(void);
    • int SwDxPress(void);
    • int SwUpPress(void);
    • int SwDwPress(void);
    • void SwTest(void);
  • Bonus
    • int SwCxClick(void); //Click = pressione e rilascio
    • int SwCxDClick(void); //Doppio click
    • int SwCxLPress(void); //Pressione lunga > 3sec

25

© FB - 06.2022

26 of 31

Completare il codice

  • void SwTest(void);
    • se premuto il pulsante sx accendi rosso e spegni verde
    • se premuto il pulsante dx accendi verde e spegni rosso
    • se click pulsante sx toggle led rosso
    • se click pulsante dx toggle led verde

26

© FB - 06.2022

27 of 31

Valutazione

27

28 of 31

Consegna del lavoro

Raccogliere in un documento, da aggiungere alla tabella progetti, i passaggi principali dell’esercitazione

28

© FB - 05.2021

29 of 31

Grazie per l’attenzione

29

30 of 31

Link/Riferimenti

30

31 of 31

Revisioni

v1.0 01/04/19 - versione inizialev1.1 07/05/21 - Aggiornata l’introduzione teorica e gli esercizi�v1.2 19/04/22 -

31