This code listens to keypresses and displays the ASCII code of the key pressed on the LEDs. Port A needs to be connected to the keypad and port D needs to be connected to the LED port.
I need you Modify the code such that the array is cleared when the ‘*’ key is pressed and an LED is turned on when the ‘#’ key is pressed.
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h> // Delay header
#define KEY_PRT PORTA //Keyboard port, DDR, PIN
#define KEY_DDR DDRA
#define KEY_PIN PINA
unsigned char keypad[4][3] = {{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
int main ()
{
unsigned char colloc, rowloc;
DDRD = 0xFF;
KEY_DDR = 0xFF;
KEY_PRT = 0x0F;
while(1)
{
while(1)
{
KEY_DDR =
0xF0;
KEY_PRT =
0xFF;
do
{
KEY_PRT &= 0x0F; //Ground all columns
asm("NOP");
rowloc = (KEY_PIN & 0x0F); //read all
rows
}while(rowloc !=
0x0F); //check until all keys are released
do
{
//PORTD = 0b00000100;
do
{
//PORTD = 0b00001000;
_delay_ms(20);
rowloc = (KEY_PIN &
0x0F); //see if any key is pressed
}while(rowloc == 0x0F);//keep checking for key
press
_delay_ms (20); //delay for debounce
rowloc = (KEY_PIN & 0x0F); //read rows
}while(rowloc ==
0x0F); //wait for key press
KEY_PRT = 0x3F;
//ground column 0
asm("NOP");
rowloc =
(KEY_PIN & 0x0F); //read the rows
if(rowloc !=
0x0F) //column detected
{
colloc = 0; //save column location
break; // exit while loop
}
KEY_PRT = 0x5F;
// ground column 1
asm("NOP");
rowloc =
(KEY_PIN & 0x0F); //read the rows
if(rowloc !=
0x0F)
{
colloc = 1; //save column location
break;
}
KEY_PRT = 0x6F;
// ground column 2
asm("NOP");
rowloc =
(KEY_PIN & 0x0F);
if(rowloc !=
0x0F) //read the rows
{
colloc = 2; //save column location
break;
}
}
//Check row and send result to port
D
if(rowloc == 0x0E)
PORTD = keypad[0][colloc];
else
if(rowloc == 0x0D)
PORTD = keypad[1][colloc];
else
if(rowloc == 0x0B)
PORTD = keypad[2][colloc];
else
PORTD = keypad[3][colloc];
}
return 0;
}
1)For clearing array:
It will be efficient to first convert the array to vector and use the clear() function.
Code for this part:
// converting array to vector.
#include iostream
#include vector
int main(){
std::vector<int> V;
V.insert(V.begin(),std::begin(keypad),std::end(keypad));
for(int j:V){std::cout<<i<<“”;}return 0;}
//for erasing the contents of vector.
V.clear();
2)For the LED on:
int pin=13;
void process()
{pinmode(pin,OUTPUT);}
void process2(){
digitalWrite(pin,HIGH);
delay(800);
digitalWrite(pin,LOW);
delay(800);
}
Get Answers For Free
Most questions answered within 1 hours.