This is a tutorial for owners of the EEDT6.0 development board. Recommended tools include the Keil C51 IDE and the included EM-P-NXP programmer with Flash Magic programming software. Usage of these was covered in the first tutorial (just install C51 the same way as you did MDK-ARM).
This program will display a pressed keypad's number on the second line of the LCD. Setup and Connections- Remove the ATmega32 microcontroller from Socket U17 (Sec-17). This is important!
- Insert the P89V51RD2 microcontroller to Socket U18 (Sec-13).
(Sockets U17 and U18 share some pins, which is why you must not have a chip in both at the same time.) - Connect P0 to the LCD's data port: P0.0 through P0.7 to D0 through D7.
- Connect P2.0, P2.1, and P2.2 to the RS, RW, and EN pins of the LCD section.
- Connect all eight of the P1 port pins to the Keypad section's eight pins.
- Power up the EEDT6.0 with 9V DC.
- Test the code. Rather than copying the code below and compiling it yourself, you can download the full project code as source and compiled hex files.
Sample Program #include <reg52.h> /* special function register declarations */ /* for the intended 8051 derivative */
#include <stdio.h> /* prototype declarations for I/O functions */
//LCD Defines #define PDATA P0 sbit RS = P2^0; sbit RW = P2^1; sbit EN = P2^2; sbit D7 = P0^7;
//LCD Functions void Init_LCD(void); void Clear_LCD(void); void Wait_LCD(void); void Write_Char(unsigned char c,int x, int y); void Write_Text(char* str,int x, int y);
//Keypad defines sbit COL_1 = P1^7; sbit COL_2 = P1^6; sbit COL_3 = P1^5; sbit COL_4 = P1^4;
sbit ROW_1 = P1^0; sbit ROW_2 = P1^1; sbit ROW_3 = P1^2; sbit ROW_4 = P1^3;
#define KEY_PORT P1
void Delay(void); //Function prototype declaration
void main (void) { Init_LCD(); Clear_LCD(); Write_Text("Key number - ",0,1); while(1) //End less while so that program never ends { KEY_PORT = 0xff; COL_1=0; if(ROW_1 == 0) { Write_Text(" ",0,2); Write_Text("1",0,2); while(ROW_1 == 0); } if(ROW_2 == 0) { Write_Text(" ",0,2); Write_Text("2",0,2); while(ROW_2 == 0); } if(ROW_3 == 0) { Write_Text(" ",0,2); Write_Text("3",0,2); while(ROW_3 == 0); } if(ROW_4 == 0) { Write_Text(" ",0,2); Write_Text("4",0,2); while(ROW_4 == 0); } KEY_PORT = 0xff; COL_2=0; if(ROW_1 == 0) { Write_Text(" ",0,2); Write_Text("5",0,2); while(ROW_1 == 0); } if(ROW_2 == 0) { Write_Text(" ",0,2); Write_Text("6",0,2); while(ROW_2 == 0); } if(ROW_3 == 0) { Write_Text(" ",0,2); Write_Text("7",0,2); while(ROW_3 == 0); } if(ROW_4 == 0) { Write_Text(" ",0,2); Write_Text("8",0,2); while(ROW_4 == 0); }
KEY_PORT = 0xff; COL_3=0; if(ROW_1 == 0) { Write_Text(" ",0,2); Write_Text("9",0,2); while(ROW_1 == 0); } if(ROW_2 == 0) { Write_Text(" ",0,2); Write_Text("10",0,2); while(ROW_2 == 0); } if(ROW_3 == 0) { Write_Text(" ",0,2); Write_Text("11",0,2); while(ROW_3 == 0); } if(ROW_4 == 0) { Write_Text(" ",0,2); Write_Text("12",0,2); while(ROW_4 == 0); }
KEY_PORT = 0xff; COL_4=0; if(ROW_1 == 0) { Write_Text(" ",0,2); Write_Text("13",0,2); while(ROW_1 == 0); } if(ROW_2 == 0) { Write_Text(" ",0,2); Write_Text("14",0,2); while(ROW_2 == 0); } if(ROW_3 == 0) { Write_Text(" ",0,2); Write_Text("15",0,2); while(ROW_3 == 0); } if(ROW_4 == 0) { Write_Text(" ",0,2); Write_Text("16",0,2); while(ROW_4 == 0); }
} }
void Delay(void) { int j; int i; for(i=0;i<10;i++) { for(j=0;j<10000;j++) { } } }
void Write_Text(char* str,int x, int y) { while(*str!=' ') { Write_Char(*str,x,y); x++; str++; } }
void Init_LCD(void) { RS = 0; //CLR RS PDATA = 0x34; //MOV PDATA,#00110100b EN = 1; //SETB EN EN = 0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD
RS = 0; //CLR RS PDATA = 0x3C; //MOV PDATA,#00111100b EN = 1; //SETB EN EN = 0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD
RS = 0; //CLR RS PDATA= 0x0C; //MOV PDATA,#0Ch EN = 1; //SETB EN EN = 0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD RS = 0; //CLR RS PDATA= 0x06; //MOV PDATA,#06h EN=1; //SETB EN EN=0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD }
void Clear_LCD(void) { RS = 0; //CLR RS PDATA = 0x01; //MOV PDATA,#01h EN = 1; //SETB EN EN = 0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD }
void Wait_LCD(void) { EN = 0; //CLR EN RS = 0; //CLR RS RW = 1; //SETB RW D7 = 1; //MOV PDATA,#0FFh EN = 1; //SETB EN while(D7) { EN = 0; EN = 1; } //MOV A,PDATA //JB ACC.7,WAIT_LCD EN = 0; //CLR EN RW = 0; //CLR RW }
void Write_Char(unsigned char c,int x, int y) //x is 0 to 15 horizontal position of character. //y is 1 for first line, 2 for second line. { int location; if(y==1) { location = 0x80 + x; }
if(y==2) { location = 0x80 + 0x40 + x;
} RS = 0; //CLR RS PDATA=location; //MOV PDATA,#0C4h EN = 1; //SETB EN EN=0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD RS = 1; //SETB RS PDATA = c; //MOV PDATA,A EN = 1; //SETB EN EN = 0; //CLR EN Wait_LCD(); //LCALL WAIT_LCD }
|