S3C2440-RTC

發(fā)布時(shí)間:2011-3-25 14:25    發(fā)布者:techshare
關(guān)鍵詞: RTC , S3C2440
實(shí)時(shí)時(shí)鐘(RTC)的主要功能是在系統掉電的情況下,利用后備電源使時(shí)鐘繼續運行,從而不會(huì )丟失時(shí)間信息。

s3c2440內部集成了RTC模塊,而且用起來(lái)也十分簡(jiǎn)單。其內部的寄存器BCDSEC,BCDMIN,BCDHOUR,BCDDAY,BCDDATE,BCDMON和BCDYEAR分別存儲了當前的秒,分,小時(shí),星期,日,月和年,表示時(shí)間的數值都是BCD碼。這些寄存器的內容可讀可寫(xiě),并且只有在寄存器RTCCON的第0位為1時(shí)才能進(jìn)行讀寫(xiě)操作。為了防止誤操作,當不進(jìn)行讀寫(xiě)時(shí),要把該位清零。當讀取這些寄存器時(shí),能夠獲知當前的時(shí)間;當寫(xiě)入這些寄存器時(shí),能夠改變當前的時(shí)間。另外需要注意的是,因為有所謂的“一秒誤差”,因此當讀取到的秒為0時(shí),需要重新再讀取一遍這些寄存器的內容,才能保證時(shí)間的正確。

下面是一個(gè)程序,可以在LCD上顯示當前時(shí)間,如果按下按鍵,中斷產(chǎn)生,會(huì )更新LCD上的時(shí)間。如果不通過(guò)按鍵,直接刷屏,會(huì )造成閃爍。






#include "Font_Libs.h"   
#include "2440addr.h"   
#define _ISR_STARTADDRESS 0x33ffff00      
#define pISR_EINT0 (*(unsigned *)(_ISR_STARTADDRESS+0x20))      
//垂直同步信號的脈寬、后肩和前肩        
#define VSPW       15        
#define VBPD       3        
#define VFPD       5         
//水平同步信號的脈寬、后肩和前肩        
#define HSPW       8        
#define HBPD       58        
#define HFPD      15        
#define CLKVAL 10        
#define HOZVAL 319        
#define LINEVAL 239        
#define PWREN 1        
#define MMODE 0        
#define PNRMODE 3        
#define BPPMODE 13        
#define INVVCLK 0        
#define INVVD 0        
#define INVVDEN 0         
#define U32 unsigned int         
#define M5D(n) ((n) & 0x1fffff)        
#define PAGEWIDTH 320        
#define OFFSIZE 0        
#define LCD_XSIZE 320        
#define LCD_YSIZE 240        
#define SCR_XSIZE 320        
#define SCR_YSIZE 240        
#define INVVLINE  1            
#define INVVFRAME 1            
#define BPP24BL 0        
#define BSWP 0        
#define HWSWP 0         
volatile U32 LCD_BUFFER[240][320];      
unsigned char data_buffer[7] = {5*16+1,1*16+7,1*16+9,2*16+6,7,1*16+2,1*16+0};      
unsigned char *temp;   
unsigned char str0[] = "當前時(shí)間為";   
unsigned char str1[] = "年";   
unsigned char str2[] = "月";   
unsigned char str3[] = "日";   
unsigned char str[][7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};      
U32 flag;   
   
void Init_LCD(){        
rLCDCON1=(CLKVAL<<8)|(MMODE<<7)|(PNRMODE<<5)|(BPPMODE<<1)|0;   //設置CLKVAL,VCLK=HCLK/[(CLKVAL+1)*2],決定VM的觸發(fā)方式,選擇顯示模式和BPP模式,暫時(shí)不要開(kāi)啟LCD,因為還沒(méi)有設置好      
       rLCDCON2=(VBPD<<24)|(LINEVAL<<14)|(VFPD<<6)|(VSPW);    //rLCDCON2,rLCDCON3和rLCDCON4主要設置時(shí)序        
       rLCDCON3=(HBPD<<19)|(HOZVAL<<8)|(HFPD);        
       rLCDCON4=(HSPW);        
       rLCDCON5 = (BPP24BL<<12) | (INVVCLK<<10) | (INVVLINE<<9) | (INVVFRAME<<8) | (0<<7) | (INVVDEN<<6) | (PWREN<<3)  |(BSWP<<1) | (HWSWP);                         //INVVLINE和INVVFRAME需要進(jìn)行翻轉,因為CPU發(fā)出的是正脈沖,LCD使用的是負脈沖,所以要改變極性,PWREN使能電源信號      
       rLCDSADDR1=(((U32)LCD_BUFFER>>22)<<21)|M5D((U32)LCD_BUFFER>>1);        
       rLCDSADDR2=M5D(((U32)LCD_BUFFER+(SCR_XSIZE*SCR_YSIZE*4))>>1 );        
       rLCDSADDR3=PAGEWIDTH*32/16;        
       rLCDINTMSK|=(3);              
       rTCONSEL = 0;                  
       rGPCUP = 0x0;        
       rGPDCON = 0xaaaaaaaa;        
       rGPCCON = 0xaaaa02a9;        
       rGPDUP = 0x0;        
       rGPGUP=rGPGUP&("(1<<4))|(1<<4);              
       rGPGCON=rGPGCON&("(3<<8))|(3<<8);         
       rLCDCON1 |= 1;   //使能數據輸出和LCD控制信號      
}              
                  
void Paint_background(U32 c, U32 startx, U32 starty, U32 endx, U32 endy){         
    U32 i,j;            
    for(j = starty; j < endy; j++)            
        for(i = startx; i < endx; i++)            
            LCD_BUFFER[j][ i] = c;            
}         
   
void SetTime(){   
    rRTCCON |= 0x1;   
    rBCDSEC = data_buffer[0];   
    rBCDMIN = data_buffer[1];   
    rBCDHOUR = data_buffer[2];   
    rBCDDATE = data_buffer[3];   
    rBCDDAY = data_buffer[4];   
    rBCDMON = data_buffer[5];                     
    rBCDYEAR = data_buffer[6];   
    rRTCCON &= 0xfe;   
}   
   
void GetTime(){   
    rRTCCON |= 0x1;   
    data_buffer[0] = rBCDSEC;   
    data_buffer[1] = rBCDMIN;   
    data_buffer[2] = rBCDHOUR;   
    data_buffer[3] = rBCDDATE;   
    data_buffer[4] = rBCDDAY;   
    data_buffer[5] = rBCDMON;               
    data_buffer[6] = rBCDYEAR;   
    rRTCCON &= 0xfe;   
    if(data_buffer[0] == 0){   
        rRTCCON |= 0x1;   
        data_buffer[0] = rBCDSEC;   
        data_buffer[1] = rBCDMIN;   
        data_buffer[2] = rBCDHOUR;   
        data_buffer[3] = rBCDDATE;   
        data_buffer[4] = rBCDDAY;   
        data_buffer[5] = rBCDMON;               
        data_buffer[6] = rBCDYEAR;   
        rRTCCON &= 0xfe;   
    }   
}   
   
         
void Paint_text(U32 x, U32 y, U32 color, unsigned char ch[]){           
    int i, j, test,t = 0;           
    for(i = 0; i < 16; i++){           
        test = 0x80;           
        for(j = 0; j < 16; j++){           
            if(j == 8){           
                test = 0x80;           
                t++;           
            }           
            if(ch[t] & test)           
                LCD_BUFFER[x+i][y+j] = color;           
            test >>= 1;              
        }           
        t++;           
    }           
}     
   
void Paint_Ascii(U32 x, U32 y, U32 color, unsigned char ch[]){        
    int i, j, test;        
    for(i = 0; i < 16; i++){        
        test = 0x80;        
        for(j = 0; j < 8; j++){        
            if(test & ch[ i])        
                LCD_BUFFER[x+i][y+j] = color;        
            test >>= 1;        
        }        
    }        
}         
//2010年12月26日星期日19:17:51   
void ShowTime(){   
    U32 qh, wh;   
    unsigned char s0,s1;   
    int i, t, k;   
    char h, l;     
    GetTime();   
    //當前時(shí)間為   
    for(i = 0,t = 0, k = 0; i < 5; i++){   
        s0 = str0[t];   
        s1 = str0[t+1];   
        qh = s0-0xa0;   
        wh = s1-0xa0;   
        temp = & __HZK[((qh-1)*94+wh-1)*32];   
        Paint_text(100,k,0x0,temp);   
        t = t + 2;   
        k += 16;   
    }   
    //:   
    temp = &__ASCII[':'*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //20   
    temp = &__ASCII['2'*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    temp = &__ASCII['0'*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //10   
    h = (data_buffer[6]>>4)+48;   
    temp = &__ASCII[h*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    l = (data_buffer[6]&0x0f)+48;   
    temp = &__ASCII[l*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //年   
    qh = str1[0]-0xa0;   
    wh = str1[1]-0xa0;   
    temp = & __HZK[((qh-1)*94+wh-1)*32];   
    Paint_text(100,k,0x0,temp);   
    k+=16;   
    //12   
    h = (data_buffer[5]>>4)+48;   
    temp = &__ASCII[h*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    l = (data_buffer[5]&0xf)+48;   
    temp = &__ASCII[l*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //月   
    qh = str2[0]-0xa0;   
    wh = str2[1]-0xa0;   
    temp = & __HZK[((qh-1)*94+wh-1)*32];   
    Paint_text(100,k,0x0,temp);   
    k+=16;   
    //26   
    h = (data_buffer[3]>>4)+48;   
    temp = &__ASCII[h*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    l = (data_buffer[3]&0xf)+48;   
    temp = &__ASCII[l*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //日   
    qh = str3[0]-0xa0;   
    wh = str3[1]-0xa0;   
    temp = & __HZK[((qh-1)*94+wh-1)*32];   
    Paint_text(100,k,0x0,temp);   
    k+=16;   
    //星期日      
    for(i = 0,t = 0; i < 3; i++){   
        s0 = str[0][t];   
        s1 = str[0][t+1];   
        qh = s0-0xa0;   
        wh = s1-0xa0;   
        temp = & __HZK[((qh-1)*94+wh-1)*32];   
        Paint_text(100,k,0x0,temp);   
        t = t + 2;   
        k += 16;   
    }     
    //19:17:51   
    //19   
    h = (data_buffer[2]>>4)+48;   
    temp = &__ASCII[h*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    l = (data_buffer[2]&0xf)+48;   
    temp = &__ASCII[l*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //:   
    temp = &__ASCII[':'*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //17   
    h = (data_buffer[1]>>4)+48;   
    temp = &__ASCII[h*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    l = (data_buffer[1]&0xf)+48;   
    temp = &__ASCII[l*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //:   
    temp = &__ASCII[':'*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    //51  
    h = (data_buffer[0]>>4)+48;   
    temp = &__ASCII[h*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;   
    l = (data_buffer[0]&0xf)+48;   
    temp = &__ASCII[l*16];   
    Paint_Ascii(100,k,0x0,temp);   
    k+=8;      
}     
   
void __irq Key_ISR(void){   
    rSRCPND |= 1;                 //SRCPND 通過(guò)寫(xiě)入數據清零,如果不清零,會(huì )反復進(jìn)行請求      
    rINTPND |= 1;                 //INDPND 通過(guò)置1清零      
    flag = 1;   
}   
        
int Main(){         
    flag = 0;   
    rGPFCON &= 0xfffc;    //0      
    rGPFCON |= 1<<1;      
    rGPFUP = 0xfe;      
    rSRCPND |= 1<<0;      
    rINTPND |= 1<<0;      
    rINTMSK &= "(0x1<<0);     //開(kāi)中斷      
    pISR_EINT0 = (U32)Key_ISR;      
    Init_LCD();   
    Paint_background(0xffffff,0,0,320,240);   
    SetTime();   
    ShowTime();   
    while(1){   
        if(flag){   
            Paint_background(0xffffff,0,0,320,240);   
            ShowTime();   
            flag = 0;   
        }   
    }   
}


李萬(wàn)鵬
本文地址:http://selenalain.com/thread-59911-1-1.html     【打印本頁(yè)】

本站部分文章為轉載或網(wǎng)友發(fā)布,目的在于傳遞和分享信息,并不代表本網(wǎng)贊同其觀(guān)點(diǎn)和對其真實(shí)性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問(wèn)題,我們將根據著(zhù)作權人的要求,第一時(shí)間更正或刪除。
您需要登錄后才可以發(fā)表評論 登錄 | 立即注冊

相關(guān)在線(xiàn)工具

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復 返回頂部 返回列表
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页