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

📄 rtc.c

📁 基于c的电动机保护程序
💻 C
字号:
/*************************************************************************************
*	Copyright (c) 2005 by National ASIC System Engineering Research Center.
*	PROPRIETARY RIGHTS of ASIC are involved in the subject matter of this 
*	material.  All manufacturing, reproduction, use, and sales rights 
*	pertaining to this subject matter are governed by the license agreement.
*	The recipient of this software implicitly accepts the terms of the license.
*
*	File Name: rtc.c
*
*	File Description:
*			The file includes three functions to initialize the lcd controller and
*		display some pictures on the lcd.
*
*	Function Description:
*		STATUS	ModuleRtc(void)
*								The top function of rtc experiment.
*
*		void	run_rtc(void)			
*								Initialize the real-timer controller throuth registers.
*							You could change the paremeter in register for different
*							effect.
*
*		STATUS	set_ymd(U32 year, U32 month, U32 data)
*								The function is to set year, month and data.
*								year:	set year into real-timer controller arrange from 0
*										to 2047 year. 
*								month:	set month into real-timer controller arrange from 1
*										to 12 month. 
*								data:	set month into real-timer controller arrange from 1
*										to 31 month. 
*
*		STATUS	set_hms(U32 hour, U32 minute, U32 second)
*								The function is to set year, month and data.
*								hour:	set hour into real-timer controller arrange from 0
*										to 23 hour. 
*								minute:	set month into real-timer controller arrange from 1
*										to 59 minute. 
*								second:	set second into real-timer controller arrange from 1
*										to 59 second.
*
*		void	get_ymd(U32* year, U32* month, U32* data)
*								The function is to get year, month and data.
*								year:	get year from real-timer controller throuth variable
*										*year. 
*								month:	get month from real-timer controller throuth variable
*										*month. 
*								data:	get month from real-timer controller throuth variable
*										*data. 
*
*		void	get_hms(U32* hour, U32* minute, U32* second)
*								The function is to get year, month and data.
*								hour:	get hour from real-timer controller throuth variable
*										*hour. 
*								minute:	get month from real-timer controller throuth variable
*										*minute. 
*								second:	get second from real-timer controller throuth variable
*										*second. 

*		void	set_alarm(U32 hour, U32 minute)
*								The function is to set hour and minute.
*								hour:	set hour into real-timer controller arrange from 0
*										to 23 hour. 
*								minute:	set month into real-timer controller arrange from 1
*										to 59 minute. 
*								second:	set second into real-timer controller arrange from 1
*										to 59 second.
*		
*		void	rtc_handler(void)
*								The function is a interrupt server function. It identifies different
*							interrupt sources and handle them correspondly.
*
*		void minute_server(void)
*								The function is to print now time as interrupt occur. The function 
*							run_rtc will call this function. Every one minute will execute it.
*
*		void alarm_server(void)
*								The function is to print now time as interrupt occur. The function 
*							run_rtc will call this function. When the time you set is coming, it will 
*							be executed.
*
*	Created by Michael <yuyu_zh@seu.edu.cn>, 2005-03-22
**************************************************************************************/
#include "hardware_intc.h"
#include "hardware_reg.h"
#include "rtc.h"



#define HISR_STACK_SIZE    4096

extern char Task_Suspended;
extern NU_MEMORY_POOL  System_Memory;
extern VOID ERC_System_Error(INT error_code);
NU_HISR  RTC_Alarm_HISR;
NU_HISR	 EX_creat_time_HISR;
STATUS set_ymd(U32 year, U32 month, U32 day)
{
	U32 data;

	if(year >2014 || month >12|| day > 31 )
	{
		return E_CTX;
	}

	data = (year <<16) | (month << 8) | (U8)(day);
	write_reg(RTC_YMD, data);
	
	return E_OK;
}

STATUS set_hms(U32 hour, U32 minute, U32 second)
{
	U32 data;

	if(hour >24 || minute >60 || second > 60)
	{
		return E_CTX;
	}

	data = (hour << 24) | (minute << 16) | (second );
	write_reg(RTC_HMS, data);
		
	return E_OK;
}

void get_ymd(U32* year, U32 * month, U32* day)
{
	*year = ( read_reg(RTC_YMD) >> 16 ) & 0x7FF;
	*month = (read_reg(RTC_YMD) >> 8) & 0xF;
	*day = read_reg(RTC_YMD) & 0x3F; 

	return ;
}

void get_hms(U32* hour, U32* minute, U32* second)
{
	*hour = (read_reg(RTC_HMS) >> 24) & 0x1F;
	*minute = (read_reg(RTC_HMS) >> 16) & 0x3F;
	*second = read_reg(RTC_HMS) & 0x3F;
	
	return ;
}



void read_time(_SystemSetting * time)
{

	U32 year, month, date, hour, minute, second;
	get_ymd(&year, &month, &date);
	get_hms(&hour, &minute, &second);
	
	time->year = (U32)year;
	time->month = (U8)month;
	time->date = (U8)date;
	time->hour = (U8)hour;
	time->minute = (U8)minute;
	time->second = (U8)second;
	
}

