Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.01 KB | None | 0 0
  1. /* USER CODE BEGIN 4 */
  2.  
  3. void State_Machine(TextLCDType lcd)
  4. {
  5. int counter = 0, timer = 1;
  6. static int triggered_cnt = 0;
  7. switch(state)
  8. {
  9. case 0: //DEACTIVATED STATE
  10. HAL_GPIO_WritePin(GPIOA, LED_GREEN_Pin, GPIO_PIN_SET);
  11. TextLCD_Printf(&lcd, "DISARMED");
  12. TextLCD_Cmd(&lcd, 0xc0);
  13. TextLCD_Printf(&lcd, "# to activate");
  14. TextLCD_Home(&lcd);
  15. if(Keypad_ScanKeyChar() == '#') Lock_state(lcd);
  16. break;
  17.  
  18. case 1: // ARMING STATE
  19. while(counter <=1)
  20. {
  21. HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
  22. TextLCD_Printf(&lcd, "ARMING");
  23. TextLCD_Cmd(&lcd, 0xc0);
  24. Active_Buzzer_on();
  25. TextLCD_Printf(&lcd, "%d, # deactivate", timer--);
  26. HAL_Delay(1000);
  27. if(Keypad_ScanKeyChar() == '#') Lock_state(lcd);
  28. if(state == 0)
  29. break;
  30.  
  31. HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_SET);
  32. TextLCD_Clear(&lcd);
  33. TextLCD_Home(&lcd);
  34. counter++;
  35. }
  36. Active_Buzzer_off();
  37. counter =0;
  38. if(state == 0)
  39. {
  40. HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
  41. state =0;
  42. }
  43.  
  44. else
  45. {
  46. HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
  47. state = 2;
  48. }
  49. break;
  50.  
  51. case 2: //ARMED STATE
  52. HAL_GPIO_WritePin(GPIOA, LED_RED_Pin, GPIO_PIN_SET);
  53. TextLCD_Printf(&lcd, "ARMED");
  54. TextLCD_Cmd(&lcd, 0xc0);
  55. TextLCD_Printf(&lcd, "# to deactivate");
  56. TextLCD_Home(&lcd);
  57. if(Keypad_ScanKeyChar() == '#') Lock_state(lcd);
  58. Read_PIR();
  59. break;
  60.  
  61. case 3: //ALARM TRIGGERED
  62. TextLCD_Printf(&lcd, "ALARM TRIGGERED!");
  63. TextLCD_Cmd(&lcd, 0xc0);
  64. TextLCD_Printf(&lcd, "# to deactivate");
  65. TextLCD_Home(&lcd);
  66. while(triggered_cnt <=2 )
  67. {
  68. if(Keypad_ScanKeyChar() == '#') Lock_state_master(lcd);
  69. HAL_Delay(1000);
  70. triggered_cnt++;//only works first time around... fix this mby function
  71. }
  72.  
  73. Passive_Buzzer_on();
  74. if(Keypad_ScanKeyChar() == '#') Lock_state_master(lcd);
  75. Passive_Buzzer_off();
  76. break;
  77. }
  78. }
  79.  
  80. //Buzzer on
  81. void Active_Buzzer_on()
  82. {
  83. HAL_GPIO_WritePin(GPIOA, ACTIVE_BUZZER_Pin, GPIO_PIN_SET);
  84. }
  85. //Buzzer off
  86. void Active_Buzzer_off()
  87. {
  88. HAL_GPIO_WritePin(GPIOA, ACTIVE_BUZZER_Pin, GPIO_PIN_RESET);
  89. }
  90. //Buzzer on
  91. void Passive_Buzzer_on()
  92. {
  93. __HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 5000);
  94. HAL_Delay(100);
  95. __HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 100000);
  96. }
  97. //Buzzer off
  98. void Passive_Buzzer_off()
  99. {
  100. __HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0);
  101. HAL_TIM_Base_Stop(&htim10);
  102. }
  103. //Read PIR value
  104. void Read_PIR()
  105. {
  106. int PIR_Value;
  107. PIR_Value = HAL_GPIO_ReadPin(GPIOA, PIR_INPUT_Pin);
  108. if(PIR_Value == 1)
  109. state = 3;
  110. }
  111. //This is when the alarm has been triggered. only master code will disarm this.
  112. void Lock_state_master(TextLCDType lcd)
  113. {
  114. int lock_state=0, complete=0, PinCode;
  115. TextLCD_Clear(&lcd);
  116. TextLCD_Printf(&lcd, "PIN:");
  117. while(complete==0)
  118. {
  119. Passive_Buzzer_on();
  120. PinCode = Keypad_ScanKeyNum();
  121. if(PinCode < 10)
  122. {
  123. switch(lock_state)
  124. {
  125. case 0:
  126. Input_combination[0] = PinCode;
  127. TextLCD_Printf(&lcd, "%d", Input_combination[0]);
  128. lock_state = lock_state + 1;
  129. HAL_Delay(500);
  130. break;
  131.  
  132. case 1:
  133. Input_combination[1] = PinCode;
  134. TextLCD_Printf(&lcd, "%d", Input_combination[1]);
  135. lock_state = lock_state + 1;
  136. HAL_Delay(500);
  137. break;
  138.  
  139. case 2:
  140. Input_combination[2] = PinCode;
  141. TextLCD_Printf(&lcd, "%d", Input_combination[2]);
  142. lock_state = lock_state + 1;
  143. HAL_Delay(500);
  144. break;
  145.  
  146. case 3:
  147. Input_combination[3] = PinCode;
  148. TextLCD_Printf(&lcd, "%d", Input_combination[3]);
  149. complete = 1;
  150. HAL_Delay(500);
  151. break;
  152. }
  153. }
  154. }
  155.  
  156. if(Input_combination[0] == Master_Code[0] && Input_combination[1] == Master_Code[1] && Input_combination[2] == Master_Code[2] && Input_combination[3] == Master_Code[3])
  157. {
  158. TextLCD_Printf(&lcd,"Master code!");
  159. HAL_GPIO_WritePin(GPIOA, LED_RED_Pin, GPIO_PIN_RESET);
  160. state = 0;
  161. }
  162. else
  163. TextLCD_Printf(&lcd, "Wrong Pin.");
  164.  
  165. TextLCD_Clear(&lcd);
  166. TextLCD_Home(&lcd);
  167. HAL_Delay(1000);
  168. }
  169. //This function is for the regular unlock code and mater code.
  170. void Lock_state(TextLCDType lcd)
  171. {
  172. int lock_state=0, complete=0, PinCode;
  173. TextLCD_Clear(&lcd);
  174. TextLCD_Printf(&lcd, "PIN:");
  175. while(complete==0)
  176. {
  177. PinCode = Keypad_ScanKeyNum();
  178. if(PinCode < 10)
  179. {
  180. switch(lock_state)
  181. {
  182. case 0:
  183. Input_combination[0] = PinCode;
  184. TextLCD_Printf(&lcd, "%d", Input_combination[0]);
  185. lock_state = lock_state + 1;
  186. HAL_Delay(500);
  187. break;
  188.  
  189. case 1:
  190. Input_combination[1] = PinCode;
  191. TextLCD_Printf(&lcd, "%d", Input_combination[1]);
  192. lock_state = lock_state + 1;
  193. HAL_Delay(500);
  194. break;
  195.  
  196. case 2:
  197. Input_combination[2] = PinCode;
  198. TextLCD_Printf(&lcd, "%d", Input_combination[2]);
  199. lock_state = lock_state + 1;
  200. HAL_Delay(500);
  201. break;
  202.  
  203. case 3:
  204. Input_combination[3] = PinCode;
  205. TextLCD_Printf(&lcd, "%d", Input_combination[3]);
  206. complete = 1;
  207. HAL_Delay(500);
  208. break;
  209. }
  210. }
  211. }
  212. TextLCD_Clear(&lcd);
  213.  
  214. if(Input_combination[0] == Lock_combination[0] && Input_combination[1] == Lock_combination[1] && Input_combination[2] == Lock_combination[2] && Input_combination[3] == Lock_combination[3])
  215. {
  216. TextLCD_Printf(&lcd,"Correct Pin!");
  217. if(state == 0)
  218. {
  219. state = 1;
  220. HAL_GPIO_WritePin(GPIOA, LED_GREEN_Pin, GPIO_PIN_RESET);
  221. }
  222. else
  223. {
  224. state = 0;
  225. HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
  226. }
  227. }
  228. else if(Input_combination[0] == Master_Code[0] && Input_combination[1] == Master_Code[1] && Input_combination[2] == Master_Code[2] && Input_combination[3] == Master_Code[3])
  229. {
  230. TextLCD_Printf(&lcd,"Master code!");
  231. if(state == 0)
  232. {
  233. state = 1;
  234. HAL_GPIO_WritePin(GPIOA, LED_GREEN_Pin, GPIO_PIN_RESET);
  235. }
  236. else
  237. {
  238. state = 0;
  239. HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
  240. }
  241.  
  242. }
  243. else
  244. TextLCD_Printf(&lcd, "Wrong Pin.");
  245.  
  246. HAL_Delay(1000);
  247. TextLCD_Clear(&lcd);
  248. }
  249. //Function to check which of the keys have been pressed. (CHARACTER ONLY)
  250. int Keypad_ScanKeyChar()
  251. {
  252. unsigned char Keypad_character = 'K';
  253. Keypad_SetColumn1();
  254. HAL_Delay(5);
  255.  
  256. if(Read_Keypad_Column1 && Read_Keypad_Row4)
  257. Keypad_character = 'A';
  258. else if(Read_Keypad_Column1 && Read_Keypad_Row3)
  259. Keypad_character = 'B';
  260. else if(Read_Keypad_Column1 && Read_Keypad_Row2)
  261. Keypad_character = 'C';
  262. else if(Read_Keypad_Column1 && Read_Keypad_Row1)
  263. Keypad_character = 'D';
  264.  
  265. HAL_Delay(5);
  266. Keypad_SetColumn2();
  267. HAL_Delay(5);
  268.  
  269. if(Read_Keypad_Column2 && Read_Keypad_Row1)
  270. Keypad_character = '#';
  271.  
  272. HAL_Delay(5);
  273. Keypad_SetColumn4();
  274. HAL_Delay(5);
  275.  
  276. if(Read_Keypad_Column3 && Read_Keypad_Row1)
  277. Keypad_character = '*';
  278.  
  279. return Keypad_character;
  280.  
  281. }
  282. //Function to check which of the keys have been pressed. (NUMBERS ONLY)
  283. int Keypad_ScanKeyNum()
  284. {
  285. uint16_t Keypad_number = 33;
  286. HAL_Delay(5);
  287. Keypad_SetColumn2();
  288. HAL_Delay(5);
  289.  
  290. if(Read_Keypad_Column2 && Read_Keypad_Row4)
  291. Keypad_number = 3;
  292. else if(Read_Keypad_Column2 && Read_Keypad_Row3)
  293. Keypad_number = 6;
  294. else if(Read_Keypad_Column2 && Read_Keypad_Row2)
  295. Keypad_number = 9;
  296.  
  297. HAL_Delay(5);
  298. Keypad_SetColumn3();
  299. HAL_Delay(5);
  300.  
  301. if(Read_Keypad_Column3 && Read_Keypad_Row4)
  302. Keypad_number = 2;
  303. else if(Read_Keypad_Column3 && Read_Keypad_Row3)
  304. Keypad_number = 5;
  305. else if(Read_Keypad_Column3 && Read_Keypad_Row2)
  306. Keypad_number = 8;
  307. else if(Read_Keypad_Column3 && Read_Keypad_Row1)
  308. Keypad_number = 0;
  309.  
  310. HAL_Delay(5);
  311. Keypad_SetColumn4();
  312. HAL_Delay(5);
  313.  
  314. if(Read_Keypad_Column4 && Read_Keypad_Row4)
  315. Keypad_number = 1;
  316. else if(Read_Keypad_Column4 && Read_Keypad_Row3)
  317. Keypad_number = 4;
  318. else if(Read_Keypad_Column4 && Read_Keypad_Row2)
  319. Keypad_number = 7;
  320.  
  321. HAL_Delay(5);
  322.  
  323. return Keypad_number;
  324.  
  325. }
  326. //Set column 1 low and the other high to see if a button on column 1 has been pressed or not.
  327. void Keypad_SetColumn1()
  328. {
  329. HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin, GPIO_PIN_SET);
  330. HAL_GPIO_WritePin(GPIOB, KEYPAD_2_Pin | KEYPAD_3_Pin | KEYPAD_4_Pin, GPIO_PIN_RESET);
  331. }
  332. //See comment above, this is column 2
  333. void Keypad_SetColumn2()
  334. {
  335. HAL_GPIO_WritePin(GPIOB, KEYPAD_2_Pin, GPIO_PIN_SET);
  336. HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin | KEYPAD_3_Pin | KEYPAD_4_Pin, GPIO_PIN_RESET);
  337. }
  338. //See comment above, this is column 3
  339. void Keypad_SetColumn3()
  340. {
  341. HAL_GPIO_WritePin(GPIOB, KEYPAD_3_Pin, GPIO_PIN_SET);
  342. HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin | KEYPAD_2_Pin | KEYPAD_4_Pin, GPIO_PIN_RESET);
  343. }
  344. //See comment above, this is column 4
  345. void Keypad_SetColumn4()
  346. {
  347. HAL_GPIO_WritePin(GPIOB, KEYPAD_4_Pin, GPIO_PIN_SET);
  348. HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin | KEYPAD_2_Pin | KEYPAD_3_Pin, GPIO_PIN_RESET);
  349. }
  350. //Serial UART numbers
  351. void UART_Transmit_num(int16_t num_value)
  352. {
  353. unsigned char transmit_num[sizeof(num_value)];
  354. sprintf(transmit_num, "%d", num_value);
  355. HAL_UART_Transmit(&huart2, transmit_num, strlen(transmit_num), 100);
  356. }
  357. //Serial UART character
  358. void UART_Transmit_string(char * message)
  359. {
  360. HAL_UART_Transmit(&huart2, message, strlen(message), 100);
  361. }
  362. /* USER CODE END 4 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement