Advertisement
Guest User

proj2-c

a guest
Jan 20th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.37 KB | None | 0 0
  1. #include <ioavr.h>
  2. #include <inavr.h>
  3. #include "hd44780.h"
  4.  
  5. #define IN_DEBUG 0
  6.  
  7. #define DV_480Hz 15
  8.  
  9. #define KB_IN PINE
  10. #define KB_OUT PORTE
  11. #define KB_CTRL DDRE
  12.  
  13. #define KEY_0 0x01
  14. #define KEY_1 0x02
  15. #define KEY_2 0x04
  16. #define KEY_3 0x08
  17. #define KEY_4 0x10
  18. #define KEY_5 0x20
  19. #define KEY_6 0x40
  20. #define KEY_7 0x80
  21.  
  22. #define KB_DEB_TM 2
  23. #define KB_TPM_DY 200
  24. #define KB_REP_DY 50
  25. #define KB_IDLE 0
  26. #define KB_DEB 1
  27. #define KB_REP 2
  28. #define KB_REL 3
  29. #define KB_REL_DEB 4
  30.  
  31. char c_reset[] =        "reset";
  32. char c_newgame[] =      "new-g";
  33. char c_stand[] =        "stand";
  34. char c_hit[] =          "-hit-";
  35.  
  36. int UserCards[11];
  37. int playerSum;
  38. int ComputerSum;
  39. int ComputerCards[2];
  40. int playersScore;
  41. int lcdPosition;
  42. int newGameLock;
  43.  
  44. typedef union TSystemEvent {
  45.   unsigned char ev;
  46.   struct {
  47.     unsigned char time : 1;
  48.     unsigned char kb : 1;
  49.     unsigned char dummy : 6;
  50.   };
  51. } TSystemEvent;
  52.  
  53. void kbService();
  54. __no_init unsigned char kb_rep;
  55. __no_init unsigned char kb_reg;
  56. volatile TSystemEvent rq;
  57. unsigned char rnd_cnt;
  58. unsigned char rnd_rg = 9; // TODO 9, 12
  59. unsigned char rnd_val;
  60.  
  61. void kbInit()
  62. {
  63.   KB_CTRL = 0x00;
  64.   KB_OUT = 0xFF; //Enable Pull-Up on port
  65. }
  66.  
  67. void initDevices()
  68. {
  69.   OCR0 = DV_480Hz - 1;
  70.   TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01) | (1 << CS00);
  71.   TIMSK = (1 << OCIE0);
  72.   __enable_interrupt();
  73.   kb_rep = 0;
  74.   LCDInit();
  75. }
  76.  
  77. int getCardVal()
  78. {
  79.   return rnd_cnt;
  80. }
  81.  
  82. void mypf(char c[], int pos)
  83. {
  84.   if (IN_DEBUG)
  85.   {
  86.     LCDClear();
  87.     int i = 0;
  88.     while (i < 5)
  89.     {
  90.       LCDGoTo(lcdPosition);
  91.       LCDPutChar(c[i]);
  92.       ++lcdPosition;
  93.       i++;
  94.     }
  95.   }
  96. }
  97.  
  98. void newGame(){
  99.   for (int i = 0; i < 11 ; ++i)
  100.   {
  101.     UserCards[i] = 0;
  102.   }
  103.  
  104.   ComputerCards[0] = 0;
  105.   ComputerCards[1] = 0;
  106.   lcdPosition = 0;
  107.   playerSum = 0;
  108.   ComputerSum=0;
  109.   newGameLock = 0;
  110.  
  111.   mypf(c_newgame, lcdPosition);
  112.  
  113.   UserCards[0] = getCardVal();
  114.   UserCards[1] = getCardVal();
  115.  
  116.   playerSum += UserCards[0];
  117.   playerSum += UserCards[1];
  118.  
  119.   ComputerCards[0] = getCardVal();
  120.   ComputerCards[1] = getCardVal();
  121.   ComputerSum += ComputerCards[0];
  122.   ComputerSum += ComputerCards[1];
  123.  
  124.   LCDGoTo(lcdPosition);
  125.   LCDPutChar('C');
  126.   ++lcdPosition;
  127.  
  128.   LCDGoTo(lcdPosition);
  129.   LCDPrintDgt(ComputerCards[0]);
  130.   ++lcdPosition;
  131.   ++lcdPosition;
  132.  
  133.   LCDGoTo(lcdPosition);
  134.   LCDPrintSpace(1);
  135.   ++lcdPosition;
  136.  
  137.   LCDGoTo(lcdPosition);
  138.   LCDPutChar('P');
  139.   ++lcdPosition;
  140.  
  141.   LCDGoTo(lcdPosition);
  142.   LCDPrintDgt(playerSum);
  143.   LCDPrintSpace(1);
  144.   LCDPrintDgt(playersScore);
  145.  
  146.   if(playerSum > 21)
  147.   {
  148.     --playersScore;
  149.     newGameLock = 1;
  150.   }
  151. }
  152.  
  153. void reset()
  154. {
  155.   mypf(c_reset, lcdPosition);
  156.  
  157.   playersScore = 0;
  158.   newGame();
  159.   LCDClear();
  160. }
  161.  
  162. void stand()
  163. {
  164.   if (newGameLock == 0)
  165.   {
  166.     mypf(c_stand, lcdPosition);
  167.  
  168.     int UserResult = 21 - playerSum;
  169.     int CompResult = 21 - ComputerSum;
  170.  
  171.     if (UserResult < CompResult)
  172.     {
  173.       ++playersScore;
  174.     }
  175.     else
  176.     {
  177.       --playersScore;
  178.     }
  179.    
  180.     newGameLock = 1;
  181.     LCDPrintDgt(playersScore);
  182.   }
  183. }
  184.  
  185. void hit()
  186. {
  187.   if (newGameLock == 0)
  188.   {
  189.     mypf(c_hit, lcdPosition);
  190.     playerSum += getCardVal();
  191.     LCDGoTo(lcdPosition);
  192.     LCDPrintDgt(playerSum);
  193.  
  194.     if(playerSum > 21)
  195.     {
  196.       --playersScore;
  197.       newGameLock = 1;
  198.     }
  199.   }
  200. }
  201.  
  202. void main()
  203. {
  204.   initDevices();
  205.   reset();
  206.  
  207.   for( ; ; )
  208.   {
  209.     if(rq.kb)
  210.     {
  211.       // “RESET”, “NEW GAME”, “STAND”, “HIT”
  212.       if(kb_reg & 0x10) reset(); // kb_req - wartosc przycisku
  213.       else if(kb_reg & 0x40) newGame();
  214.       else if(kb_reg & 0x20) stand();
  215.       else if(kb_reg & 0x80) hit();
  216.      
  217.       //Remove notifications
  218.       kb_reg = KB_IDLE; // zerowanie zeby nie dzialo sie w nieskonczonosc
  219.     }
  220.   }
  221. }
  222.  
  223. #pragma vector = TIMER0_COMP_vect
  224. __interrupt void T0_COMP_ISR()
  225. {
  226.   if((++rnd_cnt) == rnd_rg)
  227.   rnd_cnt = 2; // TODO 2
  228.   kbService();
  229. }
  230. void kbService()
  231. {
  232.   static char kb_st;
  233.   static unsigned char kb_prev;
  234.  
  235.   static unsigned char kb_tmr;
  236.   unsigned char kb;
  237.  
  238.   kb = ~KB_IN;
  239.  
  240.   switch(kb_st)
  241.   {
  242.     case KB_IDLE:
  243.       if(kb == 0) return;
  244.  
  245.       kb_prev = kb;
  246.  
  247.       while(1)
  248.       {
  249.         if(kb_prev & 0x01) break;
  250.         kb_prev = kb_prev >> 1;
  251.       }
  252.    
  253.       kb_prev = kb_prev >> 1;
  254.  
  255.       if(kb_prev) return;
  256.      
  257.       kb_prev = kb;
  258.       kb_st = KB_DEB;
  259.       kb_tmr = KB_DEB_TM;
  260.       break;
  261.  
  262.     case KB_DEB:
  263.       if(kb != kb_prev) kb_st = KB_REL;
  264.      
  265.       if(--kb_tmr) return;
  266.      
  267.       kb_reg = kb;
  268.       rq.kb = 1;
  269.       rnd_val = rnd_cnt;
  270.      
  271.       if(kb & kb_rep)
  272.       {
  273.         kb_st = KB_REP;
  274.         kb_tmr = KB_TPM_DY;
  275.       }
  276.       else
  277.         kb_st = KB_REL;
  278.      
  279.       break;
  280.      
  281.     case KB_REP:
  282.       if(kb != kb_prev)
  283.       {
  284.         kb_st = KB_REL;
  285.         return;
  286.       }
  287.      
  288.       if(--kb_tmr) return;
  289.      
  290.       kb_tmr = KB_REP_DY;
  291.       kb_reg = kb;
  292.       rq.kb = 1;
  293.      
  294.       break;
  295.  
  296.     case KB_REL:
  297.       if(kb == 0)
  298.       {
  299.         kb_st = KB_REL_DEB;
  300.         kb_tmr = KB_DEB_TM;
  301.       }
  302.       break;
  303.    
  304.     case KB_REL_DEB:
  305.       if(kb)
  306.       {
  307.         kb_st = KB_REL;
  308.         return;
  309.       }
  310.      
  311.       if(--kb_tmr) return;
  312.      
  313.       kb_st = KB_IDLE;
  314.      
  315.       break;
  316.      
  317.     default:
  318.       kb_st = KB_IDLE;
  319.   }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement