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

📄 timer_samplesession.c

📁 nec demo source code
💻 C
字号:
//============================================================================
// PROJECT      = 78K0S/KA1+ Low Pin Count - DemoKit-KA1
// MODULE       = timer_samplesession.c
// SHORT DESC.  = -
// DEVICE       = uPD78F9222
// VERSION      = 1.0
// DATE         = 22.12.2004
// LAST CHANGE  = -
// ===========================================================================
// Description:  This sample program simulates a darkroom timer. The board starts
//               after reset flashing all LED's. After first key press of SW1, the
//               board starts counting up expose times in unit of minutes (binary
//               output format). By pressing SW1 a second time the shown elapse time
//               is selected and counting is started. After the selected time is
//               finished the elapse time is displayed by flashing the LED's twenty
//               times and the stop mode is entered. By pressing SW1 stop mode can
//               be released.
//
//				 Set sim value to switch code for SM+ or flash device.  Note that
//				 for SM+ the timer is a lot faster.
// ===========================================================================

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


//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
struct {
          unsigned char no0:1;
          unsigned char no1:1;
          unsigned char no2:1;
          unsigned char no3:1;
          }status__FLAGS;

struct {
          unsigned char sec;
          unsigned char min;
          unsigned char hour;
         }time;

unsigned char elapse_time;
unsigned char ucState;
unsigned char sim = 0;						//sim = 1 for SM+
											//sim = 0 for flash device
//-----------------------------------------------------------------------------
// 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

#define SW1    P3.0         // key SW1

#define IntP0Flag   status__FLAGS.no0
#define TimerH1Flag status__FLAGS.no1
#define Timer80Flag status__FLAGS.no2
#define match_Flag  status__FLAGS.no3

#define START     0x00
#define INPUT     0x01
#define TIMER     0x02
#define TIMEREND  0x03

//-----------------------------------------------------------------------------
// Function prototyps
//-----------------------------------------------------------------------------
void drive_LED(unsigned char value);
void flash_LED(unsigned char Number, unsigned char value);

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

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

//  HSRCM = 0x00;			// high speed ring oscillator operates
  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_SW
// Function: Initialization of variables
//-----------------------------------------------------------------------------
void init_SW (void)
{
  IntP0Flag=0;                  //reset SW status flag for interrupt INTP0
  TimerH1Flag=0;                //reset SW status flag for interrupt INTTMH1
  Timer80Flag=0;                //reset SW status flag for interrupt INTTM80
  match_Flag=0;                 //reset SW status flag for

  time.sec = 0;
  time.min = 0;
  time.hour= 0;

  elapse_time = 0;
  ucState = START;
}

//-----------------------------------------------------------------------------
// 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 (unsigned char interval)
{
   TCE80 = 0;           //start timer80
   TMC80 = 0x06;	//set input time to fxp / 2^16 = 120Hz @ 8MHz
   CR80  = interval;	//set interval	
   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)
{
   TCE80 = 0;           //stop timer80
   TCE80 = 1;           //start timer80
}

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

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

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

  DI();        //global interrupt disable
  init_CPU();
  init_LED();			//LED port initialization
  init_SW();
  init_TM80(0x78);		//initialization of timer80 (interval time = 1 sec)
  init_TMH1();                  //initialization of timerH1

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

  EI(); 	//global interrupt enable

  while(1)
  {
     switch (ucState)
     {
          case START:
             		flash_LED(1,0xf);                  //flash LEDs
             		if(IntP0Flag)
             		{
                  		ucState=INPUT;
                  		IntP0Flag=0;               //clear SW flag for INTP0
             		}
             		break;
         case INPUT:
            		Wait_50ms(10);
            		if(IntP0Flag)
            		{
                 		ucState=TIMER;
	                 	IntP0Flag=0;
	                 	flash_LED(3,elapse_time);  //show elapse_timer
	                 	restart_TM80();            //start time measurment
	                 	led_pattern=1;
	                  	break;
	             	}
	             	elapse_time++;
	             	if(elapse_time>15) elapse_time=0;
	             	drive_LED(elapse_time);
             		break;
          case TIMER :
             		if(match_Flag)                     //elapse timer reached?
             		{
                  		ucState=TIMEREND;
                  		TCE80 = 0;                 //stop timer80
                 		break;
             		}
             		if(Timer80Flag)
             		{
                  		Timer80Flag=0;
                  		drive_LED(led_pattern);               //update LEDs
                  		led_pattern=(led_pattern<<1)+1;
                  		if(led_pattern==0x1f) led_pattern=0;
             		}
             		break;
          case TIMEREND :
                  	for(i=0;i<20;i++)                  //show elapse time reached
                  	{
                      		drive_LED(elapse_time);
                      		Wait_50ms(5);
                      		drive_LED(0x0);
                      		Wait_50ms(5);
                  	}
                  	STOP();                         //switch to stop mode
                  	init_SW();
                  	break;
          default:
             		ucState = START;
             		break;
     }
  }
}

//-----------------------------------------------------------------------------
// ISR: 	  isr_INTTM80
// Function: Interrupt service routine of Timer80
//-----------------------------------------------------------------------------
__interrupt void isr_INTTM80(void)
{
   Timer80Flag=1;
   time.sec++;				
   if(time.sec==60)
   {
     time.sec=0;
     time.min++;
     if(time.min==60)
     {
       time.min=0;
       time.hour++;
       if(time.hour==24)
       {
         time.hour=0;
       }
     }
   }
   if (time.min == elapse_time) match_Flag=1;
}

//-----------------------------------------------------------------------------
// ISR: 	  isr_INTTMH1
// Function: Interrupt service routine of Timer80
//-----------------------------------------------------------------------------
__interrupt void isr_INTTMH1(void)
{
  TimerH1Flag=1;    // 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 n LEDs n times.
//-----------------------------------------------------------------------------
void flash_LED(unsigned char number, unsigned char value)
{
    while(number>0) {
       drive_LED(value);
       Wait_50ms(5);                    // Delay of 500 ms
       drive_LED(0x0);
       Wait_50ms(5);                    // Delay of 500 ms
       number--;
    }
}

⌨️ 快捷键说明

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