You are on page 1of 4

1 [Document title]

Microprocessors
Eee-342

Lab Report-##

NAME: Muhammad Umar


CLASS: BEE-5c
REG No: FA20-BEE-150
INSTRUCTO
Sir raheel ahmed
R:
In Lab Tasks:

Task 1: Interfacing ATmega328P with keypad


2 [Document title]

Code:

#include <inttypes.h> //Used for type casting


#include <avr/io.h> //Basic I/O definitions
#define F_CPU 16000000UL
#include <util/delay.h> //to generate delays

#include "debug_prints.c"
#define BAUD0 9600 // Baud Rate for UART
#define MYUBRR (F_CPU/8/BAUD0-1)

#define KEYPAD_DDR_ROWS DDRB


#define KEYPAD_PORT_ROWS PORTB
#define KEYPAD_PIN_ROWS PINB

#define KEYPAD_DDR_COLS DDRD


#define KEYPAD_PORT_COLS PORTD
#define KEYPAD_PIN_COLS PIND

unsigned char keypad_tbl1[] = {255,0,1,255,2,255,255,255,3};

unsigned char keypad_tbl2[] =


{'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};

//unsigned char keypad_tbl2[] =


{'7','8','9','/','4','5','6','X','1','2','3','-','C','0','=','+'};

unsigned char read_keypad(void);

int main()
{
unsigned char key_pressed = 0;
//Initalizting UART, Print UBRR value and F_CPU
UART0_init(MYUBRR);
printSerialStr("Muhammad Umar");

while (1)
{
key_pressed = read_keypad();
if(key_pressed != 0) //wait for key to be pressed
{

UART0_send_char(key_pressed);//display pressed key on serial monitor


key_pressed = 0;
}
}
return 0;

unsigned char read_keypad()


{

unsigned char columns;


unsigned char rows;
3 [Document title]

unsigned char key, temp;

KEYPAD_PORT_COLS |= 0xF0;// Pull-up the columns


KEYPAD_DDR_COLS &= 0x0F;// Set columns to Input
KEYPAD_PORT_ROWS = 0xF0;//pull down the rows
KEYPAD_DDR_ROWS |= 0x0F;// Set rows to Output

_delay_ms(2);//give delay of 2ms

columns=0x0F&(~(KEYPAD_PIN_COLS &0xF0)>>4); // read the value of columns

_delay_us(100);
KEYPAD_PORT_ROWS|=0x0F;// Pull-up the rows
KEYPAD_DDR_ROWS&=0x00;// Set rows to Input
KEYPAD_PORT_COLS&=0x0F;//pull down the columns
KEYPAD_DDR_COLS|=0xF0;// Set columns to Output
_delay_ms(2);//give delay of 2ms
rows=0x0F&(~(KEYPAD_PIN_ROWS &0x0F));// read the value of rows
/* value of columns and rows will be 1,2,4,8
we will convert it to 0,1,2,3 using keypad_tbl1*/
// we will convert it to 0,1,2,3 using keypad_tbl1*/
temp = (keypad_tbl1[columns]) + (keypad_tbl1[rows]<<2);

if(temp > 15)


/*If value is greater than 15,return 0, otherwise
return the key.*/
key = 0;
else
key = keypad_tbl2[temp];
_delay_ms(200);

return key;

Task 2: Simulation
4 [Document title]

Task 3: Hardware Implementation

Serial Monitor Output:

You might also like