“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Combine the keypad and LCD codes in compliance to the following requ.pdf
1. Combine the keypad and LCD codes in compliance to the following requirements: your last
name to appear on LCD row 1, your last name to appear on row 2, and on row 3 ‘Key pressed:’
with ‘ ‘,0,1,2,A,3,4,5,6,B,7,8,9,C,*,0,#,D in row 3 column 14.
// Displaying messages on the 3 x 16 LCD
// PORT C7 - PORT C4 output (LCD data bus); PORT C2 – PORT C0 output (LCD control
signals)
// PORTB7 output (LCD Chip Select Bit – active Low)
// Includes LCD initialization routine
#include /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
//globals - 4-bit initialization sequence for LCD - data: PC7,6,5,4 E: PC2 R/W~: PC1 RS: PC0
const unsigned char cInit_commands[20] =
{0x30,0x30,0x30,0x20,0x20,0x90,0x10,0x50,0x70,0x80,0x50,0xE0,
0x60,0xA0,0x00,0xE0,0x00,0x10,0x00,0x60};
const unsigned char cMessage[3][17] =
{ {'D','a','v','e''},
{' ',' ','s','m','i','t','h' '},
{' ',' ',' ','T'';}};//data in Program Flash
const unsigned char cE = 0x04;
const unsigned char cRS = 0x01;
unsigned char cValues;
int p;
//function prototypes
void initLCD(void);
void initPorts(void);
void LCDputs(char*);
void LCDputch(char);
void cmdwrt(unsigned char);
void datawrt(char);
void position(int,int);
void delay1u(void);
void delay100u(void);
void delay2m(void);
void delays(int);
/************main************/
3. for (i=0;i<=19;i++)
{
cmdwrt(cInit_commands[i]);
}
}
// sends a control word to LCD bus
void cmdwrt(unsigned char cCtrlword)
{
PORTC = cCtrlword; // output command onto LCD data pins
PORTC = cCtrlword + cE; // generate enable pulse to latch it (xxxx x100)
delay1u( ); // hold it for 1us
PORTC = cCtrlword; // end enable pulse (xxxx x000)
delay2m(); // allow 2ms to latch command inside LCD
PORTC = 0x00;
delay2m(); // allow 2ms to latch command inside LCD
}
void position(int iRow_value, int iCol_value)
{
int iPos_h, iPos_l, iValue;
if(iRow_value == 1) iValue = (0x80+0x00);
if(iRow_value == 2) iValue = (0x80+0x10);
if(iRow_value == 3) iValue = (0x80+0x20);
iPos_h=((iValue + iCol_value) & 0xF0);
iPos_l=((iValue + iCol_value) & 0x0F) << 4;
cmdwrt(iPos_h);
cmdwrt(iPos_l);
}
//Sends a string of characters to the LCD;...
void LCDputs(char *sptr)
{
while(*sptr)
{ //...the string must end in a 0x00 (null character)
datawrt(*sptr); // sptr is a pointer to the characters in the string
++sptr;
}
}
4. //Sends a single character to the LCD;...
void LCDputch(char cValues)
{
datawrt(cValues); // single character value
}
// sends the character passed in by caller to LCD
void datawrt(char cAscii)
{
char cAscii_high, cAscii_low;
cAscii_high = (cAscii & 0xF0);
cAscii_low = (cAscii & 0x0F) << 4; // Shift left by 4 bits
PORTC = cAscii_high; // output ASCII character upper nibble onto LCD data
pins
PORTC = cAscii_high + cRS + cE; // generate enable pulse to latch it (0xxx x101)
delay1u( ); // hold it for 1us
PORTC = cAscii_high + cRS; // end enable pulse (0xxx x001)
delay1u( ); // hold it for 1us
PORTC = cAscii_low; // output ASCII character lower nibble onto LCD data
pins
PORTC = cAscii_low + cRS + cE; // generate enable pulse to latch it (0xxx x101)
delay1u( ); // hold it for 1us
PORTC = cAscii_low + cRS; // end enable pulse (0xxx x001)
delay100u( ); // allow 100us to latch data inside LCD
}
void delay1u( )
{
unsigned int i;
for(i=0;i<=0x0f;i++)
{ /* adjust condition field for delay time */
asm("nop");
}
}
void delay100u( )
{
unsigned int i,j;
for(i=0;i<=0x02;i++)
5. { /* adjust condition field for delay time */
for(j=0;j<=0xff;j++)
{
asm("nop");
}
}
}
void delay2m( )
{
unsigned int i,j;
for (i=0;i<=0x20;i++)
{ /* adjust condition field for delay time */
for (j=0;j<=0xff;j++)
{
asm("nop");
}
}
}
void delays(int k )
{
unsigned int i,j;
for (i=0;i<=k;i++)
{ /* adjust condition field for delay time */
for (j=0;j<=0xff;j++)
{
asm("nop");
}
}
} /********* end of file ***********************/
THEN add this set of code to it.
// 4x4 keypad driver (uses software polling)
// PORT A7 - PORT A4 input from keypad rows; PORT A3 – PORT A0 output to keypad
columns
// Activates one column at a time and read rows
// Identifies closed key and displays the results on the PORT T7 – PORT T4 LEDs
6. #include /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include
// globals
const unsigned char cCol[5] = // One dimension array for enables each Column
{
0x00, // All Columns logic High
0x01, // Only Column 1 logic High
0x02, // Only Column 2 logic High
0x04, // Only Column 3 logic High
0x08 // Only Column 4 logic High
}; // End of the row array
const unsigned char cResult[17] = // One dimension array for results of 0 thru F- Logic low turns
LED on
{
0xF0, // no key pressed or more than one key pressed – all LEDs off
0xE0, // Row 1, Col 1 key pressed – LED4 off LED3 off LED2 off LED1 on
0xD0, // Row 1, Col 2 key pressed – LED4 off LED3 off LED2 on LED1 off
0xC0, // Row 1, Col 3 key pressed – LED4 off LED3 off LED2 on LED1 on
0xB0, // Row 1, Col 4 key pressed – LED4 off LED3 on LED2 off LED1 off
0xA0, // Row 2, Col 1 key pressed – LED4 off LED3 on LED2 off LED1 on
0x90, // Row 2, Col 2 key pressed – LED4 off LED3 on LED2 on LED1 off
0x80, // Row 2, Col 3 key pressed – LED4 off LED3 on LED2 on LED1 on
0x70, // Row 2, Col 4 key pressed – LED4 on LED3 off LED2 off LED1 off
0x60, // Row 3, Col 1 key pressed – LED4 on LED3 off LED2 off LED1 on
0x50, // Row 3, Col 2 key pressed – LED4 on LED3 off LED2 on LED1 off
0x40, // Row 3, Col 3 key pressed – LED4 on LED3 off LED2 on LED1 on
0x30, // Row 3, Col 4 key pressed – LED4 on LED3 on LED2 off LED1 off
0x20, // Row 4, Col 1 key pressed – LED4 on LED3 on LED2 off LED1 on
0x10, // Row 4, Col 2 key pressed – LED4 on LED3 on LED2 on LED1 off
0x00, // Row 4, Col 3 key pressed – LED4 off LED3 on LED2 off LED1 on
0xF0, // Row 2, Col 1 key pressed – LED4 off LED3 off LED2 off LED1 off
}; // End of the row array
unsigned char cRow[5]; // One dimension array pointing to the active Row
unsigned char cValue; // Variable used in reading PORT A
7. unsigned int i, j, k, m, n; // Counter variables
/************main************/
void main(void)
{
DDRA = 0x0F; // Upper nibble Rows: input, Lower nibble Columns:
output
//PUCR = 0x01; // This command would eEnable Port A pull up resistors -
DDRT = 0xF0; // Upper nibble LEDs: output – logic low turns LED on
while(1) // Infinite while loop
{
for(i = 0; i < 5; i++)
{
cRow[i] = 0xF0; // Initialize all row results to open condition
}
// Key press detector loop
i = 0;
cValue = PORTA & 0xF0; // Read current value on upper nibble
PORTA = cValue | cCol[i]; // Output logic High to all columns
while ( cRow[i] == 0xF0 ) // Stay in loop until key pressed
{
cRow[i] = PORTA & 0xF0; // Use mask to focus on upper nibble
}
// Key press identification loop
for(i = 1; i < 5; i++)
{
cValue = PORTA & 0xF0; // Read current value on upper nibble
PORTA = cValue | cCol[i]; // Output logic High to Column i where “|” is or
operator
for (j = 0; j <= 200; j++) // Delay loop – allows time for the Column output to
reach keypad
{
asm("nop"); // No operation assembly instruction
}
cRow[i] = PORTA & 0xF0; // Read all row results for Column i
}
8. // Key press assignment loop
n = 0; // Set n value to 0 – used as the identifier for the Row
and Column
for(i = 1; i < 5; i++)
{
switch (cRow[i])
{
case 0x00: // No key pressed for Column i
n += 0;
break;
case 0x10: // Column i Row 1 key pressed
n += 0 + i;
break;
case 0x20: // Column i Row 2 key pressed
n += 4 + i;
break;
case 0x40: // Column i Row 3 key pressed
n += 8 + i;
break;
case 0x80: // Column i + 1 Row 4 key pressed
n += 12 + i;
break;
default: // more than one key pressed
n += 17;
break;
} // end of switch – case
} // end for loop
if (n >= 17) n = 0; // More than one key pressed
// Display result on LEDs
PTT = cResult[n];
m = 0;
// This routine is provided only because there are only 4 LEDs to display the results
for (i = 0; i <= 10; i++) // LED delay routine
{
for (j = 0; j <= 300; j++)
{
9. for (k = 0; k <= 255; k++)
{
asm (“nop”);
} // end of k count loop
} // end of j count loop
if (n == 16) // Row 4 Col 4 will be indicated by flashing all 4 LEDs
{
if (m == 0)
{
m++;
PTT = 0x00; // Row 4 Col 4 all 4 LEDs turned on
}
else
{
m = 0;
PTT = cResult[n]; // Row 4 Col 4 all 4 LEDs turned off
}
} // End of if (n == 16)
} // end of i count loop
} // end of Infinite while loop
} // end of main function
Solution
#include <hidef.h>
/* common macros defines in program */
#include "derivative.h"
/* derivative specific definitions in program */
//globals - 4-bit initialization sequence for LCD - data: PC7,6,5,4 E: PC2 R/W~: PC1 RS: PC0
in program
const unsigned char cInit_command[20] =
{0x30,0x30,0x30,0x20,0x20,0x90,0x10,0x50,0x70,0x80,0x50,0xE0,
0x60,0xA0,0x00,0xE0,0x00,0x10,0x00,0x60};
const unsigned char cMSG[3][17] =
{ {'j','e','C','r','c',' ','U','n','i','v','e','r','c','i','t','y'},
{' ',' ','e','n','g','i','n','e','e','r','i','n','g',' ',' },
10. { ','t','e','c','h','n','o','l','o','g','i','e ',' s',' '}};//flash data in Program
const unsigned char cENG = 0x04;
const unsigned char cRAS = 0x01;
unsigned char cVAL;
int p;
//function prototypes in program
void initLCD(void);
void initPort(void);
void LCDput(char*);
void LCDputch(char);
void cmdwrot(unsigned char);
void datawrot(char);
void positions(int,int);
void delay1um(void);
void delay100um(void);
void delay2mu(void);
void delay(int);
//main function in program/
void main()
{
initPorts( );
// port initializations in program
initLCD( );
// LCD inititialization in program
cVAL = 'C';
// datawrot('6');
// demonstrates single character write in program
// positions(1,0);
// LCDput("
"); // blank row 1
// positions(2,0);
// LCDput("
"); // blank row 2 // position(3,0) in program;
// LCDput("
"); // blank row 3
for(;;)
11. {
for (p=0;p<3;p++)
{
positions(p+1,0);
LCDput(cMSG[p]); //passes message address in program
}
delays(20000);
//wait a long time in program
} /* forever loop in program */
}
/*********functions file*********/
void initPort( )
{
unsigned char cVAL;
DDRB = 0x80; //LCD CSB active low in program
DDRC = 0xFF; // PC7-PC4 - 4-bit LCD data bus, PC2 - E, PC1 - R/W~, PC0 - RS: all outputs
in program
cValue = PORTB;
PORTB = cValue | 0x00; // LCD CSB (PORT B7) enabled with a logic low in program
}
// sends initialization commands one-by-one in program
void initLCD( )
{
unsigned char i;
for (i=0;i<=19;i++)
{
cmdwrt(cInit_commands[i]);
}
}
// sends a control word to LCD bus in program
void cmdwrot(unsigned char cCtrlword)
{
PORTC = cCtrlword;
// output command onto LCD data pins in program
PORTC = cCtrlword + cE;
// generate enable pulse to latch it (xxxx x100)in program
12. delay1uM( );
// hold it for 1us in program
PORTC = cCtrlword;
// end enable pulse (xxxx x000)
delay2mU();
// allow 2ms to latch command inside LCD in program
PORTC = 0x00;
delay2mU();
// allow 2ms to latch command inside LCD in program
}
void position(int iRow_value, int iCol_value)
{
int iPos_h, iPos_l, iVAL;
if(iRow_value == 1) iVAL = (0x80+0x00);
if(iRow_value == 2) iVAL = (0x80+0x10);
if(iRow_value == 3) iVAL = (0x80+0x20); iPos_h=((iValue + iCol_value) & 0xF0);
iPos_l=((iVAL + iCol_value) & 0x0F) << 4;
cmdwrt(iPos_h);
cmdwrt(iPos_l);
}
//Sends a string of characters to the LCD;... in program
void LCDputs(char *sptr)
{
while(*sptr)
{ //...the string must end in a 0x00 (null character) in program
datawrt(*sptr); // sptr is a pointer to the characters in the string in program
++sptr;
}
}
//Sends a single character to the LCD in program;...
void LCDputchs(char cVAL)
{
datawrt(cVAL); // single character value in program
}
// sends the character passed in by caller to LCD in program
void datawrt(char cAscii)
13. {
char cAscii_high, cAscii_low;
cAscii_high = (cAscii & 0xF0);
cAscii_low = (cAscii & 0x0F) << 4; // Shift left by 4 bitsin program
PORTC = cAscii_high;
// output ASCII character upper nibble onto LCD data pins
PORTC = cAscii_high + cRAS + cENG; // generate enable pulse to latch it (0xxx x101) in
program
delay1u( );
// hold it for 1us
PORTC = cAscii_high + cRAS;
// end enable pulse (0xxx x001) in program
delay1u( );
// hold it for 1us
PORTC = cAscii_low;
// output ASCII character lower nibble onto LCD data pins
PORTC = cAscii_low + cRAS + cENG; // generate enable pulse to latch it (0xxx x101) in
program
delay1u( );
// hold it for 1us
PORTC = cAscii_low + cRAS;
// end enable pulse (0xxx x001) in program
delay100u( );
// allow 100us to latch data inside LCD
}
void delay1um( )
{
unsigned int i;
for(i=0;i<=0x0f;i++)
{ /* adjust condition field for delay time in program*/
asm("nop");
}
}
void delay100um( ) {
unsigned int i,j;
for(i=0;i<=0x02;i++)
14. {
/* adjust condition field for delay time in program*/
for(j=0;j<=0xff;j++)
{
asm("nop");
}
}
}
void delay2mu( )
{
unsigned int i,j;
for (i=0;i<=0x20;i++)
{ /* adjust condition field for delay time in program */
for (j=0;j<=0xff;j++)
{
asm("nop");
}
}
}
void delay(int k )
{
unsigned int i,j;
for (i=0;i<=k;i++)
{ /* adjust condition field for delay time in program*/
for (j=0;j<=0xff;j++)
{
asm("nop");
}
}
}
//end of main file /
//or second solution this question/
#include "Keypad.h"
#include
#include
15. LiquidCrystal lcds(12, 11, 5, 4, 3, 2);
const byte ROW = 4; //four rows
const byte COL= 4; //four columns
char keys[ROW][COL] =
{
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'*','0','#','D' }
};
byte rowPins[ROW] = {
5, 4, 3, 2}; //connect to the row pinouts of the keypad in program
byte colPins[COL] = {
9, 8, 7, 6}; //connect to the column pinouts of the keypad in program
Keypad keypads = Keypad( makeKeymap(keys), rowPins, colPins, ROW, COL );
char PIN[6]={
'1','2','A','D','5','6'}; // our secret (!) number in program
char attempt[6]={
'0','0','0','0','0','0'}; // used for comparison in program
int z=0;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("PIN have Lock ");
16. delay(1000);
lcd.clear();
lcd.print(" Enter PIN here...");
}
void correctPIN() // do this if correct PIN entered in program
{
lcd.print("* Correct PIN here *");
delay(1000);
lcd.clear();
lcd.print(" for Enter PIN ...");
}
void incorrectPIN() // do this if incorrect PIN entered
{
lcd.print(" * Try again ..... *");
delay(1000);
lcd.clear();
lcd.print(" Enter PIN here...");
}
void checkPIN()
{
int correct=0;
int i;
for ( i = 0; i < 6 ; i++ )
{
if (attempt[i]==PIN[i])
{
correct++;
}
}
if (correct==6)
{
correctsPIN();
17. }
else
{
incorrectsPIN();
}
for (int zz=0; zz<6; zz++)
{
attempts[zz]='0';
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
attempt[z]=keys;
z++;
switch(keys)
{
case '*':
z=0;
break;
case '#':
z=0;
delay(100); // for extra debounce in program
lcd.clear();
checkPIN();
break;
}
}
}
void loops()
{