Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include<msp430x14x.h>
- # include "lcd.h"
- # include "portyLcd.h"
- /******************************************************************
- ***** *****
- ***** Name: display.c *****
- ***** modified version of Andreas Dannenberg *****
- ***** Includes only display specific code. ***** *****
- ******************************************************************/
- #define bitset(var,bitno) ((var) |= 1 << (bitno))
- #define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
- void InitPortsLcd(void)
- {
- P2SEL = 0;
- P2OUT = 0;
- P2DIR = ~BIT0; //only P2.0 is input
- }
- void Delay(unsigned int a);
- void Delayx100us(unsigned char b);
- void _E(void);
- void putc(char c)
- {
- SEND_CHAR(c);
- }
- void clearDisplay()
- {
- SEND_CMD(CLR_DISP);
- Delayx100us(10);
- }
- void printDecDigit(int Number)
- {
- if (Number < 0)
- {
- putc('-');
- putc(Number + '0');
- }
- else
- putc(Number + '0');
- }
- void Delay(unsigned int a)
- {
- int k;
- for (k = 0; k != a; ++k)
- {
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- }
- }
- void Delayx100us(unsigned char b)
- {
- int j;
- for (j = 0; j != b; ++j) Delay(_100us);
- }
- void _E(void)
- {
- bitset(P2OUT, E); //toggle E for LCD
- Delay(_10us);
- bitclr(P2OUT, E);
- }
- void SEND_CHAR(unsigned char d)
- {
- int temp;
- Delayx100us(5); //.5ms
- temp = d & 0xf0; //get upper nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp;
- bitset(P2OUT, RS); //set LCD to data mode
- _E(); //toggle E for LCD
- temp = d & 0x0f;
- temp = temp << 4; //get down nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp;
- bitset(P2OUT, RS); //set LCD to data mode
- _E(); //toggle E for LCD
- }
- void SEND_CMD(unsigned char e)
- {
- int temp;
- Delayx100us(10); //10ms
- temp = e & 0xf0; //get upper nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp; //send CMD to LCD
- bitclr(P2OUT, RS); //set LCD to CMD mode
- _E(); //toggle E for LCD
- temp = e & 0x0f;
- temp = temp << 4; //get down nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp;
- bitclr(P2OUT, RS); //set LCD to CMD mode
- _E(); //toggle E for LCD
- }
- void InitLCD(void)
- {
- bitclr(P2OUT, RS);
- Delayx100us(250); //Delay 100ms
- Delayx100us(250);
- Delayx100us(250);
- Delayx100us(250);
- LCD_Data |= BIT4 | BIT5; //D7-D4 = 0011
- LCD_Data &= ~BIT6 & ~BIT7;
- _E(); //toggle E for LCD
- Delayx100us(100); //10ms
- _E(); //toggle E for LCD
- Delayx100us(100); //10ms
- _E(); //toggle E for LCD
- Delayx100us(100); //10ms
- LCD_Data &= ~BIT4;
- _E(); //toggle E for LCD
- SEND_CMD(DISP_ON);
- SEND_CMD(CLR_DISP);
- Delayx100us(250);
- Delayx100us(250);
- Delayx100us(250);
- Delayx100us(250);
- }
- //---------------- zmienne globalne -------------
- unsigned int i = 0;
- unsigned int sekundy = 0;
- unsigned int licznik = 0;
- int godziny = 0;
- int minuty = 0;
- int milisekundy = 0;
- int stop = 0;
- int godz1 = 0;
- int min1 = 0;
- int sek1 = 0;
- unsigned int punkty = 0;
- int tiles = 1;
- int przegrana = 0;
- int x1 = 0;
- int x2 = 3;
- int best = 0;
- int hard = 0;
- int czasNaGre = 150;
- int czyFalse = 0;
- int kolejnaZmienna = 0;
- char bestPlayer[4] = "AAA";
- void Clock(void);
- //----------------- main program -------------------
- void main(void )
- {
- P2DIR |= BIT1; // STATUS LED
- WDTCTL = WDTPW + WDTHOLD; // Wy31czenie WDT
- InitPortsLcd(); // inicjalizacja portÛw LCD
- InitLCD(); // inicjalizacja LCD
- clearDisplay(); // czyszczenie wyowietlacza
- // Basic Clock Module ustawiamy na ACLK(zegar 8 MHz ) i dzielimy czestotliwooa przez 2 (4 MHz)
- BCSCTL1 |= XTS; // ACLK = LFXT1 = HF XTAL 8MHz
- do
- {
- IFG1 &= ~OFIFG; // Czyszczenie flgi OSCFault
- for (i = 0xFF; i > 0; i--) ; // odczekanie
- }
- while ((IFG1 & OFIFG) == OFIFG); // dopÛki OSCFault jest ci1gle ustawiona
- BCSCTL1 |= DIVA_1; // ACLK=8 MHz/2=4 MHz
- BCSCTL2 |= SELM0 | SELM1; // MCLK= LFTX1 =ACLK
- // Timer_A ustawiamy na 500 kHz
- // a przerwanie generujemy co 100 ms
- TACTL = TASSEL_1 + MC_1 + ID_3; // Wybieram ACLK, ACLK/8=500kHz,tryb Up
- CCTL0 = CCIE; // w31czenie przerwan od CCR0
- CCR0 = 500000; // podzielnik 50000: przerwanie co 100 ms
- _EINT(); // w31czenie przerwan
- P4DIR &= 0x00;
- P4DIR |= 0x0C;
- P1DIR |= (BIT1 + BIT2 + BIT3 + BIT4);
- //menuGlowne();
- //wyswietl(0, 3);
- znaczek();
- for (; ; )
- {
- _BIS_SR(LPM3_bits); // przejscie do trybu LPM3
- Clock();
- }
- }
- int losowaLiczba(int liczba)
- {
- int wynik = 0;
- wynik = punkty * 13 * 31 * 29;
- if (wynik < 0) wynik = wynik * (-1);
- return wynik % liczba;
- }
- int znaczek()
- {
- SEND_CMD(CG_RAM_ADDR);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CHAR(0x1F);
- SEND_CMD(DD_RAM_ADDR);
- }
- int ustCzasu()
- {
- while(1)
- {
- clearDisplay();
- char napis[16] = "Czas na gre: ";
- for(int i=0; i<strlen(napis); i++)
- {
- SEND_CHAR(napis[i]);
- }
- if (czasNaGre < 100) SEND_CHAR((czasNaGre/10) + 48);
- else
- {
- SEND_CHAR(((czasNaGre/10) / 10) + 48);
- SEND_CHAR(((czasNaGre/10) % 10) + 48);
- }
- opoznienie();
- while(1)
- {
- if((P4IN & 0x10) == 0)
- {
- if(czasNaGre != 50) czasNaGre-=10;
- break;
- }
- if((P4IN & 0x20) == 0)
- {
- if(czasNaGre != 300) czasNaGre+=10;
- break;
- }
- if((P4IN & 0x80) == 0)
- {
- kolejnaZmienna = 1;
- break;
- }
- }
- if(kolejnaZmienna == 1)
- {
- kolejnaZmienna = 0;
- opoznienie();
- licznik = 0;
- break;
- }
- opoznienie();
- }
- }
- int menuGlowne()
- {
- clearDisplay();
- char menu1[7] = "1.Graj";
- for (int i = 0; i < strlen(menu1); i++)
- {
- SEND_CHAR(menu1[i]);
- }
- SEND_CHAR(' ');
- char menu2[7] = "2.Opis";
- for (int i = 0; i < strlen(menu2); i++)
- {
- SEND_CHAR(menu2[i]);
- }
- SEND_CMD(DD_RAM_ADDR2);
- char menu3[16] = "3.Autorzy";
- for (int i = 0; i < strlen(menu3); i++)
- {
- SEND_CHAR(menu3[i]);
- }
- SEND_CHAR(' ');
- char menu4[16] = "4.High";
- for(int i=0; i < strlen(menu4); i++)
- {
- SEND_CHAR(menu4[i]);
- }
- P4DIR &= 0x00;
- P4DIR |= 0x0C;
- while (1)
- {
- if ((P4IN & 0x10) == 0)
- {
- clearDisplay();
- char menu1[16] = "1.Easy";
- for (int i = 0; i < strlen(menu1); i++)
- {
- SEND_CHAR(menu1[i]);
- }
- SEND_CHAR(' ');
- char menu2[16] = "2.Hard";
- for (int i = 0; i < strlen(menu2); i++)
- {
- SEND_CHAR(menu2[i]);
- }
- while(1)
- {
- if((P4IN & 0x10) == 0)
- {
- ustCzasu();
- opoznienie();
- hard = 0;
- break;
- }
- if((P4IN & 0x20) == 0)
- {
- ustCzasu();
- opoznienie();
- hard = 1;
- break;
- }
- opoznienie();
- }
- break;
- }
- if ((P4IN & 0x20) == 0)
- {
- char opis1[16] = "Kliknij guzikPod";
- char opis2[16] = "czarnym znakiem";
- char opis3[16] = "w drugiej linii";
- clearDisplay();
- for (int i = 0; i < strlen(opis1); i++)
- {
- SEND_CHAR(opis1[i]);
- }
- SEND_CMD(DD_RAM_ADDR2);
- for (int i = 0; i < strlen(opis2); i++)
- {
- SEND_CHAR(opis2[i]);
- }
- while (1)
- {
- if ((P4IN & 0x10) == 0)
- {
- opoznienie();
- clearDisplay();
- SEND_CMD(DD_RAM_ADDR);
- for (int i = 0; i < strlen(opis3); i++)
- {
- SEND_CHAR(opis3[i]);
- }
- while(1)
- {
- opoznienie();
- if((P4IN & 0x10) == 0) break;
- }
- break;
- }
- }
- menuGlowne();
- }
- if ((P4IN & 0x40) == 0)
- {
- clearDisplay();
- char autor1[16] = "Tomasz Smietalo";
- char autor2[16] = "Michal Mironczuk";
- for (int i = 0; i < strlen(autor1); i++)
- {
- SEND_CHAR(autor1[i]);
- }
- SEND_CMD(DD_RAM_ADDR2);
- for (int i = 0; i < strlen(autor2); i++)
- {
- SEND_CHAR(autor2[i]);
- }
- while (1)
- {
- if ((P4IN & 0x10) == 0) break;
- opoznienie();
- }
- menuGlowne();
- }
- if ((P4IN & 0x80) == 0)
- {
- clearDisplay();
- char napis[16] = "Best player";
- for(int i=0; i<strlen(napis); i++)
- {
- SEND_CHAR(napis[i]);
- }
- SEND_CMD(DD_RAM_ADDR2);
- for(int i=0; i<3; i++)
- {
- SEND_CHAR(bestPlayer[i]);
- }
- while(1)
- {
- if((P4IN & 0x10) == 0) break;
- }
- menuGlowne();
- }
- }
- }
- int przesun()
- {
- for (int i = 0; i < 16; i++)
- {
- SEND_CMD(DATA_ROL_RIGHT);
- for (int i = 0; i < 30000; i++) asm("nop");
- }
- }
- int wyswietl(int x3, int x4)
- {
- clearDisplay();
- if (hard == 0)
- {
- SEND_CHAR(' ');
- for (int i = 0; i < x3; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- SEND_CHAR(' ');
- }
- }
- SEND_CHAR(0);
- SEND_CHAR(0);
- }
- SEND_CMD(DD_RAM_ADDR2);
- SEND_CHAR(' ');
- for (int i = 0; i < x4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- SEND_CHAR(' ');
- }
- }
- SEND_CHAR(0);
- SEND_CHAR(0);
- return 0;
- }
- int ile = 0;
- int pierwsza = 0;
- int opoznienie()
- {
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- for (int i = 0; i < 30000; i++) asm("nop");
- }
- int hscore()
- {
- int out = 0;
- while(1)
- {
- clearDisplay();
- for(int i=0; i<3; i++)
- {
- SEND_CHAR(bestPlayer[i]);
- }
- while(1)
- {
- if((P4IN & 0x10) == 0)
- {
- if(bestPlayer[0] != 'Z') bestPlayer[0]++;
- else bestPlayer[0] = 'A';
- opoznienie();
- break;
- }
- if((P4IN & 0x20) == 0)
- {
- if(bestPlayer[1] != 'Z') bestPlayer[1]++;
- else bestPlayer[1] = 'A';
- opoznienie();
- break;
- }
- if((P4IN & 0x40) == 0)
- {
- if(bestPlayer[2] != 'Z') bestPlayer[2]++;
- else bestPlayer[2] = 'A';
- opoznienie();
- break;
- }
- if((P4IN & 0x80) == 0)
- {
- out = 1;
- break;
- }
- }
- if(out == 1) break;
- }
- }
- void Clock(void)
- {
- P4DIR &= 0x00;
- if (licznik % 1 == 0)
- {
- menuGlowne();
- if(czyFalse == 0) wyswietl(0, 3);
- else wyswietl(x1,x2);
- czyFalse = 1;
- while (1)
- {
- while (1)
- {
- if ((P4IN & 0x10) == 0)
- {
- if (x2 == 0)
- {
- P1OUT ^= BIT1;
- x2 = x1;
- x1 = losowaLiczba(4);
- punkty++;
- clearDisplay();
- wyswietl(x1, x2);
- }
- else
- {
- przegrana = 1;
- break;
- }
- opoznienie();
- }
- if ((P4IN & 0x20) == 0)
- {
- if (x2 == 1)
- {
- P1OUT ^= BIT2;
- x2 = x1;
- x1 = losowaLiczba(4);
- punkty++;
- clearDisplay();
- wyswietl(x1, x2);
- }
- else
- {
- przegrana = 1;
- break;
- }
- opoznienie();
- }
- if ((P4IN & 0x40) == 0)
- {
- if (x2 == 2)
- {
- P1OUT ^= BIT3;
- x2 = x1;
- x1 = losowaLiczba(4);
- punkty++;
- clearDisplay();
- wyswietl(x1, x2);
- }
- else
- {
- przegrana = 1;
- break;
- }
- opoznienie();
- }
- if ((P4IN & 0x80) == 0)
- {
- if (x2 == 3)
- {
- P1OUT ^= BIT4;
- x2 = x1;
- x1 = losowaLiczba(4);
- punkty++;
- clearDisplay();
- wyswietl(x1, x2);
- }
- else
- {
- przegrana = 1;
- break;
- }
- opoznienie();
- }
- if (licznik >= czasNaGre)
- {
- licznik = 0;
- break;
- }
- }
- if (przegrana == 1)
- {
- if(best < punkty) hscore();
- clearDisplay();
- char przegrales[11] = "Ups!";
- SEND_CMD(DD_RAM_ADDR);
- for (int i = 0; i < strlen(przegrales); i++)
- {
- SEND_CHAR(przegrales[i]);
- }
- SEND_CHAR(' ');
- char score[9] = "Punkty: ";
- for (int i = 0; i < strlen(score); i++)
- {
- SEND_CHAR(score[i]);
- }
- if (punkty < 10) SEND_CHAR(punkty + 48);
- else
- {
- SEND_CHAR((punkty / 10) + 48);
- SEND_CHAR((punkty % 10) + 48);
- }
- if (punkty > best) best = punkty;
- punkty = 0;
- SEND_CMD(DD_RAM_ADDR2);
- char najlepszy[13] = "Best Score: ";
- for (int i = 0; i < strlen(najlepszy); i++)
- {
- SEND_CHAR(najlepszy[i]);
- }
- if (best < 10) SEND_CHAR(best + 48);
- else
- {
- SEND_CHAR((best / 10) + 48);
- SEND_CHAR((best % 10) + 48);
- }
- for (int i = 0; i < 100; i++)
- {
- P4OUT ^= 0x08;
- for (int i = 0; i < 10000; i++) asm("nop");
- }
- while(1)
- {
- if((P4IN & 0x30) == 0)
- {
- przesun();
- break;
- }
- }
- przegrana = 0;
- break;
- }
- else
- {
- if(best < punkty) hscore();
- clearDisplay();
- char score[9] = "Punkty: ";
- SEND_CMD(DD_RAM_ADDR);
- for (int i = 0; i < strlen(score); i++)
- {
- SEND_CHAR(score[i]);
- }
- if (punkty < 10) SEND_CHAR(punkty + 48);
- else
- {
- SEND_CHAR((punkty / 10) + 48);
- SEND_CHAR((punkty % 10) + 48);
- }
- if (punkty > best) best = punkty;
- punkty = 0;
- SEND_CMD(DD_RAM_ADDR2);
- char najlepszy[13] = "Best Score: ";
- for (int i = 0; i < strlen(najlepszy); i++)
- {
- SEND_CHAR(najlepszy[i]);
- }
- if (best < 10) SEND_CHAR(best + 48);
- else
- {
- SEND_CHAR((best / 10) + 48);
- SEND_CHAR((best % 10) + 48);
- }
- while(1)
- {
- if((P4IN & 0x30) == 0)
- {
- przesun();
- break;
- }
- }
- break;
- }
- break;
- }
- }
- }
- // procedura obs3ugi przerwania od TimerA
- #pragma vector=TIMERA0_VECTOR
- __interrupt void Timer_A(void)
- {
- ++licznik;
- _BIC_SR_IRQ(LPM3_bits); // wyjocie z trybu LPM3
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement