C8051F930RTC初始化樣例程序_第1頁
C8051F930RTC初始化樣例程序_第2頁
C8051F930RTC初始化樣例程序_第3頁
C8051F930RTC初始化樣例程序_第4頁
C8051F930RTC初始化樣例程序_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、/在32.768KHz頻率下功耗很低兩節(jié)電吃模式下:870uA,單節(jié)電池模式:1.238ma/本例注意發(fā)生時鐘報警事件后,振蕩器故障報警事件也發(fā)生#include <C8051F930.h> / SFR declarations#define uint unsigned int#define uchar unsigned char #define ulong unsigned long int#define nop() _nop_();_nop_();_nop_();_nop_();union tcfint32 ulong mydword;structuchar by4;uchar

2、 by3;uchar by2;uchar by1;bytes;mylongint;/用聯(lián)合體定義32位操作#define SYSCLK 20000000 / 定義系統(tǒng)頻率 #define RTCCLK 32768 / 定義smaRTClock 頻率 #define WAKE_INTERVAL 2000 /喚醒時間間隔 #define SUSPEND 0x40 /定義 掛起方式PMU0CF值#define SLEEP 0x80 / 定義休眠方式PMU0CF值#define POWER_MODE SUSPEND #define LED_ON 0#define LED_OFF 1#define CA

3、PTURE0 0x00 / 定義smaRTClock內(nèi)部寄存器CAPTURE0 地址#define CAPTURE1 0x01 / 定義smaRTClock內(nèi)部寄存器 CAPTURE1 地址#define CAPTURE2 0x02 /定義smaRTClock內(nèi)部寄存器 CAPTURE2 地址#define CAPTURE3 0x03 /定義smaRTClock內(nèi)部寄存器CAPTURE3 地址#define RTC0CN 0x04 / 定義smaRTClock內(nèi)部寄存器 RTC0CN 地址 #define RTC0XCN 0x05 / 定義smaRTClock內(nèi)部寄存器 RTC0XCN 地址

4、#define RTC0XCF 0x06 / 定義smaRTClock內(nèi)部寄存器 RTC0XCF 地址#define RTC0PIN 0x07 / 定義smaRTClock內(nèi)部寄存器RTC0PIN 地址 #define ALARM0 0x08 / 定義smaRTClock內(nèi)部寄存器 ALARM0 地址 #define ALARM1 0x09 / 定義smaRTClock內(nèi)部寄存器ALARM1 地址 #define ALARM2 0x0A / 定義smaRTClock內(nèi)部寄存器ALARM2 地址 #define ALARM3 0x0B / 定義smaRTClock內(nèi)部寄存器 ALARM3 地址

5、sbit RED_LED=P15; sbit YELLOW_LED=P16; sbit s1=P02; sbit s2=P03; /-/ 函數(shù)聲明/- void PCA_Init(); void PORT_Init (void);void smaRTClock_Init (void);void OSCILLATOR_Init (void);uchar RTC_Read (uchar);void RTC_Write (uchar, uchar);void Timer2_Init (int counts);void TIMER2_ISR(void);/-/ main() 函數(shù)/-void main

6、 (void)uchar wakeup_source; PCA_Init(); PORT_Init(); PORT_Init (); OSCILLATOR_Init (); smaRTClock_Init (); / Initialize RTC Timer2_Init (SYSCLK / 12 / 10); / Init Timer2 to generate interrupts / at a 10 Hz rate. EA = 1; / Enable global interrupts /- / Main Application Loop /- while (1) if(PMU0CF &am

7、p; 0x0C) /發(fā)生 smaRTClock報警或者時鐘丟失事件 if(PMU0CF & 0x08) /檢查時鐘是否有效 EA = 0; YELLOW_LED = LED_ON; RED_LED = LED_ON; / while(1); /發(fā)生時鐘報警事件后,也發(fā)生了振蕩器報警 / Configure the Port I/O for Sleep Mode RED_LED = LED_OFF; / Turn off the LED or other / high-current devices / Place the device in Sleep Mode PMU0CF = 0x

8、20; / Clear all wake-up flags YELLOW_LED = LED_ON; PMU0CF = (POWER_MODE | 0x0C); YELLOW_LED = LED_OFF; wakeup_source = PMU0CF & 0x1F; PMU0CF = 0x20; if(wakeup_source & 0x10) EA = 0; YELLOW_LED = LED_ON; RED_LED = LED_ON; while(1); if(wakeup_source & 0x08) EA = 0; YELLOW_LED = LED_ON; RED

9、_LED = LED_ON; while(1); if(wakeup_source & 0x04) /-/端口初始化/-void PORT_Init (void) P0MDIN |= 0x0C; / P0.2, P0.3 are digital P1MDIN |= 0x60; / P1.5, P1.6 are digital P0MDOUT &= 0x0C; / P0.2, P0.3 are open-drain P1MDOUT |= 0x60; / P1.5, P1.6 are push-pull P0 |= 0x0C; / Set P0.2, P0.3 latches to

10、 '1' XBR2 = 0x40; / Enable crossbar and enable / weak pull-ups/-/ smaRTClock 初始化/- void smaRTClock_Init (void) uchar i; uchar CLKSEL_SAVE = CLKSEL; /保存系統(tǒng)時鐘 RTC0KEY = 0xA5; / 時鐘解鎖 RTC0KEY = 0xF1; RTC_Write (RTC0XCN, 0xC0); /使能自動增益控制,選擇時鐘為晶體模式 RTC_Write (RTC0XCF, 0x88); / 使能電容負(fù)載步進(jìn)到設(shè)定值 RTC_Writ

11、e (RTC0CN, 0x80); / 使能 smaRTClock 振蕩器 CLKSEL = 0x74; / 將時鐘切換到片內(nèi)低功耗時鐘并128分頻此時系統(tǒng)頻率約156KHz for (i=0xFF;i!=0;i-); /等待2ms while (RTC_Read (RTC0XCN) & 0x10)= 0x00); /查詢時鐘有效位 while (RTC_Read (RTC0XCF) & 0x40)= 0x00); /查詢負(fù)載電容就序位 RTC_Write (RTC0CN, 0xC0); / 使能 smaRTClock 丟失檢測 for (i=0xFF;i!=0;i-); /等

12、待2ms PMU0CF = 0x20; /清除電源管理喚醒源標(biāo)志 CLKSEL = CLKSEL_SAVE; / 恢復(fù)系統(tǒng)時鐘 mylongint.mydword=(RTCCLK * WAKE_INTERVAL) / 1000L); /設(shè)置時鐘報警值 RTC_Write (ALARM0, mylongint.bytes.by1); / Least significant byte RTC_Write (ALARM1, mylongint.bytes.by2); RTC_Write (ALARM2, mylongint.bytes.by3); RTC_Write (ALARM3, mylongi

13、nt.bytes.by4); / Most significant byte RTC_Write (RTC0CN, 0xDC); /使能 smaRTClock定時報警自動復(fù)位 /-/ 時鐘初始化/-/-void OSCILLATOR_Init (void) RSTSRC = 0x06; CLKSEL = 0x04; / 選擇片內(nèi)20Mhz低功耗振蕩器,分頻系數(shù)選1 /-/PCA初始化函數(shù)/- void PCA_Init() PCA0MD &= 0x40; PCA0MD = 0x00; /- /smaRTClock 讀函數(shù)/-uchar RTC_Read (uchar reg) reg

14、&= 0x0F; RTC0ADR = reg; / 設(shè)置RTC內(nèi)部存儲器地址 RTC0ADR |= 0x80; / 置1 BUSY 位 讀數(shù)據(jù) while (RTC0ADR & 0x80) = 0x80); /查詢 BUSY 位 return RTC0DAT; / 返回讀取值/- /smaRTClock 寫函數(shù)/-void RTC_Write (uchar reg, uchar value) reg &= 0x0F; RTC0ADR = reg; / 設(shè)置RTC內(nèi)部存儲器地址 RTC0DAT = value; / 寫數(shù)據(jù) while (RTC0ADR & 0x80) = 0x80); /查詢 BUSY 位 /-/ Timer2_Init/- /void Timer2_Init (int counts) TMR2CN = 0x00; / Stop Timer2; Clear TF2; / use SYSCLK/12 as timebase CKCON &= 0x60; / Timer2 clocked based on T2XCLK; TMR2RL = -coun

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論