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

📄 main.c

📁 电子时钟 通过按键设置时钟
💻 C
字号:
//==========================================================================
//	The information contained herein is the exclusive property of
//	Sunnnorth Technology Co. And shall not be distributed, reproduced,
//	or disclosed in whole in part without prior written permission.
//	(C) COPYRIGHT 2003 SUNNORTH TECHNOLOGY CO.
//	ALL RIGHTS RESERVED
//	The entire notice above must be reproduced on all authorized copies.
//============================================================================
//============================================================================
//  工程名称:  ex3_Clock  
//  功能描述:   《实验指导书》综合实验
//              电子时钟
//              通过按键设置时钟,按确认键后,时钟运行。
//  IDE环境:   SUNPLUS u'nSPTM  IDE 1.8.0(or later)
//  涉及的库:	CMacro1016.lib
//  组成文件:  main.c
//              isr.asm/Dig.asm/Key.asm/system.asm
//              Dig.inc/SPCE061A.inc/Key.inc
//              Dig.h/SPCE061A.h/Key.h
//	硬件连接:	IOA口低8位连接1*8数码管的段选
//				IOB口IOB15 、IOB14、IOB13、IOB12、IOB2、IOB1、IOB0连接位选
//				IOA口高8位连接4*4键盘
//  日期:       2005-10-14 v1.0
//============================================================================

//========================================================
//	文件名称:	main.c
//	功能描述:	时钟时间设置和显示
//	维护记录:	2005-10-14	v1.0
//========================================================
#include "Key.h"
#include "Dig.h"
#include "SPCE061A.h"

typedef  unsigned char uchar;
#define KEY_DEL			11
#define KEY_ENT			12

#define  YESCarryFlag   1
#define  ClearCarryFlag 0
#define  YESIntFlag     1
#define  ClearIntFlag   0
#define  true     1
#define  false    0
unsigned int INTflag;             
unsigned int KeyVal;
unsigned int KeyDownTimes;                              //保存按键次数
unsigned int KeycodeLED[6] ;                            //保存显示的LED数字
unsigned int EnterFg ;                                  //按确认键标识1、有确认键按下0无确认键按下
unsigned int secFlag,minFlag,hourFlag;
void clockPro(void);                                    //时钟处理函数
unsigned int  SetKeyValPro(void);                       //设置时钟值的合法性检测函数
unsigned int KeyDataChange(unsigned int Key_Data);		//键值转换子程序,把4X4键盘的键值转换成程序所需要的
unsigned int g_Data[11] = {0x003f,0x0006,0x005b,0x004f,0x0066,0x006d,0x007d,0x0007,0x007f,0x006f};
						   //0,	     1,		2,		3,		4,	  5,	6,		7,		8,		9
//========================================================
//	语法格式:	int main(void)
//	实现功能:	时钟设置及显示
//	参数:		无
//	返回值:	无
//========================================================
int main()
{
	unsigned int  i;
	KeyDownTimes = 0;                   
	INTflag = 0;
	EnterFg = 0;
	i = 0;

	Key_Init();												//键盘扫描初始化,包括IRQ6的TMB2 128Hz中断
	DIG_Init();												//显示初始化,初始端口以及IRQ4的1KHz中断
	
	while(1)
	{	 
		if(EnterFg == 0)//判断是否已按下过"确认"键?
		{
			KeyVal = Key_Get();								//获取键值
			KeyVal = KeyDataChange(KeyVal);					//转换键值,无键按下或对于本程序无用的键按下时
															//返回0xff
			switch(KeyVal)										
			{
				case KEY_DEL:								//删除键处理
					if(KeyDownTimes>0)  KeyDownTimes--;
					break;			    
				case 0xff:   								//无效的键值       
					break;	
				case KEY_ENT:								//确认键处理
					if(KeyDownTimes==6)
					{  
						SP_INT_IRQ5();
						EnterFg = true;
					}			         
					break;    
				default:									//数字键处理
					if(KeyDownTimes<6)
					{
						KeycodeLED[KeyDownTimes] = KeyVal;				      
						if(SetKeyValPro()== true)
						{
							KeyDownTimes++;
						}  
					}	
					break;
		    }
      	}   
	for(i=0;i<6;i++)                                 //显示所有键值即时钟
		DIG_Set(i+1,g_Data[KeycodeLED[i]]);         
	
	clockPro();                                      //时钟数据处理
	F_ClearWatchdog();                               //清看门狗
    }
}
//============================================================================================
//语法格式:unsigned int KeyDataChange(unsigned int Key_Data)
//实现功能:键值转换子程序
//参数:	Key_Data--机器码
//返回值:	0~12,其中0~9为数字键,10、11、12为功能键;
//			0xff,不合法的键输入
//============================================================================================
unsigned int KeyDataChange(unsigned int Key_Data)
{
	switch(Key_Data)
	{
		case KEY_0: Key_Data = 7;break;
		case KEY_1: Key_Data = 8;break;
		case KEY_2: Key_Data = 9;break;
		case KEY_3: Key_Data = 0xff;break;
		case KEY_4: Key_Data = 4;break;
		case KEY_5: Key_Data = 5;break;
		case KEY_6: Key_Data = 6;break;
		case KEY_7: Key_Data = 0xff;break;
		case KEY_8: Key_Data = 1;break;
		case KEY_9: Key_Data = 2;break;
		case KEY_A: Key_Data = 3;break;
		case KEY_B: Key_Data = 0xff;break;
		case KEY_C: Key_Data = 11;break;
		case KEY_D: Key_Data = 0;break;
		case KEY_E: Key_Data = 0xff;break;
		case KEY_F: Key_Data = 12;break;
		default: Key_Data = 0xff;break;
	}
	return Key_Data;
}
//============================================================================================
//语法格式:void SetKeyValPro(void)
//实现功能:设置时钟数据和发行检测函数名称
//参数:	无
//返回值:	1-合法;
//			0-不合法;
//=============================================================================================
unsigned int  SetKeyValPro(void)
{	
	uchar flag;			     
	switch(KeyDownTimes){
		case 0 : if(KeycodeLED[KeyDownTimes]<3)
				{                                                  //小时高位
		            flag = true; 
	             }
		         else flag = false;
		          break;
		case 1: if(KeycodeLED[KeyDownTimes-1]==2 )
				{
		         	if(KeycodeLED[KeyDownTimes]<5)                  //小时低位
		            	flag = true;
				    else 
				    	flag = false;
	             } 
		         else 
		         	flag = true;
				 break;
	    case 2: 
	    case 4:
	     
              if(KeycodeLED[KeyDownTimes]<6)
              {                                                     //秒和分的高位
	               flag = true;   
	          }
		       else flag = false;
	                 break;
	    case 3: 
	    case 5:
	          flag = true;	         
	          break;                                                //秒和分的地位
	    default: flag = false;
	    break;
    }
     return(flag);	    
}

//============================================================================================
//语法格式:void clockPro()
//描述功能:时钟处理函数
//参数:	无
//返回值:	无
//=============================================================================================
void clockPro(void)
{
	                                                                 //秒处理 
	while(INTflag==1)
	{
		INTflag = ClearIntFlag;		
		if(KeycodeLED[4] == 5 && KeycodeLED[5] == 9)
		{                                                            //判断秒是否需回零
	     	KeycodeLED[4]  = 0;                                      //如果为59回零
		    KeycodeLED[5] =  0;
		    secFlag = YESCarryFlag;                                  //设置秒进位标识
		}
		else  
		{	
			if(KeycodeLED[5]==9)
			{
				 KeycodeLED[5]= 0;
				 KeycodeLED[4]++;
			}
			else KeycodeLED[5]++;                                     //否则秒加一
		}
	}	
	
	                                                                  //分处理
	while(secFlag==1)
	{ 		   
		secFlag = ClearCarryFlag;
		if(KeycodeLED[2]  == 5 && KeycodeLED[3] ==9)
		{                                                             //判断分是否需回零
		     KeycodeLED[2] = 0;                                       //如果为59回零
		     KeycodeLED[3] = 0;
		     minFlag = YESCarryFlag;                                  //设置分进位标识
		}	
		else
		{ 
			if(KeycodeLED[3]==9)
			{
				 KeycodeLED[3]= 0;
				 KeycodeLED[2]++;
			}
		    else KeycodeLED[3]++;                                     //否则分加一
	    }
	}	
	                                                                  //时处理
	while(minFlag==1)
	{
		minFlag = ClearCarryFlag;
		if(KeycodeLED[0]==2 && KeycodeLED[1] ==3)
		{                                                             //判断时是否需回零
		   KeycodeLED[0] = 0;                                         //如果为23 回零
		   KeycodeLED[1] = 0;
		   hourFlag = YESCarryFlag;                                   //设置时进位标识		     
		}
		else 
		{                                                             //否则时加一
			if(KeycodeLED[1]==9)
			{
				 KeycodeLED[1]= 0;
				 KeycodeLED[0]++;
			}
			else KeycodeLED[1]++;
	    }    
	}
}

⌨️ 快捷键说明

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