at89s51을 이용한 키패드 입력에 따른 LCD 출력
#include<at89x51.h>
#include<stdio.h>
sbit RS=P3^4;
sbit RW=P3^5;
sbit CE=P3^6;
#define RIGHT 1
#define LEFT 2
#define On 1
#define Off 2
#define NULL 0
typedef unsigned char BYTE;
typedef unsigned int WORD;
char KeyScan(void);
void delay(int p);
void LCDIR(unsigned char a);
void LCDDR(unsigned char b);
void lcd_initialize();
void char_out(char s);
void print(char *str);
void x_y_position ( char x, char y);
void display_shift(char p);
void cursor_shift(char p);
void cursor_home();
void lcd_clear();
main()
{
int i=0;
lcd_initialize();
print("lcd start");
while(1)
{
switch(KeyScan())
{
case '0':
lcd_clear();
print("0 button push");
delay(60);
break;
case '1':
lcd_clear();
print("1 button push");
delay(60);
break;
case '2':
lcd_clear();
print("2 button push");
delay(60);
break;
case '3':
lcd_clear();
print("3 button push");
delay(60);
break;
case '4':
lcd_clear();
print("4 button push");
delay(60);
break;
case '5':
lcd_clear();
print("5 button push");
delay(60);
break;
case '6':
lcd_clear();
print("6 button push");
delay(60);
break;
case '7':
lcd_clear();
print("7 button push");
delay(60);
break;
case '8':
lcd_clear();
print("8 button push");
delay(60);
break;
case '9':
lcd_clear();
print("9 button push");
delay(60);
break;
case '*':
lcd_clear();
print("* button push");
delay(60);
break;
case '#':
lcd_clear();
print("# button push");
delay(60);
break;
case '=':
lcd_clear();
LCDIR(0xc0);
print("=");
delay(60);
break;
case '+':
lcd_clear();
print("+ button push");
delay(60);
break;
case '^':
lcd_clear();
print("CLEAR button push\n\n");
delay(60);
break;
case '-':
lcd_clear();
print("- button push");
delay(60);
break;
case '/':
lcd_clear();
print("/ button push");
delay(60);
break;
}
}
}
char KeyScan(void)
{
char KeyBuf=0xFF;
P2=0xFF;
P2_0=0;
if(!P2_4)KeyBuf='7';
if(!P2_5)KeyBuf='8';
if(!P2_6)KeyBuf='9';
if(!P2_7)KeyBuf='/';
P2_0=1;
P2_1=0;
if(!P2_4)KeyBuf='4';
if(!P2_5)KeyBuf='5';
if(!P2_6)KeyBuf='6';
if(!P2_7)KeyBuf='*';
P2_1=1;
P2_2=0;
if(!P2_4)KeyBuf='1';
if(!P2_5)KeyBuf='2';
if(!P2_6)KeyBuf='3';
if(!P2_7)KeyBuf='-';
P2_2=1;
P2_3=0;
if(!P2_4)KeyBuf='^';
if(!P2_5)KeyBuf='0';
if(!P2_6)KeyBuf='=';
if(!P2_7)KeyBuf='+';
P2_3=1;
return KeyBuf;
}
void delay(int p)
{
int i,j;
for (j=0 ;j < p;j++)
for (i=0; i<1000; i++);
}
void LCDIR(unsigned char a)
{
P1 = a;
RS=0;
RW=0;
delay(10);
CE = 1;
delay(10);
CE = 0;
}
void LCDDR(unsigned char b)
{
P1 = b;
RS=1;
RW=0;
delay(10);
CE = 1;
delay(10);
CE = 0;
}
void lcd_initialize()
{
LCDIR(0x38);
delay(40);
LCDIR(0x38);
delay(40);
LCDIR(0x38);
delay(40);
LCDIR(0x0e);
delay(5);
LCDIR(0x06);
delay(5);
LCDIR(0x01);
delay(5);
}
void char_out(char s)
{
int i;
LCDDR(s) ;
for(i=0;i<100; i++);
}
void print(char *str)
{
int i =0;
do{
char_out(str[i]);
}while(str[++i] !='\0');
}
void x_y_position ( char x, char y)
{
int i;
unsigned char position;
if (y > 1) y = 1;
if (x > 15) x = 15;
position = y ? x + 0xc0 : x + 0x80;
LCDIR(position);
for(i=0;i<100; i++);
}
void display_shift(char p)
{
int i;
if (p==RIGHT) LCDIR(0x1a) ;
else if (p==LEFT) LCDIR(0x18) ;
for (i=0;i<100;i++);
}
void cursor_shift(char p)
{
int i;
if (p==RIGHT) LCDIR(0x14) ;
else if (p==LEFT) LCDIR(0x10) ;
for (i=0;i<100;i++);
}
void cursor_home()
{
int i;
LCDIR(0x02) ;
for(i=0;i<100;i++);
}
void lcd_clear()
{
int i;
LCDIR(0x01) ;
for(i=0;i<100;i++);
}
'Embeded > 8051' 카테고리의 다른 글
at89s51을 이용한 모터제어(LB1630) (0) | 2012.03.17 |
---|---|
at89s51을 이용한 모터제어(트랜지스터) (0) | 2012.02.28 |
at89s51을 이용한 산토끼 연주 (0) | 2012.02.28 |
at89s51을 이용한 스피커 제어(전화벨소리) (0) | 2012.02.28 |
at89s51을 이용한 릴레이 제어 (0) | 2012.02.28 |