Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. /*
  2. Example program for 7segment display with keypad.
  3.  
  4. Vypisuje na LCD kod stisknute klavesy.
  5. Na 7segmentovem displeji zobrazuje cisla od 0 do 9 a postupne prepina
  6. vsechny displeje.
  7.  
  8. */
  9.  
  10. #include "MKL25Z4.h"
  11. #include "drv_systick.h"
  12. #include "drv_lcd.h"
  13. #include "7segkeypad.h"
  14. #include <stdio.h>
  15. #include <stdbool.h>
  16.  
  17. void orderOfCounter();
  18. void printToDisplay(int num, int num2, int operation);
  19. void printToLCD(int num1, int num2, int operation);
  20. int * SmurfMyNumber(int number);
  21.  
  22. int outputToSegmentsNum[4] = { 0 };
  23.  
  24. int outputToSegmentsNum2[4] = { 0 };
  25.  
  26. int outputToSegmentsResult[4] = { 0 };
  27.  
  28. int storeForSegments[3];
  29.  
  30. int main(void) {
  31. SEGKEYPAD_InitializeDisplay();
  32. SEGKEYPAD_InitializeKeyboard();
  33. SYSTICK_initialize();
  34.  
  35. LCD_initialize();
  36. LCD_clear();
  37.  
  38. int number = 0;
  39. int key = -1;
  40.  
  41. int base_val = 10;
  42.  
  43.  
  44. int num[2] = {0};
  45. int operation = 0;
  46. int operaceBool = 0;
  47.  
  48. int equalsHit = 0;
  49. int result;
  50. int calculated = 0;
  51.  
  52. char* c;
  53.  
  54. int counter1 = 0;
  55. int counter2 = 0;
  56.  
  57.  
  58. for (;;) {
  59. // vypis stisknutou klavesu
  60. while (key == -1){
  61. key = SEGKEYPAD_GetKey();
  62. if(calculated == 0) {
  63.  
  64. int * numberToSegment1 = SmurfMyNumber(num[0]);
  65. int pom = 3;
  66. for(int Show1 = 4;Show1>0;Show1--)
  67. {
  68. SEGKEYPAD_SelectDisplay(Show1);
  69. SEGKEYPAD_ShowNumber(numberToSegment1[pom--]);
  70. SYSTICK_delay_ms(2);
  71. }
  72.  
  73.  
  74. int * numberToSegment2 = SmurfMyNumber(num[1]);
  75.  
  76. int pom2 = 3;
  77. for(int Show2 = 8;Show2>4;Show2--)
  78. {
  79. SEGKEYPAD_SelectDisplay(Show2);
  80. SEGKEYPAD_ShowNumber(numberToSegment2[pom2--]);
  81. SYSTICK_delay_ms(2);
  82. }
  83.  
  84. }else{
  85.  
  86. int * numberToSegment3 = SmurfMyNumber(storeForSegments[2]);
  87.  
  88. int pom4 = 7;
  89. for(int Show4 = 8;Show4>4;Show4--)
  90. {
  91. SEGKEYPAD_SelectDisplay(Show4);
  92. SEGKEYPAD_ShowNumber(numberToSegment3[pom4--]);
  93. SYSTICK_delay_ms(2);
  94. }
  95. int pom3 = 3;
  96. for(int Show3 = 4;Show3>0;Show3--)
  97. {
  98. SEGKEYPAD_SelectDisplay(Show3);
  99. SEGKEYPAD_ShowNumber(numberToSegment3[pom3--]);
  100. SYSTICK_delay_ms(2);
  101. }
  102.  
  103. }
  104.  
  105.  
  106. }
  107. if (key == 0) {
  108. LCD_clear();
  109. calculated = 0;
  110. if (!operaceBool){
  111. if(counter1 < 4){
  112. num[0] = 10 * num[0] + 7;
  113. counter1++;
  114. }
  115. }
  116. if (operaceBool){calculated = 0;
  117. if(counter2 < 4){
  118. num[1] = 10 * num[1] + 7;
  119. counter2++;
  120. }
  121. }
  122. printToDisplay(num[0], num[1], operation);
  123. printToLCD(num[0], num[1], operation);
  124. key = -1;
  125.  
  126.  
  127. }
  128.  
  129. if (key == 1) {calculated = 0;
  130. LCD_clear();
  131. if (!operaceBool){
  132. if(counter1 < 4){
  133. num[0] = 10 * num[0] + 4;
  134. counter1++;
  135. }
  136. }
  137. if (operaceBool){
  138. if(counter2 < 4){
  139. num[1] = 10 * num[1] + 4;
  140. counter2++;
  141. }
  142. }
  143. printToDisplay(num[0], num[1], operation);
  144. printToLCD(num[0], num[1], operation);
  145. key = -1;
  146. }
  147. if (key == 2) {calculated = 0;
  148. LCD_clear();
  149. if (!operaceBool){
  150. if(counter1 < 4){
  151. num[0] = 10 * num[0] + 1;
  152. counter1++;
  153. }
  154. }
  155. if (operaceBool){
  156. if(counter2 < 4){
  157. num[1] = 10 * num[1] + 1;
  158. counter2++;
  159. }
  160. }
  161.  
  162. printToDisplay(num[0], num[1], operation);
  163. printToLCD(num[0], num[1], operation);
  164. key = -1;
  165. }
  166. if (key == 3) {calculated = 0;
  167. LCD_clear();
  168. if (num[0] != 0 || num[1] != 0) {
  169. if (!operaceBool){
  170. num[0] = num[0] / 10;
  171. counter1--;
  172. }
  173. if (operaceBool){
  174. num[1] = num[1] / 10;
  175. counter2--;
  176. }}
  177.  
  178. printToDisplay(num[0], num[1], operation);
  179. printToLCD(num[0], num[1], operation);
  180. key = -1;
  181. }
  182.  
  183. if (key == 4) {calculated = 0;
  184. LCD_clear();
  185. if (!operaceBool){
  186. if(counter1 < 4){
  187. num[0] = 10 * num[0] + 8;
  188. counter1++;
  189. }
  190. }
  191. if (operaceBool){
  192. if(counter2 < 4){
  193. num[1] = 10 * num[1] + 8;
  194. counter2++;
  195. }
  196. }
  197. printToDisplay(num[0], num[1], operation);
  198. printToLCD(num[0], num[1], operation);
  199. key = -1;
  200. }
  201. if (key == 5) {calculated = 0;
  202. LCD_clear();
  203. if (!operaceBool){
  204. if(counter1 < 4){
  205. num[0] = 10 * num[0] + 5;
  206. counter1++;
  207. }
  208. }
  209. if (operaceBool){
  210. if(counter2 < 4){
  211. num[1] = 10 * num[1] + 5;
  212. counter2++;
  213. }
  214. }
  215.  
  216. printToDisplay(num[0], num[1], operation);
  217. printToLCD(num[0], num[1], operation);
  218. key = -1;
  219. }
  220. if (key == 6) {calculated = 0;
  221. LCD_clear();
  222. if (!operaceBool){
  223. if(counter1 < 4){
  224. num[0] = 10 * num[0] + 2;
  225. counter1++;
  226. }
  227. }
  228. if (operaceBool){
  229. if(counter2 < 4){
  230. num[1] = 10 * num[1] + 2;
  231. counter2++;
  232. }
  233. }
  234. printToDisplay(num[0], num[1], operation);
  235. printToLCD(num[0], num[1], operation);
  236. key = -1;
  237. }
  238. if (key == 7) {calculated = 0;
  239. LCD_clear();
  240. if (!operaceBool){
  241. if(counter1 < 4){
  242. num[0] = 10 * num[0] + 0;
  243. counter1++;
  244. }
  245. }
  246. if (operaceBool){
  247. if(counter2 < 4){
  248. num[1] = 10 * num[1] + 0;
  249. counter2++;
  250. }
  251. }
  252. printToDisplay(num[0], num[1], operation);
  253. printToLCD(num[0], num[1], operation);
  254. key = -1;
  255. }
  256. if (key == 8) {calculated = 0;
  257. LCD_clear();
  258. if (!operaceBool){
  259. if(counter1 < 4){
  260. num[0] = 10 * num[0] + 9;
  261. counter1++;
  262. }
  263. }
  264. if (operaceBool){
  265. if(counter2 < 4){
  266. num[1] = 10 * num[1] + 9;
  267. counter2++;
  268. }
  269. }
  270. printToDisplay(num[0], num[1], operation);
  271. printToLCD(num[0], num[1], operation);
  272. key = -1;
  273. }
  274. if (key == 9) {calculated = 0;
  275. LCD_clear();
  276. if (!operaceBool){
  277. if(counter1 < 4){
  278. num[0] = 10 * num[0] + 6;
  279. counter1++;
  280. }
  281. }
  282. if(operaceBool){
  283. if(counter2 < 4){
  284. num[1] = 10 * num[1] + 6;
  285. counter2++;
  286. }
  287. }
  288. printToDisplay(num[0], num[1], operation);
  289. printToLCD(num[0], num[1], operation);
  290. key = -1;
  291. }
  292. if (key == 10) {calculated = 0;
  293. LCD_clear();
  294. if (!operaceBool){
  295. if(counter1 < 4){
  296. num[0] = 10 * num[0] + 3;
  297. counter1++;
  298. }
  299. }
  300. if(operaceBool){
  301. if(counter2 < 4){
  302. num[1] = 10 * num[1] + 3;
  303. counter2++;
  304. }
  305. }
  306. printToDisplay(num[0], num[1], operation);
  307. printToLCD(num[0], num[1], operation);
  308. key = -1;
  309. }
  310. if (key == 11) {calculated = 0;
  311. LCD_clear();
  312. if (operation == 0)
  313. result = num[0] + num[1];
  314. else if (operation == 1)
  315. result = num[0] - num[1];
  316. else if (operation == 2)
  317. result = num[0] * num[1];
  318. else if (operation == 3) {
  319. if (num[0] != 0 || num[1] != 0) {
  320. result = num[0] / num[1];
  321. }
  322. }
  323. calculated = 1;
  324. printToDisplay(num[0], num[1], operation);
  325.  
  326. LCD_set_cursor(2, 1);
  327.  
  328. //printToLCD(num[0], num[1], operation);
  329. char Barbaronan[100];
  330. sprintf(Barbaronan," = %d ",result);
  331. LCD_puts(Barbaronan);
  332.  
  333. storeForSegments[0] = num[0];
  334. storeForSegments[1] = num[1];
  335. storeForSegments[2] = result;
  336. // printf(" = %d\n", result);
  337. num[0] = num[1] = result = operaceBool = 0;
  338. counter1 = counter2 = 0;
  339. key = -1;
  340. }
  341.  
  342. if (key == 12) {
  343.  
  344. operation = 0;
  345. operaceBool = 1;
  346. key = -1;
  347. }
  348. if (key == 13) {
  349. operation = 1;
  350. operaceBool = 1;
  351. key = -1;
  352. //-
  353. }
  354. //multiply
  355. if (key == 14) {
  356. operation = 2;
  357. operaceBool = 1;
  358. key = -1;
  359. }
  360. // /
  361. if (key == 15) {
  362. operation = 3;
  363. operaceBool = 1;
  364. key = -1;
  365. }
  366.  
  367. SYSTICK_delay_ms(500);
  368.  
  369.  
  370. }
  371.  
  372. /* int inc = 0;
  373. int disp = 3;
  374. for (int i = counter; i > 0; i--) {
  375. SEGKEYPAD_SelectDisplay(disp);
  376. SEGKEYPAD_ShowNumber(prom1[i]);
  377. inc++;
  378. disp--;
  379.  
  380. }*/
  381.  
  382. // vypis cislo na displeji
  383. //SEGKEYPAD_ShowNumber(number);
  384.  
  385.  
  386. // posun na dalsi cislo a dalsi display
  387. /*
  388. number++;
  389. if (number > 9) {
  390. number = 0;
  391. disp++;
  392. if (disp > 8)
  393. disp = 1;
  394. SEGKEYPAD_SelectDisplay(disp);
  395. }
  396. */
  397.  
  398. return 0;
  399. }
  400.  
  401. void printToDisplay(int num, int num2, int operation) {
  402. char c;
  403. if (operation == 0)
  404. c = '+';
  405. else if (operation == 1)
  406. c = '-';
  407. else if (operation == 2)
  408. c = '*';
  409. else if (operation == 3)
  410. c = '/';
  411.  
  412. printf("cislo1 cislo2 operace => %d %c %d \n", num, c, num2);
  413.  
  414. return;
  415. }
  416. void printToLCD(int num1, int num2, int operation) {
  417. char c[100];
  418. char o;
  419. if (operation == 0)
  420. o = '+';
  421. else if (operation == 1)
  422. o = '-';
  423. else if (operation == 2)
  424. o = '*';
  425. else if (operation == 3)
  426. o = '/';
  427.  
  428. sprintf(c, "%d %c %d", num1, o, num2);
  429. LCD_puts(c);
  430.  
  431. return;
  432. }
  433. int * SmurfMyNumber(int number){
  434.  
  435. int base_val = 10, digit, i = 0, n = 0;
  436. int t, numstring[32];
  437. while (number) {
  438. digit = number % base_val;
  439. number /= base_val;
  440. if (digit < 10) {
  441. numstring[n++] = digit;
  442.  
  443. } else {
  444. numstring[n++] = digit;
  445.  
  446. }
  447. }
  448. for (int i = 0; i < n; i++) {
  449. t = numstring[i];
  450. numstring[i] = numstring[n - i - 1];
  451. numstring[n - i - 1] = t;
  452. }
  453. return numstring;
  454. }
  455. ////////////////////////////////////////////////////////////////////////////////
  456. // EOF
  457. ////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement