Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.37 KB | None | 0 0
  1. #include "tm4c123gh6pm.h" //Including the standard header for our chip
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. #define col 3
  7. #define row 4
  8. #define RS 0x40
  9. #define EN 0x80
  10. #define LCD_DATA GPIO_PORTB_DATA_R
  11. #define LCD_CTRL GPIO_PORTB_DATA_R
  12.  
  13. unsigned char ReadKeypad()
  14. {
  15. unsigned char colCount = 0;
  16. unsigned char rowCount = 0;
  17. while(1)
  18. {
  19. for(colCount = 0;colCount < col;colCount++)
  20. {
  21. GPIO_PORTE_DATA_R = 1<<colCount;
  22. for(rowCount = 0;rowCount < row;rowCount++)
  23. {
  24. if((GPIO_PORTA_DATA_R &(1<<(rowCount+2))) != 0)
  25. {
  26. msDelay(400);
  27. return ((rowCount*4)+colCount+1); //Returns the corresponding matrix value for this scenario
  28. }
  29. }
  30. }
  31. }
  32. }
  33. char GetKeyPressed(unsigned char value){
  34. char key;
  35. switch(value)
  36. {
  37. case 1:
  38. key = 1;
  39. break;
  40. case 2:
  41. key = 2;
  42. break;
  43. case 3:
  44. key = 3;
  45. break;
  46. case 4:
  47. key = -5; //These keys cannot be returned by a keypad press because of the algorithm we used to detect the keypress
  48. break;
  49. case 5:
  50. key = 4;
  51. break;
  52. case 6:
  53. key = 5;
  54. break;
  55. case 7:
  56. key = 6;
  57. break;
  58. case 8:
  59. key = -3; //These keys cannot be returned by a keypad press because of the algorithm we used to detect the keypress
  60. break;
  61. case 9:
  62. key = 7;
  63. break;
  64. case 10:
  65. key = 8;
  66. break;
  67. case 11:
  68. key = 9;
  69. break;
  70. case 12:
  71. key = -6; //These keys cannot be returned by a keypad press because of the algorithm we used to detect the keypress
  72. break;
  73. case 13:
  74. key = -6;
  75. break;
  76. case 14:
  77. key = 0;
  78. break;
  79. case 15:
  80. key = -13;
  81. break;
  82. }
  83. return key;
  84. }
  85.  
  86. void KeypadInit()
  87. {
  88. volatile unsigned long delay;
  89.  
  90. //Port E Init
  91. SYSCTL_RCGC2_R |= 0x00000010; //enable clock for Port E (columns)
  92. delay = SYSCTL_RCGC2_R;
  93. GPIO_PORTE_AMSEL_R = 0x00;
  94. GPIO_PORTE_PCTL_R = 0x00000000;
  95. GPIO_PORTE_DIR_R = 0xFF; //Port E as O/P
  96. GPIO_PORTE_AFSEL_R = 0x00;
  97. GPIO_PORTE_PUR_R = 0x00;
  98. GPIO_PORTE_DEN_R = 0xFF; //Digital Enable ON
  99.  
  100. //Port A Init
  101. SYSCTL_RCGC2_R |= 0x00000001; //enable clock for Port A (rows)
  102. delay = SYSCTL_RCGC2_R;
  103. GPIO_PORTA_AMSEL_R = 0x00;
  104. GPIO_PORTA_PCTL_R = 0x00000000;
  105. GPIO_PORTA_DIR_R = 0x00; //Port A as I/P
  106. GPIO_PORTA_AFSEL_R = 0x00;
  107. GPIO_PORTA_PDR_R = 0xFF;
  108. GPIO_PORTA_DEN_R = 0xFF;
  109. }
  110.  
  111. void msDelay(uint32_t t)
  112. /*Delay subroutine in millisecond*/
  113. {
  114. uint32_t i,j;
  115. for(i=0;i<t;i++)
  116. {
  117. for(j=0;j<2000;j++);
  118. }
  119. return;
  120. }
  121.  
  122. void portB_Int(void) //Used to initialize PortB for the LCD
  123. {
  124. SYSCTL_RCGC2_R |= 0x00000002; //activate clock for PortA
  125. msDelay(5);
  126. GPIO_PORTB_AMSEL_R &= ~0xFF; //disable analog on PA
  127. msDelay(20);
  128. GPIO_PORTB_PCTL_R &= ~0xFFFFFF00; //PCTL GPIO on PA7-PA2
  129. msDelay(20);
  130. GPIO_PORTB_DIR_R |= 0xFC; //PA7-PA2 output
  131. msDelay(20);
  132. GPIO_PORTB_AFSEL_R &= 0x03; //disable alt function on PA7-PA2
  133. msDelay(20);
  134. GPIO_PORTB_DEN_R |= 0xFC; //enable digital I/O on PA7-PA2
  135. msDelay(20);
  136. return;
  137. }
  138.  
  139. void LCD_CMD(uint32_t cmd)
  140. /*Used to write a command to the LCD. For sending command we used 4 bit mode of the LCD.
  141. The high 4 bits are sent first. For commands RS is set low. In order to capture the
  142. data E must be high and then low*/
  143. {
  144. uint8_t x;
  145. x = (cmd & 0xF0) >> 2; //shifting higher 4 bits to PA5-PA2
  146. LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
  147. LCD_DATA |= x; //sending higher 4 bits to PortA
  148. msDelay(1);
  149. LCD_CTRL &= ~RS; //seting RS command to low
  150. msDelay(1);
  151. LCD_CTRL |= EN; //raising enable
  152. msDelay(1);
  153. LCD_CTRL &= ~EN; //droping enable
  154. msDelay(2); //waiting for command to be captured
  155. x = (cmd & 0x0F) << 2; //shifting lower 4 bits to PA5-PA2
  156. LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
  157. LCD_DATA |= x; //sending lower 4 bits to PortA
  158. msDelay(1);
  159. LCD_CTRL |= EN; //raiseing enable
  160. msDelay(1);
  161. LCD_CTRL &= ~EN; //droping enable
  162. msDelay(2); //waiting for command to be captured
  163. return;
  164. }
  165.  
  166. void LCD_WRT(uint32_t cmd)
  167. /*Used to write data to the LCD. For sending data we used 4 bit mode of LCD.
  168. The high 4 bits are sent first. For data RS is set high. In order to capture the
  169. data E has to be toggled high then low*/
  170. {
  171. uint8_t x;
  172. x = (cmd & 0xF0) >> 2; //shifting higher 4 bits to PA5-PA2
  173. LCD_DATA &= ~0x3C; //clearing bits A5_A2
  174. LCD_DATA |= x; //sending high 4 bits to PortA
  175. msDelay(1);
  176. LCD_CTRL |= RS; //seting RS command to high
  177. msDelay(1);
  178. LCD_CTRL |= EN; //raisijng enable
  179. msDelay(1);
  180. LCD_CTRL &= ~EN; //droping enable
  181. msDelay(2); //waiting for command to be captured
  182. x = (cmd & 0x0F) << 2; //shifting lower 4 bits to PA5-PA2
  183. LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
  184. LCD_DATA |= x; //sending lower 4 bits to port A
  185. msDelay(1);
  186. LCD_CTRL |= EN; //raising enable
  187. msDelay(1);
  188. LCD_CTRL &= ~EN; //droping enable
  189. msDelay(2); //waiting for command to be captured
  190. return;
  191. }
  192.  
  193. void LCD_HIGH_4_BITS(uint32_t cmd)
  194. /*Used to write only the higher 4 bits of a command to the LCD as we dont want
  195. to send lower 4 bits*/
  196. {
  197. uint8_t x;
  198. x = (cmd & 0xF0) >> 2; //shifting higher 4 bits to PA5-PA2
  199. LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
  200. LCD_DATA |= x; //sending higher 4 bits to PortA
  201. msDelay(1);
  202. LCD_CTRL &= ~RS; //reseting RS command to low
  203. msDelay(1);
  204. LCD_CTRL |= EN; //raising enable
  205. msDelay(1);
  206. LCD_CTRL &= ~EN; //droping enable
  207. msDelay(2); //waiting for command to be captured
  208. return;
  209. }
  210.  
  211. void LCD_Int(void)
  212. /*Setting up the LCD*/
  213. {
  214. LCD_CMD(0x30);
  215. LCD_CMD(0x30);
  216. LCD_CMD(0x30); //we must send three times to turn on the LCD
  217. LCD_HIGH_4_BITS(0x20); //seting The LCD in 4 bit mode
  218. LCD_CMD(0x28); //seting on 4 bit data length, 2 display lines and 5x8 dots
  219. LCD_CMD(0x01); //clearing display
  220. LCD_CMD(0x0C); //turning on display, cursor and blinking cursor
  221. LCD_CMD(0x14); //seting direction
  222. }
  223.  
  224. void LCD_CLR(void)
  225. /*Used to clear the LCD and return the cursor to home position*/
  226. {
  227. LCD_CMD(0x01); //clears display
  228. LCD_CMD(0x02); //returns cursor home
  229. return;
  230. }
  231.  
  232. void LCD_PRNT(char ch) //Used to print a single character to the LCD.
  233. {
  234. LCD_WRT(ch);
  235. return;
  236. }
  237.  
  238. void LCD_PRNTstring(char *line)
  239. /*Used to print a string to the LCD. It uses strlen so a terminator doesn't have to
  240. be used*/
  241. {
  242. uint8_t count = 0;
  243. while(count != strlen(line)) //looping through the string until strlen is reached
  244. {
  245. LCD_WRT(line[count]);
  246. count++;
  247. }
  248. return;
  249. }
  250.  
  251. void main(void){
  252. portB_Int();
  253. LCD_Int();
  254. LCD_CLR();
  255. KeypadInit();
  256.  
  257. unsigned char aKey = 0;
  258. unsigned char aKey1 = 0;
  259. unsigned char aKey2 = 0;
  260. unsigned char aKey3 = 0;
  261.  
  262. unsigned char pass = 1;
  263. unsigned char pass1 = 2;
  264. unsigned char pass2 = 3;
  265. unsigned char pass3 = 4;
  266.  
  267. int menuStage = 1;
  268. int flag = 0;
  269. int flag1 = 0;
  270.  
  271. while(1)
  272. {
  273. if(menuStage==1)
  274. {
  275.  
  276. LCD_CLR();
  277. LCD_PRNTstring("Enter the password: ");
  278. aKey=(GetKeyPressed(ReadKeypad()));
  279. LCD_PRNT(42);
  280. aKey1=(GetKeyPressed(ReadKeypad()));
  281. LCD_PRNT(42);
  282. aKey2=(GetKeyPressed(ReadKeypad()));
  283. LCD_PRNT(42);
  284. aKey3=(GetKeyPressed(ReadKeypad()));
  285. LCD_PRNT(42);
  286. //LCD_PRNT(aKey2+48);
  287. msDelay(500);
  288.  
  289. if(aKey==pass && aKey1==pass1 && aKey2==pass2 && aKey3==pass3)
  290. {
  291. menuStage=2;
  292. }
  293. else
  294. {
  295.  
  296. menuStage=1;
  297. LCD_CLR();
  298. LCD_PRNTstring("Incorrect Password! ");
  299. LCD_PRNTstring("Please try again. ");
  300. msDelay(3000);
  301.  
  302. }
  303.  
  304. }
  305. else if(menuStage==2)
  306. {
  307.  
  308. LCD_CLR();
  309. LCD_PRNTstring("1. Motor Control ");
  310. LCD_PRNTstring("3. Change Password ");
  311. LCD_PRNTstring("2. Bluetooth Control");
  312. LCD_PRNTstring("4. More options ");
  313. char m2;
  314. m2=(GetKeyPressed(ReadKeypad()));
  315. //msDelay(250);
  316.  
  317. if(m2==1)
  318. {
  319. LCD_CLR();
  320. LCD_PRNTstring("1. Motor ON/OFF ");
  321. //LCD_PRNTstring(" ");
  322. LCD_PRNTstring("2. Back to main menu");
  323. char m2a;
  324. m2a=(GetKeyPressed(ReadKeypad()));
  325. //msDelay(500);
  326. if(m2a==1)
  327. {
  328. LCD_CLR();
  329. if(flag==0)
  330. {
  331.  
  332. LCD_PRNTstring(" ");
  333. LCD_PRNTstring(" ");
  334. LCD_PRNTstring(" Motor ON ");
  335. msDelay(1000);
  336. flag=1;
  337.  
  338. }
  339. else if(flag==1)
  340. {
  341.  
  342. LCD_PRNTstring(" ");
  343. LCD_PRNTstring(" ");
  344. LCD_PRNTstring(" Motor OFF ");
  345. msDelay(1000);
  346. flag=0;
  347.  
  348. }
  349. }
  350. else if(m2a==2)
  351. {
  352.  
  353. LCD_CLR();
  354. //msDelay(250);
  355.  
  356. }
  357. }
  358. else if(m2==2)
  359. {
  360.  
  361. LCD_CLR();
  362. LCD_PRNTstring("1. Bluetooth ON/OFF ");
  363. //LCD_PRNTstring(" ");
  364. LCD_PRNTstring("2. Back to main menu");
  365. char m2b;
  366. m2b=(GetKeyPressed(ReadKeypad()));
  367. //msDelay(500);
  368. if(m2b==1)
  369. {
  370.  
  371. LCD_CLR();
  372. if(flag1==0)
  373. {
  374.  
  375. LCD_CLR();
  376. LCD_PRNTstring(" ");
  377. LCD_PRNTstring(" ");
  378. LCD_PRNTstring(" Bluetooth ON ");
  379. msDelay(1000);
  380. flag1=1;
  381.  
  382. }
  383. else if(flag1==1)
  384. {
  385.  
  386. LCD_CLR();
  387. LCD_PRNTstring(" ");
  388. LCD_PRNTstring(" ");
  389. LCD_PRNTstring(" Bluetooth OFF ");
  390. msDelay(1000);
  391. flag1=0;
  392.  
  393. }
  394. }
  395. else if(m2b==2)
  396. {
  397.  
  398. LCD_CLR();
  399. //msDelay(250);
  400.  
  401. }
  402. }
  403. else if(m2==3)
  404. {
  405. LCD_CLR();
  406. LCD_PRNTstring("Enter old password: ");
  407.  
  408. char oldKey=pass;
  409. char oldKey1=pass1;
  410. char oldKey2=pass2;
  411. char oldKey3=pass3;
  412.  
  413. char temp;
  414. char temp1;
  415. char temp2;
  416. char temp3;
  417.  
  418. temp=(GetKeyPressed(ReadKeypad()));
  419. LCD_PRNT(temp+48);
  420. temp1=(GetKeyPressed(ReadKeypad()));
  421. LCD_PRNT(temp1+48);
  422. temp2=(GetKeyPressed(ReadKeypad()));
  423. LCD_PRNT(temp2+48);
  424. temp3=(GetKeyPressed(ReadKeypad()));
  425. LCD_PRNT(temp3+48);
  426.  
  427. msDelay(750);
  428.  
  429. if(temp==oldKey && temp1==oldKey1 && temp2==oldKey2 && temp3==oldKey3)
  430. {
  431.  
  432. LCD_CLR();
  433.  
  434. LCD_PRNTstring("Enter new password: ");
  435.  
  436. temp=(GetKeyPressed(ReadKeypad()));
  437. LCD_PRNT(temp+48);
  438. temp1=(GetKeyPressed(ReadKeypad()));
  439. LCD_PRNT(temp1+48);
  440. temp2=(GetKeyPressed(ReadKeypad()));
  441. LCD_PRNT(temp2+48);
  442. temp3=(GetKeyPressed(ReadKeypad()));
  443. LCD_PRNT(temp3+48);
  444. msDelay(750);
  445.  
  446. pass=temp;
  447. pass1=temp1;
  448. pass2=temp2;
  449. pass3=temp3;
  450.  
  451. LCD_CLR();
  452. LCD_PRNTstring(" ");
  453. LCD_PRNTstring(" ");
  454. LCD_PRNTstring(" New password set! ");
  455. msDelay(1000);
  456. }
  457. else
  458. {
  459. LCD_CLR();
  460. LCD_PRNTstring(" ");
  461. LCD_PRNTstring(" ");
  462. LCD_PRNTstring(" Invalid password! ");
  463. msDelay(1000);
  464. }
  465. }
  466. else if(m2==4)
  467. {
  468. menuStage=3;
  469. }
  470. }
  471. else if(menuStage==3)
  472. {
  473. LCD_CLR();
  474. LCD_PRNTstring("1. Lock the screen ");
  475. LCD_PRNTstring(" ");
  476. LCD_PRNTstring("2. Previous screen ");
  477. char m3;
  478. m3=(GetKeyPressed(ReadKeypad()));
  479. //msDelay(500);
  480. if(m3==1)
  481. {
  482. menuStage=1;
  483. }
  484. else if(m3==2)
  485. {
  486. menuStage=2;
  487. }
  488. }
  489. }
  490. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement