0% found this document useful (0 votes)
28 views

Experiment#9: Question#1

The document contains code for an LCD interfacing experiment with the following key points: 1. It initializes an LCD display and defines functions for initializing the LCD, sending commands/data, and displaying strings. 2. It implements a 4x4 keypad to read input values which are displayed on the LCD. Pressing the '*' key calculates and displays the square of the input number. 3. The code is modified to use the keypad to light up individual LEDs connected to port B pins based on the key pressed. 4. Further code displays a string on the LCD and implements the keypad to read input and display it on the LCD.

Uploaded by

jameeshudson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Experiment#9: Question#1

The document contains code for an LCD interfacing experiment with the following key points: 1. It initializes an LCD display and defines functions for initializing the LCD, sending commands/data, and displaying strings. 2. It implements a 4x4 keypad to read input values which are displayed on the LCD. Pressing the '*' key calculates and displays the square of the input number. 3. The code is modified to use the keypad to light up individual LEDs connected to port B pins based on the key pressed. 4. Further code displays a string on the LCD and implements the keypad to read input and display it on the LCD.

Uploaded by

jameeshudson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment#9

Question#1
Code:

void LCD_init (void);


void Delay (int x);
void LCD (unsigned char val, unsigned char cmd);
void LCD_string (unsigned char *word);
//RS PA8
//EN PA9
//D4 PA4
//D5 PA5
//D6 PA6
//D7 PA7
void main (void) {
RCC_APB2ENR |= 0x000000014;
RCC_APB1ENR |= 0x00000001;
GPIOA_CRL &= 0x2222FFFF;
GPIOA_CRL |= 0x22220000;
GPIOA_CRH &= 0xFFFFFF22;
GPIOA_CRH |= 0x00000022;
GPIOC_CRH &= 0xFF8FFFFF;
GPIOC_CRH |= 0x00800000;

LCD_init ();
LCD_string ("String is Displayed");
}
}

void LCD_init (void) {


Delay (5000);
LCD (0x33, 0);
Delay (50);
LCD (0x32, 0);
Delay (50);
LCD (0x28, 0); //4-bit mode, 2 Line
Delay (50);
LCD (0x01, 0); //Clear display
Delay (50);
LCD (0x06, 0); //Entry mode
Delay (50);
LCD (0x80, 0); //Force Cursor to beginning of 1st row
Delay (50);
LCD (0x0F, 0); //Display on, Cursor blinking
}
void Delay (int x) {
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
TIM2_ARR = x - 1;
TIM2_CR1 = 1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_SR = 0;
}

void LCD (unsigned char val, unsigned char cmd) {


unsigned char val_1;
val_1 = ((val) & 0xF0);
GPIOA_ODR = val_1;
if (cmd == 0) {
GPIOA_BSRR = 0x01000000;
}
else {
GPIOA_BSRR = 0x00000100;
}
GPIOA_BSRR = 0x00000200;
Delay (10);
GPIOA_BSRR = 0x02000000;
Delay (10);

val_1 = ((val << 4) & 0xF0);


GPIOA_ODR = val_1;
if (cmd == 0) {
GPIOA_BSRR = 0x01000000;
}
else {
GPIOA_BSRR = 0x00000100;
}
GPIOA_BSRR = 0x00000200;
Delay (10);
GPIOA_BSRR = 0x02000000;
Delay (10);
}

void LCD_string (unsigned char *word) {


while (word) {
LCD (*word, 1);
word ++;
Delay (50);
}
} //END
Question#2
Code:

int val = 0, ans;


char a;
void main(void) {
RCC_APB2ENR = 0x18; //enable Port B & C
RCC_APB1ENR = 0X1; //enable Timer2
GPIOC_CRL &= 0X88882222; //rows as input and columns as output
GPIOC_CRL |= 0X88882222; //rows as input and columns as output
GPIOB_CRL &= 0XF2222222; //PB0-PB3 as LCD data pins
GPIOB_CRL |= 0X02222222; // R/W on PB4, E on PB5, RS on PB6
GPIOB_BSRR = 0X00000010; //R/W on PB4 as continuous Low
LCD_init ();

while (1) {
GPIOC_BSRR = 0x0000000F; //Set all Columns
while ((GPIOC_IDR & 0xF0) == 0); //wait till any key is pressed
keypad ();
while ((GPIOC_IDR & 0xF0) != 0); //debouncing
}
}

void keypad (void) {


GPIOC_BSRR = 0x000E0001; //Set Column1 and Reset other Columns
if ((GPIOC_IDR & 0x10) != 0) {
LCD ('1', 1); //1 is pressed
val = (val * 10) + 1;
}
else if ((GPIOC_IDR & 0x20) != 0) {
LCD ('4', 1); //4 is pressed
val = (val * 10) + 4;
}
else if ((GPIOC_IDR & 0x40) != 0) {
LCD ('7', 1); //7 is pressed
val = (val * 10) + 7;
}

GPIOC_BSRR = 0x000D0002; //Set Column2 and Reset other Columns


if ((GPIOC_IDR & 0x10) != 0) {
LCD ('2', 1); //2 is pressed
val = (val * 10) + 2;
}
else if ((GPIOC_IDR & 0x20) != 0) {
LCD ('5', 1); //5 is pressed
val = (val * 10) + 5;
}
else if ((GPIOC_IDR & 0x40) != 0) {
LCD ('8', 1); //8 is pressed
val = (val * 10) + 8;
}
else if ((GPIOC_IDR & 0x80) != 0) {
LCD ('0', 1); //0 is pressed
val = (val * 10) + 0;
}

GPIOC_BSRR = 0x000B0004; //Set Column3 and Reset other Columns


if ((GPIOC_IDR & 0x10) != 0) {
LCD ('3', 1); //3 is pressed
val = (val * 10) + 3;
}
else if ((GPIOC_IDR & 0x20) != 0) {
LCD ('6', 1); //6 is pressed
val = (val * 10) + 6;
}
else if ((GPIOC_IDR & 0x40) != 0) {
LCD ('9', 1); //9 is pressed
val = (val * 10) + 9;
}

GPIOC_BSRR = 0x00070008; //Set Column4 and Reset other Columns


if ((GPIOC_IDR & 0x80) != 0) {
LCD (0x94, 0); //Cursor to 3rd line
LCD_string ("Square is: ");
ans = val * val;
itoa (ans, a, 10);
LCD_string (a, 1);
LCD (0x01, 0); //Clear display
LCD (0x06, 0); //Entry mode
LCD (0x80, 0); //Force Cursor to beginning of 1st row
ans = 0, val = 0, a = 0;
}
}

void Delay (int x) {


TIM2_CR1=0;
TIM2_CNT=0;
TIM2_PSC=7999;
TIM2_ARR=x-1;
TIM2_CR1=1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1=0;
TIM2_CNT=0;
TIM2_SR=0;
}

void LCD (unsigned char val, unsigned char cmd) {


unsigned char val_1;
val_1 = ((val) & 0xF0); //to send 4-bit data
GPIOB_ODR = val_1;
if (cmd == 0) {
GPIOB_BSRR = 0x00400000; //reset R/S pin to send command
}
else {
GPIOB_BSRR = 0x00000040; //set R/S pin to send data
GPIOB_BSRR = 0x00000020; //set Enable pin
Delay (10);
GPIOB_BSRR = 0x00200000; //reset Enable pin
Delay (10);
}
val_1 = ((val << 4) & 0xF0); //to send remaining 4-bits
GPIOB_ODR = val_1;
if (cmd == 0) {
GPIOB_BSRR=0x00400000;
}
else {
GPIOB_BSRR = 0x00000040; //set R/S pin to send data
GPIOB_BSRR = 0x00000020; //set Enable pin
Delay (10);
GPIOB_BSRR = 0x00200000; //reset Enable pin
Delay (10);
}
}

void LCD_init (void) {


Delay (5000);
LCD (0x30, 0);
Delay (50);
LCD (0x30, 0);
Delay (50);
LCD (0x30, 0);
Delay (50);
LCD (0x01, 0); //Clear display
LCD (0x06, 0); //Entry mode
LCD (0x80, 0); //Force Cursor to beginning of 1st row
}

void LCD_string (unsigned char *word) {


while (*word) {
LCD (*word, 0);
word ++;
}
}
//END
Question#3
Code:

int val = 0;

void main(void) {
RCC_APB2ENR = 0x18; //enable Port B & C
RCC_APB1ENR = 0x1; //enable Timer2
GPIOC_CRL &= 0x88882222; //rows as input and columns as output
GPIOC_CRL |= 0x88882222; //rows as input and columns as output
GPIOB_CRL &= 0x22222222; //LEDs connected from PB0-PB7
GPIOB_CRL |= 0x22222222;

while (1) {
GPIOC_BSRR = 0x0000000F; //Set all Columns
while ((GPIOC_IDR & 0xF0) == 0); //wait till any key is pressed
keypad ();
while ((GPIOC_IDR & 0xF0) != 0); //debouncing
}
}

void keypad (void) {


GPIOC_BSRR = 0x000E0001; //Set Column1 and Reset other Columns
if ((GPIOC_IDR & 0x10) != 0) {
val = 1;
}
else if ((GPIOC_IDR & 0x20) != 0) {
val = 4;
}
else if ((GPIOC_IDR & 0x40) != 0) {
val = 7;
}

GPIOC_BSRR = 0x000D0002; //Set Column2 and Reset other Columns


if ((GPIOC_IDR & 0x10) != 0) {
val = 2;
}
else if ((GPIOC_IDR & 0x20) != 0) {
val = 5;
}
else if ((GPIOC_IDR & 0x40) != 0) {
val = 8;
}
else if ((GPIOC_IDR & 0x80) != 0) {
val = 0;
}

GPIOC_BSRR = 0x000B0004; //Set Column3 and Reset other Columns


if ((GPIOC_IDR & 0x10) != 0) {
val = 3;
}
else if ((GPIOC_IDR & 0x20) != 0) {
val = 6;
}
else if ((GPIOC_IDR & 0x40) != 0) {
val = 9;
}

GPIOC_BSRR = 0x00070008; //Set Column4 and Reset other Columns


if ((GPIOC_IDR & 0x80) != 0) {
if (val == 0) {
GPIOB_BSRR = 0xFFFF0000;
}
if ( val == 1) {
GPIOB_BSRR = 0xFFFF0001;
}
if ( val == 2) {
GPIOB_BSRR = 0xFFFF0003;
}
if ( val == 3) {
GPIOB_BSRR = 0xFFFF0007;
}
if (val == 4) {
GPIOB_BSRR = 0xFFFF000F;
}
if ( val == 5) {
GPIOB_BSRR = 0xFFFF001F;
}
if ( val == 6) {
GPIOB_BSRR = 0xFFFF003F;
}
if ( val == 7) {
GPIOB_BSRR = 0xFFFF007F;
}
if ( val == 8) {
GPIOB_BSRR = 0xFFFF00FF;
}
if ( val == 9) {
GPIOB_BSRR = 0xFFFF01FF;
}
}
}
//END
Question#4
Code:

int val = 0;

void main(void) {
RCC_APB2ENR = 0x18; //enable Port B & C
RCC_APB1ENR = 0X1; //enable Timer2
GPIOC_CRL &= 0X88882222; //rows as input and columns as output
GPIOC_CRL |= 0X88882222; //rows as input and columns as output
GPIOB_CRL &= 0XF2222222; //PB0-PB3 as LCD data pins
GPIOB_CRL |= 0X02222222; // R/W on PB4, E on PB5, RS on PB6
GPIOB_BSRR = 0X00000010; //R/W on PB4 as continuous Low
LCD_init ();
LCD_string ("L1F18BSEE");

while (1) {
GPIOC_BSRR = 0x0000000F; //Set all Columns
while ((GPIOC_IDR & 0xF0) == 0); //wait till any key is pressed
keypad ();
while ((GPIOC_IDR & 0xF0) != 0); //debouncing
}
}

void keypad (void) {


GPIOC_BSRR = 0x000E0001; //Set Column1 and Reset other Columns
if ((GPIOC_IDR & 0x10) != 0) {
LCD ('1', 1); //1 is pressed
val = (val * 10) + 1;
}
else if ((GPIOC_IDR & 0x20) != 0) {
LCD ('4', 1); //4 is pressed
val = (val * 10) + 4;
}
else if ((GPIOC_IDR & 0x40) != 0) {
LCD ('7', 1); //7 is pressed
val = (val * 10) + 7;
}

GPIOC_BSRR = 0x000D0002; //Set Column2 and Reset other Columns


if ((GPIOC_IDR & 0x10) != 0) {
LCD ('2', 1); //2 is pressed
val = (val * 10) + 2;
}
else if ((GPIOC_IDR & 0x20) != 0) {
LCD ('5', 1); //5 is pressed
val = (val * 10) + 5;
}
else if ((GPIOC_IDR & 0x40) != 0) {
LCD ('8', 1); //8 is pressed
val = (val * 10) + 8;
}
else if ((GPIOC_IDR & 0x80) != 0) {
LCD ('0', 1); //0 is pressed
val = (val * 10) + 0;
}

GPIOC_BSRR = 0x000B0004; //Set Column3 and Reset other Columns


if ((GPIOC_IDR & 0x10) != 0) {
LCD ('3', 1); //3 is pressed
val = (val * 10) + 3;
}
else if ((GPIOC_IDR & 0x20) != 0) {
LCD ('6', 1); //6 is pressed
val = (val * 10) + 6;
}
else if ((GPIOC_IDR & 0x40) != 0) {
LCD ('9', 1); //9 is pressed
val = (val * 10) + 9;
}

GPIOC_BSRR = 0x00070008; //Set Column4 and Reset other Columns


if ((GPIOC_IDR & 0x80) != 0) {
LCD (0x94, 0); //Cursor to 3rd line
if ( val == 01) {
LCD_string ("Hamza");
}
if ( val == 05) {
LCD_string ("Ali");
}
if ( val == 10) {
LCD_string ("Ahmed");
}
if ( val == 20) {
LCD_string ("Hussain");
}
LCD (0x01, 0); //Clear display
LCD (0x06, 0); //Entry mode
LCD (0x80, 0); //Force Cursor to beginning of 1st row
LCD_string ("L1F18BSEE");
val = 0;
}
}

void Delay (int x) {


TIM2_CR1=0;
TIM2_CNT=0;
TIM2_PSC=7999;
TIM2_ARR=x-1;
TIM2_CR1=1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1=0;
TIM2_CNT=0;
TIM2_SR=0;
}

void LCD (unsigned char val, unsigned char cmd) {


unsigned char val_1;
val_1 = ((val) & 0xF0); //to send 4-bit data
GPIOB_ODR = val_1;
if (cmd == 0) {
GPIOB_BSRR = 0x00400000; //reset R/S pin to send command
}
else {
GPIOB_BSRR = 0x00000040; //set R/S pin to send data
GPIOB_BSRR = 0x00000020; //set Enable pin
Delay (10);
GPIOB_BSRR = 0x00200000; //reset Enable pin
Delay (10);
}
val_1 = ((val << 4) & 0xF0); //to send remaining 4-bits
GPIOB_ODR = val_1;
if (cmd == 0) {
GPIOB_BSRR=0x00400000;
}
else {
GPIOB_BSRR = 0x00000040; //set R/S pin to send data
GPIOB_BSRR = 0x00000020; //set Enable pin
Delay (10);
GPIOB_BSRR = 0x00200000; //reset Enable pin
Delay (10);
}
}

void LCD_init (void) {


Delay (5000);
LCD (0x30, 0);
Delay (50);
LCD (0x30, 0);
Delay (50);
LCD (0x30, 0);
Delay (50);
LCD (0x01, 0); //Clear display
LCD (0x06, 0); //Entry mode
LCD (0x80, 0); //Force Cursor to beginning of 1st row
}
void LCD_string (unsigned char *word) {
while (*word) {
LCD (*word, 0);
word ++;
}
}
//END

Conclusion:
From this experiment it is learnt that how to interface a Keypad with STM32 board. It is also
learnt that how to design a logic for controlling the input from the Keypad and displaying it on an
LCD. A keypad consists of switches on each button that let the current pass through them and complete
the circuit. It is learnt that there are two ways to configure the keypad, one is to set all rows and take
output from columns, other way is to set all columns and take output from rows. It is also learnt that
the numbers written on the keypad are not given as output to the controller, in fact the output can be
programmed to by the programmer in any way, the program written will decide what to do with the
input from keypad. Different logics are implemented on keypad’s output and different functions are
performed in this experiment. Keypad is no different than a push button, we can consider it as a matrix
of push buttons, of which the output can be programmed in any way, according to our requirements.
The interconnected operation of keypad with LCD is also studied and understood.

You might also like