⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smartclock.c

📁 uCos-ii 2.86 在C8051F410单片机上移植成功!!! 其中包括:UART驱动
💻 C
字号:

#include "..\header files\includes.H"
#include "smaRTClock.h"	 

//-----------------------------------------------------------------------------
// Global Constants and Variables
//-----------------------------------------------------------------------------

#define SYSCLK         24500000        // SYSCLK frequency in Hz

#define BAUDRATE         115200        // Baud rate of UART in bps

#define RTCCLK            32768        // SmaRTClock frequency in Hz

#define RTC_DAY      2831155200        // Number of RTC counts in a day
#define RTC_HOUR      117964800        // Number of RTC counts in an hour
#define RTC_MINUTE      1966080        // Number of RTC counts in a minute
#define RTC_SECOND        32768        // Number of RTC counts in a second
#define RTC_HUNDRETH        328        // Number of RTC counts in a hundreth
                                       // of a second (rounded up)

#define SUSPEND            0x40        // Value to write to PMU0CF to place
                                       // the device in Suspend mode

#define SLEEP              0x80        // Value to write to PMU0CF to place
                                       // the device in Sleep Mode

#define POWER_MODE      SUSPEND        // Select between Suspend and Sleep	mode.	                                    

#define SWITCH_PRESSED        0        // Macros to determine switch state
#define SWITCH_NOT_PRESSED    1

#define FIVE_MS    5*(SYSCLK/12/1000)  // 5 ms delay constant
#define US_DELAY   (SYSCLK/12/1000)/10 // 500 us delay constant

// Used in debounce section of code
#define DEBOUNCE_INTERVAL  10          // Debounce interval in milliseconds
#define T3_DEBOUNCE_TICKS  DEBOUNCE_INTERVAL*(SYSCLK/12/1000)


 																								

//------------------------------------------------------------------------------- 



DATE_INFO event1;   // This will keep the runnnig date information                                       



void Init_DateINFO(void)
{
	 // Initialize DATE_INFO struct
   event1.seconds = SECOND_INIT;
   event1.minutes = MINUTE_INIT;
   event1.hours   = HOUR_INIT;
   event1.days    = DAY_INIT;
   event1.years   = (2000+YEAR_INIT);

}

//-----------------------------------------------------------------------------
// Leap_Year
//-----------------------------------------------------------------------------	
// Return Value : '1' if a leap year, '0' if not a leap year
// Parameters   : U16 year - the current year in the counter	
//-----------------------------------------------------------------------------

U8 Leap_Year (U16 year)
{
   if ((((year%4)==0) && ((year%100)!=0)) || ((year%400)==0))
   {
      return 1;
   }
   else
   {
      return 0;
   }
}

//-----------------------------------------------------------------------------
// Compute_Month
//-----------------------------------------------------------------------------
// Return Value : none
// Parameters   : 1) U16* month - pointer to month number variable (1-12)
//                2) U16* days - pointer to number of days  variable (1-31)
//                3) U16 total - total number of days to compute
//                   month and days from
//                4) U16 total - current year (in 20xx format)	
//-----------------------------------------------------------------------------

void Compute_Month (U16* month, U16* days, U16 total, U16 year)
{
   if (total < 32)                     // First month does not depend on
   {                                   // leap year
      (*month) = 1;                    // January
      (*days) = total;
   }
   else if (Leap_Year(year))
   {
      // Calculate month based on leap year
      if (total < 61)
      {
         (*month) = 2;                 // February
         (*days) = total-31;
      }
      else if (total < 92)
      {
         (*month) = 3;                 // March
         (*days) = total-60;
      }
      else if (total < 122)
      {
         (*month) = 4;                 // April
         (*days) = total - 91;
      }
      else if (total < 153)
      {
         (*month) = 5;                 // May
         (*days) = total - 121;
      }
      else if (total < 183)
      {
         (*month) = 6;                 // June
         (*days) = total-152;
      }
      else if (total < 214)
      {
         (*month) = 7;                 // July
         (*days) = total - 182;
      }
      else if (total < 245)
      {
         (*month) = 8;                 // August
         (*days) = total - 213;
      }
      else if (total < 275)
      {
         (*month) = 9;                 // September
         (*days) = total - 244;
      }
      else if (total < 306)
      {
         (*month) = 10;                // October
         (*days) = total-274;
      }
      else if (total < 336)
      {
         (*month) = 11;                // November
         (*days) = total - 305;
      }
      else
      {
         (*month) = 12;                // December
         (*days) = total - 335;
      }

   }
   else
   {
      // Calculate month based on non leap year
      if (total < 60)
      {
         (*month) = 2;                 // February
         (*days) = total-31;
      }
      else if (total < 91)
      {
         (*month) = 3;                 // March
         (*days) = total-59;
      }
      else if (total < 121)
      {
         (*month) = 4;                 // April
         (*days) = total - 90;
      }
      else if (total < 152)
      {
         (*month) = 5;                 // May
         (*days) = total - 120;
      }
      else if (total < 182)
      {
         (*month) = 6;                 // June
         (*days) = total-151;
      }
      else if (total < 213)
      {
         (*month) = 7;                 // July
         (*days) = total - 181;
      }
      else if (total < 244)
      {
         (*month) = 8;                 // August
         (*days) = total - 212;
      }
      else if (total < 274)
      {
         (*month) = 9;                 // September
         (*days) = total - 243;
      }
      else if (total < 305)
      {
         (*month) = 10;                // October
         (*days) = total-273;
      }
      else if (total < 335)
      {
         (*month) = 11;                // November
         (*days) = total - 304;
      }
      else
      {
         (*month) = 12;                // December
         (*days) = total - 334;
      }
   }
}


//-----------------------------------------------------------------------------
// RTC_Read
//-----------------------------------------------------------------------------	

INT8U RTC_Read (INT8U reg)
{
   reg &= 0x0F;                        // Mask low nibble
   RTC0ADR  = reg;                     // Pick register
   RTC0ADR |= 0x80;                    // Set BUSY bit to read
   while ((RTC0ADR & 0x80) == 0x80);   // Poll on the BUSY bit
   return RTC0DAT;                     // Return value
}

//-----------------------------------------------------------------------------
// RTC_Write
//-----------------------------------------------------------------------------


void RTC_Write (INT8U reg, INT8U value)
{
   reg &= 0x0F;                        // Mask low nibble
   RTC0ADR  = reg;                     // Pick register
   RTC0DAT = value;                    // Write data
   while ((RTC0ADR & 0x80) == 0x80);   // Poll on the BUSY bit
}
 
//-----------------------------------------------------------------------------
// RTC_Init
//-----------------------------------------------------------------------------

void RTC_Init (void)
{
   INT16U i;                              // Counter used in for loop

   UU32 alarm_interval_first;          // Stores first alarm value

   INT8U CLKSEL_SAVE = CLKSEL;


   RTC0KEY = 0xA5;                     // Unlock the smaRTClock interface
   RTC0KEY = 0xF1;	
	 
   RTC_Write (RTC0XCN, 0x60);          // Configure the smaRTClock
                                       // oscillator for crystal mode
                                       // Bias Doubling Enabled
                                       // AGC Disabled
																			 //0: smaRTClock is powered from VDD.

   RTC_Write (RTC0CN, 0x80);           // Enable smaRTClock oscillator


   //----------------------------------
   // Wait for smaRTClock to start
   //----------------------------------	  

	 for(i=0;i<1000;i++);		
   
   while ((RTC_Read (RTC0XCN) & 0x10)== 0x00);	// Wait for smaRTClock valid

   
   //----------------------------------
   // smaRTClock has been started
   //----------------------------------

   RTC_Write (RTC0XCN, 0xC0);          // Enable Automatic Gain Control
                                       // and disable bias doubling

   RTC_Write (RTC0CN, 0xC0);           // Enable missing smaRTClock detector
                                       // and leave smaRTClock oscillator
                                       // enabled. 


   //----------------------------------
   // Set the smaRTClock Alarm
   //----------------------------------

    // Calculate the alarm interval in smaRTClock ticks
   alarm_interval_first.U32 = (RTC_DAY-TIME_INIT);

   // Copy the alarm interval to the alarm registers
   RTC_Write (ALARM0, alarm_interval_first.U8[b0]);   // LSB
   RTC_Write (ALARM1, alarm_interval_first.U8[b1]);
   RTC_Write (ALARM2, alarm_interval_first.U8[b2]);
   RTC_Write (ALARM3, alarm_interval_first.U8[b3]);   // MSB

   // Enable the smaRTClock timer and alarm with auto-reset
   RTC_Write (RTC0CN, 0xDC);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -