Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Global Variables
- const unsigned char MSG0[20] = "HW05.c ";
- const unsigned char MSG1[20] = "UNLOCKED ";
- const unsigned char MSG2[20] = "LOCKED ";
- const unsigned char MSG3[20] = "Enter Code ";
- const unsigned char TABLE[4] = {1, 2, 4, 8};
- // Subroutine Declarations
- #include <pic18.h>
- // Subroutines
- #include "lcd_portd.c"
- unsigned char Keypad(void)
- {
- unsigned char RESULT, i;
- RESULT = 0xFF;
- TRISC = 0xF8;
- PORTC = 4; // scan column #1
- for (i=0; i<10; i++); // kill 10us to let voltages settle
- if (RC6) RESULT = 1;
- if (RC5) RESULT = 4;
- if (RC4) RESULT = 7;
- if (RC3) RESULT = 10;
- PORTC = 2; // scan column #2
- for (i=0; i<10; i++); // kill 10us to let voltages settle
- if (RC6) RESULT = 2;
- if (RC5) RESULT = 5;
- if (RC4) RESULT = 8;
- if (RC3) RESULT = 0;
- PORTC = 1; // scan column #3
- for (i=0; i<10; i++); // kill 10us to let voltages settle
- if (RC6) RESULT = 3;
- if (RC5) RESULT = 6;
- if (RC4) RESULT = 9;
- if (RC3) RESULT = 11;
- return(RESULT);
- }
- int GetNumber(void)
- {
- unsigned int RESULT;
- unsigned int KEY;
- RESULT = 0;
- KEY = 0;
- while(KEY != 10) {
- while(Keypad() == 0xFF);
- KEY = Keypad();
- while(Keypad() != 0xFF);
- if (KEY < 10) RESULT = (RESULT * 10) + KEY;
- }
- if (KEY == 11) RESULT = -RESULT;
- return(RESULT);
- }
- // Main Routine
- void main(void)
- {
- unsigned int i, j, check,W,X,Y,Z;
- unsigned int CODE=0, open = 50, STEP;
- unsigned int KEY = 1517;
- TRISA = 0;
- TRISB = 0;
- TRISD = 0;
- TRISE = 0;
- ADCON1 = 0x0F;
- LCD_Init(); // initialize the LCD
- LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]);
- PORTA = 0;
- PORTB = 0;
- STEP = 0;
- while(1) {
- check = 0;
- CODE = 0;
- CODE = GetNumber();
- LCD_Move(1,0);
- LCD_Out(CODE, 4, 0); // 1 decimal place, 5 digits
- Wait_ms(1000);
- if (CODE == KEY){
- LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG1[i]);
- while(STEP < open){
- STEP = STEP + 1;
- PORTB = TABLE[STEP % 4];
- LCD_Move(1,0);
- LCD_Out(STEP, 4, 0);
- Wait_ms(100);
- }
- Wait_ms(2000);
- while(STEP > 0){
- STEP = STEP - 1;
- PORTB = TABLE[STEP % 4];
- LCD_Move(1,0);
- LCD_Out(STEP, 4, 0);
- Wait_ms(100);
- }
- }
- else{
- LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG2[i]);
- Wait_ms(1000);
- }
- LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG3[i]);
- LCD_Move(1,0);
- LCD_Out(0, 4, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement