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

📄 rtc12887_main.c

📁 Realtime clock DS12c887
💻 C
字号:


/* 28 May 2008              ****** Real Time Clock using the Maxim DS12C887 Chip   ******
                                    ( Clk : 0.5us using 2 MHz on board crystal)
								       ( PCB used is the 80C51F020 DK )
  
Version History :

Got the base code without RTC interface to complile. 

08 June 2008

The RTC byte wide write and read routines are checked OK.
The Clcok set routines also checked to function OK.

CODE COMPLETE.

*/   

#include <stdio.h>
#include <c8051F020.h>
#include <DS_12887.h>

/*================ Bit Defines =================*/
                    
// Digital Inputs:                               // P2.1, 2.2, 2.3 used for LCD control    

sbit EditMode	 = P2^0;                         // General purpose. Not used in this code.                                            
sbit NextPB      = P2^4;                         // General purpose. Not used in this code.
sbit Min_Adj     = P2^5;                         // Used to adjust Minutes in clock set mode
sbit Hour_Adj    = P2^6;                         // Used to adjust Hour in clock set mode
sbit Set_Clk     = P2^7;                         // Save the entered values to RTC

// Digital Outputs: 

sbit Pulse        = P1^6;                        // To know that Main is live..
bit  flg_ScanMain = 0;
bit  OneSecTik    = 0;
extern bit  F_Clk_adj;
		
/*================ Globals ====================*/

unsigned int ScanTick     = 0;
unsigned int ScnTikCnt    = 100;                // Enter required ms value for each scan of Main loop
unsigned int msTick       = 0;
unsigned char OneSecCnt   = 0;
unsigned int hour, minute, second ;

extern unsigned char  string[32];                // LCD display buffer
              
/*=========== Function Prototypes ============= */

void Init_Device (void);
void Timer_Init(void);
void Port_IO_Init (void);
void Interrupts_Init(void);
void init_lcd (void);
void display(char *);
void msDelay(unsigned long);

extern unsigned int BCDtoBin(unsigned char);
extern unsigned int BinToBCD(unsigned char);
extern void RTC_Byte_Wr(unsigned char, unsigned char);
extern unsigned char RTC_Byte_Rd (unsigned char);
extern void RTC_Init (void);
extern void ReadTime();
extern void set_CurrentTime();

/*=============================================*/


void main (void)

{
       
  //  Using internal oscillator at 2MHz and as it is  the default, no code for it..

  //  OSCICN    = 0x05;                          // for 4 MHz selection

  WDTCN       = 0xde;	                         // Switch off WDT
  WDTCN       = 0xad;
  
  // Initialize hardware...
  ScanTick = ScnTikCnt;                          // Set at 100ms loop rate  
  Init_Device();
  init_lcd();
  
  Pulse     = 1;
  OneSecCnt = 0;
  OneSecTik = 0;
     
  display (" Real Time Clock Ver:1.0 / 2008 ");
  msDelay(200);                                 // Display this 2 secs 

  RTC_Init();

   
   while (1)                                          // Start 100ms scan loop
         {
          if (flg_ScanMain)
            {
              flg_ScanMain = 0;                       // Reset the looping flag which is set in interrpt.
              Pulse = ~Pulse;                         // Heart beat pulse in run mode..
			  if (!F_Clk_adj & OneSecTik)
			    {
			     OneSecTik = 0;                       // Update Clock display once every sec
				 ReadTime(); 
                 sprintf( string, "  %02u %02u %02u Hrs", hour ,minute,second );                  
                 display(string);
                }

               set_CurrentTime();

	   	    } // Flg_ScanMain closing brace.

           }// while(1) closing brace 
}// main() closing brace

/*================ Interrupts =================*/

void Timer0_ISR(void) interrupt 1 using 0        // Basic 1ms interrupt for the program
{
    TR0 = 0;
	TL0       = (65536UL-2000UL);                // Load  TMR0 registers (2000 x 0.5us = 1ms)
    TH0       = (65536UL-2000UL) >> 8;
	TR0 = 1;

    ScanTick--;
  
    if (!ScanTick )                              // Do this once every ScnTikCnt times
    {
	 ScanTick = ScnTikCnt ;                            
     flg_ScanMain = 1; 
	 
	 OneSecCnt++;                               // Valid only if ScnTikCnt = 100
	 if ( OneSecCnt == 10)                      // DO this once every second
	   {
	     OneSecCnt = 0;
		 OneSecTik = 1;
	   } 	   
    }
}

/*==============================================*/

void Timer2_ISR (void) interrupt 5 using 2     // Set to interrupt at 1ms interval ONLY 
                                               //   when msDelay function is called
{
     TF2 =0 ;
	 ++msTick ;
}

/*=============================================*/

void msDelay(unsigned long msec)
{
  TR2 = 1;                                       // Start Timer 2
  while ( msTick <= msec);                       // Wait for msec milliseconds
  msTick = 0;		                             // Reset the tick counter
  TR2 = 0 ;                                      // Stop Timer 2
}

/*============================================*/

void Timer_Init()                                   
{
    
    CKCON     = 0x08;                            // TMR0 use clk 0f 0.5micro directly.                               
	TCON      = 0x10;                            // TMR0 enabled.
    TMOD      = 0x01;                            // TMR0  in 16 bit timer mode.
    TL0       = (65536UL-2000UL);                // First time load of TMR0 registers 
    TH0       = (65536UL-2000UL) >> 8;
	T2CON     = 0x00;                            // TMR2 in 16 bit auotreload ( default)
	RCAP2L    = (65536UL-2000UL);				 // Interrupt in 2000 clock cycles or 1ms 
    RCAP2H    = (65536UL-2000UL) >> 8;
    TL2       = RCAP2L;
    TH2       = RCAP2H;
    
}
/*============================================*/

void Port_IO_Init()
{
    // P0.0  -  Unassigned,  Push-Pull,  Digital
    // P0.1  -  Unassigned,  Push-Pull,  Digital
    // P0.2  -  Unassigned,  Push-Pull,  Digital
    // P0.3  -  Unassigned,  Push-Pull,  Digital
    // P0.4  -  Unassigned,  Push-Pull,  Digital
    // P0.5  -  Unassigned,  Push-Pull,  Digital
    // P0.6  -  Unassigned,  Push-Pull,  Digital
    // P0.7  -  Unassigned,  Push-Pull,  Digital

    // P1.0  -  Unassigned,  Push-Pull,  Digital
    // P1.1  -  Unassigned,  Push-Pull,  Digital
    // P1.2  -  Unassigned,  Push-Pull,  Digital
    // P1.3  -  Unassigned,  Push-Pull,  Digital
    // P1.4  -  Unassigned,  Push-Pull,  Digital
    // P1.5  -  Unassigned,  Push-Pull,  Digital
    // P1.6  -  Unassigned,  Push-Pull,  Digital
    // P1.7  -  Unassigned,  Push-Pull,  Digital

    // P2.0  -  Unassigned,  Open-Drain, Digital
    // P2.1  -  Unassigned,  Push-Pull,  Digital
    // P2.2  -  Unassigned,  Push-Pull,  Digital
    // P2.3  -  Unassigned,  Push-Pull,  Digital
    // P2.4  -  Unassigned,  Open-Drain, Digital
    // P2.5  -  Unassigned,  Open-Drain, Digital
    // P2.6  -  Unassigned,  Open-Drain, Digital
    // P2.7  -  Unassigned,  Open-Drain, Digital

    // P3.0  -  Unassigned,  Push-Pull,  Digital
    // P3.1  -  Unassigned,  Push-Pull,  Digital
    // P3.2  -  Unassigned,  Push-Pull,  Digital
    // P3.3  -  Unassigned,  Push-Pull,  Digital
    // P3.4  -  Unassigned,  Push-Pull,  Digital
    // P3.5  -  Unassigned,  Push-Pull,  Digital
    // P3.6  -  Unassigned,  Push-Pull,  Digital
    // P3.7  -  Unassigned,  Push-Pull,  Digital

    P0MDOUT   = 0xFF;
    P1MDOUT   = 0xFF;
    P2MDOUT   = 0x0E;
    P3MDOUT   = 0xFF;
    XBR2      = 0x40;
}

/*============================================*/

void Interrupts_Init()
{
    IE        = 0xAA;							 // Enable all Interrupts + Timer0 + Timer1 + Timer2
	EIE2      = 0x05;                            // Enable Timer3 and Timer4 interrupts
}
/*============================================*/

// Initialization function for device,


void Init_Device(void)
{
    Timer_Init();
    Port_IO_Init();
    Interrupts_Init();
}


//===================E N D====================

⌨️ 快捷键说明

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