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

📄 reactime_samplesession.c

📁 nec demo source code
💻 C
字号:
//============================================================================
// PROJECT      = 78K0S/KA1+ Low Pin Count - DemoKit-KA1
// MODULE       = reactime_samplesession.c
// SHORT DESC.  = -
// DEVICE       = uPD78F9222
// VERSION      = 1.0
// DATE         = 22.12.2004
// LAST CHANGE  = -
// ===========================================================================
// Description:  This sample program demonstrates a reaction time measurement.
//               The application starts by flashing LED's D1-D4 two times. After
//               a press of button SW1 the application waits for a random time
//               between 0.50 and 3.45 seconds. Then LED D4 is switched on and
//               measurement starts by incrementing a reaction counter every
//               50ms. The actual counter value is shown by LED's D1-D4 (binary format)
//               until the next keystroke of button SW1. After a press of button SW1
//               is detected, the measurement stops and the reaction time is shown
//               by flashing LED's D1-D4. Pressing button SW2 starts a new measuring cycle
//
// ===========================================================================

#pragma sfr
#pragma DI
#pragma EI
#pragma interrupt INTTM80 isr_INTTM80
#pragma interrupt INTP0 isr_INTP0
#pragma interrupt INTTMH1 isr_INTTMH1
#pragma section @@CNST  OPT AT 80H
const char OPTION=0x98;


//-----------------------------------------------------------------------------
// Global Defines
//-----------------------------------------------------------------------------

#define LED1   P2.3    	    // LED D1
#define LED2   P13.0	    // LED D2
#define LED3   P4.5         // LED D3
#define LED4   P12.3	    // LED D4

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
unsigned char Timer80Flag;
unsigned char TimerH1Flag;
unsigned char IntP0Flag;

//-----------------------------------------------------------------------------
// Function prototyps
//-----------------------------------------------------------------------------

void drive_LED(unsigned char value);
void flash_LED(unsigned char Number);
 

//-----------------------------------------------------------------------------
// Module:   init_CPU
// Function: Initialization of CPU
//-----------------------------------------------------------------------------
void init_CPU (void)
{
// stop watchdog timer
  WDTM = 0x70;

//clock gererator settings
  PCC = 0x00;			// set CPU clock to fx
  PPCC = 0x00;

  LSRCM = 0x01;			// low speed ring oscillator stops

  OSTS  = 0x00;			//shortest stabilisation time 2^10/fx

// interrupt setting
   IF0  = 0x00;
   IF1  = 0x00;
   MK0  = 0xFF;
   MK1  = 0xFF;
}

//-----------------------------------------------------------------------------
// Module:   init_LED
// Function: Initialization of LED ports
//-----------------------------------------------------------------------------
void init_LED (void)
{
  PMC2.3=0;           // set port mode control of P23 to port mode
  PM2.3=0;            // set port P23 to output mode -> LED1
  PM4.5=0;            // set port P45 to output mode -> LED3
  PM12.3=0;           // set port P123 to output mode -> LED4

  LED1=0;     	    // drive LED1
  LED2=0;	    // drive LED2
  LED3=0;           // drive LED3
  LED4=0;	    // drive LED4

  LED1=1;	    // switch LED1 off
  LED2=1;	    // switch LED2 off
  LED3=1;	    // switch LED3 off
  LED4=1;	    // switch LED4 off
}

//-----------------------------------------------------------------------------
// Module:   init_TM80
// Function: Initialization of Timer80
//-----------------------------------------------------------------------------
void init_TM80()
{
   TCE80 = 0;		//stop timer80
   TMC80 = 0x02;	//set input clock to fxp / 2^8 = 31,25 kHz @ 8MHz
   CR80  = 0x7;		//set interval time to 225 us	 
   TMIF80 = 0;		//clear interrupt request flag
   TMMK80=0;		//enable timer80 interrupt
}

//-----------------------------------------------------------------------------
// Module:   init_TMH1
// Function: Initialization of TimerH1
//-----------------------------------------------------------------------------
void init_TMH1()
{
   TMHE1 = 0;           //stop timerH1
   TMHMD1 = 0x40;	//set input clock to fxp / 2^12 = 1953,125 Hz @ 8MHz
   CMP01  = 0x62;	//set interval time to 50ms 
   TMIFH1 = 0;		//clear interrupt request flag
   TMMKH1=0;		//enable timerH1 interrupt
}

