RTC(Real Time Clock)單元能在系統斷電時通過系統備用電池供電,RTC能通過ARM的STRB/LDRB指令傳輸8位BCD數據到CPU,該數據包括時,分,秒,小時,天,月和年,RTC使用一個外部32.768KHZ的晶體也能執行報警功能。 報警功能。在掉電模式或正常工作模式下,RTC能夠在指定的時間產生報警信號。在正常工作模式下,報警中斷(ALMINT)被激活。在掉電模式下,電源管理蘇醒信號PMWKUP也與ALMINT一樣處于激活狀態。RTC的報警寄存器(RTCALM)可以決定報警的使能或禁止和報警時間的設置條件。 節拍中斷。RTC節拍時間用于中斷請求。TICNT寄存器具有一個中斷使能位,同時其中的計數值用于中斷。當計數值到達0時,節拍時間中斷就會觸發。中斷的時間間隔計算如下: 中斷的間隔時間 = (n+1)/128 單位是秒 其中n的值在1"127 下面的程序實現了,按一個鍵,可以更新當前時間,可以通過串口發送數據修改當前時間,設定報警寄存器,報警被觸發后,會觸發時間節拍中斷,中斷時間間隔位1s,持續時間是20s,也就是間隔1s蜂鳴器響一下。按另一個鍵,停止節拍時間中斷,停止蜂鳴器。 #include "Font_Libs.h" #include "2440addr.h" #define _ISR_STARTADDRESS 0x33ffff00 #define pISR_EINT0 (*(unsigned *)(_ISR_STARTADDRESS+0x20)) #define pISR_TICK (*(unsigned *)(_ISR_STARTADDRESS+0x40)) #define pISR_RTC (*(unsigned *)(_ISR_STARTADDRESS+0x98)) #define pISR_EINT2 (*(unsigned *)(_ISR_STARTADDRESS+0x28)) #define pISR_UART0 (*(unsigned *)(_ISR_STARTADDRESS+0x90)) //垂直同步信號的脈寬、后肩和前肩 #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 alarm_buffer[6] ={0,1*16+8,0,0,0,0}; unsigned char *temp; unsigned char str0[] = "當前時間為"; unsigned char str1[] = "年"; unsigned char str2[] = "月"; unsigned char str3[] = "日"; unsigned char str[][7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; U32 flag, count, t, i; void Init_LCD(){ rLCDCON1=(CLKVAL<<8)|(MMODE<<7)|(PNRMODE<<5)|(BPPMODE<<1)|0; //設置CLKVAL,VCLK=HCLK/[(CLKVAL+1)*2],決定VM的觸發方式,選擇顯示模式和BPP模式,暫時不要開啟LCD,因為還沒有設置好 rLCDCON2=(VBPD<<24)|(LINEVAL<<14)|(VFPD<<6)|(VSPW); //rLCDCON2,rLCDCON3和rLCDCON4主要設置時序 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需要進行翻轉,因為CPU發出的是正脈沖,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; if(data_buffer[0] == 0){ 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; } } } void __irq Uart_ISR(void){ char buf; rSUBSRCPND |= 0x3; rSRCPND |= 0x1<<28; rINTPND |= 0x1<<28; if(rUTRSTAT0&0x1){ buf = rURXH0; if((buf == 0xaa) && (t == 0)){ t = 1; i = 0; rUTXH0 = 0xaa; } else{ if(t){ data_buffer[ i] = buf; i++; rUTXH0 = 0xdd; if(i == 7){ rUTXH0 = 0xbb; SetTime(); i = 0; t = 0; flag = 1; } } else{ rUTXH0 = 0xcc; } } } } //2010年12月26日星期日19:17:51 void ShowTime(){ U32 qh, wh; unsigned char s0,s1; int i, t, k; char h, l; GetTime(); //當前時間為 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 SetAlarm(){ rALMSEC = alarm_buffer[0]; rALMMIN = alarm_buffer[1]; rALMHOUR = alarm_buffer[2]; rALMDATE = alarm_buffer[3]; rALMMON = alarm_buffer[4]; rALMYEAR = alarm_buffer[5]; } void __irq EINT0_ISR(void){ rSRCPND |= 1; //SRCPND 通過寫入數據清零,如果不清零,會反復進行請求 rINTPND |= 1; //INDPND 通過置1清零 flag = 1; } void __irq EINT2_ISR(void){ //使用__irq這個關鍵字定義終端服務例程,這樣系統會自動為我們保留一些變量,并能在中斷處理完后正確的返回 rSRCPND |= 1<<2; rINTPND |= 1<<2; rTICNT &= "(1<<7); rGPBDAT = 0xfe; //如果按鍵被按下,關閉節拍時間中斷 } void __irq Alarm_ISR(void){ rSRCPND |= 1<<30; //SRCPND 通過寫入數據清零,如果不清零,會反復進行請求 rINTPND |= 1<<30; //INDPND 通過置1清零 rTICNT = (1<<7)|(127<<0); //開啟節拍時間中斷,設置每1s中斷一次 } void __irq Tick_ISR(void){ rSRCPND |= 1<<8; //SRCPND 通過寫入數據清零,如果不清零,會反復進行請求 rINTPND |= 1<<8; //INDPND 通過置1清零 if(count%2 == 0) rGPBDAT = 0x1; else rGPBDAT = 0xfe; count++; if(count == 20){ count = 0; rGPBDAT = 0; rTICNT &= "(1<<7); //中斷20次,然后停止節拍時間中斷 } } int Main(){ flag = 0; count = 0; t = 0; rGPFCON &= 0xffcc; //0 rGPFCON |= (1<<1)|(1<<5); rGPFUP = 0xfe; rGPBCON &= 0xfffc; rGPBCON |= 0x1; rGPBUP = 0xfe; rULCON0 = 0xfff00; rULCON0 |= 0x3; rUCON0 = 0x0800; rUCON0 |= 0x05; rUBRDIV0 = 26; rRTCALM = (1<<1)|(1<<6); //主要全局報警使能,然后是分鐘報警使能 rSUBSRCPND |= 0x3; rSRCPND |= (1<<0)|(1<<2)|(1<<8)|(1<<28)|(1<<30); //先清一下 rINTPND |= (1<<0)|(1<<2)|(1<<8)|(1<<28)|(1<<30); //先清一下 rINTMSK &= ("(0x1<<0))&("(0x1<<2))&("(0x1<<8))&("(0x1<<28))&("(0x1<<30)); //開中斷 rINTSUBMSK &= ("(0x1<<0)) & ("(0x1<<1)); pISR_EINT0 = (U32)EINT0_ISR; pISR_EINT2 = (U32)EINT2_ISR; pISR_TICK = (U32)Tick_ISR; pISR_RTC = (U32)Alarm_ISR; pISR_UART0 = (U32)Uart_ISR; Init_LCD(); Paint_background(0xffffff,0,0,320,240); SetAlarm(); SetTime(); ShowTime(); while(1){ if(flag){ Paint_background(0xffffff,0,0,320,240); ShowTime(); flag = 0; } } } 李萬鵬 |