S3C2440 bare metal clock RTC

Source: Internet
Author: User
Tags 04x

The real-time clock (RTC) unit can work with a backup battery when the system power is off. RTC can be operated by using STRB/LDRB ARM
Sends 8-bit binary Exchange Code (BCD) value data to the CPU. The data includes the time information of the year, month, day, week, hour, minute, and second.
The RTC unit operates on an external 32.768kHz crystal oscillator and can implement the alarm function.

 

Relatively simple RTC operations

Single, register settings are detailed in the Data Manual, so you don't need to write it. paste an RTC code. This program references the RTC program code of Tianxiang, the main reason is that he wrote well and all of them were modularized. I modified his code and set an alarm clock (using a buzzer) to run on the TQ2440 board. The program function is as follows: the serial port displays the time and LED1 flashes once per second. In the alarm setting, when the second is 20, the alarm time is displayed and the buzzer sounds for several seconds.

# Include "2440addr. h" # include "Option. h" # include "2440lib. h" # include "def. h" # define LED1_ON (rGPBDAT & = ~ (1 <5) # define led0000off (rGPBDAT | = (1 <5) void _ irq RTC_tickHandler (void); void _ irq RTC_alarmHandler (void ); u8 alarmflag = 0; typedef struct Date // defines a struct that represents the Date and time {year; U8 month; U8 day; U8 week_day; U8 hour; U8 minute; U8 second ;} date; date C_date; char * week_num [7] = {"SUN", "MON", "TUES", "WED", "THURS", "FRI ", "SAT"}; // define a pointer array void Beep_Freq_Set (U32 freq) {rGPBCON & = ~ 3; rGPBCON | = 2; // set GPB0 to OUT0 rGPBUP = 0x0; // enable rt1_0 & = ~ 0xff; rt00000 | = 15; // The pre-division value is 15 rTCFG1 & = ~ 0x0f; rTCFG1 | = 0x02; // The crossover value is 8 rTCNTB0 = (PCLK> 7)/freq; // set the value of the timer 0 counting buffer rTCMPB0 = rTCNTB0> 1; // The timer 0 compares the value of the buffer, and the PWM output duty cycle is 50% rTCON & = ~ 0x1f; rTCON | = 0xb; // auto-Reload, disable disguised form, manually update, enable timer 0 rTCON & = ~ 2; // clear the manual update bit} void Beep_Stop (void) {rGPBCON & = ~ 3; // set GPB0 as output rGPBCON | = 1; rGPBDAT & = ~ 1; // output low level} void delay (int x) {int I, j; for (I = 0; I <x; I ++) for (j = 0; j <1000000; j ++ );} /********************************** set the real-time clock date, time **********************************/void RTC_setdate (date * p_date) {rRTCCON = 0x01; // RTC read/write enabling, BCD clock, counter, no reset rBCDYEAR = p_date-> year; rBCDMON = p_date-> month; rBCDDATE = p_date-> day; rBCDDAY = p_date-> week_day; // set the date and time rBCDHOUR = p_date-> hour; rBCDMIN = p_date-> min Ute; rBCDSEC = p_date-> second; rRTCCON = 0x00; // disable RTC read/write, BCD clock, counter, no reset }/******************************* ******************************* * **/void RTC_getdate (date * p_date) {rRTCCON = 0x01; // RTC read/write enabling, BCD clock, counter, no reset p_date-> year = rBCDYEAR + 0x2000; p_date-> month = rBCDMON; p_date-> day = rBCDDATE; p_date-> week_day = rBCDDAY; // read Date and Time p_date-> hour = rBCDHOUR; p_date-> minute = rBCDMIN; p _ Date-> second = rBCDSEC; rRTCCON = 0x00; // read/write prohibition of RTC, BCD clock, counter, no reset }/******************************* * ** TICK interrupt initialization ********************************** /void RTC_tickIRQ_Init (U8 tick) {ClearPending (BIT_TICK); // clear the flag EnableIrq (BIT_TICK); // enable the source pISR_TICK = (unsigned) RTC_tickHandler to be interrupted; // interrupt function entry address: rRTCCON = 0x00; rTICNT = (tick & 0x7f) | 0x80; // enable interruption }/********************************* * set the alert date, time, and wake-up mode ** * *****************************/Void RTC_alarm_setdate (date * p_date, u8 mode) {rRTCCON = 0x01; rALMYEAR = p_date-> year; rALMMON = p_date-> month; rALMDATE = p_date-> day; rALMHOUR = p_date-> hour; rALMMIN = p_date-> minute; rALMSEC = p_date-> second; rRTCALM = mode; // RTC alarm control register rRTCCON = 0x00; ClearPending (BIT_RTC ); // clear the flag EnableIrq (BIT_RTC); // open RTC alarm INTERRUPT pISR_RTC = (unsigned) RTC_alar MHandler;} void Main (void) {SelectFclk (2); // set the system clock to 400 M ChangeClockDivider (2, 1); // set the frequency division to CalcBusClk (); // calculate bus frequency rGPHCON & = ~ (3 <4) | (3 <6); rGPHCON | = (2 <4) | (2 <6); // GPH2--TXD [0]; GPH3--RXD [0] rGPHUP = 0x00; // enables the pull-up function Uart_Init (); Uart_Select (0); rGPBCON & = ~ (3 <10) | (3 <12) | (3 <14) | (3 <16 )); // clear rGPBCON for GPBCON [] | = (1 <10) | (1 <12) | (1 <14) | (1 <16); // set GPB5 ~ 8: Output rGPBUP & = ~ (1 <5) | (1 <6) | (1 <7) | (1 <8); // set GPB5 ~ 8. rGPBDAT | = (1 <5) | (1 <6) | (1 <7) | (1 <8 ); // turn off the LED Beep_Stop (); // The Buzzer stops sound. The buzzer is used as the alarm clock sound C_date.year = 0x12; C_date.month = 0x05; C_date.day = 0x09; c_date.week_day = 0x03; // set the current date and time C_date.hour = 0x12; C_date.minute = 0x00; C_date.second = 0x10; RTC_setdate (& C_date ); c_date.second = 0x20; RTC_alarm_setdate (& C_date, 0x41); // 0x41 indicates enabling RTC and RTC_tickIRQ_Init (127 ); // set tick to Uart_Printf ("\ n -- -Real-time clock test program --- \ n "); while (Uart_GetKey ()! = ESC_KEY) {LED1_OFF; RTC_getdate (& C_date); if (alarmflag) {alarmflag = 0; Uart_Printf ("\ nRTC ALARM % 02x: % 02x: % 02x \ n ", c_date.hour, C_date.minute, C_date.second); Beep_Freq_Set (1000); delay (5); Beep_Stop ();}}} /*********************************** TICK interruption *** * ****************************/void _ irq RTC_tickHandler (void) {ClearPending (BIT_TICK); ledpendon; // refresh LED1 Delay (500); RTC_getdate (& C_date); Uart_Printf ("rtc time: % 04x-% 02x-% 02x % s % 02x: % 02x: % 02x \ n ", C_date.year, C_date.month, C_date.day, week_num [C_date.week_day], C_date.hour, C_date.minute, c_date.second );} /*********************************** TICK interruption *** * ****************************/void _ irq RTC_alarmHandler (void) {alarmflag = 1; ClearPending (BIT_RTC );}
# Include "2440addr. h" # include "Option. h" # include "2440lib. h" # include "def. h" # define LED1_ON (rGPBDAT & = ~ (1 <5) # define led0000off (rGPBDAT | = (1 <5) void _ irq RTC_tickHandler (void); void _ irq RTC_alarmHandler (void ); u8 alarmflag = 0; typedef struct Date // defines a struct that represents the Date and time {year; U8 month; U8 day; U8 week_day; U8 hour; U8 minute; U8 second ;} date; date C_date; char * week_num [7] = {"SUN", "MON", "TUES", "WED", "THURS", "FRI ", "SAT"}; // define a pointer array void Beep_Freq_Set (U32 freq) {rGPBCON & = ~ 3; rGPBCON | = 2; // set GPB0 to OUT0 rGPBUP = 0x0; // enable rt1_0 & = ~ 0xff; rt00000 | = 15; // The pre-division value is 15 rTCFG1 & = ~ 0x0f; rTCFG1 | = 0x02; // The crossover value is 8 rTCNTB0 = (PCLK> 7)/freq; // set the value of the timer 0 counting buffer rTCMPB0 = rTCNTB0> 1; // The timer 0 compares the value of the buffer, and the PWM output duty cycle is 50% rTCON & = ~ 0x1f; rTCON | = 0xb; // auto-Reload, disable disguised form, manually update, enable timer 0 rTCON & = ~ 2; // clear the manual update bit} void Beep_Stop (void) {rGPBCON & = ~ 3; // set GPB0 as output rGPBCON | = 1; rGPBDAT & = ~ 1; // output low level} void delay (int x) {int I, j; for (I = 0; I <x; I ++) for (j = 0; j <1000000; j ++ );} /********************************** set the real-time clock date, time **********************************/void RTC_setdate (date * p_date) {rRTCCON = 0x01; // RTC read/write enabling, BCD clock, counter, no reset rBCDYEAR = p_date-> year; rBCDMON = p_date-> month; rBCDDATE = p_date-> day; rBCDDAY = p_date-> week_day; // set the date and time rBCDHOUR = p_date-> hour; rBCDMIN = p_date-> min Ute; rBCDSEC = p_date-> second; rRTCCON = 0x00; // disable RTC read/write, BCD clock, counter, no reset }/******************************* ******************************* * **/void RTC_getdate (date * p_date) {rRTCCON = 0x01; // RTC read/write enabling, BCD clock, counter, no reset p_date-> year = rBCDYEAR + 0x2000; p_date-> month = rBCDMON; p_date-> day = rBCDDATE; p_date-> week_day = rBCDDAY; // read Date and Time p_date-> hour = rBCDHOUR; p_date-> minute = rBCDMIN; p _ Date-> second = rBCDSEC; rRTCCON = 0x00; // read/write prohibition of RTC, BCD clock, counter, no reset }/******************************* * ** TICK interrupt initialization ********************************** /void RTC_tickIRQ_Init (U8 tick) {ClearPending (BIT_TICK); // clear the flag EnableIrq (BIT_TICK); // enable the source pISR_TICK = (unsigned) RTC_tickHandler to be interrupted; // interrupt function entry address: rRTCCON = 0x00; rTICNT = (tick & 0x7f) | 0x80; // enable interruption }/********************************* * set the alert date, time, and wake-up mode ** * *****************************/Void RTC_alarm_setdate (date * p_date, u8 mode) {rRTCCON = 0x01; rALMYEAR = p_date-> year; rALMMON = p_date-> month; rALMDATE = p_date-> day; rALMHOUR = p_date-> hour; rALMMIN = p_date-> minute; rALMSEC = p_date-> second; rRTCALM = mode; // RTC alarm control register rRTCCON = 0x00; ClearPending (BIT_RTC ); // clear the flag EnableIrq (BIT_RTC); // open RTC alarm INTERRUPT pISR_RTC = (unsigned) RTC_alar MHandler;} void Main (void) {SelectFclk (2); // set the system clock to 400 M ChangeClockDivider (2, 1); // set the frequency division to CalcBusClk (); // calculate bus frequency rGPHCON & = ~ (3 <4) | (3 <6); rGPHCON | = (2 <4) | (2 <6); // GPH2--TXD [0]; GPH3--RXD [0] rGPHUP = 0x00; // enables the pull-up function Uart_Init (); Uart_Select (0); rGPBCON & = ~ (3 <10) | (3 <12) | (3 <14) | (3 <16 )); // clear rGPBCON for GPBCON [] | = (1 <10) | (1 <12) | (1 <14) | (1 <16); // set GPB5 ~ 8: Output rGPBUP & = ~ (1 <5) | (1 <6) | (1 <7) | (1 <8); // set GPB5 ~ 8. rGPBDAT | = (1 <5) | (1 <6) | (1 <7) | (1 <8 ); // turn off the LED Beep_Stop (); // The Buzzer stops sound. The buzzer is used as the alarm clock sound C_date.year = 0x12; C_date.month = 0x05; C_date.day = 0x09; c_date.week_day = 0x03; // set the current date and time C_date.hour = 0x12; C_date.minute = 0x00; C_date.second = 0x10; RTC_setdate (& C_date ); c_date.second = 0x20; RTC_alarm_setdate (& C_date, 0x41); // 0x41 indicates enabling RTC and RTC_tickIRQ_Init (127 ); // set tick to Uart_Printf ("\ n -- -Real-time clock test program --- \ n "); while (Uart_GetKey ()! = ESC_KEY) {LED1_OFF; RTC_getdate (& C_date); if (alarmflag) {alarmflag = 0; Uart_Printf ("\ nRTC ALARM % 02x: % 02x: % 02x \ n ", c_date.hour, C_date.minute, C_date.second); Beep_Freq_Set (1000); delay (5); Beep_Stop ();}}} /*********************************** TICK interruption *** * ****************************/void _ irq RTC_tickHandler (void) {ClearPending (BIT_TICK); ledpendon; // refresh LED1 Delay (500); RTC_getdate (& C_date); Uart_Printf ("rtc time: % 04x-% 02x-% 02x % s % 02x: % 02x: % 02x \ n ", C_date.year, C_date.month, C_date.day, week_num [C_date.week_day], C_date.hour, C_date.minute, c_date.second );} /*********************************** TICK interruption *** * ****************************/void _ irq RTC_alarmHandler (void) {alarmflag = 1; ClearPending (BIT_RTC );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.