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

📄 wanjtest.c

📁 一个用嵌入c写的万年历程序
💻 C
字号:
#include "ht82k95a1.h"
//#include "OCMj.c"
/******Location of Interrupt Vector*************/
#pragma vector isr_4 @ 0x4
#pragma vector isr_8 @ 0x8
#pragma vector isr_c @ 0xc
#define pin	_14_0
/******isr zi cheng xu*************/

#define uint  unsigned int 
#define uchar unsigned char 
#define x     0x80 
#define y     0x80 
#define comm  0 
#define dat   1 
#define sclk   _pc0
#define cs  _pc2
#define std _pc3
//bit psb  = _pa3   H=并口; L=串口; 
#define rst  _pc4   //Reset Signal 低电平有效 
const uchar msg[41] = "mondaymondaymonday  2006.05.02 16:41:23 ";
/*---------------------------------------*/ 

void delay (uint us)   //delay time 
{ 
  while(--us); 
} 
void delay1 (uint ms) 
{ 
  uint i,j; 
  while(++i<=(1000*ms)) 
  {while(++j<=1115) 
  delay(1); 
} }

/*---------------------------------------*/ 
void wr_lcd (uchar dat_comm,uchar content) 
{ 
  uchar a,i,j; 
  //delay (40); 
  a=content; 
  cs=1; 
  sclk=0; 
  std=1; 
  for(i=0;i<5;i++) 
  { 
    sclk=1; 
    sclk=0; 
  } 
  std=0; 
  sclk=1; 
  sclk=0; 
  if(dat_comm) 
    std=1;   //data 
  else 
    std=0;   //command 
  sclk=1; 
  sclk=0; 
  std=0; 
  sclk=1; 
  sclk=0; 
  for(j=0;j<2;j++) 
  { 
    for(i=0;i<4;i++) 
    { 
      a=a<<1; 
      std=_c; 
      sclk=1; 
      sclk=0; 
    } 
    std=0; 
    for(i=0;i<4;i++) 
    { 
      sclk=1; 
      sclk=0; 
    } 
  } 
} 
/*------------------初始化-----------------*/ 
void init_lcd (void) 
{ 
  rst=1; 
  //psb=0; 
  wr_lcd (comm,0x30);  /*30---基本指令动作*/    
  wr_lcd (comm,0x01);  /*清屏,地址指针指向00H*/ 
  delay (80); 
  wr_lcd (comm,0x06);  /*光标的移动方向*/ 
  wr_lcd (comm,0x0c);  /*开显示,关游标*/ 
} 
/*---------------显示汉字或字符----------------*/ 
void chn_disp1 () 
{ 
  uchar i,j;
  wr_lcd (comm,0x30); 
  wr_lcd (comm,0x80); 
  j=0; 
  for (i=0;i<20;i++) 
  wr_lcd (dat,msg[j*20+i]);
   
} 
void chn_disp2 () 
{ 
   uchar i,j;
  wr_lcd (comm,0x30); 
  wr_lcd (comm,0x90); 
  j=1; 
  for (i=0;i<18;i++) 
  wr_lcd (dat,msg[j*20+i]);  
} 

/*--------------清DDRAM------------------*/ 
void clrram (void) 
{ 
  wr_lcd (comm,0x30); 
  wr_lcd (comm,0x01); 
  //delay (120); 
} 


struct wan_hms{
 unsigned int wan_hour;
 unsigned int wan_minite;
 unsigned int wan_hfsec;
};
struct wan_time{
 unsigned int wan_year;
 unsigned int wan_month;
 unsigned int wan_week;
 unsigned int wan_day;
 struct wan_hms hms;
}curtime,wan_cur;
#define curyea curtime.wan_year
#define curmon curtime.wan_month
#define curwda curtime.wan_week
#define curday curtime.wan_day
#define curhou curtime.hms.wan_hour
#define curmin curtime.hms.wan_minite
#define hfcursec curtime.hms.wan_hfsec

//test's format 定义八个任务数组
struct wan_int {
   unsigned int exist;
   unsigned int mdo;//switch trigged one amonth aday only once
   struct wan_time tesktime;
   }wan_ints[8];
unsigned int dispen;
unsigned int wan_dispen;//show if need flash the lcd
//unsigned int flag;//to see which step we are in now
//unsigned int monflag[13];
unsigned int focnbmax;
unsigned int focvalue;
unsigned int focvmax;
unsigned char key[7];
unsigned char keyvalue;
unsigned int picnb;
unsigned int focnb;
unsigned int consec;
unsigned int wan_t;
unsigned int pflag1;
unsigned int pflag2;
unsigned int upflag;
/*han shu shengming*/
void set_timer();
void wan_testkb();
unsigned int getkey();
void key_adjust();
/*han shu shengming*/
void showp()
{_pb0=0;}
void showpn()
{init_lcd ();
 chn_disp1 ();
 chn_disp2 ();}
void savetesk()
{_pb1=0;}
unsigned int begdaysinm(struct wan_time curtime)
{unsigned int j=31;
return j;}
void init()
{    //_intc = 0;
     //_tmr1c = 0;
     //_tmr = 0;
     //_pac = 0xff; //input mode
     //_pbc = 0xff;
     //_pcc = 0xff;
	 //。。。  //调用lcd接口输入为curtime 将显示对应的画面:welcome
	_pac = 0xff; //set port a to input port exclude pa7
    _pbc = 0; //set port b to output port
    _pb = 0;
    _pa = 0xff;
    _pcc=0x00;
    _intc = 0x09; //enable timer1 设定Timer/Counter为Timer Mode (TM1、TM0=10H)  打开相对应中断致能旗标 (ETI和EMI)
    _tmr1c = 0x80; //timer1 mode (internal clock)  设定计数初始值 (TMR)
    set_timer();
//  设定 Timer ON (TMRC.4=1) 
//当Timer被打开后,Timer开始向上计数,
//若Timer OFF (TMRC.4=0),则Timer立即停止。
//下次Timer再ON,则从上次停留值继续往上数,直到满位溢出产生中断,同时Timer 重新加载初始值
key[0]='F';key[1]='S';key[2]='D';key[3]='l';key[4]='r';key[5]='u';key[6]='d';


/******Location of varinit*************/
/******Location of varinit*************/
}
//end of init()
void set_timer()
{
   _tmr1c4 = 0;
   _tmr1l = 0xb0;//to be modefined
   _tmr1h = 0x3c;//to be modefined
   _tmr1c4 = 1; //start timer1
}
void isr_c()
{++wan_t;}//end of isr_c()
void arrt()
{if(wan_t==96)
 {wan_t=0;
  if(++hfcursec==120)
    {hfcursec=0;
       if(++curmin==60)
        {curmin=0;
          if(++curhou==24)
           {curhou=0;
            if(++curwda==7)
             {
              curwda=0;
             }
            if(++curday==begdaysinm(curtime))
             {curday=0;
               if(++curmon==12)
               { curmon=0;
                 ++curyea; 
               }
             } 
           }
        }
}}
if(wan_dispen==1)
     { 
     showp(); //调用lcd接口输入为curtime 将显示对应的画面
      }
}
void wan_testkb()//其中的picnb有待进一步检查
{
   if(getkey())
   {
     switch(keyvalue)
     {
        case 0x06:
           picnb=2;
           showpn();//调用lcd接口输入为以上数组 将显示对应的画面
           break;
        case 0x05:
              if(picnb==2)
                  {
                  	  if(focnb==0)
                  	  {
              	        pflag1=0;//time the curtime
              	      }
                      else if(focnb==1)
              	      {pflag1=1;}//time the task
                      else if(focnb==2)
              	      {pflag1=2;}//del the task
                  }
                  if((pflag1==1)&&(picnb==5))
                  {
                  	  switch(focnb)
                  	  {  case 0:
                  	  	   pflag2=0;
                           break;
                  	      case 1:
                  	       pflag2=1;
                           break;
                  	      case 2:
                  	       pflag2=2;
                           break;
                  	      default:
                           break;
                      }
                  }	  
              
                  if(pflag1==0&&picnb==2)
                  {
              	      picnb++;
                      showpn();//调用lcd接口输入为以上数组 将显next
                      savetesk();//tobe finished
                      goto lable2;
                  }
                  if(pflag1==1&&pflag2==1&&picnb==4)
                  {
                      picnb++;
                      showpn();//调用lcd接口输入为以上数组 将显next
                      savetesk();
                      goto lable2;
                  }
                  if(pflag1==1&&(pflag2==0||pflag2==2)&&picnb==5)
                  {
              	      picnb++;
                      showpn();//调用lcd接口输入为以上数组 将显next
                      savetesk();
                      goto lable2;
                  }  
                  picnb++;
                  showpn();//调用lcd接口输入为以上数组 将显next    
                  break;

              case 0x04:
                  picnb--;
                  showpn();//调用lcd接口输入为以上数组 将显示对应的画面
                  break;

              case 0x02:
                  focnb++;
                  focnb=focnb%focnbmax;
                  showp();//调用lcd接口输入为以上数组 将显示对应的画面
                  break;

              case 0x03:
                  focnb=(--focnb==0)?(focnb+focnbmax):focnb;
                  showp();//调用lcd接口输入为以上数组 将显示对应的画面
                  break;

              case 0x01:
                  upflag=1;
              	  key_adjust();
                  showp();//调用lcd接口输入为以上数组 将显示对应的画面
                  break;
              
             case 0x00:
                  upflag=0;
              	  key_adjust();
              	  showp();//调用lcd接口输入为以上数组 将显示对应的画面
                  break;
             default:
                  lable2:break;
    }
 }//end of if

}//end of wan_testkb

 unsigned int getkey()
 {
    unsigned int j=0;
    //unsigned char i;
    //_pbc = 0xff; 
    if (~_pa)
    { 
    	//key pressed
         _delay(2000); //debounce
         //after debounce, if the key is still pressed
         //we claim it a key pressed, otherwise ignore it
        if (~_pa)
        {
           //while(~_pb);
           //getkey_flag=1;
           if((~_pa)&(unsigned char)0x40){keyvalue=0x06;}
           else if((~_pa)&(unsigned char)0x20){keyvalue=0x05;}
           else if((~_pa)&(unsigned char)0x10){keyvalue=0x04;}
           else if((~_pa)&(unsigned char)0x08){keyvalue=0x03;}
           else if((~_pa)&(unsigned char)0x04){keyvalue=0x02;}
           else if((~_pa)&(unsigned char)0x02){keyvalue=0x01;}
           else if((~_pa)&(unsigned char)0x01){keyvalue=0x00;}
           j=1;
        }
    }
    return(j);
 }//end of getkey


void key_adjust()
{
    unsigned int hfconsec;
    unsigned int held_long_time = 0;//whether long press
    repeat_inc:
    if(upflag==1)
    {
       focvalue++;//需要让curtime同时加一 以让下面的showpic函数显示出最新值
       focvalue=focvalue%focvmax;//又有了一个工作
    }
    else
    {
       //focvalue--;//需要让curtime同时加一 以让下面的showpic函数显示出最新值
       focvalue=(focvalue==0)?focvmax:--focvalue;//又有了一个工作
    }
    hfconsec = hfcursec;
    while(_pa == 0)
    {
       showp();
       if (!held_long_time)
       {
          if (((hfcursec+120-hfconsec)%120)>2)
          {//longer than 1.5 sec
              held_long_time = 1; //set flag
              goto repeat_inc;//increase hour
          }
           //less than 1.5 seconds, do nothing
       }
       else{
       if (hfcursec!=hfconsec)
           goto repeat_inc;//inc 1, 0.5 sec
       //less than 0.5 second, do nothing
       }
    }
  //half_second = 0;
  set_timer();//dont understand why call the subroutine
  //return held_long_time;
}

unsigned int ftiequal(struct wan_time one,struct wan_time twe)
{unsigned int flag=0;

    //unsigned int=0;
    if((one.wan_year==twe.wan_year)&&(one.wan_month==twe.wan_month)&&(one.wan_week==twe.wan_week)&&(one.wan_day==twe.wan_day))
        if((one.hms.wan_hour==twe.hms.wan_hour)&&(one.hms.wan_minite==twe.hms.wan_minite)&&(one.hms.wan_hfsec==twe.hms.wan_hfsec))
        {flag=1;}
return flag;
}
unsigned int hmsequal(struct wan_hms one,struct wan_hms twe)
{
unsigned int flag=0;
if((one.wan_hour==twe.wan_hour)&&(one.wan_minite==twe.wan_minite)&&(one.wan_hfsec==twe.wan_hfsec))
{flag=1;}
return flag;
}



void main()
{unsigned int n=0;
struct wan_hms temphms=wan_ints[n].tesktime.hms;
struct wan_time temptime=wan_ints[n].tesktime;
  picnb=0;
  focnb=0;
  wan_t=0;
  pflag1=0;
  pflag2=0;
  init();
  arrt();
  //unsigned int n=0;//to falg which tesk we are comparing
while(1)
    {
    if(wan_ints[0].exist=1)
    { n=0;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb0=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb0=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb0=0;}
    }
    if(wan_ints[1].exist=1)
    { n=1;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb1=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb1=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb1=0;}
    }
if(wan_ints[2].exist=1)
    { n=2;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb2=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb2=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb2=0;}
    }
if(wan_ints[3].exist=1)
    { n=3;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb3=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb3=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb3=0;}
    }
if(wan_ints[4].exist=1)
    { n=4;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb4=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb4=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb4=0;}
    }
if(wan_ints[5].exist=1)
    { n=5;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb5=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb5=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb5=0;}
    }
if(wan_ints[6].exist=1)
    { n=6;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb6=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb6=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb6=0;}
    }
if(wan_ints[7].exist=1)
    { n=7;
      if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==8))
      {
      _pb7=0;//have been defined
      }
      else if(hmsequal(curtime.hms,temphms)&&(wan_ints[n].mdo==0))
      {
      _pb7=0;
      }
      else if(ftiequal(curtime,temptime)&&(wan_ints[n].mdo==curtime.wan_week)&&(1<wan_ints[n].mdo<7)){_pb7=0;}
    }

    wan_testkb();
    }
  
}


    













⌨️ 快捷键说明

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