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

📄 coin_output.c

📁 利用freescale公司的MC9S08QE128开发的一套游戏机程序
💻 C
字号:
#include "derivative.h" /* include peripheral declarations */
#include "ManipulationLED.h"
#include "global.h"
#include "delay.h"

static const byte Period_MotorOn = 4;
static byte Count_MotorOn = 0;
static byte TimeOut_MotorOn = 0;
static byte IsMotorWork = 0;
static byte Count_CoinOut_this = 0;
static byte Count_NoCoinOut = 0;
static byte NoCoinInMachine = 0;

// timer2 for Coin Output
interrupt VectorNumber_Vtpm2ovf void  TPM2ovf_ISR(void) 
{     
    if(IsMotorWork) 
    {      
      Count_MotorOn++;
      if(Count_MotorOn >= Period_MotorOn) 
      {
        TimeOut_MotorOn = 1;
        Count_MotorOn = 0;
      }
    }
    TPM2SC_TOF = 0;                         // clear interrupt flag
}

void CoinOutput_timer2_start() 
{
    // timer2 start
    TPM2SC_TOF = 0;                         // clear interrupt flag
    TPM2SC_TOIE = 0;                        // disable timer interrupt

    TPM2CNT = 0x0;                          // reset counter
    TPM2MOD = 0xFFFF;                       // delay time 2s
    TPM2SC_TOIE = 1;                        // enable timer interrupt         
}


byte coin_output(byte nOutput_cion) 
{
  byte i;
  byte Output_current = 0;
  byte flag_a,flag_b;
      
  
  PTDPE = 0xFF;                // Enable PORT D Internal Pullups  
  PTDD_PTDD5 = 0;  
  
  PTGPE = 0xFF;                // Enable PORT G  Internal Pullups 
  PTGD_PTGD6 = 1;
  PTGDD_PTGDD6 =  1;           // Start motor
  
  IsMotorWork = 1;
  Count_MotorOn = 0;
  TimeOut_MotorOn = 0;
  // CoinOutput_timer2_start();   // Start timer2

  flag_a = 0;
  flag_b = 0;
  while(1) 
  {
      if( (PTDD_PTDD5 == 1) && (!flag_a) ) 
      {
        flag_a = 1;
      }
    
      if((PTDD_PTDD5 == 0) && flag_a)
      {
        flag_b = 1;
      }
      
      if(flag_b && flag_a)
      {
        Count_CoinOut_this++;
        Output_current++;    
        flag_b = 0;
        flag_a = 0;
        if(Output_current == nOutput_cion) 
        {
            // stop motor              
            PTGD_PTGD6 = 0;
            PTGDD_PTGDD6 = 1;
            
            
            // 出币计数器增加nOutput_cion个
            for(i=0; i<nOutput_cion; i++) 
            {
              PTGD_PTGD5 = 1;
              PTGDD_PTGDD5 = 1;
              delay_50ms();
              PTGD_PTGD5 = 0;
              delay_50ms();     
            }

            
            return RIGHT; 
        }
      }
      
      if(TimeOut_MotorOn)
      {
          // Stop motor
          PTGD_PTGD6 = 0;
          PTGDD_PTGDD6 = 1;
          IsMotorWork = 0;
          
          if(Count_CoinOut_this==0) 
          {
            Count_NoCoinOut++;
            if(Count_NoCoinOut >= 4) 
            {
               NoCoinInMachine = 1; 
            }
          }
          else
          {
             Count_NoCoinOut=0; 
          }
          
          // delay 4s
          delay_s(2);
          
          // restart motor
          PTGD_PTGD6 = 1;
          PTGDD_PTGDD6 = 1;
          Count_MotorOn = 0;
          TimeOut_MotorOn = 0;
          IsMotorWork = 1;
          
          Count_CoinOut_this = 0;          
      }
      
     
      if(NoCoinInMachine && (Output_current != nOutput_cion)) 
      {
        // stop motor  
        PTGD_PTGD6 = 0;
        PTGDD_PTGDD6 = 1;
        
        // 出币计数器增加Output_current个
        for(i=0; i<Output_current; i++) 
        {
          PTGD_PTGD5 = 1;
          PTGDD_PTGDD5 = 1;
          delay_50ms();
          PTGD_PTGD5 = 0;
          delay_50ms();     
        }
        // output machine error
        Update_LEDs_Data(3, 12, 1);
        // 显示未出币个数
        Update_LEDs_Data(1, ((nOutput_cion-Output_current)%10),30);
        Update_LEDs_Data(2, ((nOutput_cion-Output_current)/10), 30);
        return ERROR;        
      }      
  }
 
}

⌨️ 快捷键说明

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