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

📄 rtc.c

📁 门禁系统 interated with LCD
💻 C
字号:
#include "RTC.h"
#include "always.h"

int RTC_Init(void) 
{
	unsigned char DATA[16];
	DATA[0] = 0x00;
	DATA[1] = 0x03; //Enable timer/Alarm interrupts
	DATA[2] = 0x59;
	DATA[3] = 0x06;
	DATA[4] = 0x00;
	DATA[5] = 0x00;
	DATA[6] = 0x00;
	DATA[7] = 0x00;
	DATA[8] = 0x00;
	DATA[9] = 0x80;  // Disable Alarm (1 Disabled, 0 Enable)
	DATA[10] = 0x80; // Disable Alarm (1 Disabled, 0 Enable)
	DATA[11] = 0x80; // Disable Alarm (1 Disabled, 0 Enable)
	DATA[12] = 0x80; // Disable Alarm (1 Disabled, 0 Enable)
	DATA[13] = 0x83; //Enable Clockout at 1Hz
	DATA[14] = 0x00;
	DATA[15] = 0x00;
	if (RTC_Write_All(DATA)== I2C_ERROR) return I2C_ERROR;
	return FALSE;

}

//---------------------------------------------------------
//Reset the alarm flags
int RTC_Reset_Alarm(void)
{
	int temp;
	if ((temp = RTC_Read(RTC_CONTROL_2))== I2C_ERROR) return I2C_ERROR;
	bit_clear(temp,2);
	bit_clear(temp,3);
	return FALSE;
	
}

//Block Write the particular data
int RTC_Write(int type,int data) 
{
	if ((i2c_Write(RTC_W,type,data)) == I2C_ERROR) return I2C_ERROR;
	//unsigned char DATA[16];
	//if (RTC_Read_All(DATA)== I2C_ERROR) return I2C_ERROR;
	//DATA[type] = data;
	//if (RTC_Write_All(DATA) == I2C_ERROR) return I2C_ERROR;
	return FALSE;
}


//block write the entire RTC registers
int RTC_Write_All(unsigned char *DATA) 
{
	if (i2c_WriteTo(RTC_W)) return I2C_ERROR;
	if (i2c_PutByte(0x00)== I2C_ERROR) return I2C_ERROR;
	if (i2c_PutString(DATA,16)== I2C_ERROR) return I2C_ERROR;
	i2c_Stop();
	return FALSE;
}

int RTC_Read(int type) 
{
	int data,temp;
	if ((temp = i2c_Read(RTC_R,type))== I2C_ERROR) return I2C_ERROR;
	switch (type) 
	// adjust data to reflect only data bits
	{
	case 0: //RTC_CONTROL Register 1		
	case 1: //RTC_CONTROL Register 1
	case 13://Clk_Out
	case 14://Timer Control
	case 15:// Timer countdown
			break;	
	case 2:	//Seconds
	case 3: //Minutes
		data = (0x0F & temp) + (10*((0x70 & temp)>>4));		//Lower BCD
		break;
	case 4: //Hours
	case 5: //Days
		data = (0x0F & temp) + (10*((0x30 & temp)>>4)); //BCD to decimal		
		break;
	case 6: //Weekdays
		data = (0x07 & temp); //BCD to decimal	
		break;
	case 7: //Months
	case 8: //Years
	case 9: //Minute_Alarm
	case 10: //Hour_Alarm
	case 11: //Day_Alarm
	case 12: //Weekday_Alarm
			data = (0x07 & temp); 
			break;
	default:
		return I2C_ERROR;
	}
	
	return data;
}

//---------------------------------------------------------
// Enables the selected alarms
int RTC_Enable_Alarm(int type)
{
	int temp;
	if( (temp = RTC_Read(RTC_CONTROL_2)) == I2C_ERROR) return I2C_ERROR;	
	temp |= 0x02; //To activate the AIE bit
	if ((RTC_Write(RTC_CONTROL_2,temp)) == I2C_ERROR) return I2C_ERROR;
	 
	
	switch(type)
	{
	case 9: //Minute_Alarm
		if ((temp=RTC_Read(MINUTES_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
		temp &= 0x7F; //Clear Bit 7 to enable Alarm
		if ((RTC_Write(MINUTES_ALARM,temp))== I2C_ERROR) return I2C_ERROR;
		break;
	
	case 10://Hour_Alarm
		if ((temp=RTC_Read(HOURS_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
		temp &= 0x7F; //Clear Bit 7 to enable Alarm
		if ((RTC_Write(HOURS_ALARM,temp))== I2C_ERROR) return I2C_ERROR;
		break;
	
	case 11://Day_Alarm
		if ((temp=RTC_Read(DAYS_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
		temp &= 0x7F; //Clear Bit 7 to enable Alarm
		if ((RTC_Write(DAYS_ALARM,temp))== I2C_ERROR) return I2C_ERROR;
		break;
	
	case 12://Weekday_Alarm
		if ((temp=RTC_Read(WEEKDAYS_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
		temp &= 0x7F; //Clear Bit 7 to enable Alarm
		if ((RTC_Write(WEEKDAYS_ALARM,temp))== I2C_ERROR) return I2C_ERROR;
		break;
	
	default:
		return I2C_ERROR;
	}
	return FALSE;
}
//Disables the selected Alarm
int RTC_Disable_Alarm(int type)
{
	int temp;	
	if( (temp = RTC_Read(RTC_CONTROL_2))== I2C_ERROR) return I2C_ERROR;	
	temp &= 0xFD; //To deactivate the AIE bit
	if ((RTC_Write(RTC_CONTROL_2,temp))== I2C_ERROR) return I2C_ERROR;
	
	switch(type)
	{
		case 9: //Minute_Alarm
			if ((temp=RTC_Read(MINUTES_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
			temp |= 0x80; //Set Bit 7 to disable Alarm
			if (i2c_Write(RTC_W,0x09,temp)) return I2C_ERROR;	//activate RTS module
			break;
		
		case 10://Hour_Alarm
			if ((temp=RTC_Read(MINUTES_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
			temp |= 0x80; //Set Bit 7 to disable Alarm
			if (i2c_Write(RTC_W,0x0A,temp)) return I2C_ERROR;	//activate RTS module
			break;
		
		case 11://Day_Alarm
			if ((temp=RTC_Read(MINUTES_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
			temp |= 0x80; //Set Bit 7 to disable Alarm
			if (i2c_Write(RTC_W,0x0B,temp)) return I2C_ERROR;	//activate RTS module
			break;
		
		case 12://Weekday_Alarm
			if ((temp=RTC_Read(MINUTES_ALARM))== I2C_ERROR) return I2C_ERROR;	//read data to maintain the contents
			temp |= 0x80; //Set Bit 7 to disable Alarm
			if (i2c_Write(RTC_W,0x0C,temp)) return I2C_ERROR;	//activate RTS module
			break;
		
		default:
			return I2C_ERROR;
	}
	return FALSE;
}

⌨️ 快捷键说明

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