#include <pic.h>
#include <htc.h> //required to
interface with delay routines
__CONFIG(0x3F32);
#define BCD PORTA
#define LCD_RS
RD4 // Set Port RD4
sebagai sambungan ke RS (Register Select) di LCD
#define LCD_EN RD5 // Set Port RD5 sebagai sambungan ke EN
(Enable) di LCD
#define LCD_DATA PORTD // Set PortD sebagai data ( tapi dalam
kes ini hanya 4 bit DATA
// digunakan ( Pin
RD0-RD3 disambung kpd pin D4-D7 di LCD)
#define LCD_PULSE() (LCD_EN = 1, LCD_EN = 0)
#define _XTAL_FREQ 20000000 //this
definition is required to calibrate __delay_us() and __delay_ms()
void pic_init(void);
void lcd_init(void);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_goto(unsigned char pos); //,
unsigned char row)
void lcd_string(const char *s);
int read_keypad(void);
void lcd_number(int no, char base, char
digit);
main(void)
{
char
count=0,data,pass[5],password[5]={"84214"}; //(sila tukar password
anda disini)
int i;
int j=0;
pic_init(); //initialize
PIC
lcd_init(); //initialize
LCD
__delay_ms(1000);
//__delay_ms(1000);
for(;;){
lcd_goto(0x0);
lcd_string("
Enter Password");
while(RE0!=1)
RE0=0;
if (BCD == 0b1010)
break;
data=read_data();
if((data>=1)&(data
<=10)){
pass[count]=data%10+0x30;
lcd_goto(0x44+count);
lcd_string("*");
count++;}
if(count==5){
count=0;
for(i=0;i<=4;i++){
if(pass[i]==password[i])
count++;}
lcd_goto(0x40);
if(count==5){
lcd_string(" Welcome");
RE1=1;
__delay_ms(5000);
RE1=0;
}
else
lcd_string(" Wrong password");
count=0;
__delay_ms(500);
lcd_goto(0x40);
lcd_string(" ");}
}
}
void pic_init(void)
{
TRISA=0b00001111;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000001;
ADCON1=0b00000110; //configures the functions of the
port pins
PORTA=0b00000000;
PORTB=0b00000000;
PORTC=0b00000000;
PORTD=0b00000000;
PORTE=0b00000000;
}
int read_data(void)
{int i=0;
if (BCD == 0b0001) i=i+1; // 1
if (BCD == 0b0010) i=i+2; // 2
if (BCD == 0b0011) i=i+3; // 3
if (BCD == 0b0100) i=i+4; // 4
if (BCD == 0b0101) i=i+5; // 5
if (BCD == 0b0110) i=i+6; // 6
if (BCD == 0b0111) i=i+7; // 7
if (BCD == 0b1000) i=i+8; // 8
if (BCD == 0b1001) i=i+9; // 9
if (BCD == 0b1010) i=i+11; // *
if (BCD == 0b1010) i=i+10; // 0
if (BCD == 0b1100) i=i+12; // #
if(i>0) __delay_ms(300);
return i;}
char RS, EN, Temp;
/* write a byte to the LCD in 4 bit mode */
void lcd_write(unsigned char c)
{
RS
= LCD_RS << 4; //
LCD_RS = RD4
EN
= LCD_EN << 5; // LCD_EN
= RD5
Temp
= RS | EN;
LCD_DATA
= ( ( c >> 4 ) | Temp );
LCD_PULSE();
LCD_DATA
= (( c & 0x0F ) | Temp );
LCD_PULSE();
__delay_us(40);
}
/* Clear
and home the LCD */
void lcd_clear(void)
{
LCD_RS
= 0;
lcd_write(0x1);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void lcd_string(const char *s)
{
LCD_RS
= 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void lcd_putch(char c)
{
LCD_RS
= 1; // write characters
lcd_write(
c );
}
/* Go to the specified position */
void lcd_goto(unsigned char pos)
{
LCD_RS
= 0;
lcd_write(0x80+pos);
}
/* go to the specified position
void lcd_goto(unsigned char pos, unsigned
char row)
{
LCD_RS=0;
switch(row){
case 0:{lcd_write(0x80+pos);break;}
case 1:{lcd_write(0xC0+pos);break;}
case 2:{lcd_write(0x94+pos);break;}
case 3:{lcd_write(0xD4+pos);break;}}
LCD_RS=1;
} */
/*
initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char
init_value;
init_value
= 0x3;
TRISD=0;
LCD_RS
= 0;
LCD_EN
= 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_PULSE();
__delay_ms(5);
LCD_PULSE();
__delay_us(200);
LCD_PULSE();
__delay_us(200);
LCD_DATA
= 2; // Four bit mode
LCD_PULSE();
lcd_write(0x28); // Set interface length
lcd_write(0xC); // Display On
lcd_clear(); // Clear screen
lcd_write(0x6);
// Set entry Mode
}
/* write a number */
void lcd_number(int no, char base, char
digit)
{char i,j,di[10];
for(i=0;i<=9;i++) di[i]=0;
i=0;
do{
di[i]=no%base;
no=no/base;
i=i+1;}
while(no!=0);
for(j=digit;j>0;j--){
if(di[j-1]<=9)
lcd_write(di[j-1]+'0');
else
lcd_write(di[j-1]-10+'A');}
}
No comments:
Post a Comment