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

📄 realtimer.c.bak

📁 ARM s3c44box的一些程序代码
💻 BAK
字号:
/*********************************************************************
 *	Description:	
 *		This pgm demostrate the RTC block control and setting.	
 *		Use polling mothed to handle input/output.
 *	History:
 *		1) Rev. 1.0: primarily version; by felix; Oct.10/2002
 *	
 *	Status:
 *		Function test only, not overall. Please contat us if bug detected.
 *		All rights reserved by Fudan JHB Tech. Co. Ltd.
 *
 *	Any quesiton or comments, please feel free to contact us.
 *********************************************************************/

#include "..\include\global_function.h"
#include "realTimer.h"

int main(void);
B32 getKey(B32 KeyStatus_Save);
void PortGInit(void);
void RTCInit(void);


int main(void)
{
   B8   regData;									//data read from register
   B8   regSEC_Save, regMIN_Save, regHOR_Save;	//back up for reg content
   B8   BCD_OH, BCD_OL;							//output(display) code
   
   B32 KeyStatus;		//indicate whitch button is pushed
   
   // function mode & state: present the status for each button
   B32 select;			//button1: loop between Sec -> Min -> Hour -> Sec
   B32 count;			//button2: active in SET state: increase the setting number
   B32 pause;			//button3: active in RUN state: pause the time
   B32 state;			//button4: loop between RUN -> SET -> RUN

   B32 CountH, CountL, CountNum;	//use in SET mode:  decimal counter number

   //* config portG as input4567(button) and output1234(LED)
   PortGInit();
   //* block init
   RTCInit();

   //* default setting
   KeyStatus = 0;				//no button push
   select = SELECT_SEC;
   count = OFF;
   pause = OFF;
   state = RUN_STATE;

   while(TRUE)				//infinite loop
   {   
   	switch(KeyStatus = getKey(KeyStatus) ) // read input port state
   		{
		  case 0:						//no button status change
		  	break;					//no action
		  case BUTTON_1:					
		  	if(select == 2)			//count 0~2: Sec, Min, Hor
		  		select = 0;
		  	else
		  		select++;
		  	break;
		  case BUTTON_2:
			if(state == SET_STATE)
				count = ON;
			else
				count = OFF;
			break;
		  case BUTTON_3:
		  	if(state == RUN_STATE)	//pause state: ON <=> OFF
		  		if(pause == ON)
		  			pause = OFF;
		  		else
		  			pause = ON;
		  	break;
		  case BUTTON_4:
		  	if(state == RUN_STATE)	//mode state: RUN <=> SET
		  		state = SET_STATE;
		  	else
		  		state = RUN_STATE;
		  	break;
   		}      
 	
	if(state == RUN_STATE)				// RUN mode
		{
		  if(pause == ON)
		  	{
		  	  rBCDSEC = regSEC_Save;		// keep rBCDSEC reg 
		  	  continue;					//continue the big while(TRUE) loop
		  	}
		  else
		  	{
			 switch(select)
	  		    {
		  		case SELECT_HOR:
					regData = rBCDHOUR & 0x3f;
					break;
		  		case SELECT_MIN:
					regData = rBCDMIN & 0x7f;
					break;
		  		case SELECT_SEC:
					regData = rBCDSEC & 0x7f;
					break;
			     }
			  //* back up current time for setting or pause use
			  regSEC_Save = rBCDSEC;
			  regMIN_Save = rBCDMIN;
			  regHOR_Save = rBCDHOUR;
			  
			  BCD_OH = getBCD( (regData & 0xf0) >> 4 );		
	  		  BCD_OL = getBCD( (regData & 0x0f) );	
		  	}
		}
	else									// SET mode
		{
		 switch(select)
	  	    {
		  	case SELECT_HOR:
				regData = regHOR_Save;
				break;
		  	case SELECT_MIN:
				regData = regMIN_Save;
				break;
		  	case SELECT_SEC:
				regData = regSEC_Save;
				break;
	  	    }
	  	    
		CountH = (regData & 0xf0) >> 4;
		CountL  = regData & 0x0f;
		CountNum = CountH * 10 + CountL;
			
		if(count == ON)
		    {
			  if(select == SELECT_HOR)		// hour counter: 0-23
			  	{
				  if(CountNum < 23)
				  	CountNum++;
				  else
				  	CountNum = 0;
			  	}
			 else						// min/sec counter: 0-59
			 	{
				  if(CountNum < 59)
				  	CountNum++;
				  else
				  	CountNum = 0;
			 	}  	
			 count = OFF;					// increase is done
			 CountL = CountNum % 10;
			 CountH = CountNum / 10;
		    }

		BCD_OH = getBCD(CountH) | 0x80;		// light point
	  	BCD_OL = getBCD (CountL) | 0x80;
		
		//* update new setted value to register
		switch(select)
		    {
			case(SELECT_SEC):
				rBCDSEC = CountH << 4 | CountL;
				regSEC_Save = rBCDSEC;
				break;
			case(SELECT_MIN):
				rBCDMIN = CountH << 4 | CountL;
				regMIN_Save = rBCDMIN;
				break;
			case(SELECT_HOR):
				rBCDHOUR = CountH << 4 | CountL;
				regHOR_Save = rBCDHOUR;
				break;
		    }
		}
	
     //* output: display LED	
     rPDATG = ~((state)|(pause << 1));//state and pause LED will be lighted in actived mode
     mLEDSET = (BCD_OH<<8) | BCD_OL;
     
   }    
}


// config GPIO_G port as input and output
void PortGInit(void)
{
   rPCONG = 0x0055;		       // GPIOG [3:0]: output; [7:4]: intput
   rPUPG = 0x1;				//internal pull-up enable
   rPDATG = 0xf;
}


// initialize the RTC block
void RTCInit(void)
{
   rRTCCON = 0x80;			//reset RTC
   __asm {nop					// insert asm into C lines demo
          	nop
          	nop
          	nop}
   rRTCCON = 0x01;			//enable R/W RTC block registers
   rRTCRST = 0x0d;			//round reset enable; carry gen over 50s
   rBCDHOUR = 0x01;			//init time 
   rBCDMIN  = 0x02;
   rBCDSEC = 0x0;
}


// polling the input state: which button is pushed
// return value: BUTTON_1 or 2 or 3 or 4
//				or 0 (no button pushed)
B32 getKey(B32 KeyStatus_Save)
{
  B32 input;

  input = (~rPDATG) & 0x000000f0;
  
  rPDATG = (rPDATG & 0x0f) & (~input >> 4); //light the related LED if a button is pushed

  Delay(120);							// jitter protect: delay 200ms
  
  if( input == KeyStatus_Save)
  	return (0);						// no change in input
  else
  	{
  	 switch(input)
  		{
		  case 0x10:
		  	return (BUTTON_4);
		  	break;
		  case 0x20:
		  	return (BUTTON_3);
		  	break;
		  case 0x40:
		  	return (BUTTON_2);
		  	break;
		  case 0x80:
		  	return (BUTTON_1);
		  	break;
		  default:
		  	return (0);
  		}
  	}
}

⌨️ 快捷键说明

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