Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I would like to build a code that displays the pressed button when a button is pressed on a 4 x 4 keypad. When you

I would like to build a code that displays the pressed button when a button is pressed on a 4x4 keypad. When you run the code below, the vertical columns will output the same value. (Press keypad "4" to display 1; press keypad "8" to display 2.) Please teach me what is wrong and how to correct it.
/* Template to help create the scan function. Refer to the scanning algorithm in lecture/book section 15.9*/
#include
#include
#include "stm32l476xx.h"
#include "UART.h"
#include "SysClock.h"
#include "keypad.h"// Include keypad header file
// Let us make configs in one function and declare in the header file. Much better, right?
void Keypad_Init(){
UART2_Init();
System_Clock_Init();
// Port C config. Go to the header file as shown in class. Check your header file for macros.
// Enable GPIO clock
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
// Setting PC pins 0,1,2,3 as GPIO output
// GPIO Mode: Input(00), Output(01), AlterFunc(10), Analog(11, reset)
GPIOC->MODER &= ~(GPIO_MODER_MODE0| GPIO_MODER_MODE1| GPIO_MODER_MODE2| GPIO_MODER_MODE3);
GPIOC->MODER |=(GPIO_MODER_MODE0_0| GPIO_MODER_MODE1_0| GPIO_MODER_MODE2_0| GPIO_MODER_MODE3_0); // Output
// Setting PC pins 4,10,11,12 as GPIO input
// GPIO Mode: Input(00), Output(01), AlterFunc(10), Analog(11, reset)
GPIOC->MODER &= ~(GPIO_MODER_MODE4| GPIO_MODER_MODE10| GPIO_MODER_MODE11| GPIO_MODER_MODE12); // Input
// GPIO Output Type: Output push-pull (0, reset), Output open drain (1)
GPIOC->OTYPER &= ~(GPIO_OTYPER_OT0| GPIO_OTYPER_OT1| GPIO_OTYPER_OT2| GPIO_OTYPER_OT3); // Push-pull
}
unsigned char keypad_scan(){
unsigned char row, col;
unsigned char key;
unsigned char keymap[4][4]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Define/initialize two integers, for "input" and "output" masks assignments.
uint32_t input_mask = GPIO_IDR_ID4| GPIO_IDR_ID10| GPIO_IDR_ID11| GPIO_IDR_ID12; // input mask
uint32_t output_mask = GPIO_ODR_OD0| GPIO_ODR_OD1| GPIO_ODR_OD2| GPIO_ODR_OD3; // output mask
// Masking outputs array
uint32_t outputArray[4]={ GPIO_ODR_OD0, GPIO_ODR_OD1, GPIO_ODR_OD2, GPIO_ODR_OD3};
// Masking inputs array
uint32_t inputArray[4]={ GPIO_IDR_ID4, GPIO_IDR_ID10, GPIO_IDR_ID11, GPIO_IDR_ID12};
waitms(3); // short delay debounce
// Wait until key pressed
while ((GPIOC->IDR & input_mask)== input_mask);
for (row =0; row <4; row++){// Row scan
// Set all rows to Output
GPIOC->MODER &= ~(GPIO_MODER_MODE0| GPIO_MODER_MODE1| GPIO_MODER_MODE2| GPIO_MODER_MODE3);
GPIOC->MODER |=(GPIO_MODER_MODE0_0| GPIO_MODER_MODE1_0| GPIO_MODER_MODE2_0| GPIO_MODER_MODE3_0); // Output
// Set current row to low
GPIOC->ODR &= ~outputArray[row];
waitms(3); // short delay
for (col =0; col <4; col++){// Column scan
if ((GPIOC->IDR & inputArray[col])==0){
// Grab the chosen key
key = keymap[row][col];
// Wait until key released
while ((GPIOC->IDR & inputArray[col])==0);
return key; // return the key pressed
}
}
}
return 0xFF; // Nothing is going on, No press of any key
}
// Delay function
void waitms(unsigned int ms){
int i, j;
for (i =0; i < ms; i++){
for (j =0; j <4000; j++);
}
}
int main(){
Keypad_Init(); // Initialize keypad
while (1){
unsigned char key = keypad_scan(); // Scan keypad for key press
if (key !=0xFF){
printf("Pressed key: %c\r
", key); // Print the pressed key
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

3540416641, 978-3540416647

More Books

Students also viewed these Databases questions