Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. // Global Variables
  2.  
  3. const unsigned char MSG0[20] = "HW05.c ";
  4. const unsigned char MSG1[20] = "UNLOCKED ";
  5. const unsigned char MSG2[20] = "LOCKED ";
  6. const unsigned char MSG3[20] = "Enter Code ";
  7. const unsigned char TABLE[4] = {1, 2, 4, 8};
  8. // Subroutine Declarations
  9. #include <pic18.h>
  10.  
  11. // Subroutines
  12. #include "lcd_portd.c"
  13.  
  14. unsigned char Keypad(void)
  15. {
  16. unsigned char RESULT, i;
  17. RESULT = 0xFF;
  18. TRISC = 0xF8;
  19. PORTC = 4; // scan column #1
  20. for (i=0; i<10; i++); // kill 10us to let voltages settle
  21. if (RC6) RESULT = 1;
  22. if (RC5) RESULT = 4;
  23. if (RC4) RESULT = 7;
  24. if (RC3) RESULT = 10;
  25. PORTC = 2; // scan column #2
  26. for (i=0; i<10; i++); // kill 10us to let voltages settle
  27. if (RC6) RESULT = 2;
  28. if (RC5) RESULT = 5;
  29. if (RC4) RESULT = 8;
  30. if (RC3) RESULT = 0;
  31. PORTC = 1; // scan column #3
  32. for (i=0; i<10; i++); // kill 10us to let voltages settle
  33. if (RC6) RESULT = 3;
  34. if (RC5) RESULT = 6;
  35. if (RC4) RESULT = 9;
  36. if (RC3) RESULT = 11;
  37. return(RESULT);
  38. }
  39.  
  40. int GetNumber(void)
  41. {
  42. unsigned int RESULT;
  43. unsigned int KEY;
  44. RESULT = 0;
  45. KEY = 0;
  46. while(KEY != 10) {
  47. while(Keypad() == 0xFF);
  48. KEY = Keypad();
  49. while(Keypad() != 0xFF);
  50. if (KEY < 10) RESULT = (RESULT * 10) + KEY;
  51. }
  52. if (KEY == 11) RESULT = -RESULT;
  53. return(RESULT);
  54. }
  55. // Main Routine
  56.  
  57. void main(void)
  58. {
  59. unsigned int i, j, check,W,X,Y,Z;
  60. unsigned int CODE=0, open = 50, STEP;
  61. unsigned int KEY = 1517;
  62.  
  63. TRISA = 0;
  64. TRISB = 0;
  65. TRISD = 0;
  66. TRISE = 0;
  67. ADCON1 = 0x0F;
  68.  
  69.  
  70. LCD_Init(); // initialize the LCD
  71.  
  72. LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]);
  73.  
  74. PORTA = 0;
  75. PORTB = 0;
  76. STEP = 0;
  77. while(1) {
  78.  
  79. check = 0;
  80. CODE = 0;
  81. CODE = GetNumber();
  82. LCD_Move(1,0);
  83. LCD_Out(CODE, 4, 0); // 1 decimal place, 5 digits
  84. Wait_ms(1000);
  85. if (CODE == KEY){
  86. LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG1[i]);
  87. while(STEP < open){
  88. STEP = STEP + 1;
  89. PORTB = TABLE[STEP % 4];
  90. LCD_Move(1,0);
  91. LCD_Out(STEP, 4, 0);
  92. Wait_ms(100);
  93. }
  94. Wait_ms(2000);
  95. while(STEP > 0){
  96. STEP = STEP - 1;
  97. PORTB = TABLE[STEP % 4];
  98. LCD_Move(1,0);
  99. LCD_Out(STEP, 4, 0);
  100. Wait_ms(100);
  101. }
  102. }
  103. else{
  104. LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG2[i]);
  105. Wait_ms(1000);
  106. }
  107. LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG3[i]);
  108. LCD_Move(1,0);
  109. LCD_Out(0, 4, 0);
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement