85Views3Replies

Author Options:

URGENT HELP FOR ATMEGA32? Answered

I write c code for lcd and ketpad interface but when i run simulation it detect value from keypad but not show detected value here is code!

/*
* my_kp.c
*
* Created: 1/19/2014 5:23:42 PM
*  Author: ALEES GEE!
*/


#include <avr/io.h>
#include <util/delay.h>
#include "IncLcd.h"

#define KEY_PRT PORTC
#define KEY_DDR DDRC
#define KEY_PIN PINC

void delay_ms()
{
_delay_ms(20);
}

unsigned char keypad[4][4] ={ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};


int main(void)
{
unsigned char collloc, rowloc, data;

DDRA = 0xff;
DDRB = 0x0ff;
KEY_DDR = 0XF0;
KEY_PRT = 0XFF;
while(1)
{
  do
  {
   KEY_PRT &= 0x0F;
   collloc = (KEY_PIN & 0x0F);
  } while (collloc != 0x0F);
  do
  {
   do
   {
    delay_ms();
    collloc = (KEY_PIN & 0x0F);
   } while (collloc == 0x0F);
  
   delay_ms();
   collloc = (KEY_PIN & 0x0F);
  } while (collloc == 0x0F);
  while (1)
  {
   KEY_PRT = 0xEF;
   collloc = (KEY_PIN & 0x0F);
   if (collloc != 0x0F)
   {
    rowloc = 0;
    break;
   }
   KEY_PRT = 0xDF;
   collloc = (KEY_PIN & 0x0F);
  
   if (collloc != 0x0F)
   {
    rowloc = 1;
    break;
   }
   KEY_PRT = 0xBF;
   collloc = (KEY_PIN & 0x0F);
   if (collloc != 0x0F)
   {
    rowloc = 2;
    break;
   }
   KEY_PRT = 0x7F;
   collloc = (KEY_PIN & 0x0F);
   rowloc = 3;
   break;
  }
  if (collloc == 0x0E)
  {
   data = (keypad[rowloc][0]);
   LCD_init();
   set2();
   LCD_SendData(data);     // write first lcd line
   wrcomm();
  } 
  else if(collloc == 0x0D)
  {
   data = (keypad[rowloc][1]);
   LCD_init();
   set2();
   LCD_SendData(data);     // write first lcd line
   wrcomm();
  } 
  else if(collloc == 0x0B)
  { data = (keypad[rowloc][2]);
   LCD_init();
   set2();
   LCD_SendData(data);     // write first lcd line
   wrcomm();
  } 
  else
   data = (keypad[rowloc][3]);
   LCD_init();
   set2();
   LCD_SendData(data);     // write first lcd line
   wrcomm();
  }
return 0;
}



and here is lcd .h code

/*
* IncLcd.h
*
* Created: 1/5/2014 4:23:30 PM
*  Author: ALEES GEE!
*/

#include <avr/io.h>
#ifndef INCLCD_H_
#define INCLCD_H_

#include<util/delay.h>
#include<string.h>

#define dataport PORTA              //define port B ATMEGA16 as dataport
#define commport PORTB            //define port D ATMEGA16 as communication / control port

#define rs PB0                  //define Pin D4 (Pin no.18) ATMEGA16 as RS control
#define wr PB1                  //define Pin D5 (Pin no.19) ATMEGA16 as WR control
#define en PB2



////LCD Initialization///
int LCD_init()
{
dataport=0x38;    // initialize lcd 2 lines, 5x7 matrix
wrcomm();        // right the command byte to a command register
dataport = 0x01; // clear the lcd
wrcomm();         // right the command byte to a command register
dataport = 0x0E;  //display the blinking cursor
wrcomm();         // right the command byte to a command register
dataport = 0x80;  // cursor at line 1, position 1
wrcomm();         // right the command byte to a command register
dataport=0x1C;    //shift entire display to right
wrcomm();         // right the command byte to a command register
return 1;
}

//setting1
void set1()
{
dataport = 0xC0;
}

//setting2
void set2()
{
dataport = 0x01;
}


/////sending data to lcd display////

int LCD_SendData(unsigned char s)
{
//unsigned char *j=s;
  dataport=s;
  wrdata();
return 1;
}


///righting the command byte to command register///
int wrcomm(void)
{
commport &=~(1 << rs); //setting rs=0; selecting comand register//0 for command
commport &=~(1 << wr); //setting wr=0; selecting comand register//0 for write the data to lcd
commport |=(1 << en); // EN = 1 lcd access enabling
commport &=~(1 <<en); //EN = 0, giving low pulse enable pin
_delay_ms(20);
dataport = 0x01;
return 1;
}

///righting the databyte to data register///
int wrdata(void)
{
commport |=(1 << rs); //setting rs=1; selecting command register
commport &=~(1 << wr); //setting wr=0; selecting command register
commport |=(1 << en); // EN = 1
commport &=~(1 <<en); //EN = 0, giving low pulse enable pin
_delay_ms(300);
return 1;
}




#endif /* INCLCD_H_ */



Discussions

0
None
steveastroukiceng

Answer 5 years ago

They're not using Arduino code though./

0
None
steveastrouk

5 years ago

What happpens ? Does your LCD initialise ? When it first switches on the LCD displays a solid single line. When its initialised, BOTH lines should appear faintly.

Your code says

#define dataport PORTA //define port B ATMEGA16 as dataport
#define commport PORTB //define port D ATMEGA16 as communication /

Which is right ? The statement, or the comment ????

The first thing I'd check, apart from my wiring is that I send the first initialisation command TWICE. You're sending

0x38,0x01,0x0E;0x80;0x1C;

Try

0x38, 0x38,0x01,0x0E;0x80;0x1C;