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

📄 main.c

📁 基于LPC21xx的PCF8563(I2C RTC)实时时钟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************Copyright (c)**************************************************
**                                 Zhejiang University
**                          Institute of Industry Process Control 
**                                   WJW's Studio!
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			main.c
** Last modified Date:  2007-08-07
** Last Version:		1.0
** Descriptions:		The main() function
**------------------------------------------------------------------------------------------------------
** Modified by:         Jianwei Wang   
** Modified date:       2007-08-19
** Version:             1.0
** Descriptions:        I2C test
**------------------------------------------------------------------------------------------------------
**/

#include "config.h"
#include "string.h"
#include "I2CINT.h"

#define  LCDRS  (1 << 22)	// P1.24控制LCD使能(下降沿使能),P1.25控制LCD背光开关(未使用)
#define  LCDRW  (1 << 21)   // P1.16~P1.23为八位数据,且P1.16为低位
#define  LCDEN  (1 << 24)   // P0.22控制LCD寄存器(0指令,1数据),P0.21控制LCD读写(0写,1读)
#define  LCDOFF (1 << 25)
#define  LCD8   (0xFF << 16)

#define  KEY1   (1 << 16)   // P0.16~P0.20五个按键,低电平为有键按下,P0.16为Key1
#define  KEY2   (1 << 17)  
#define  KEY3   (1 << 18)  
#define  KEY4   (1 << 19)  
#define  KEY5   (1 << 20)  
#define  KEY    KEY1|KEY2|KEY3|KEY4|KEY5

#define  PRTC   0xA2

char PRTCScreen[][16]={"1:Sh 2:Ed 4:Syn","1:-> 2++ 4:Load"};

uchar PRTCBuf[16];
uchar PRTCsetBuf[16];

char Datebuf[16];                 //占两行的日期时间完整显示
char Timebuf[16];
char TDBuf[16]="  :  :  -       ";//一行内显示时间日期(精简)
char TimeScreen[][16]={"2:Edit  3:Back","1:-> 2++ 3:Back"};

TimeStruct CurTime;

/*
*********************************************************************************************************
** 函数名称 :DelayNS()
** 函数功能 :长软件延时
** 入口参数 :dly	延时参数,值越大,延时越久。i<500,约为50us。
** 出口参数 :无          
*********************************************************************************************************
*/
void DelayNS (uint32 dly)
{
	uint32 i;
	
	for ( ; dly>0; dly--)
		for (i=0; i<500; i++);
}

/////////////////////////////////////////
uchar scankey(void) //扫描键值
{                  
	uint32 c,d;
	d=KEY;
	c=IO0PIN & d;
	if(c==0x1F0000)// 都为高,无键按下
		return 0;
		
		///否则有键按下

	switch(c)
	{
		case 0xF0000:
			return 5; 
		case 0x170000:
			return 4;
		case 0x1B0000:
			return 3;
		case 0x1D0000:   
			return 2;
		case 0x1E0000:   
			return 1;	
		default: 
			return 0;
	}
}

//////////////////////////////////////////
uchar getkey(void)//取健值
{	
	uchar key_num=0;
	if((key_num=scankey())==0)
		return 0;
	DelayNS(400);//20ms
	if((key_num=scankey())==0)
		return 0;
	while(scankey()!=0);//等待健放开
	return key_num;		
}

///////////////////////////////////////////////
void lcd_sendCmd(uchar cmd)
{   
    uint32 cmd32=0x00FF0000;
    IO1CLR=cmd32;
    cmd32=cmd;
    cmd32=(cmd32<<16);
    
    IO0CLR=LCDRS;
	IO0CLR=LCDRW;
	IO1SET=LCDEN; 
	IO1SET=cmd32;
	DelayNS(40);//2ms Delay
	IO1CLR=LCDEN;
	IO0SET=LCDRW;
	
}

//////////////////////////////////////////////////
void lcd_sendData(uchar dt)
{	
    uint32 dt32=0x00FF0000;
    IO1CLR=dt32;
    dt32=dt;
    dt32=(dt32<<16);   

	IO0SET=LCDRS;
	IO0CLR=LCDRW;
	IO1SET=LCDEN;
	IO1SET=dt32;
	DelayNS(1);//50us Delay
	IO1CLR=LCDEN;
	IO0SET=LCDRW;
}



