一、实验平台:开发板fs2410,采用三星s3c2410的CPU,无操作系统。
二、实现功能:设置开发板时间,并通过串口实时显示开发板的时间。
三、实验原理:
实时时钟单元 RTC能够在系统电源关闭后依靠后备电池供电工作。RTC可以将8位数据通过STRB/LDRB ARM指令以BCD码的形式传给CPU,这些数据包括秒、分、小时、日期、星期几、月、和年。RTC单元在外部32.768KHz的晶振下工作,并可以执行报警功能。
四、实验现象:
五、实验总结:
1、要注意寄存器里存储的时间都是以BCD码的形式进行存储的;
2、先用settime()函数将开发板的时间设置为当前时间;
3、再注释掉settime()函数,这样通过串口就可以观察到开发板的实时时间了。
六、示例代码:
/*s3c2410.h*/
#ifndef _S3C2410_H_
#define _S3C2410_H_
#define ULCON0 (*(volatile unsigned long *)0x50000000)
#define UCON0 (*(volatile unsigned long *)0x50000004)
#define UFCON0 (*(volatile unsigned long *)0x50000008)
#define UMCON0 (*(volatile unsigned long *)0x5000000C)
#define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
#define UTXH0 (*(volatile unsigned long *)0x50000020)
#define URXH0 (*(volatile unsigned long *)0x50000024)
#define UBRDIV0 (*(volatile unsigned long *)0x50000028)
#define GPHCON (*(volatile unsigned long *)0x56000070)
#define GPHDAT (*(volatile unsigned long *)0x56000074)
#define GPHUP (*(volatile unsigned long *)0x56000078)
#define ADCCON (*(volatile unsigned long *)0x58000000)
#define ADCDAT0 (*(volatile unsigned long *)0x5800000c)
#define RTCCON (*(volatile unsigned long *)0x57000040)
#define TICNT (*(volatile unsigned long *)0x57000044)
#define RTCRST (*(volatile unsigned long *)0x5700006c)
#define BCDSEC (*(volatile unsigned long *)0x57000070)
#define BCDMIN (*(volatile unsigned long *)0x57000074)
#define BCDHOUR (*(volatile unsigned long *)0x57000078)
#define BCDDATE (*(volatile unsigned long *)0x5700007c)
#define BCDDAY (*(volatile unsigned long *)0x57000080)
#define BCDMON (*(volatile unsigned long *)0x57000084)
#define BCDYEAR (*(volatile unsigned long *)0x57000088)
#define GPH2_out (1<<5)
#define GPH3_in (1<<7)
void putc(char ch);
char getc(void);
void putstr(char *str);
#endif
/*串口UART0*/
#include "s3c2410.h"
void putc(char ch)
{
while(!(UTRSTAT0 & 0X2));
UTXH0 = ch;
}
char getc(void)
{
while(!(UTRSTAT0 & 0X1));
return URXH0;
}
void putstr(char *str)
{
while(*str != '\0'){
if(*str == '\n'){
putc('\n');
putc('\r');
}
else
putc(*str);
str++;
}
}
/*RTC.c*/
#include "s3c2410.h"
void wait(unsigned long dly)
{
for(;dly>0;dly--);
}
void atoi(unsigned long x)
{
unsigned long temp;
int i;
char data[3];
for(i=0;i<3;i++)
data[i] = '\0';
for(i=0;i<2;i++){
temp = (x>>4*i)&(~0xfffffff0);
if(temp <10)
data[1-i] = '0'+temp;
else
data[1-i] = 'a'+temp-10;
}
putstr(data);
}
void atoI(unsigned long x)
{
unsigned long temp;
char ch;
temp = x&0xf;
switch(temp){
case 1:
putstr("一");
break;
case 2:
putstr("二");
break;
case 3:
putstr("三");
break;
case 4:
putstr("四");
break;
case 5:
putstr("五");
break;
case 6:
putstr("六");
break;
case 7:
putstr("日");
break;
}
}
void settime(int y,int mon,int date,int h,int min,int s,int day)
{
BCDYEAR = y;
BCDMON = mon;
BCDDATE = date;
BCDHOUR = h;
BCDMIN = min;
BCDSEC = s;
BCDDAY = day;
}
int main(void)
{
char sec[3];
GPHCON = GPH2_out|GPH3_in; //串口初始化
GPHUP = 0X0C;
UFCON0 &= 0;
UMCON0 &= ~(1<<4);
ULCON0 = 0X3;
UCON0 = 0X5;
UBRDIV0 = ((int)(50000000/115200/16)-1);
/*RTC初始化*/
RTCCON |= 1; //数据读写使能
TICNT = 0; //中断不使能
//settime(0x11,0x11,0x23,0x21,0x13,0x0,0x4);
putstr("\n");
while(1){
putstr(" 北京时间:");
putstr("20");
atoi(BCDYEAR);
putstr("年");
atoi(BCDMON);
putstr("月");
atoi(BCDDATE);
putstr("日 ");
atoi(BCDHOUR);
putc(':');
atoi(BCDMIN);
putc(':');
atoi(BCDSEC);
putstr(" 星期");
atoI(BCDDAY);
putc('\r');
wait(200000);
}
return 0;
}