Understanding GPIOs

As you may know memory holds code and data for the CPU to process ,and  the I/ O ports are used by the CPU to access input and output devices.

In the microcontroller we have two types of I/ O. They are: a. General Purpose I/ O (GPIO): The GPIO ports are used for interfacing devices such as LEDs, switches, LCD, keypad, and so on. b. Special purpose I/ O: These I/ O ports have designated function such as ADC (Analog-to-Digital), Timer, UART (universal asynchronous receiver transmitter), and so on.

Buses

After read out open datasheet of the 3 different arm mcus to show the APB bus and AHB bus

Direction and Data registers

Every microcontroller has a minimum of two registers for io control. The keyword here is minimum. We we go to program our mcu we need setup at least 3 or 4 registers depending on the mcu we are using.

The to minimum required registers are the data register and the direction register.

The Direction register is used to make the pin either input or output.

After the Direction register is properly set up, then we use the Data register to actually write to the pin or read data from the pin.

STM32F411-NUCLEO LED BLINK

#include "stm32f4xx.h"

void delayMs(int n);

int main(void) {
    RCC->AHB1ENR |=  1;             /* enable GPIOA clock */
    
    GPIOA->MODER &= ~0x00000C00;   
    GPIOA->MODER |=  0x00000400;   

    while(1) {
        GPIOA->ODR |=  0x00000020; 
        delayMs(500);
        GPIOA->ODR &= ~0x00000020; 
        delayMs(500);
    }
}

/* Psuedo delay based on Keil uVision compiler and  16 MHz SYSCLK */
void delayMs(int n) {
    int i;
    for (; n > 0; n--)
        for (i = 0; i < 3195; i++) ;
}

STM32F411-NUCLEO INPUT/OUTPUT

#include "stm32f4xx.h"

int main(void) {
    RCC->AHB1ENR |=  4;                 
    RCC->AHB1ENR |=  1;                

    GPIOA->MODER &= ~0x00000C00;        
    GPIOA->MODER |=  0x00000400;        

    GPIOC->MODER &= ~0x0C000000;       

    while(1) {
        if (GPIOC->IDR & 0x2000)        /* if PC13 is high */
            GPIOA->BSRR = 0x00200000;   /* turn off green LED */
        else
            GPIOA->BSRR = 0x00000020;   /* turn on green LED */
    }
}

FRDM-KL25Z LED BLINK

/ * The red LED is connected to PTB18.
 * The green LED is connected to PTB19.
 * The blue LED is connected to PTD1.
 * The LEDs are low active (a '0' turns ON the LED).
 */

#include <MKL25Z4.H>

int main (void) {
    void delayMs(int n);
    
    SIM->SCGC5 |= 0x400;        /* enable clock to Port B */
    SIM->SCGC5 |= 0x1000;       /* enable clock to Port D */
    PORTB->PCR[18] = 0x100;     /* make PTB18 pin as GPIO (See Table 2-4)*/
    PORTB->PCR[19] = 0x100;     /* make PTB19 pin as GPIO */
    PTB->PDDR |= 0xC0000;       /* make PTB18, 19 as output pin */
    PORTD->PCR[1] = 0x100;      /* make PTD1 pin as GPIO */
    PTD->PDDR |= 0x02;          /* make PTD1 as output pin */
    
    while (1) {
        PTB->PDOR &= ~0xC0000;  
        PTD->PDOR &= ~0x02;     
        delayMs(500);
        PTB->PDOR |= 0xC0000;   
        PTD->PDOR |= 0x02;      
        delayMs(500);
    }
}


void delayMs(int n) {
    int i;
    int j;
    for(i = 0 ; i < n; i++)
        for (j = 0; j < 7000; j++) {}
}

Add Comment

Your email address will not be published. Required fields are marked *

Phone: +447491265055
W12 7JN, London, United Kingdom
34A Frithville Gardens
Style switcher RESET
Body styles
Color settings
Link color
Menu color
User color
Background pattern
Background image