void lcd_init(void)
{	
    lcd_sendCmd(0x01);//clear
	lcd_sendCmd(0x02);//cursor return home
	lcd_sendCmd(0x38);//0011 1000 function set:8bits 2 lines 5*7dots
	lcd_sendCmd(0x06);//0000 0110 entry mode set:cursor increment,display no shift
	lcd_sendCmd(0x0f);//0000 1100 display cotrol:display/cursor(NO) both on,cur NOT blink
}

//////////////////////////////////////////////////////////
void lcd_disp(char *str,uint8 line)
{	
	uint8 i=0x0;

	if(!line)
	{
		lcd_sendCmd(0x80);
		while(*str!='\0'&&(i++)<16)
			lcd_sendData(*(str++));
		while((i++)<16)
			lcd_sendData(' ');
	}
	else
	{
		lcd_sendCmd(0xc0);
		while(*str!='\0'&&(i++)<16)
			lcd_sendData(*(str++));
		while((i++)<16)
			lcd_sendData(' ');
	 }
}


////////////////////////////////////////////////
void lcd_dispCursor(uchar x,uchar y)
{
	lcd_sendCmd((x==0)?(0x80+y):(0xc0+y));
}

////////////////////////////////////////////////
void showmessage(char *str)
{                      //动态显示字符,可显示n个字符,一般用于开机画面或想炫一把地显示时才用
	uchar i,j;
	char tempstr[16]="                ";	
	lcd_disp(" Welcome to IIPC",1);
	for(i=0;i<15;i++)
	{
		for(j=0;j<i;j++)
		{
			tempstr[15-i+1+j]=str[j];
		}
		lcd_disp(tempstr,0);
		for(j=0;j<7;j++)
		{
			if(getkey()!=0)
			    return;
			DelayNS(400);//2ms*10
		}
	}
	
	i=strlen(str);
	
	for(i=0;i<strlen(str)-2;i++)
	{
		lcd_disp(&(str[i]),0);
		for(j=0;j<7;j++)
		{
			if(getkey()!=0)
				return;
			DelayNS(400);
		}
	}
	
}

/*
*********************************************************************************************************
** 函数名称 :I2cInit()
** 函数功能 :I2C初始化
** 入口参数 :Fi2c	I2C总线频率(最大400K)
** 出口参数 :无
*********************************************************************************************************
*/
void I2cInit(uint32 Fi2c)
{
	if (Fi2c > 400000)
		Fi2c = 400000;
		
	PINSEL0 = (PINSEL0 & (~0xF0)) | 0x50; 	            /* 设置I2C控制口有效,不影响其它管脚连接*/ 
	I2SCLH = (Fpclk/Fi2c + 1) / 2;						/* 设定I2C时钟 						*/
	I2SCLL = (Fpclk/Fi2c) / 2;
	I2CONCLR = 0x2C;
	I2CONSET = 0x40;									/* 使能主I2C 						*/
	
	/* 设置I2C中断允许 */
	VICIntSelect = 0x00000000;							/* 设置所有通道为IRQ中断 			*/
	VICVectCntl1 = (0x20 | 0x09);	//0010 1001   		/* I2C通道分配到IRQ slot1           */
	VICVectAddr1 = (int32)IRQ_I2C;						/* 设置I2C中断向量 					*/
	VICIntEnable = VICIntEnable|(1 << 9);				/* 使能I2C中断 						*/

}

/********************************************
内部函数,读入时间到内部缓冲区
********************************************/
void PRTC_Read()
{   
     uchar time[7];
     
     I2C_ReadNByte(PRTC,ONE_BYTE_SUBA,0x02,time, 7);
     PRTCBuf[8]=time[0]&0x7F; /*秒*/
     PRTCBuf[7]=time[1]&0x7F; /*分*/
     PRTCBuf[6]=time[2]&0x3F; /*小时*/
     PRTCBuf[5]=time[4]&0x07; /*星期*/
     PRTCBuf[4]=time[3]&0x3F; /*日*/
     PRTCBuf[3]=time[5]&0x1F; /*月*/
     PRTCBuf[2]=time[6]&0xFF; /*年*/
     PRTCBuf[1]=time[5]&0x80; /*世纪*/
     PRTCBuf[0]=time[0]&0x80; /*有效标志*/
     
}

