📄 ex_rtc.c
字号:
/******************************************************************************
* Copyright (C) Asic Center. 2001
* All Rights Reserved
*
* Filename : ex_rtc.c
* Function : be used to control the external rtc
* Revision :
* 2005/6/20 yuchao Create this file
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hardware_intc.h"
#include "hardware_reg.h"
#include "ex_rtc.h"
#include "MOTOType.h"
extern U8 timedemo[40];
extern U8 rtc_hour;
extern U8 rtc_minute;
extern U8 rtc_year;
extern U8 rtc_month;
extern U8 rtc_date;
void CaculateWeekDay(_RTC_TIME *time);
int delay_ns(int count)
{
int i;
// int sysclk = 30;
int sysclk = 90; //changed by xl(::sysclk is changed to 60MHz)
count = count * sysclk / 4000;
for(i = 0; i < count; i++);
return i;
}
void byte_write(U8 command, U8 data)
{
int i = 0;
RTC_CE(1)
RTC_IO_OUT
RTC_IO &= ~0x80; //clear PORTH[7]
for(i = 0; i < 8; i++)
{
RTC_IO |= ((command >> i) & 0x01) << 7;
delay_ns(3000);
RTC_SCLK(1)
delay_ns(3000);
RTC_IO &= ~0x80; //clear PORTH[7]
RTC_SCLK(0)
delay_ns(1000);
}
for(i = 0; i < 8; i++)
{
RTC_IO |= ((data >> i) & 0x01) << 7;
delay_ns(3000);
RTC_SCLK(1)
delay_ns(3000);
RTC_IO &= ~0x80; //clear PORTH[7]
RTC_SCLK(0)
delay_ns(1000);
}
RTC_CE(0)
delay_ns(4000);
}
U8 byte_read(U8 command)
{
int i = 0;
U8 data = 0;
RTC_CE(1)
RTC_IO_OUT
RTC_IO &= ~0x80; //clear PORTH[7]
for(i = 0; i < 8; i++)
{
RTC_IO |= ((command >> i) & 0x01) << 7;
delay_ns(2000); //主要影响
RTC_SCLK(1)
delay_ns(2000);
if(i == 7) break;
RTC_IO &= ~0x80; //clear PORTH[7]
RTC_SCLK(0)
delay_ns(1000);
}
RTC_IO_IN
for(i = 0; i < 8; i++)
{
RTC_SCLK(0)
delay_ns(3000);
data |= (RTC_IO & (1 << 7)) >> (7 - i );
if(i == 7) break;
RTC_SCLK(1)
delay_ns(25000);
}
RTC_CE(0)
delay_ns(2000);
return data;
}
_RTC_TIME RTC_TIME;
void EX_creat_time(_SystemSetting time)
{
(&RTC_TIME) -> second = time.second ;
(&RTC_TIME) -> minute = time.minute ;
(&RTC_TIME) -> hour = time.hour ;
(&RTC_TIME) -> date = time.date;
(&RTC_TIME) -> month = time.month;
(&RTC_TIME) -> year = time.year ;
CaculateWeekDay((&RTC_TIME));
SystemSetting.day = RTC_TIME.day;
(&RTC_TIME) -> second = (((&RTC_TIME) -> second / 10) << 4)|((&RTC_TIME) -> second % 10);
(&RTC_TIME) -> minute = (((&RTC_TIME) -> minute / 10) << 4)|((&RTC_TIME) -> minute % 10);
(&RTC_TIME) -> hour = (((&RTC_TIME) -> hour / 10) << 4)|((&RTC_TIME) -> hour % 10); //bit[7] = 0 : 24
//bit[7] = 1 : 12
(&RTC_TIME) -> date = (((&RTC_TIME) -> date / 10) << 4)|((&RTC_TIME) -> date % 10);
(&RTC_TIME) -> month = (((&RTC_TIME) -> month / 10) << 4)|((&RTC_TIME) -> month % 10);
(&RTC_TIME) -> year = (((&RTC_TIME) -> year / 10) << 4)|((&RTC_TIME) -> year % 10);
/*
RTC_TIME -> second = ((time.second / 10) << 4)|(time.second % 10);
RTC_TIME -> minute = ((time.minute / 10) << 4)|(time.minute % 10);
RTC_TIME -> hour = ((time.hour / 10) << 4)|(time.hour % 10); //bit[7] = 0 : 24
//bit[7] = 1 : 12
RTC_TIME -> date = ((time.date / 10) << 4)|(time.date % 10);
RTC_TIME -> month = ((time.month / 10) << 4)|(time.month % 10);
RTC_TIME -> year = ((time.year / 10) << 4)|(time.year % 10);
*/
/*
(&RTC_TIME) -> second = 44;
(&RTC_TIME) -> minute = 1;
(&RTC_TIME) -> hour = 0 ;
(&RTC_TIME) -> date = 0;
(&RTC_TIME) -> month = 0;
(&RTC_TIME) -> year = 0 ;
CaculateWeekDay((&RTC_TIME));
SystemSetting.day = RTC_TIME.day;
(&RTC_TIME) -> second = (((&RTC_TIME) -> second / 10) << 4)|((&RTC_TIME) -> second % 10);
(&RTC_TIME) -> minute = (((&RTC_TIME) -> minute / 10) << 4)|((&RTC_TIME) -> minute % 10);
(&RTC_TIME) -> hour = (((&RTC_TIME) -> hour / 10) << 4)|((&RTC_TIME) -> hour % 10); //bit[7] = 0 : 24
//bit[7] = 1 : 12
(&RTC_TIME) -> date = (((&RTC_TIME) -> date / 10) << 4)|((&RTC_TIME) -> date % 10);
(&RTC_TIME) -> month = (((&RTC_TIME) -> month / 10) << 4)|((&RTC_TIME) -> month % 10);
(&RTC_TIME) -> year = (((&RTC_TIME) -> month / 10) << 4)|((&RTC_TIME) -> month % 10);
*/
byte_write(WRITE_WP, WP_DISABLE);
byte_write(WRITE_SEC_COM, (&RTC_TIME) -> second);
byte_write(WRITE_MIN_COM, (&RTC_TIME) -> minute);
byte_write(WRITE_HOUR_COM, (&RTC_TIME) -> hour);
byte_write(WRITE_DATE_COM, (&RTC_TIME) -> date);
byte_write(WRITE_MONTH_COM, (&RTC_TIME) -> month);
byte_write(WRITE_DAY_COM, (&RTC_TIME) -> day);
byte_write(WRITE_YEAR_COM, (&RTC_TIME) -> year);
byte_write(WRITE_WP, WP_ENABLE);
}
void EX_creat_time_HISR_ENTRY(UNSIGNED argc, VOID *argv)
{
EX_creat_time(SystemSetting);
}
void EX_read_time(_SystemSetting *time)
{
RTC_CE_SEL
RTC_CE_OUT
RTC_SCLK_SEL
RTC_SCLK(0)
RTC_SCLK_OUT
RTC_IO_SEL
RTC_IO_OUT
time -> second = byte_read(READ_SEC_COM);
time -> minute = byte_read(READ_MIN_COM);
time -> hour = byte_read(READ_HOUR_COM);
time -> date = byte_read(READ_DATE_COM);
time -> year = byte_read(READ_YEAR_COM);
time -> day = byte_read(READ_DAY_COM);
time -> month = byte_read(READ_MONTH_COM);
time -> second = (((time -> second) >> 4) & 0x07) * 10 + ((time -> second) & 0x0F);
time -> minute = ((time -> minute >> 4) & 0x07) * 10 + (time -> minute & 0x0F);
time -> hour = ((time -> hour >> 4) & 0x03) * 10 + (time -> hour & 0x0F);
time -> date = ((time -> date >> 4) & 0x03) * 10 + (time -> date & 0x0F);
time -> month = ((time -> month >> 4) & 0x01) * 10 + (time -> month & 0x0F);
time -> year = ((time -> year >> 4) & 0x0F) * 10 + (time -> year & 0x0F);
/*
time ->year = 4321;
time ->month = 5;
time ->date = 24;
time ->hour = 14;
time ->minute = 3;
time -> second = 56;*/
}
void EX_config_rtc(void)
{
RTC_CE_SEL
RTC_CE(0)
RTC_CE_OUT
RTC_SCLK_SEL
RTC_SCLK(0)
RTC_SCLK_OUT
RTC_IO_SEL
RTC_IO &= ~(1 << 7);
RTC_IO_OUT
byte_write(WRITE_WP, WP_ENABLE);
}
/*
void ReadTimeStr(void)
{
RTC_TIME time;
U8 temp, ch[4];
memset(timedemo, 0, 40 * sizeof(U8));
read_time(&time);
rtc_hour = time.hour;
rtc_minute = time.minute;
rtc_year = time.year;
rtc_month = time.month;
rtc_date = time.date;
temp = time.year / 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
temp = time.year % 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
strcat(timedemo, " - ");
temp = time.month /10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
temp = time.month % 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
strcat(timedemo, " - ");
temp = time.date / 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
temp = time.date % 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
strcat(timedemo, " ");
temp = time.hour / 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
temp = time.hour % 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
strcat(timedemo, " : ");
temp = time.minute / 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
temp = time.minute % 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
strcat(timedemo, " : ");
temp = time.second / 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
temp = time.second % 10;
temp += 48;
sprintf(ch, "%c", temp);
strcat(timedemo, ch);
}
*/
/*
void SetupTime(void)
{
MSG msg;
U8 quit = 0;
U8 value = 0;
U32 mainwin;
U32 button1;
U32 button2;
int count = 0;
RTC_TIME time;
// U8 timedemo2[12];
U8 ch[4];
static char timedata[14];
// static volatile long long temp = 0;
mainwin = CreateWindow(WNDCLASS_WIN, NULL, NULL, 0, 0, PHY_LCD_W, PHY_LCD_H, 0, 0, NULL);
button1 = CreateWindow(WNDCLASS_BUTTON,"时间日期设置", WS_CHILD | BS_BOARD | BS_LEFT, 1, 1, 190, 16, mainwin, 0, NULL);
button2 = CreateWindow(WNDCLASS_BUTTON,"", WS_CHILD | BS_BOARD | BS_LEFT, 1, 36, 190, 16, mainwin, 0, NULL);
memset(timedata, 0, 14);
while(!quit){
ASIXGetMessage(&msg, NULL, 0, 0);
switch(msg.message){
case WM_CHAR:
value = (U8)msg.wparam;
if( value >= 48 && value <= 57 || value == 46 )
{
if(value == 46)//撤销键
{
if(count != 0)
{
timedata[count - 1] = 0;
count--;
}
else
break;
}
else if(count < 12)
{
sprintf(ch, "%c", value);//trans num to str
strcat(timedata , ch);
count++;
}
else break;
// memset(timedemo2, 0, 12);
// strcat(timedemo2, timedata);
//sscanf(timedemo2, "%d", &timedata);
SetWindowText(button2, timedata, NULL);
}
if( value == 211)//finish setting up time
{
if(count == 12)
{
time.year = (timedata[0] - 48) * 10 + (timedata[1] - 48);
time.month = (timedata[2] - 48) * 10 + (timedata[3] - 48);
time.date = (timedata[4] - 48) * 10 + (timedata[5] - 48);
time.hour = (timedata[6] - 48) * 10 + (timedata[7] - 48);
time.minute = (timedata[8] - 48) * 10 + (timedata[9] - 48);
time.second = (timedata[10] - 48) * 10 + (timedata[11] - 48);
time.day = 1; //not the exact week
creat_time(&time);
}
quit = 1;
}
break;
}
DefWindowProc(msg.message, msg.lparam, msg.data, msg.wparam);
}
DestroyWindow( mainwin );
return;
}
*/
////add the GPT reg address
#define TIMER_BASE 0X10003000
#define T1_LCR (TIMER_BASE+0X00)
#define T1_CCR (TIMER_BASE+0X04)
#define T1_CR (TIMER_BASE+0X08)
#define T1_ISCR (TIMER_BASE+0X0C)
#define T1_IMSR (TIMER_BASE+0X10)
#define T2_LCR (TIMER_BASE+0X14)
#define T2_CCR (TIMER_BASE+0X18)
#define T2_CR (TIMER_BASE+0X1C)
#define T2_ISCR (TIMER_BASE+0X20)
#define T2_IMSR (TIMER_BASE+0X24)
#define T3_LCR (TIMER_BASE+0X28)
#define T3_CCR (TIMER_BASE+0X2C)
#define T3_CR (TIMER_BASE+0X30)
#define T3_ISCR (TIMER_BASE+0X34)
#define T3_IMSR (TIMER_BASE+0X38)
#define T4_LCR (TIMER_BASE+0X3C)
#define T4_CCR (TIMER_BASE+0X40)
#define T4_CR (TIMER_BASE+0X44)
#define T4_ISCR (TIMER_BASE+0X48)
#define T4_IMSR (TIMER_BASE+0X4C)
void timer_delay(U32 time)
{
U32 tem;
mask_irq(INT_GPT); //mask interrupt
*(RP)T2_LCR = time;
*(RP)T2_CR = 0x3; //normal workmode
do
{
tem = *(RP)T2_IMSR;
}while( tem != 0x01);
tem = *(RP)T2_ISCR; //clear T2_IMSR reg
}
void CaculateWeekDay(_RTC_TIME *rawtime)
{
int year = 0;
U8 month, date, day, temp;
volatile _RTC_TIME *time = rawtime;
temp = (U8)time->year;
year = temp & 0x000000FF;
year += 2000; //year must be set after 2000
month = time->month;
date = time->date;
////kslarsen principle
day = (date + 2*month + 3*(month + 1)/5 + year + year/4 -year/100 + year/400) % 7;
day ++;
time->day = day;
// printf("%d\n", day);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -