⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 利用freescale公司的MC9S08QE128开发的一套游戏机程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */


#include "global.h"
#include "SystemInitialize.h"
#include "ManipulationLED.h"
#include "motor_control.h"
#include "compute.h"
#include "coin_output.h"
#include "delay.h"
#include "ISD4002.h"

#define DISPLAY_GAP 30


/**************** global data *******************/

// RAM variables
#pragma DATA_SEG _DATA_ZEROPAGE

// counts of coin
static byte Count_CoinInput;
static byte IsGameStart;
static byte Group0_key_pressed;
static byte Group0_key_debounced;


//static byte IsActionStart;

static byte nDelayCount;

static byte DisplayCount;
static unsigned int i_TimerCount;
 

#pragma DATA_SEG DEFAULT

static byte i_Com=4;

static const byte Display[10][3] = {{0,0,13},{1,2,13},{2,4,13},{3,6,13},{4,8,13},  \
                                    {5,0,1},{6,5,1},{7,0,2},{8,0,3},{9,0,5} };
                                    
static  byte CoinOutput_table[10] = {0, 2, 4, 6, 8, 10, 15, 20, 30, 50};
                                                                        


static byte machine_status = RIGHT;
                            
static byte Game_Result = GAME_EVEN;

static byte machine_output;
static byte IsPeopleActed = NO;

byte i_ComDisplay[4] = {13, 13 ,13 ,13};

unsigned int i_DisplayPeriod[4] = {48,48,48,30};
unsigned int i_DisplayCount[4] = {0,0,0,0};

unsigned int CurrCom3Sleep = 0;
unsigned int DisplayCom3[4] = {13, 10, 13, 11};
unsigned int CurrCom012Sleep = 0;

static byte IsMacheineNotSleep = 0;
static byte  MacheineSleepReady = 0;
unsigned int  MacheineSleepCount = 0;

static byte IsYellowLEDsFlash=0;
static byte CurrentFlashCount = 0;
static byte IsCoinIn_start = 0;

static const unsigned int Period_CoinIn = 1500;
static unsigned int Count_CoinIn = 0;

static const unsigned int Period_Game = 1500;
static unsigned int Count_Game = 0;

static byte output_random = 0xff;

static byte IsFixedTimeOut = 0;

extern CoinOutput_timer2_start();

long long Machine_Total_Win = 0;
long long Gamer_Total_Win = 0;

byte IsMachineRandOut = 0;

                                

/************************************************/

void Debounce_timer3_start() 
{
  // timer3 start
  TPM3SC_TOF = 0;
  TPM3CNT = 0x0; 
  //TPM3MOD = 0x138;
  TPM3MOD = 0xEA;
  TPM3SC_TOIE  = 1; 
}

void GameStart(byte IsGameDraw) 
{
    // Disable CoinIn 
    PTGD_PTGD1 = 0;
    PTGDD_PTGDD1 = 1;  
    
    // start sound
    play(0x30);
    play(0x18);
    
  
    // coin in end
    IsCoinIn_start = 0;
    Count_CoinIn = 0;
       
       
    // control motor
    motorCtr();
    
    play(0x30);
    play(0x0a);

    
    // turnon three LEDs
    PTFD_PTFD2 = 0;
    PTED_PTED4 = 0;
    PTFD_PTFD1 = 0;
    IsYellowLEDsFlash = 1;   // 2008-03-22
    CurrentFlashCount = 0;
    
    
    IsMachineRandOut = 0;
    IsGameStart = YES;
    
}


interrupt VectorNumber_Vkeyboard void   KBIx_ISR(void) 
{

  Group0_key_pressed  = YES;
  Group0_key_debounced = NO;
    
  KBI1SC_KBIE  = 0;                       // Disable KBI1 interrupts
  

  // timer3 start
  Debounce_timer3_start();
  
  KBI1SC_KBACK = 1;                       // Clear KBACK

}

// timer3 for key debounce
interrupt VectorNumber_Vtpm3ovf void  TPM3ovf_ISR(void) 
{
  if(Group0_key_pressed) 
  {
    Group0_key_debounced = YES;
    TPM3SC_TOIE  = 0;                     // disable timer interrupt    
  }
      
  TPM3SC_TOF = 0;                         // clear interrupt flag
}



