Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. #define F_CPU 32000000UL //!< System clock is 32 MHz
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6. #include "clock.h"
  7. #include "csrc/ucg.h"
  8. #include "ucglib_xmega_hal.h"
  9.  
  10. #define STEP 200
  11. #define DEBOUNCE_PERIOD_MS 10
  12. #define LOCK_PERIOD_MS 200
  13.  
  14. ucg_t ucg; //!< ucg is a necessary global structure for ucg_lib
  15.  
  16. int x, y, w, v;
  17.  
  18. int button_pressed(PORT_t * p, uint8_t bit) {
  19. if (bit_is_clear(p -> IN, bit)) {
  20. _delay_ms(DEBOUNCE_PERIOD_MS);
  21. while (bit_is_clear(p -> IN, bit));
  22. return 1;
  23. }
  24. return 0;
  25. }
  26.  
  27. void cijfer1(void) {
  28. int z = 0;
  29. while (z == 0) {
  30. if (button_pressed( & PORTA, PIN0_bp)) {
  31. x = 1;
  32. z = 1;
  33. }
  34. if (button_pressed( & PORTA, PIN1_bp)) {
  35. x = 2;
  36. z = 1;
  37. }
  38. if (button_pressed( & PORTA, PIN2_bp)) {
  39. x = 3;
  40. z = 1;
  41. }
  42. if (button_pressed( & PORTA, PIN3_bp)) {
  43. x = 4;
  44. z = 1;
  45. }
  46. if (button_pressed( & PORTA, PIN4_bp)) {
  47. x = 5;
  48. z = 1;
  49. }
  50. if (button_pressed( & PORTA, PIN5_bp)) {
  51. x = 6;
  52. z = 1;
  53. }
  54. if (button_pressed( & PORTA, PIN6_bp)) {
  55. x = 7;
  56. z = 1;
  57. }
  58. if (button_pressed( & PORTA, PIN7_bp)) {
  59. x = 8;
  60. z = 1;
  61. }
  62. if (button_pressed( & PORTB, PIN0_bp)) {
  63. x = 9;
  64. z = 1;
  65. }
  66. if (button_pressed( & PORTB, PIN1_bp)) {
  67. x = 0;
  68. z = 1;
  69. }
  70.  
  71. }
  72. return;
  73. }
  74. //Deze functie is bedoeld om de eerste cijfer van de code een waarde te geven.
  75. void cijfer2(void) {
  76. int z = 0;
  77. while (z == 0) {
  78. if (button_pressed( & PORTA, PIN0_bp)) {
  79. y = 1;
  80. z = 1;
  81. }
  82. if (button_pressed( & PORTA, PIN1_bp)) {
  83. y = 2;
  84. z = 1;
  85. }
  86. if (button_pressed( & PORTA, PIN2_bp)) {
  87. y = 3;
  88. z = 1;
  89. }
  90. if (button_pressed( & PORTA, PIN3_bp)) {
  91. y = 4;
  92. z = 1;
  93. }
  94. if (button_pressed( & PORTA, PIN4_bp)) {
  95. y = 5;
  96. z = 1;
  97. }
  98. if (button_pressed( & PORTA, PIN5_bp)) {
  99. y = 6;
  100. z = 1;
  101. }
  102. if (button_pressed( & PORTA, PIN6_bp)) {
  103. y = 7;
  104. z = 1;
  105. }
  106. if (button_pressed( & PORTA, PIN7_bp)) {
  107. y = 8;
  108. z = 1;
  109. }
  110. if (button_pressed( & PORTB, PIN0_bp)) {
  111. y = 9;
  112. z = 1;
  113. }
  114. if (button_pressed( & PORTB, PIN1_bp)) {
  115. y = 0;
  116. z = 1;
  117. }
  118. }
  119. return;
  120. }
  121. //Deze functie is bedoeld om de tweede cijfer van de code een waarde te geven.
  122. void cijfer3(void) {
  123. int z = 0;
  124. while (z == 0) {
  125. if (button_pressed( & PORTA, PIN0_bp)) {
  126. w = 1;
  127. z = 1;
  128. }
  129. if (button_pressed( & PORTA, PIN1_bp)) {
  130. w = 2;
  131. z = 1;
  132. }
  133. if (button_pressed( & PORTA, PIN2_bp)) {
  134. w = 3;
  135. z = 1;
  136. }
  137. if (button_pressed( & PORTA, PIN3_bp)) {
  138. w = 4;
  139. z = 1;
  140. }
  141. if (button_pressed( & PORTA, PIN4_bp)) {
  142. w = 5;
  143. z = 1;
  144. }
  145. if (button_pressed( & PORTA, PIN5_bp)) {
  146. w = 6;
  147. z = 1;
  148. }
  149. if (button_pressed( & PORTA, PIN6_bp)) {
  150. w = 7;
  151. z = 1;
  152. }
  153. if (button_pressed( & PORTA, PIN7_bp)) {
  154. w = 8;
  155. z = 1;
  156. }
  157. if (button_pressed( & PORTB, PIN0_bp)) {
  158. w = 9;
  159. z = 1;
  160. }
  161. if (button_pressed( & PORTB, PIN1_bp)) {
  162. w = 0;
  163. z = 1;
  164. }
  165. }
  166. return;
  167. }
  168. //Deze functie is bedoeld om de derde cijfer van de code een waarde te geven.
  169. void cijfer4(void) {
  170. int z = 0;
  171. while (z == 0) {
  172. if (button_pressed( & PORTA, PIN0_bp)) {
  173. v = 1;
  174. z = 1;
  175. }
  176. if (button_pressed( & PORTA, PIN1_bp)) {
  177. v = 2;
  178. z = 1;
  179. }
  180. if (button_pressed( & PORTA, PIN2_bp)) {
  181. v = 3;
  182. z = 1;
  183. }
  184. if (button_pressed( & PORTA, PIN3_bp)) {
  185. v = 4;
  186. z = 1;
  187. }
  188. if (button_pressed( & PORTA, PIN4_bp)) {
  189. v = 5;
  190. z = 1;
  191. }
  192. if (button_pressed( & PORTA, PIN5_bp)) {
  193. v = 6;
  194. z = 1;
  195. }
  196. if (button_pressed( & PORTA, PIN6_bp)) {
  197. v = 7;
  198. z = 1;
  199. }
  200. if (button_pressed( & PORTA, PIN7_bp)) {
  201. v = 8;
  202. z = 1;
  203. }
  204. if (button_pressed( & PORTB, PIN0_bp)) {
  205. v = 9;
  206. z = 1;
  207. }
  208. if (button_pressed( & PORTB, PIN1_bp)) {
  209. v = 0;
  210. z = 1;
  211. }
  212. }
  213. return;
  214. }
  215. //Deze functie is bedoeld om de vierde cijfer van de code een waarde te geven.
  216.  
  217. void beginscherm(void) {
  218. ucg_SetColor( & ucg, 0, 255, 0, 255);
  219. ucg_SetPrintDir( & ucg, 0);
  220. ucg_SetPrintPos( & ucg, 2, 60);
  221. ucg_Print( & ucg, "Voer uw ");
  222. ucg_SetPrintPos( & ucg, 2, 75);
  223. ucg_Print( & ucg, "toegangscode in:");
  224. }
  225. void varwaarde(void) {
  226. cijfer1();
  227. ucg_SetPrintPos( & ucg, 58, 90);
  228. ucg_Print( & ucg, "%d", x);
  229. _delay_ms(200);
  230. ucg_SetPrintPos( & ucg, 58, 90);
  231. ucg_Print( & ucg, "*");
  232. cijfer2();
  233. ucg_SetPrintPos( & ucg, 66, 90);
  234. ucg_Print( & ucg, "%d", y);
  235. _delay_ms(200);
  236. ucg_SetPrintPos( & ucg, 66, 90);
  237. ucg_Print( & ucg, "*");
  238. cijfer3();
  239. ucg_SetPrintPos( & ucg, 74, 90);
  240. ucg_Print( & ucg, "%d", w);
  241. _delay_ms(200);
  242. ucg_SetPrintPos( & ucg, 74, 90);
  243. ucg_Print( & ucg, "*");
  244. cijfer4();
  245. ucg_SetPrintPos( & ucg, 82, 90);
  246. ucg_Print( & ucg, "%d", v);
  247. _delay_ms(200);
  248. ucg_SetPrintPos( & ucg, 82, 90);
  249. ucg_Print( & ucg, "*");
  250. }
  251. void kleur_zwart(void) {
  252. ucg_SetPrintDir( & ucg, 0);
  253. ucg_SetColor( & ucg, 0, 0, 0, 0);
  254. }
  255. ////Deze functie is bedoeld om ruimte te maken voor nieuw tekst door het oude tekst zwart te maken.
  256. void toegangscode(void) {
  257. if (x == 9 && y == 9 && w == 9 && v == 9) {
  258. kleur_zwart(); // Tekst zwart maken om ruimte te maken voor "welkom"
  259. ucg_SetPrintPos( & ucg, 2, 60);
  260. ucg_Print( & ucg, "Voer uw ");
  261. ucg_SetPrintPos( & ucg, 2, 75);
  262. ucg_Print( & ucg, "toegangscode in:");
  263. ucg_SetPrintPos( & ucg, 58, 90);
  264. ucg_Print( & ucg, "*");
  265. ucg_SetPrintPos( & ucg, 66, 90);
  266. ucg_Print( & ucg, "*");
  267. ucg_SetPrintPos( & ucg, 74, 90);
  268. ucg_Print( & ucg, "*");
  269. ucg_SetPrintPos( & ucg, 82, 90);
  270. ucg_Print( & ucg, "*");
  271. ucg_SetColor( & ucg, 0, 0, 255, 0);
  272. ucg_SetPrintPos( & ucg, 55, 70);
  273. ucg_Print( & ucg, "WELKOM ");
  274.  
  275. TCD0.CCA <= (1500 - STEP);
  276. TCD0.CCABUF = TCD0.CCA + STEP; // Servo 90graden linksom
  277. _delay_ms(4000);
  278. TCD0.CCA >= (1100 + STEP);
  279. TCD0.CCABUF = TCD0.CCA - STEP; // Servo 90graden rechtsom
  280. kleur_zwart();
  281. ucg_SetPrintPos( & ucg, 55, 70);
  282. ucg_Print( & ucg, "Welkom ");
  283. } else {
  284. kleur_zwart(); // Tekst zwart maken om ruimte te maken voor "welkom"
  285. ucg_SetPrintPos( & ucg, 2, 60);
  286. ucg_Print( & ucg, "Voer uw ");
  287. ucg_SetPrintPos( & ucg, 2, 75);
  288. ucg_Print( & ucg, "toegangscode in:");
  289. ucg_SetPrintPos( & ucg, 58, 90);
  290. ucg_Print( & ucg, "*");
  291. ucg_SetPrintPos( & ucg, 66, 90);
  292. ucg_Print( & ucg, "*");
  293. ucg_SetPrintPos( & ucg, 74, 90);
  294. ucg_Print( & ucg, "*");
  295. ucg_SetPrintPos( & ucg, 82, 90);
  296. ucg_Print( & ucg, "*");
  297. ucg_SetColor( & ucg, 0, 255, 0, 0);
  298. ucg_SetPrintPos( & ucg, 10, 70);
  299. ucg_Print( & ucg, "TOEGANG GEWEIGERD");
  300. _delay_ms(1500);
  301. kleur_zwart();
  302. ucg_SetPrintPos( & ucg, 10, 70);
  303. ucg_Print( & ucg, "TOEGANG GEWEIGERD");
  304. }
  305. }
  306. //Deze functie is bedoeld om onderscheid te maken tussen de juiste en foute toegangscode.
  307. int main(void) {
  308. x,y,w,v = 0;
  309. uint8_t a;
  310.  
  311. init_clock();
  312.  
  313. ucg_Init( & ucg, ucg_dev_st7735_18x128x160, ucg_ext_st7735_18, (int( * )(struct _ucg_t * , int, unsigned int, unsigned char * )) ucg_comm_xmega);
  314. ucg_SetFontMode( & ucg, UCG_FONT_MODE_TRANSPARENT);
  315. ucg_ClearScreen( & ucg);
  316. ucg_SetFont( & ucg, ucg_font_8x13_tr);
  317. ucg_SetColor( & ucg, 255, 0, 0, 0);
  318. ucg_SetRotate90( & ucg);
  319.  
  320. ucg_SetFontMode( & ucg, UCG_FONT_MODE_SOLID); // overwrite background
  321. ucg_SetFont( & ucg, ucg_font_8x13_mr); // proportional font
  322. ucg_SetPrintPos( & ucg, 2, 80);
  323. a = 'A';
  324.  
  325. PORTA.DIRCLR = PIN0_bm | PIN1_bm | PIN2_bm | PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm; // input pin switch
  326. PORTB.DIRCLR = PIN0_bm | PIN1_bm;
  327.  
  328. PORTA.PIN0CTRL = PORT_OPC_PULLUP_gc; // enable pull up
  329. PORTA.PIN1CTRL = PORT_OPC_PULLUP_gc;
  330. PORTA.PIN2CTRL = PORT_OPC_PULLUP_gc;
  331. PORTA.PIN3CTRL = PORT_OPC_PULLUP_gc;
  332. PORTA.PIN4CTRL = PORT_OPC_PULLUP_gc;
  333. PORTA.PIN5CTRL = PORT_OPC_PULLUP_gc;
  334. PORTA.PIN6CTRL = PORT_OPC_PULLUP_gc;
  335. PORTA.PIN7CTRL = PORT_OPC_PULLUP_gc;
  336. PORTB.PIN0CTRL = PORT_OPC_PULLUP_gc;
  337. PORTB.PIN1CTRL = PORT_OPC_PULLUP_gc;
  338. PORTB.PIN2CTRL = PORT_OPC_PULLUP_gc;
  339.  
  340. PORTD.OUTSET = PIN0_bm;
  341. PORTD.DIRSET = PIN0_bm;
  342. TCD0.CTRLB = TC0_CCAEN_bm | TC_WGMODE_DSBOTH_gc;
  343. TCD0.CTRLA = TC_CLKSEL_DIV64_gc;
  344. TCD0.PER = 10000;
  345. TCD0.CCA = 345;
  346.  
  347. while (1) {
  348. beginscherm();
  349. varwaarde();
  350. toegangscode();
  351. }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement