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

📄 main.c

📁 用Freescale半导体公司HC08系列单片机制作了电子时钟,具有整点报时、闹钟、时间日期设置等功能(c语言编写)
💻 C
字号:
/*-----------------------------------------------------*
*工 程 名:	                                
*硬件连接:                                
*程序描述: SD-HC08 模板生成                            
*/
/*[头文件]*/
#include "GP32C.h"                //映像寄存器地址头文件
#include "KB.h"               //键盘中断头文件 
#define   PTC1 1
/*[函数声明]*/
   //在LCD第m行第n列显示一个字符
extern void LcdshowCharacter(unsigned char str,unsigned char m,unsigned char n); 
extern void Lcd_Command(unsigned char cmd);    //命令执行子程序
extern void Lcdshowtime(void);        //显示时钟
extern void Lcdshowalarm(void);       //显示闹钟
extern unsigned char ChangeASCII(unsigned char x);   //转换数值x为对应的ASCII码
extern void Timer1init(void);      //定时器1初始化
extern void LcdInit(void);         //Lcd初始化子程序
extern unsigned char KB_Def(unsigned char KB_valve);    //键值转为定义值函数
extern void MoveCursor(unsigned char m,unsigned char n);    //移到光标到第m行,第n列
extern void LcdShowCursor(void);     //开光标显示,闪烁
extern unsigned char GetKey(void);   //判断并获得按下键的定义值
extern void LcdShowMenu(void);   //显示菜单
extern void LcdShowCursor(void);     //开光标显示,闪烁
   //将输入的键值传送到相应位置的位
void Transmission(unsigned char Keyvalue,unsigned char temp[],unsigned char m,unsigned char n);
extern unsigned char Discriminant(unsigned char key);   //判断按下的键是否数字键
/*定义常量变量*/
unsigned char time[14];      //存放时钟年、月、日、时、分、秒
unsigned char alarm[14];     //存放闹钟的年、月、日、时、分、秒
unsigned char Key;     //存放按下键的定义值
unsigned char k=0;      //光标向右移动的次数
unsigned char l=0;      //光标向下移动的次数
unsigned char t;
unsigned char Alarm_Flag=1;

void delay(void)
   {
    unsigned char u1,u2;
    for(u1=0;u1<200;u1++);
    for(u2=0;u2<220;u2++);
   }

/*[主函数]*/
void main()
  {
     unsigned char i,n,h; 
	 unsigned char j;
	 unsigned char Keyvalue;
	 
 	 asm("SEI");               //关总中断
 	 Timer1init();             //调定时器1初始化子程序       
 	 KB_Init();
     INTKBSCR&= ~(1<<IntBit); //开放键盘中断(IMASKk=1)
	 asm("CLI");               //开总中断
	 
	 //赋时钟初始值2007年06月06日5点59分30秒;闹钟初始值2007年06月05日5时59分40秒
	 time[0]=alarm[0]=2; time[1]=alarm[1]=0; time[2]=alarm[2]=0; time[3]=alarm[3]=7;
	 time[4]=alarm[4]=0; time[5]=alarm[5]=6; time[6]=alarm[6]=0; time[7]=alarm[7]=6;
	 time[8]=0;alarm[8]=0; time[9]=5; alarm[9]=5;time[10]=5; alarm[10]=5;time[11]=9;
	 alarm[11]=9;time[12]=3;alarm[12]=4; time[13]=0; alarm[13]=0; 
     i=time[13];
	 DDRC=0xFF;
	 
BEGING:	 
	 while(1)
	{  
	  j=time[13];
	  if (i!=j)            //时间有更新          
	     {
	       Lcdshowtime();   //显示日期时间
	       i=j;
	     }
	     
	   if(Key=='C')
	     {
	       Alarm_Flag++;
	       if (Alarm_Flag=1)Alarm_Flag=0;
	     }
	     
	     
	      if((time[12]==0)&&(time[11]==0)&&(time[10]==0))
	    {
	    
	     n=time[9]+time[8]*10;   //多少点就响多少下
	    
	 	 //LcdshowCharacter('A', 1, 15); 
	     
	        
	       for(h=0;h<n;h++)
	        {
	          PTC|=1<<PTC1;
	          delay();
	          PTC|=~(1<<PTC1);
	          delay();
	        }
	          
	     }
	       
	/* if((time[11]==0)&&(time[10]==0)&&(Alarm_Flag==1))
	     {
	      unsigned char h;
	      for(h=0;h<=2;h++)
	      {
	         PTC|=1<<PTC1;
	         delay();
	         PTC|=~(1<<PTC1);
	         delay();
	       }
	      
	     }*/
	 
	 
	 //闹钟程序    
	  if((time[13]==alarm[13])&&(time[12]==alarm[12])&&(time[11]==alarm[11])&&(time[10]==alarm[10])&&(time[9]==alarm[9])&&(time[8]==alarm[8])&&(Alarm_Flag==1))
	     {
	      //unsigned char h;
	      for(h=0;h<=2;h++)
	      {
	         PTC|=1<<PTC1;
	         delay();
	         PTC|=~(1<<PTC1);
	         delay();
	          
	       }
	     } 
	 
	       
	  if(Key=='A')   //弹出菜单
	     { 
MENU:	   LcdShowMenu();   //显示菜单:1. Set Time;2. Set Alarm
	       Key=0;
	       while(1)
	       { 
	         if(Key=='B')     //返回
               { 
                 Key=0;
                 goto BEGING;
               }
	         if(Key=='1')    //选择子菜单1
	           { 
                 Lcdshowtime();
	             LcdShowCursor();    //开光标显示,闪烁
	             MoveCursor(1,4);    //光标初始位置(1,4),第一行第四列
	             Key=0;
	             while(1)
	              { 
	               
                    if(Key=='#')      //光标右移
	                 { 
	                   ++k;          //K为关标右移次数
	                   if(l%2==0)     //现在光标在第一行 l为光标所在行数
	                    {
	                      if(k>9) k=0;  //光标在末端时返回到起始位置
	                      MoveCursor((1+l),(4+k));
	                     }
	                   else     //现在光标在第二行
	                    {
	                      if(k>8) k=1;  //现在光标在末端
	                      MoveCursor((1+l),(4+k));
	                     }
	                   Key=0;
	                   
	                  }
	                if(Key=='*')     //光标下移
	                  {
	                    ++l;
	                  //  if(k==0) k++;   //现在光标在第一行始端
	                  //  if(k==9) k--;   //现在光标在第一行末端
	                    if(l>1) l=0;    //现在光标在第二行
	                    MoveCursor((1+l),(4+k));                 
	                    Key=0;
	                  }
	                if(Key=='B')
	                  {
	                    Key=0;
	                    goto MENU;     //返回
	                  } 
	                if(Key=='D')       //按下确定键
	                  {
	                    Key=0;
	                    goto BEGING;
	                  }
	                if(Key!=0&&Discriminant(Key))     //Discriminant为判断是否按下数字键
	                  { 
	                    Keyvalue=Key-0x30;      
	                    Transmission(Keyvalue, (unsigned char *)time,(1+l),(4+k));
	                    Lcdshowtime();
	                    LcdShowCursor();          //开光标显示,闪烁
	                    MoveCursor((1+l),(4+k)); 
	                    Key=0;
	                  }
	                 
	               }
	          
	             }
	          
              if(Key=='2')      //选择子菜单2
	           {
	             Lcdshowalarm();
	             LcdShowCursor();    //开光标显示,闪烁
	             MoveCursor(2,12);    //光标初始位置(2,5),第二行第五列
	             Key=0;
	             while(1)
	              { 
	                
                    if(Key=='#')      //光标右移
	                 { 
	                   ++k;
	                   if(l%2==0)     //现在光标在第一行
	                    {
	                      if(k>9) k=0;  //现在光标在末端
	                      MoveCursor((1+l),(4+k));
	                     }
	                   else     //现在光标在第二行
	                    {
	                      if(k>8) k=1;  //现在光标在末端
	                      MoveCursor((1+l),(4+k));
	                     }
	                   Key=0;
	                   
	                  }
	                if(Key=='*')     //光标下移
	                  {
	                    ++l;
	                    if(k==0) k++;   //现在光标在第一行始端
	                    if(k==9) k--;   //现在光标在第一行末端
	                    if(l>1) l=0;    //现在光标在第二行
	                    MoveCursor((1+l),(4+k));                 
	                    Key=0;
	                  }
	                if(Key=='B')
	                  {
	                    Key=0;
	                    goto MENU;     //返回
	                  } 
	                if(Key=='D')       //按下确定键
	                  {
	                    Key=0;
	                    goto BEGING;
	                  }
	                if(Key!=0&&Discriminant(Key))
	                  { 
	                    Keyvalue=Key-0x30;
	                    Transmission(Keyvalue,(unsigned char *) alarm,(1+l),(4+k));
	                    Lcdshowalarm();
	                    LcdShowCursor();    //开光标显示,闪烁
	                    MoveCursor((1+l),(4+k)); 
	                    Key=0;
	                  }
	                 
	               }
	           }   
              
              
               
	        }
	         
	      }
	 }
     
  }


    



⌨️ 快捷键说明

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