in C for STM32F446RE microcontroller Write a source library that contains the with the following public functions:   void keypadInit(void); /Initiallized the GPIO to read the keypad. uint16_t readKeypad(void); //Returns the state of all of the keypad buttons in the return value at the moment the function is called. void decodeKeypad(uint16_t, char *); //Takes the state of the keypad and returns (by reference) an array of the key's pressed.   The library should work with the following main:   int main (void)  {     uint16_t    key;     char        carray[17];          keypadInit();          while(1)     {         while(!(key = readKeypad()));    /*Get which keys pressed*/         decodeKeypad(key, carray);    /*What are those keys*/         printf("%s\n",carray);        /*Print those keys to screen*/         while(readKeypad() == key);    /*Wait for the keypad to change*/     } }   Problem 1: Write a library that works with the following pin assignments R0 -> PC0 R1 -> PC2 R2-> PC4 R3 -> PC6 C0-> PC8 C1->PC10 C2->PC12

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter7: Using Methods
Section: Chapter Questions
Problem 3RQ
icon
Related questions
Question

in C for STM32F446RE microcontroller

Write a source library that contains the with the following public functions:
 
void keypadInit(void); /Initiallized the GPIO to read the keypad.
uint16_t readKeypad(void); //Returns the state of all of the keypad buttons in the return value at the moment the function is called.
void decodeKeypad(uint16_t, char *); //Takes the state of the keypad and returns (by reference) an array of the key's pressed.
 
The library should work with the following main:
 
int main (void) 
{
    uint16_t    key;
    char        carray[17];
    
    keypadInit();
    
    while(1)
    {
        while(!(key = readKeypad()));    /*Get which keys pressed*/
        decodeKeypad(key, carray);    /*What are those keys*/
        printf("%s\n",carray);        /*Print those keys to screen*/
        while(readKeypad() == key);    /*Wait for the keypad to change*/
    }
}
 
Problem 1: Write a library that works with the following pin assignments
R0 -> PC0
R1 -> PC2
R2-> PC4
R3 -> PC6
C0-> PC8
C1->PC10
C2->PC12
Expert Solution
Step 1: Here's an implementation of the requested library in C for the STM32F446RE microcontroller:

Here's a possible implementation of the requested library in C for the STM32F446RE microcontroller:

CODE in C:

#include "stm32f4xx.h"
#include "keypad.h"

#define KEYPAD_COLS 3
#define KEYPAD_ROWS 4

GPIO_TypeDef* KEYPAD_GPIO_PORT = GPIOC;
const uint16_t KEYPAD_GPIO_PINS[KEYPAD_ROWS + KEYPAD_COLS] = {
    GPIO_PIN_0, GPIO_PIN_2, GPIO_PIN_4, GPIO_PIN_6, GPIO_PIN_8, GPIO_PIN_10, GPIO_PIN_12
};
const uint8_t KEYPAD_ROW_PINS[KEYPAD_ROWS] = {0, 1, 2, 3};
const uint8_t KEYPAD_COL_PINS[KEYPAD_COLS] = {4, 5, 6};
const uint8_t KEYPAD_KEYMAP[KEYPAD_ROWS][KEYPAD_COLS] = {
    {'1', '2', '3'},
    {'4', '5', '6'},
    {'7', '8', '9'},
    {'*', '0', '#'}
};

void keypadInit(void) {
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;  // enable GPIOC clock

    // configure GPIO pins as inputs with pull-up resistors
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    for (int i = 0; i < KEYPAD_ROWS + KEYPAD_COLS; i++) {
        GPIO_InitStruct.Pin = KEYPAD_GPIO_PINS[i];
        HAL_GPIO_Init(KEYPAD_GPIO_PORT, &GPIO_InitStruct);
    }
}

uint16_t readKeypad(void) {
    uint16_t state = 0;
    for (int i = 0; i < KEYPAD_COLS; i++) {
        // set column pin to low
        HAL_GPIO_WritePin(KEYPAD_GPIO_PORT, KEYPAD_GPIO_PINS[KEYPAD_ROWS + i], GPIO_PIN_RESET);
        // read row pins and update state
        for (int j = 0; j < KEYPAD_ROWS; j++) {
            uint8_t pin = KEYPAD_GPIO_PINS[j];
            if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_PORT, pin)) {
                state |= 1 << (j * KEYPAD_COLS + i);
            }
        }
        // set column pin back to high
        HAL_GPIO_WritePin(KEYPAD_GPIO_PORT, KEYPAD_GPIO_PINS[KEYPAD_ROWS + i], GPIO_PIN_SET);
    }
    return state;
}

void decodeKeypad(uint16_t state, char *keys) {
    for (int i = 0; i < KEYPAD_ROWS; i++) {
        for (int j = 0; j < KEYPAD_COLS; j++) {
            if (state & (1 << (i * KEYPAD_COLS + j))) {
                *keys++ = KEYPAD_KEYMAP[i][j];
            }
        }
    }
    *keys = '\0';
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT