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

📄 main.c

📁 rtc实时时钟程序源代码
💻 C
字号:
#include "def.h"
#include "option.h"
#include "44b.h"
#include "44blib.h"
#include "string.h"
#include "stdio.h"

#define RTC_RW_EN() rRTCCON=0x01;//要养成这样的习惯
#define RTC_RW_DS() rRTCCON=0x0;

typedef struct{
	short year;
	char month;
	char day;
	char date;
	char weekday;
	char hour;
	char min;
	char sec;
}TIME_STRUC;//注意结构体的声明方式;

char *WeekDay[7] = {"SUN","MON", "TUES", "WED", "THURS","FRI", "SAT"}; 
TIME_STRUC Systime;
//char myTimeset[15]="05031602184300";
char *funName[4]={"timeset","display","alarm","tick"};//注意字符串数组的定义方法。大括号!双引号!!
int funNum;


int chToint(char achar)//一个字符转换成一个int
{
   /*
    switch(achar)
    {
    case '0':
      return 0;
      break;
    case '1':
      return 1;
      break;
    case '2':
      return 2;
      break;
    case '3':
      return 3;
      break;
    case '4':
      return 4;
      break;
    case '5':
      return 5;
      break;
    case '6':
      return 6;
      break;
    case '7':
      return 7;
      break;
    case '8':
      return 8;
      break;
    case '9':
      return 9;
      break;//这段转换程序没有问题
    }
  */    //这一段可以被下面一句代替。
    return ((int)achar-0x30);//这一句害的我计时不准确差10秒。后来本来写的-48,差十秒,后来修改为-30;因为十进制和16进制下0的编码不同,后来发现应该是0x30!!!!教训!!
}

void myStrncpy( char *s1,  char *s2, int maxlen)//字符串复制前n个
{
   int i;

	for(i = 0; i < maxlen; i++) 
	{
		if(s1[i] != s2[i])
		s1[i] = s2[i];
		if(s1[i] == 0);
		
	}

}//这个函数有待验证和完善。其实没有用到

void myIntmncpy(int *int1,int *int2,int bn,int len)//想要实现把第二个int数组的某位开始的n位放到截取出来放到第一个int数组中
{  
    int i;
    for(i = bn; i < bn+len; i++) 
    {
		int1[i-bn]=int2[i];	
	}
   
}//这个函数有待验证和完善。其实没有用到

void __irq rtc_int(void)
{
   Uart_Printf("\nENTER RTC ALARM INT");
   rI_ISPC=BIT_RTC;
   Uart_Printf("\ntime up! now is your wanted time.");
}



void timeSet(void)
{ 
   char myTimeset[15];
   int myInt[15];
   int i;
   Uart_Printf("\nENTER TIMESET FUNCTION");
   Uart_Printf("\nENTER your time,exp:07041402130000");
// scanf("%2d,%2d,%2d,%2d,%2d,%2d,%2d",rBCDYEAR,rBCDMON,rBCDDAY,rBCDDATE,rBCDHOUR,rBCDMIN,rBCDSEC);
   for(i=0;i<14;i++)
   {  myTimeset[i]=Uart_Getch();
      myInt[i]=chToint(myTimeset[i]);
   }
   
   RTC_RW_EN();
   rBCDYEAR=(myInt[0]<<4)|myInt[1];
   rBCDMON=(myInt[2]<<4)|myInt[3];
   rBCDDAY=(myInt[4]<<4)|myInt[5];
   rBCDDATE=myInt[7];
   rBCDHOUR=(myInt[8]<<4)|myInt[9];
   rBCDMIN= (myInt[10]<<4)|myInt[11];
   rBCDSEC=(myInt[12]<<4)|myInt[13];
   Uart_Printf("\n%s",myTimeset);
   
   /*
   strncpy(rBCDYEAR,,2);
   strncpy(rBCDMON,myInt[2],2);
   strncpy(rBCDDAY,myInt[4],2);
   strncpy(rBCDDATE,myInt[6],2);
   strncpy(rBCDHOUR,myInt[8],2);
   strncpy(rBCDMIN,myInt[10],2);
   strncpy(rBCDSEC,myInt[12],2);
   */
   
 //Uart_Printf("%c",rBCDYEAR);
   Uart_Printf("\n%d,%d,%d,%d,%d,%d",rBCDYEAR,rBCDMON,rBCDDAY,rBCDHOUR,rBCDMIN,rBCDSEC);
   
   Uart_Printf("\nSET TIME SUCCEED!");
   RTC_RW_DS();
}

void displayTest(void)
{
   Uart_Printf("\nENTER DISPLAY FUN");
   
   RTC_RW_EN();
   Systime.year=2000+rBCDYEAR;
   Systime.month=(rBCDMON>>4)*10 + (rBCDMON&0xf);
   Systime.day=(rBCDDAY>>4)*10 + (rBCDDAY&0xf);
   Systime.date=rBCDDATE;
   Systime.hour=(rBCDHOUR>>4)*10 + (rBCDHOUR&0xf);
   Systime.min=(rBCDMIN>>4)*10 + (rBCDMIN&0xf);
   Systime.sec=(rBCDSEC>>4)*10 + (rBCDSEC&0xf);
   
   Uart_Printf("\n%d,%d,%d,%d,%d,%d",rBCDYEAR,rBCDMON,rBCDDAY,rBCDHOUR,rBCDMIN,rBCDSEC);
   Uart_Printf("\nnow time is %4d年%2d月%2d日,%s,%d:%d:%d",Systime.year,Systime.month,Systime.day,WeekDay[Systime.date],Systime.hour,Systime.min,Systime.sec);
   RTC_RW_DS();
}

void alarmTest(void)
{ 
 
   int i;
   char alaTime[13];int alaInt[13];
   //initial alarm registor;
   RTC_RW_EN();
   rRTCALM=0x7f;//开启到秒警报
   
   //涉及到中断设置;在main函数中进行设置;此处也设置后使用中断;
  
   pISR_RTC=(unsigned)rtc_int;
   rINTMSK=~(BIT_RTC|BIT_GLOBAL);
   
   
   Uart_Printf("\nENTER ALARM FUN");
   Uart_Printf("\nPls enter the wanted time exp:071712181200");//注意星期几写出来没有必要,所以不必写,所以比设定的时候少了两位;
   
   for(i=0;i<12;i++)
   {
    alaTime[i]=Uart_Getch();
    alaInt[i]=chToint(alaTime[i]);//
   }
   
   rALMYEAR=(alaInt[0]<<4)|alaInt[1];
   rALMMON=(alaInt[2]<<4)|alaInt[3];
   rALMDAY=(alaInt[4]<<4)|alaInt[5];
   rALMHOUR=(alaInt[6]<<4)|alaInt[7];
   rALMMIN=(alaInt[8]<<4)|alaInt[9];
   rALMSEC=(alaInt[10]<<4)|alaInt[11];
   Uart_Printf("\nTIME HAS BEEN SET!");
   
   RTC_RW_DS();
   
}

void tickTest(void)
{
   Uart_Printf("\nENTER TICK FUN");
}

void startTest()
{
  char selNum;
  for(funNum=0;funNum<4;funNum++)
   { 
    Uart_Printf("\n");
    Uart_Printf("%d:%s",funNum,funName[funNum]);
   
   }
   
  while(1)
  {  selNum=Uart_Getch();
     Uart_Printf("\n%c",selNum);
     Uart_Printf("\n%d",chToint(selNum));
//   Uart_Printf("\n%d",selNum);//打印出来是asii值,比如0会是48
     switch(selNum)
    { 
      case '0':
      timeSet();
      break;
      case '1':
      displayTest();
      break;
      case '2':
      alarmTest();
      break;
      case '3':
      tickTest();
      break;
          
    } 
     Uart_Printf("\n0,1,2,3?");
   }
}


void Main(void)
{
   char charYoN;
   rSYSCFG=CACHECFG;   // Using 8KB Cache//

   Port_Init();
   Uart_Init(0,57600);
   Delay(10);
   Uart_Select(0); //Select UART0
   
  //因为有RTC的alarm功能,所以要设置中断。如下
   rINTCON=0x05;
   rINTMOD=0x0;
//   rINTMSK|=BIT_GLOBAL;

   Uart_Printf("\nS3C44B0X rtc function test?Y OR N");
   
   while(1)
   { charYoN=Uart_Getch();
    
     switch(charYoN)
    {
      case 'y':
      startTest();
      break;
      case 'Y':
      startTest();
      break;

      defualt:
      Uart_Printf("Y or N?");
      break;
     }
    }
   
   
}

⌨️ 快捷键说明

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