void set_time(_SystemSetting * time)
{
	set_ymd(time->year, time->month, time->date);
      set_hms(time->hour, time->minute, time->second);
}
void SynTime_Form_EXRTC(void)
{
	
	
	EX_read_time(&SystemSetting);
	
	set_time(&SystemSetting);
}

STATUS set_alarm(U32 hour, U32 minute)
{
	U32 data;

	if(minute > 60 || hour >24)
	{
		return E_CTX;
	}

	data = (hour << 16) | (minute);
	write_reg( RTC_ALRM, data);

	return E_OK;
}

STATUS set_sample(U32 sample)
{
	U32 data;
	
	data = sample;
	write_reg( RTC_SAMP, data);

}


void RTC_Int_Inital(void)
{
	void  (*old_lisr)(int);
	void *pointer ;
	int    status;
	
	NU_Register_LISR(INT_RTC, RTC_Int_LISR, &old_lisr);
	//校时中断
	NU_Allocate_Memory(&System_Memory, &pointer,HISR_STACK_SIZE, NU_NO_SUSPEND); 
	status = NU_Create_HISR(&RTC_Alarm_HISR, "HISR_RTC", RTC_HISR_ENTRY, 2, pointer, HISR_STACK_SIZE); 
	if (status != NU_SUCCESS)
  	{
      		 ERC_System_Error(status);
   	}
   	//设定外部rtc中断
   	NU_Allocate_Memory(&System_Memory, &pointer,HISR_STACK_SIZE, NU_NO_SUSPEND); 
	status = NU_Create_HISR(&EX_creat_time_HISR, "HISR_EX_creat_time", EX_creat_time_HISR_ENTRY, 2, pointer, HISR_STACK_SIZE); 
	if (status != NU_SUCCESS)
  	{
      		 ERC_System_Error(status);
   	}
	
	
	/*
	set_alarm(3, 0);//每天凌晨校准时间一次
	
	write_reg(RTC_IEN, AIE);
	//write_reg(RTC_IEN, 0);
	write_reg(RTC_CTRL,RTCENABLE);
	unmask_irq(INT_RTC);
	*/
	write_reg(RTC_ISTAT,0x1f);//清中断
	write_reg(RTC_IEN, MIE);
	write_reg(RTC_CTRL,RTCENABLE);
	//write_reg(RTC_IEN, 0)
	unmask_irq(INT_RTC);
	
	EX_config_rtc();
 //  EX_creat_time(SystemSetting);
    EX_read_time(&SystemSetting) ; 
      
                   
              /*  设置内部RTC */
   
    set_time(&SystemSetting);
   
 
}

void RTC_Int_LISR(int vecter)
{
	mask_irq(INT_RTC);
	rtc_handler();
}

void RTC_HISR_ENTRY(UNSIGNED argc, VOID *argv)
{
	SynTime_Form_EXRTC();
	//Task_Suspended = 1;
	unmask_irq(INT_RTC);	
}

void rtc_handler(void)
{
	volatile U32 IntStatus;
		
	IntStatus = read_reg(RTC_ISTAT);
	
	
	if( (IntStatus & 0x01) != 0 )			//Sample int service
	{
		read_reg(RTC_ISTAT) |= 0X01;
		
	}
	if( (IntStatus & 0x02) != 0 )			//Second roll int service
		read_reg(RTC_ISTAT) |= 0X02;
		
	if( (IntStatus & 0x04) != 0 )			//Minute roll int service
	{
		read_reg(RTC_ISTAT) |= 0X04;
		//minute_server();
		NU_Activate_HISR(&RTC_Alarm_HISR);
	}
	
	if( (IntStatus & 0x08) != 0 )			//Alarm int service
	{
		read_reg(RTC_ISTAT) |= 0X08;
		NU_Activate_HISR(&RTC_Alarm_HISR);
	}
	
	if(  (IntStatus & 0x10) != 0 )			//Watchdog int service
		read_reg(RTC_ISTAT) |= 0x10;
	write_reg(RTC_ISTAT,0x1f);//清中断
	return ;
}

void minute_server(void)
{
	char *s;
	U32 year,month,data;
	U32 hour,minute,second;

	get_ymd(&year,&month,&data);
	get_hms(&hour,&minute,&second);
    
	//printf("%02d-%d-%d\t%d-%d-%d\n\n",(U8)year,(U8)month,(U8)data,(U8)hour,(U8)minute,(U8)second);
    
    #ifndef USB_ICE
        printf("%02d-%d-%d\t%d-%d-%d\n\n",(U8)year,(U8)month,(U8)data,(U8)hour,(U8)minute,(U8)second);
    #else
        sprintf(s,"%02d-%d-%d\t%d-%d-%d\n\n",(U8)year,(U8)month,(U8)data,(U8)hour,(U8)minute,(U8)second);
        print(s);
    #endif
	return ;
}

void alarm_server(void)
{   
    
	SynTime_Form_EXRTC();
   
}

/* end of rtc.c */

⌨️ 快捷键说明

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