//-----------------------------------------------------------------------------
// Module:   restart_TM80
// Function: This module restarts Timer80
//-----------------------------------------------------------------------------
void restart_TM80 (void)
{
   Timer80Flag=0;               // Reset status flag Timer80
   TCE80 = 0;
   TCE80 = 1;
}

//-----------------------------------------------------------------------------
// Module:   restart_TMH1
// Function: This module restarts TimerH1
//-----------------------------------------------------------------------------
void restart_TMH1 (void)
{
   TimerH1Flag=0;               // Reset status flag TimerH1
   TMHE1 = 0;
   TMHE1 = 1;
}

//-----------------------------------------------------------------------------
// Module:   Wait_50ms
// Function: This module delays the program for n * 50 ms.
//-----------------------------------------------------------------------------
void Wait_50ms(unsigned char Number)
{
  restart_TMH1(); 
  while(Number > 0) {
        while(TimerH1Flag == 0){
        }
        TimerH1Flag = 0;              // Reset status flag TimerH1
        Number--;
    }
}

//-----------------------------------------------------------------------------
// Module:   Random
// Function: This module generates a pseudo-random number.
//-----------------------------------------------------------------------------
unsigned char Random(void)
{
    return ((((TM80)%6)+1)*10);             // pseudo-random number between 10 ... 60   
}                       

//-----------------------------------------------------------------------------
// Module:   main
// Function: main program
//-----------------------------------------------------------------------------
void main(void)
{
  unsigned char i;

  DI();        // global interrupt disable                 
  init_CPU();           	// CPU initialization
  init_LED();		 	// LED port initialization

  init_TM80();			// initialization of timer80
  init_TMH1();			// initialization of timerH1

  restart_TM80();		// start timer80
  restart_TMH1();		// start timerH1

  EI(); 	// global interrupt enable
 
  INTM0=0x00;			// set falling edge detection for INTP0	
  PMK0=0;			// enable external interrupt INTP0

  flash_LED(2);                  // Signal Program start

  while(1)
  {
     if(IntP0Flag==1)               // Wait for key-stroke of Key1
     {
      Wait_50ms(Random());          // Wait a random time 0.5 ... 3 s
      i=0;
      restart_TMH1();               // Start TimerH1

      drive_LED(0xf);	
      IntP0Flag=0;                  // Reset Key1 Flag          
      while ((i<8)&& IntP0Flag==0)
      {
         while (TimerH1Flag==0);    // Wait 50ms
          TimerH1Flag=0;            // Reset Timer51 Flag
          drive_LED(i);             // Switch next LED on
          i++;
      }
      while(1)			    // show result	
      {	
          drive_LED(i);			
          Wait_50ms(10);            // Delay of 500 ms 
          drive_LED(0x00);
          Wait_50ms(10);            // Delay of 500 ms 
      }
    }
  }
}

//-----------------------------------------------------------------------------
// ISR: 	  isr_INTTM80
// Function: Interrupt service routine of Timer80
//-----------------------------------------------------------------------------
__interrupt void isr_INTTM80(void)
{
  Timer80Flag=0x1;    // Set status flag for Timer80
}

//-----------------------------------------------------------------------------
// ISR: 	  isr_INTTMH1
// Function: Interrupt service routine of Timer80
//-----------------------------------------------------------------------------
__interrupt void isr_INTTMH1(void)
{
  TimerH1Flag=0x1;    // Set status flag for TimerH1
}

//-----------------------------------------------------------------------------
// ISR: 	  isr_INTP0
// Function: Interrupt service routine for external interrupt INTP0
//-----------------------------------------------------------------------------
__interrupt void isr_INTP0(void)
{
  IntP0Flag=1;        // Set status flag INTP0 (Key1)
}

//-----------------------------------------------------------------------------
// Module:   drive_LED
// Function: Output of 4-bit value to LED port
//-----------------------------------------------------------------------------
void drive_LED(unsigned char value)
{
  LED1 =~(value>>3);
  LED2 =~(value>>2);
  LED3 =~(value>>1);
  LED4 =~(value);
}

//-----------------------------------------------------------------------------
// Module:   flash_LED
// Function: This module flashes all LEDs n times.
//-----------------------------------------------------------------------------
void flash_LED(unsigned char Number)
{
    while(Number>0) {
       drive_LED(0xf);
       Wait_50ms(10);                    // Delay of 500 ms 
       drive_LED(0x0);
       Wait_50ms(10);                    // Delay of 500 ms 
       Number--;
    }
}

⌨️ 快捷键说明

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