/********************************************
读入时间到内部缓冲区----外部调用 
********************************************/
uchar PRTC_gettime()
{    
     uchar ReadN=1;
          
     PRTC_Read();
     if(PRTCBuf[0]!=0)
         {
         PRTC_Read(); /*如果不保证准确性,再读一次*/
         ReadN++;
         if(ReadN>=3) return 0;
         }
         
     if(PRTCBuf[1]==0)    
         {Datebuf[0]='2';Datebuf[1]='0';}
     else
         {Datebuf[0]='1';Datebuf[1]='9';}
         
     switch(PRTCBuf[5])
	   {
	    case 0:
	       Datebuf[12] = 'S';
	       Datebuf[13] = 'u';
	       Datebuf[14] = 'n';
	       break;
	    case 1:
	       Datebuf[12] = 'M';
	       Datebuf[13] = 'o';
	       Datebuf[14] = 'n';
	       break;
	    case 2:
	       Datebuf[12] = 'T';
	       Datebuf[13] = 'u';
	       Datebuf[14] = 'e';
	       break;   
	    case 3:
	       Datebuf[12] = 'W';
	       Datebuf[13] = 'e';
	       Datebuf[14] = 'd';
	       break;    
	    case 4:
	       Datebuf[12] = 'T';
	       Datebuf[13] = 'h';
	       Datebuf[14] = 'u';
	       break;
	    case 5:
	       Datebuf[12] = 'F';
	       Datebuf[13] = 'r';
	       Datebuf[14] = 'i';
	       break;     
	    case 6:
	       Datebuf[12] = 'S';
	       Datebuf[13] = 'a';
	       Datebuf[14] = 't';        
	   }
	 
	  
	 Datebuf[2] =((PRTCBuf[2]>>4) & 0x0F)+'0';	 
	 Datebuf[3] = (PRTCBuf[2]     & 0x0F)+'0';
	 Datebuf[4] = '-';
	 Datebuf[5] =((PRTCBuf[3]>>4) & 0x0F)+'0';	 
	 Datebuf[6] = (PRTCBuf[3]     & 0x0F)+'0';
	 Datebuf[7] = '-';
	 Datebuf[8] =((PRTCBuf[4]>>4) & 0x0F)+'0';	 
	 Datebuf[9] = (PRTCBuf[4]     & 0x0F)+'0';
	 Datebuf[10] = ' ';
	 Datebuf[11] = ' ';
	 Datebuf[15] = '.';
	 
	 Timebuf[0] =((PRTCBuf[6]>>4) & 0x0F)+'0';	 
	 Timebuf[1] = (PRTCBuf[6]     & 0x0F)+'0';
	 Timebuf[2] = ':';
	 Timebuf[3] =((PRTCBuf[7]>>4) & 0x0F)+'0';	 
	 Timebuf[4] = (PRTCBuf[7]     & 0x0F)+'0';
	 Timebuf[5] = ':';
	 Timebuf[6] =((PRTCBuf[8]>>4) & 0x0F)+'0';
	 Timebuf[7] = (PRTCBuf[8]     & 0x0F)+'0';
	 		 	 
     return 1;
         
}

void timesyn(void)
{    
     extern void RTCInit (TimeStruct TimeDate);
     CurTime.year = (Datebuf[0]-0x30)*1000+(Datebuf[1]-0x30)*100+(Datebuf[2]-0x30)*10+(Datebuf[3]-0x30);
     CurTime.month= (Datebuf[5]-0x30)*10+(Datebuf[6]-0x30);
     CurTime.day  = (Datebuf[8]-0x30)*10+(Datebuf[9]-0x30);
     CurTime.week = PRTCBuf[5];
     CurTime.hour = (Timebuf[0]-0x30)*10+(Timebuf[1]-0x30);     

⌨️ 快捷键说明

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