byte ComputeDisplayNum(byte IsNotSleep, byte Com) 
{
  // machine active
  if(IsNotSleep) 
  {
    
    i_DisplayCount[Com] += 1;
    if(i_DisplayCount[Com] >= i_DisplayPeriod[Com]) 
    {
      i_DisplayCount[Com] = 0;
    }
  
    if(i_DisplayCount[Com] < (i_DisplayPeriod[Com]>>1) ) 
    {
      // display NULL
      return 13;
    }
    else
    {
      return i_ComDisplay[Com];
    }
  } 
  else 
  {
    if(Com==3) 
    {
      i_DisplayCount[Com] += 1;
      if(i_DisplayCount[Com] >= i_DisplayPeriod[Com]) 
      {
        i_DisplayCount[Com] = 0;
        CurrCom3Sleep++;
        if(CurrCom3Sleep>=4) 
        {
          CurrCom3Sleep = 0;
        }
      }
      return  DisplayCom3[CurrCom3Sleep];      
    } 
    else 
    {

      i_DisplayCount[0] += 1;
      if(i_DisplayCount[0] >= i_DisplayPeriod[0]) 
      {
        i_DisplayCount[0] = 0;
        CurrCom012Sleep++;
        if(CurrCom012Sleep >= 128) 
        {
          CurrCom012Sleep = 0;
        }
      }
      return  (14+CurrCom012Sleep);      
    }
  }
  
}

// timer1 for LED display & game within fixed time
interrupt VectorNumber_Vtpm1ovf void  TPM1ovf_ISR(void) 
{
  byte DispalyCurr;
  i_Com++;
  if(i_Com >= 4) 
  {
    i_Com = 0;
  }
  
  if(MacheineSleepReady) 
  {
    MacheineSleepCount++;
    if(MacheineSleepCount >= 1000) 
    {
      i_DisplayPeriod[0] = 48;
      i_DisplayPeriod[3] = 30;
      // already sleep
      MacheineSleepReady = 0;
      IsMacheineNotSleep = 0;      
    }

  }
  
  if(IsCoinIn_start) 
  {
      Count_CoinIn++;
      if(Count_CoinIn >= (Period_CoinIn-200)) 
      {

         PTGD_PTGD1 = 0;
         PTGDD_PTGDD1 = 1;
      }
      
      if(Count_CoinIn >= Period_CoinIn) 
      {
  

         IsFixedTimeOut = 1;
         IsCoinIn_start = 0; 
      }
  }
  
  if(IsGameStart && (!IsPeopleActed)) 
  {
      Count_Game++;
      if(Count_Game >= Period_Game) 
      {
         if(TPM2CNT < 21845) 
         {
           output_random = CINKER; 
         } 
         else if(TPM2CNT < 43690)
         {
           output_random = CUT;
         } 
         else 
         {
           output_random = CLOTH;
         }
         
         IsMachineRandOut = 1;
         
         Count_Game = 0;      
      }
      
  }
  
  DispalyCurr = ComputeDisplayNum(IsMacheineNotSleep, i_Com);
   
  // Display[Count_CoinInput][i_Com]
  TurnOnLEDs(i_Com, DispalyCurr);
  
  if( (IsYellowLEDsFlash == 1) && (!IsPeopleActed)) 
  {
     CurrentFlashCount++;
     if(CurrentFlashCount >= 60) 
     {
        if(PTFD_PTFD2 == 0) 
        {
          PTFD_PTFD2 = 1;
          PTED_PTED4 = 1;
          PTFD_PTFD1 = 1;
        } 
        else 
        {

          PTFD_PTFD2 = 0;
          PTED_PTED4 = 0;
          PTFD_PTFD1 = 0;
        }
        
        CurrentFlashCount = 0;
     }
  }
  
  TPM1SC_TOF = 0;                         // clear interrupt flag 
}
void Display_timer1_start() 
{
    // timer1 start
    TPM1SC_TOF = 0;                         // clear interrupt flag
    TPM1CNT = 0x0;                          // reset counter
    TPM1MOD = 0x009F;                       // delay time ?ms
    TPM1SC_TOIE = 1;                        // enable timer interrupt         
}



void Game_restart(result) 
{
     unsigned int i;
      
     // delay 1s
     delay_s(1);
          
     PTJD_PTJD0 = 0;
     PTDD_PTDD4 = 0;
     PTDD_PTDD3 = 0;
     
     // delay 1s
     // delay_s(1);
     
     // turn 0ff three LEDs
     PTFD_PTFD2 = 0;
     PTED_PTED4 = 0;
     PTFD_PTFD1 = 0;     
           
     
     if(result == GAME_EVEN) 
     {
          
          GameStart(1);
          IsGameStart = YES;
          IsPeopleActed = NO;       
     }
     else
     {
          IsGameStart = NO;
          IsPeopleActed = NO;
          

          Count_CoinInput = 0;
          

          Update_LEDs_Data(0, 0, DISPLAY_GAP);
          Update_LEDs_Data(1, 13, 1);
          Update_LEDs_Data(2, 13, 1);
          Update_LEDs_Data(3, 13, 1);
          

          PTGD_PTGD1 = 1;
          PTGDD_PTGDD1 = 1;
          

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -