Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.96 KB | None | 0 0
  1. # include<msp430x14x.h>
  2. # include "lcd.h"
  3. # include "portyLcd.h"
  4.  
  5. /******************************************************************
  6. ***** *****
  7. ***** Name: display.c *****
  8. ***** modified version of Andreas Dannenberg *****
  9. ***** Includes only display specific code. ***** *****
  10. ******************************************************************/
  11.  
  12. #define bitset(var,bitno) ((var) |= 1 << (bitno))
  13. #define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
  14.  
  15. void InitPortsLcd(void)
  16. {
  17.  
  18. P2SEL = 0;
  19. P2OUT = 0;
  20. P2DIR = ~BIT0; //only P2.0 is input
  21. }
  22.  
  23.  
  24. void Delay(unsigned int a);
  25. void Delayx100us(unsigned char b);
  26. void _E(void);
  27.  
  28. void putc(char c)
  29. {
  30. SEND_CHAR(c);
  31. }
  32. void clearDisplay()
  33. {
  34. SEND_CMD(CLR_DISP);
  35. Delayx100us(10);
  36. }
  37.  
  38. void printDecDigit(int Number)
  39. {
  40. if (Number < 0)
  41. {
  42. putc('-');
  43. putc(Number + '0');
  44. }
  45. else
  46. putc(Number + '0');
  47. }
  48.  
  49. void Delay(unsigned int a)
  50. {
  51. int k;
  52. for (k = 0; k != a; ++k)
  53. {
  54. _NOP();
  55. _NOP();
  56. _NOP();
  57. _NOP();
  58. }
  59. }
  60.  
  61. void Delayx100us(unsigned char b)
  62. {
  63. int j;
  64. for (j = 0; j != b; ++j) Delay(_100us);
  65. }
  66.  
  67.  
  68. void _E(void)
  69. {
  70. bitset(P2OUT, E); //toggle E for LCD
  71. Delay(_10us);
  72. bitclr(P2OUT, E);
  73. }
  74.  
  75.  
  76. void SEND_CHAR(unsigned char d)
  77. {
  78. int temp;
  79. Delayx100us(5); //.5ms
  80. temp = d & 0xf0; //get upper nibble
  81. LCD_Data &= 0x0f;
  82. LCD_Data |= temp;
  83. bitset(P2OUT, RS); //set LCD to data mode
  84. _E(); //toggle E for LCD
  85. temp = d & 0x0f;
  86. temp = temp << 4; //get down nibble
  87. LCD_Data &= 0x0f;
  88. LCD_Data |= temp;
  89. bitset(P2OUT, RS); //set LCD to data mode
  90. _E(); //toggle E for LCD
  91. }
  92.  
  93. void SEND_CMD(unsigned char e)
  94. {
  95. int temp;
  96. Delayx100us(10); //10ms
  97. temp = e & 0xf0; //get upper nibble
  98. LCD_Data &= 0x0f;
  99. LCD_Data |= temp; //send CMD to LCD
  100. bitclr(P2OUT, RS); //set LCD to CMD mode
  101. _E(); //toggle E for LCD
  102. temp = e & 0x0f;
  103. temp = temp << 4; //get down nibble
  104. LCD_Data &= 0x0f;
  105. LCD_Data |= temp;
  106. bitclr(P2OUT, RS); //set LCD to CMD mode
  107. _E(); //toggle E for LCD
  108. }
  109.  
  110. void InitLCD(void)
  111. {
  112. bitclr(P2OUT, RS);
  113. Delayx100us(250); //Delay 100ms
  114. Delayx100us(250);
  115. Delayx100us(250);
  116. Delayx100us(250);
  117. LCD_Data |= BIT4 | BIT5; //D7-D4 = 0011
  118. LCD_Data &= ~BIT6 & ~BIT7;
  119. _E(); //toggle E for LCD
  120. Delayx100us(100); //10ms
  121. _E(); //toggle E for LCD
  122. Delayx100us(100); //10ms
  123. _E(); //toggle E for LCD
  124. Delayx100us(100); //10ms
  125. LCD_Data &= ~BIT4;
  126. _E(); //toggle E for LCD
  127.  
  128. SEND_CMD(DISP_ON);
  129. SEND_CMD(CLR_DISP);
  130. Delayx100us(250);
  131. Delayx100us(250);
  132. Delayx100us(250);
  133. Delayx100us(250);
  134. }
  135.  
  136.  
  137.  
  138. //---------------- zmienne globalne -------------
  139. unsigned int i = 0;
  140. unsigned int sekundy = 0;
  141. unsigned int licznik = 0;
  142. int godziny = 0;
  143. int minuty = 0;
  144. int milisekundy = 0;
  145. int stop = 0;
  146. int godz1 = 0;
  147. int min1 = 0;
  148. int sek1 = 0;
  149. unsigned int punkty = 0;
  150. int tiles = 1;
  151. int przegrana = 0;
  152. int x1 = 0;
  153. int x2 = 3;
  154. int best = 0;
  155. int hard = 0;
  156. int czasNaGre = 150;
  157. int czyFalse = 0;
  158. int kolejnaZmienna = 0;
  159. char bestPlayer[4] = "AAA";
  160. void Clock(void);
  161.  
  162. //----------------- main program -------------------
  163. void main(void )
  164. {
  165.  
  166. P2DIR |= BIT1; // STATUS LED
  167.  
  168. WDTCTL = WDTPW + WDTHOLD; // Wy31czenie WDT
  169.  
  170. InitPortsLcd(); // inicjalizacja portÛw LCD
  171. InitLCD(); // inicjalizacja LCD
  172. clearDisplay(); // czyszczenie wyowietlacza
  173.  
  174. // Basic Clock Module ustawiamy na ACLK(zegar 8 MHz ) i dzielimy czestotliwooa przez 2 (4 MHz)
  175. BCSCTL1 |= XTS; // ACLK = LFXT1 = HF XTAL 8MHz
  176.  
  177. do
  178. {
  179. IFG1 &= ~OFIFG; // Czyszczenie flgi OSCFault
  180. for (i = 0xFF; i > 0; i--) ; // odczekanie
  181. }
  182. while ((IFG1 & OFIFG) == OFIFG); // dopÛki OSCFault jest ci1gle ustawiona
  183.  
  184. BCSCTL1 |= DIVA_1; // ACLK=8 MHz/2=4 MHz
  185. BCSCTL2 |= SELM0 | SELM1; // MCLK= LFTX1 =ACLK
  186.  
  187. // Timer_A ustawiamy na 500 kHz
  188. // a przerwanie generujemy co 100 ms
  189. TACTL = TASSEL_1 + MC_1 + ID_3; // Wybieram ACLK, ACLK/8=500kHz,tryb Up
  190. CCTL0 = CCIE; // w31czenie przerwan od CCR0
  191. CCR0 = 500000; // podzielnik 50000: przerwanie co 100 ms
  192.  
  193. _EINT(); // w31czenie przerwan
  194.  
  195.  
  196. P4DIR &= 0x00;
  197. P4DIR |= 0x0C;
  198. P1DIR |= (BIT1 + BIT2 + BIT3 + BIT4);
  199.  
  200. //menuGlowne();
  201.  
  202. //wyswietl(0, 3);
  203.  
  204. znaczek();
  205.  
  206. for (; ; )
  207. {
  208. _BIS_SR(LPM3_bits); // przejscie do trybu LPM3
  209. Clock();
  210. }
  211. }
  212. int losowaLiczba(int liczba)
  213. {
  214. int wynik = 0;
  215. wynik = punkty * 13 * 31 * 29;
  216. if (wynik < 0) wynik = wynik * (-1);
  217. return wynik % liczba;
  218. }
  219.  
  220. int znaczek()
  221. {
  222. SEND_CMD(CG_RAM_ADDR);
  223. SEND_CHAR(0x1F);
  224. SEND_CHAR(0x1F);
  225. SEND_CHAR(0x1F);
  226. SEND_CHAR(0x1F);
  227. SEND_CHAR(0x1F);
  228. SEND_CHAR(0x1F);
  229. SEND_CHAR(0x1F);
  230. SEND_CHAR(0x1F);
  231. SEND_CMD(DD_RAM_ADDR);
  232. }
  233.  
  234. int ustCzasu()
  235. {
  236. while(1)
  237. {
  238. clearDisplay();
  239. char napis[16] = "Czas na gre: ";
  240. for(int i=0; i<strlen(napis); i++)
  241. {
  242. SEND_CHAR(napis[i]);
  243. }
  244.  
  245. if (czasNaGre < 100) SEND_CHAR((czasNaGre/10) + 48);
  246. else
  247. {
  248. SEND_CHAR(((czasNaGre/10) / 10) + 48);
  249. SEND_CHAR(((czasNaGre/10) % 10) + 48);
  250. }
  251.  
  252. opoznienie();
  253. while(1)
  254. {
  255. if((P4IN & 0x10) == 0)
  256. {
  257. if(czasNaGre != 50) czasNaGre-=10;
  258. break;
  259. }
  260.  
  261. if((P4IN & 0x20) == 0)
  262. {
  263. if(czasNaGre != 300) czasNaGre+=10;
  264. break;
  265. }
  266.  
  267. if((P4IN & 0x80) == 0)
  268. {
  269. kolejnaZmienna = 1;
  270. break;
  271. }
  272. }
  273.  
  274. if(kolejnaZmienna == 1)
  275. {
  276. kolejnaZmienna = 0;
  277. opoznienie();
  278. licznik = 0;
  279. break;
  280. }
  281. opoznienie();
  282. }
  283. }
  284.  
  285. int menuGlowne()
  286. {
  287. clearDisplay();
  288. char menu1[7] = "1.Graj";
  289. for (int i = 0; i < strlen(menu1); i++)
  290. {
  291. SEND_CHAR(menu1[i]);
  292. }
  293.  
  294. SEND_CHAR(' ');
  295. char menu2[7] = "2.Opis";
  296. for (int i = 0; i < strlen(menu2); i++)
  297. {
  298. SEND_CHAR(menu2[i]);
  299. }
  300.  
  301. SEND_CMD(DD_RAM_ADDR2);
  302. char menu3[16] = "3.Autorzy";
  303. for (int i = 0; i < strlen(menu3); i++)
  304. {
  305. SEND_CHAR(menu3[i]);
  306. }
  307.  
  308. SEND_CHAR(' ');
  309. char menu4[16] = "4.High";
  310. for(int i=0; i < strlen(menu4); i++)
  311. {
  312. SEND_CHAR(menu4[i]);
  313. }
  314.  
  315. P4DIR &= 0x00;
  316. P4DIR |= 0x0C;
  317. while (1)
  318. {
  319. if ((P4IN & 0x10) == 0)
  320. {
  321. clearDisplay();
  322. char menu1[16] = "1.Easy";
  323. for (int i = 0; i < strlen(menu1); i++)
  324. {
  325. SEND_CHAR(menu1[i]);
  326. }
  327.  
  328. SEND_CHAR(' ');
  329. char menu2[16] = "2.Hard";
  330. for (int i = 0; i < strlen(menu2); i++)
  331. {
  332. SEND_CHAR(menu2[i]);
  333. }
  334.  
  335. while(1)
  336. {
  337. if((P4IN & 0x10) == 0)
  338. {
  339. ustCzasu();
  340. opoznienie();
  341. hard = 0;
  342. break;
  343. }
  344. if((P4IN & 0x20) == 0)
  345. {
  346. ustCzasu();
  347. opoznienie();
  348. hard = 1;
  349. break;
  350. }
  351. opoznienie();
  352. }
  353. break;
  354. }
  355.  
  356. if ((P4IN & 0x20) == 0)
  357. {
  358. char opis1[16] = "Kliknij guzikPod";
  359. char opis2[16] = "czarnym znakiem";
  360. char opis3[16] = "w drugiej linii";
  361. clearDisplay();
  362.  
  363. for (int i = 0; i < strlen(opis1); i++)
  364. {
  365. SEND_CHAR(opis1[i]);
  366. }
  367.  
  368. SEND_CMD(DD_RAM_ADDR2);
  369. for (int i = 0; i < strlen(opis2); i++)
  370. {
  371. SEND_CHAR(opis2[i]);
  372. }
  373.  
  374. while (1)
  375. {
  376. if ((P4IN & 0x10) == 0)
  377. {
  378. opoznienie();
  379. clearDisplay();
  380. SEND_CMD(DD_RAM_ADDR);
  381. for (int i = 0; i < strlen(opis3); i++)
  382. {
  383. SEND_CHAR(opis3[i]);
  384. }
  385. while(1)
  386. {
  387. opoznienie();
  388. if((P4IN & 0x10) == 0) break;
  389. }
  390. break;
  391. }
  392. }
  393. menuGlowne();
  394. }
  395.  
  396. if ((P4IN & 0x40) == 0)
  397. {
  398. clearDisplay();
  399. char autor1[16] = "Tomasz Smietalo";
  400. char autor2[16] = "Michal Mironczuk";
  401. for (int i = 0; i < strlen(autor1); i++)
  402. {
  403. SEND_CHAR(autor1[i]);
  404. }
  405. SEND_CMD(DD_RAM_ADDR2);
  406. for (int i = 0; i < strlen(autor2); i++)
  407. {
  408. SEND_CHAR(autor2[i]);
  409. }
  410. while (1)
  411. {
  412. if ((P4IN & 0x10) == 0) break;
  413. opoznienie();
  414. }
  415. menuGlowne();
  416. }
  417.  
  418. if ((P4IN & 0x80) == 0)
  419. {
  420. clearDisplay();
  421. char napis[16] = "Best player";
  422. for(int i=0; i<strlen(napis); i++)
  423. {
  424. SEND_CHAR(napis[i]);
  425. }
  426.  
  427. SEND_CMD(DD_RAM_ADDR2);
  428. for(int i=0; i<3; i++)
  429. {
  430. SEND_CHAR(bestPlayer[i]);
  431. }
  432. while(1)
  433. {
  434. if((P4IN & 0x10) == 0) break;
  435. }
  436. menuGlowne();
  437. }
  438. }
  439. }
  440.  
  441. int przesun()
  442. {
  443. for (int i = 0; i < 16; i++)
  444. {
  445. SEND_CMD(DATA_ROL_RIGHT);
  446. for (int i = 0; i < 30000; i++) asm("nop");
  447. }
  448. }
  449.  
  450. int wyswietl(int x3, int x4)
  451. {
  452. clearDisplay();
  453.  
  454. if (hard == 0)
  455. {
  456. SEND_CHAR(' ');
  457. for (int i = 0; i < x3; i++)
  458. {
  459. for (int j = 0; j < 4; j++)
  460. {
  461. SEND_CHAR(' ');
  462. }
  463. }
  464. SEND_CHAR(0);
  465. SEND_CHAR(0);
  466. }
  467.  
  468. SEND_CMD(DD_RAM_ADDR2);
  469. SEND_CHAR(' ');
  470. for (int i = 0; i < x4; i++)
  471. {
  472. for (int j = 0; j < 4; j++)
  473. {
  474. SEND_CHAR(' ');
  475. }
  476. }
  477. SEND_CHAR(0);
  478. SEND_CHAR(0);
  479. return 0;
  480. }
  481.  
  482. int ile = 0;
  483. int pierwsza = 0;
  484.  
  485. int opoznienie()
  486. {
  487. for (int i = 0; i < 30000; i++) asm("nop");
  488. for (int i = 0; i < 30000; i++) asm("nop");
  489. for (int i = 0; i < 30000; i++) asm("nop");
  490. for (int i = 0; i < 30000; i++) asm("nop");
  491. for (int i = 0; i < 30000; i++) asm("nop");
  492. for (int i = 0; i < 30000; i++) asm("nop");
  493. for (int i = 0; i < 30000; i++) asm("nop");
  494. for (int i = 0; i < 30000; i++) asm("nop");
  495. for (int i = 0; i < 30000; i++) asm("nop");
  496. for (int i = 0; i < 30000; i++) asm("nop");
  497. }
  498.  
  499. int hscore()
  500. {
  501. int out = 0;
  502. while(1)
  503. {
  504. clearDisplay();
  505. for(int i=0; i<3; i++)
  506. {
  507. SEND_CHAR(bestPlayer[i]);
  508. }
  509. while(1)
  510. {
  511. if((P4IN & 0x10) == 0)
  512. {
  513. if(bestPlayer[0] != 'Z') bestPlayer[0]++;
  514. else bestPlayer[0] = 'A';
  515. opoznienie();
  516. break;
  517. }
  518. if((P4IN & 0x20) == 0)
  519. {
  520. if(bestPlayer[1] != 'Z') bestPlayer[1]++;
  521. else bestPlayer[1] = 'A';
  522. opoznienie();
  523. break;
  524. }
  525. if((P4IN & 0x40) == 0)
  526. {
  527. if(bestPlayer[2] != 'Z') bestPlayer[2]++;
  528. else bestPlayer[2] = 'A';
  529. opoznienie();
  530. break;
  531. }
  532. if((P4IN & 0x80) == 0)
  533. {
  534. out = 1;
  535. break;
  536. }
  537. }
  538. if(out == 1) break;
  539. }
  540. }
  541.  
  542. void Clock(void)
  543. {
  544. P4DIR &= 0x00;
  545. if (licznik % 1 == 0)
  546. {
  547. menuGlowne();
  548. if(czyFalse == 0) wyswietl(0, 3);
  549. else wyswietl(x1,x2);
  550. czyFalse = 1;
  551. while (1)
  552. {
  553. while (1)
  554. {
  555. if ((P4IN & 0x10) == 0)
  556. {
  557. if (x2 == 0)
  558. {
  559. P1OUT ^= BIT1;
  560. x2 = x1;
  561. x1 = losowaLiczba(4);
  562. punkty++;
  563. clearDisplay();
  564. wyswietl(x1, x2);
  565. }
  566. else
  567. {
  568. przegrana = 1;
  569. break;
  570. }
  571. opoznienie();
  572. }
  573.  
  574. if ((P4IN & 0x20) == 0)
  575. {
  576. if (x2 == 1)
  577. {
  578. P1OUT ^= BIT2;
  579. x2 = x1;
  580. x1 = losowaLiczba(4);
  581. punkty++;
  582. clearDisplay();
  583. wyswietl(x1, x2);
  584. }
  585. else
  586. {
  587. przegrana = 1;
  588. break;
  589. }
  590. opoznienie();
  591. }
  592.  
  593. if ((P4IN & 0x40) == 0)
  594. {
  595. if (x2 == 2)
  596. {
  597. P1OUT ^= BIT3;
  598. x2 = x1;
  599. x1 = losowaLiczba(4);
  600. punkty++;
  601. clearDisplay();
  602. wyswietl(x1, x2);
  603. }
  604. else
  605. {
  606. przegrana = 1;
  607. break;
  608. }
  609. opoznienie();
  610. }
  611.  
  612. if ((P4IN & 0x80) == 0)
  613. {
  614. if (x2 == 3)
  615. {
  616. P1OUT ^= BIT4;
  617. x2 = x1;
  618. x1 = losowaLiczba(4);
  619. punkty++;
  620. clearDisplay();
  621. wyswietl(x1, x2);
  622. }
  623. else
  624. {
  625. przegrana = 1;
  626. break;
  627. }
  628. opoznienie();
  629. }
  630.  
  631. if (licznik >= czasNaGre)
  632. {
  633. licznik = 0;
  634. break;
  635. }
  636. }
  637.  
  638. if (przegrana == 1)
  639. {
  640. if(best < punkty) hscore();
  641.  
  642. clearDisplay();
  643. char przegrales[11] = "Ups!";
  644. SEND_CMD(DD_RAM_ADDR);
  645. for (int i = 0; i < strlen(przegrales); i++)
  646. {
  647. SEND_CHAR(przegrales[i]);
  648. }
  649.  
  650. SEND_CHAR(' ');
  651. char score[9] = "Punkty: ";
  652. for (int i = 0; i < strlen(score); i++)
  653. {
  654. SEND_CHAR(score[i]);
  655. }
  656. if (punkty < 10) SEND_CHAR(punkty + 48);
  657. else
  658. {
  659. SEND_CHAR((punkty / 10) + 48);
  660. SEND_CHAR((punkty % 10) + 48);
  661. }
  662. if (punkty > best) best = punkty;
  663. punkty = 0;
  664.  
  665. SEND_CMD(DD_RAM_ADDR2);
  666. char najlepszy[13] = "Best Score: ";
  667.  
  668. for (int i = 0; i < strlen(najlepszy); i++)
  669. {
  670. SEND_CHAR(najlepszy[i]);
  671. }
  672. if (best < 10) SEND_CHAR(best + 48);
  673. else
  674. {
  675. SEND_CHAR((best / 10) + 48);
  676. SEND_CHAR((best % 10) + 48);
  677. }
  678.  
  679. for (int i = 0; i < 100; i++)
  680. {
  681. P4OUT ^= 0x08;
  682. for (int i = 0; i < 10000; i++) asm("nop");
  683. }
  684. while(1)
  685. {
  686. if((P4IN & 0x30) == 0)
  687. {
  688. przesun();
  689. break;
  690. }
  691. }
  692. przegrana = 0;
  693. break;
  694. }
  695. else
  696. {
  697. if(best < punkty) hscore();
  698.  
  699. clearDisplay();
  700. char score[9] = "Punkty: ";
  701. SEND_CMD(DD_RAM_ADDR);
  702. for (int i = 0; i < strlen(score); i++)
  703. {
  704. SEND_CHAR(score[i]);
  705. }
  706. if (punkty < 10) SEND_CHAR(punkty + 48);
  707. else
  708. {
  709. SEND_CHAR((punkty / 10) + 48);
  710. SEND_CHAR((punkty % 10) + 48);
  711. }
  712.  
  713. if (punkty > best) best = punkty;
  714. punkty = 0;
  715.  
  716. SEND_CMD(DD_RAM_ADDR2);
  717. char najlepszy[13] = "Best Score: ";
  718.  
  719. for (int i = 0; i < strlen(najlepszy); i++)
  720. {
  721. SEND_CHAR(najlepszy[i]);
  722. }
  723. if (best < 10) SEND_CHAR(best + 48);
  724. else
  725. {
  726. SEND_CHAR((best / 10) + 48);
  727. SEND_CHAR((best % 10) + 48);
  728. }
  729.  
  730. while(1)
  731. {
  732. if((P4IN & 0x30) == 0)
  733. {
  734. przesun();
  735. break;
  736. }
  737. }
  738. break;
  739. }
  740. break;
  741. }
  742. }
  743. }
  744.  
  745. // procedura obs3ugi przerwania od TimerA
  746. #pragma vector=TIMERA0_VECTOR
  747. __interrupt void Timer_A(void)
  748. {
  749. ++licznik;
  750. _BIC_SR_IRQ(LPM3_bits); // wyjocie z trybu LPM3
  751. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement