Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Student\'s Name: . (Code 2) program to interface ATMEGA32 with LCD, Display Cloc

ID: 2266534 • Letter: S

Question

Student's Name: . (Code 2) program to interface ATMEGA32 with LCD, Display Clock (format hh:mmss) in second line of LCD, at the same time get key input from buttons. When Button is pressed, the first line of LCD will display data in following requirements Button O is pressed, position (1.1) of LCD will display "value o" Button 1 is pressed, position (1,1) of LCD will display "value 1 Button 2 is pressed , position (1,1) of LCD will display "value 2" Button 3 is pressed, position (1,1) of LCD will display "value 3" Button 4 is pressed, position (1,1) of LCD will display "value 4" Button 5 is pressed, position (1,1) of LCD will display "value 5” Button 6 is pressed, position (1,1) of LCD will display "value 6 Button 7 is pressed , position (1,1) of LCD will display "value 7" After pressing 10 times, position (1.6) of LCD will display the summing value of all input values.

Explanation / Answer

PORTD is connected to LCD and PORTB is used for input buttons

Code:

//********************************************************

//Controller: ATmega32 (Crystal: 16 Mhz)

//********************************************************

void LCD_init(void);

void LCD_WriteCommand (unsigned char CMD);

void LCD_WriteData (unsigned char Data);

void LCD_Cursor(char row, char column);

void delay_ms(int miliSec);

void dis_clk(int,int,int);

void cal();

void press(int n);

#define ENABLE_LCD PORTD |= 0x80

#define DISABLE_LCD PORTD &= ~0x80

#define SET_LCD_DATA PORTD |= 0x20

#define SET_LCD_CMD PORTD &= ~0x20

#include <iom32v.h>

#include <macros.h>

int sum=0,count=0;

//MAIN FUNCTION

void main(void)

{

init_devices();

int hr,mm,ss;

while(1)

{

for(hr=00;hr<=24;hr++) // 24 hour format

{

for(mm=00;mm<=59;mm++)

{

for(ss=00;ss<=59;ss++)

{

dis_clk(hr,mm,ss);

Cal();

}

}

}

}

}

// display num and sum after 10 pushes

void Cal()

{

int x=0;

int num;

if(PIND & (1<<PB0) == 1)

{press(0);}

else if(PIND & (1<<PB1) == 1)

{press(1);}

else if(PIND & (1<<PB2) == 1)

{press(2);}

else if(PIND & (1<<PB3) == 1)

{press(3);}

else if(PIND & (1<<PB4) == 1)

{press(4);}

else if(PIND & (1<<PB5) == 1)

{press(5);}

else if(PIND & (1<<PB6) == 1)

{press(6);}

else if(PIND & (1<<PB7) == 1)

{press(7);}

  

if (count==10) // when count becomes 10 sum is printed

{

x=sum/10;

sum=sum%10;

LCD_Cursor(1,6);

LCD_WriteData(x+48);

LCD_WriteData(sum+48);

sum=0; // sum and count reset to zero

count=0;

}

delay_ms (800);

}

// To Display preesed value

void press(int n)

{

count++;

sum =sum+n;

LCD_Cursor(1,1);

LCD_WriteData(n+48);

}

// converts Time to LCD format

void dis_clk(int x,int y,int z) // conversion

{

int a,b,c,d,e,f;

a=x/10;

b=x%10;

LCD_Cursor(2,1);

LCD_WriteData(a+48);

LCD_WriteData(b+48);

LCD_WriteData(':');

c=y/10;

d=y%10;

LCD_WriteData(c+48);

LCD_WriteData(d+48);

LCD_WriteData(':');

e=z/10;

f=z%10;

LCD_WriteData(e+48);

LCD_WriteData(f+48);

delay_ms(100);

}

//call this routine to initialize all peripherals

void init_devices(void)

{

CLI(); //disable all interrupts

port_init();

LCD_init();

MCUCR = 0x00;

GICR = 0x00;

TIMSK = 0x00; //timer interrupt sources

}

void port_init(void)

{

DDRA = 0x00;

PORTA = 0x00;

DDRB = 0x00;

PORTB = 0x00; //set as input for buttons

DDRC = 0xFF;

PORTC = 0x00;

DDRD = 0xF0;

PORTD = 0x00;

}

// *** Initialize the LCD driver ***

void LCD_init(void)

{

delay_ms(100); // wait for 100ms

LCD_WriteCommand (0x38); // 8 data lines

LCD_WriteCommand (0x06); // cursor setting

LCD_WriteCommand (0x0f); // display ON

LCD_WriteCommand (0x01); // clear LCD memory

delay_ms (10); // 10ms delay after clearing LCD

}

//********** LCD Functions ******

// Write a command instruction to the LCD

void LCD_WriteCommand (unsigned char Command)

{

SET_LCD_CMD; // Set LCD in command mode

PORTC = Command; // Load data to port

ENABLE_LCD; // Write data to LCD

asm("nop");

asm("nop");

DISABLE_LCD; // Disable LCD

delay_ms(1); // wait for 1ms

}

// *** Write one byte of data to the LCD ***

// *****************************************

void LCD_WriteData (unsigned char Data)

{

SET_LCD_DATA; // Set LCD in data mode

PORTC = Data; // Load data to port

ENABLE_LCD; // Write data to LCD

asm("nop");

asm("nop");

DISABLE_LCD; // Disable LCD

delay_ms(1); // wait for 1ms

}

// Position the LCD cursor at "row", "column".

void LCD_Cursor (char row, char column)

{

switch (row)

{

case 1: LCD_WriteCommand (0x80 + column - 1); break;

case 2: LCD_WriteCommand (0xc0 + column - 1); break;

default: break;

}

}

//Function for delay of 1 msec (appx.) at 16Mhz

void delay_ms(int miliSec) //for 16 Mhz crystal

{

int i,j;

for(i=0;i<miliSec;i++)

for(j=0;j<1550;j++)

{

asm("nop");

asm("nop");

}

}

// *********** END *********