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

📄 ex4c.c

📁 nRF24E1 sample transmit & receiver
💻 C
字号:
/*= ex4c.c =====================================================================
 *
 * Copyright (C) 2003, 2004 Nordic Semiconductor
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * WARRANTY OF ANY KIND.
 *
 * Author(s): Ole Saether
 *
 * DESCRIPTION:
 *
 *  This program shows how to use the Real Time Clock (RTC) and shows one method
 *  of calibrating the internal TICK. 
 *
 *  The operation is as follows. After initializing the message "Hello World!"
 *  is written to the serial port, then the TICK is calibrated and finally the
 *  clock is stopped for 2min. This sequence is repeated indefinitely.
 *
 *  The calibration is a simple PLL (Phase Locked Loop) type. Two periods of the
 *  internal low frequency oscillactor (LP_OSC) is measured using Timer2 and if
 *  it runs too fast or too slow the divide factor TICK_DV is incremented or
 *  decremented. 
 *
 *  Please note that the calibration procedure described here is not optimal.
 *  One improvement could be to calculate the actual error of LP_OSC and adjust
 *  TICK_DV accordingly instead of merely incrementing or decrementing. Another
 *  improvement could be to measure the time between two RTC interrupts (for
 *  example 20ms) instead of only two periods of LP_OSC.
 *
 * COMPILER:
 *  
 *  This program has been tested with Keil C51 V7.07a.
 *
 * $Revision: 4 $
 *
 *==============================================================================
*/
#include <Nordic\reg24e1.h>


#define TICK   10e-3                    // 10ms (100Hz) tick
#define TRTC   60*2                     // 2min sleep time
#define FXO    16e6                     // Crystal Oscillator (XO) frequency 
const unsigned long NTXOLP=TICK*FXO;    // Number of XO periods between each TICK
                                        // (uses const to avoid FP library not
                                        // available in eval version)

void Init(void)
{
    TH1 = 243;                          // 19200@16MHz (when T1M=1 and SMOD=1)
    CKCON |= 0x10;                      // T1M=1 (/4 timer clock)
    PCON = 0x80;                        // SMOD=1 (double baud rate)
    SCON = 0x52;                        // Serial mode1, enable receiver
    TMOD = 0x20;                        // Timer1 8bit auto reload 
    TR1 = 1;                            // Start timer1
    P0_DIR |= 0x02;                     // P0.1 (RxD) is an input
    P0_ALT |= 0x06;                     // Select alternate functions on pins P0.1 and P0.2
}

void PutChar(char c)
{
    while(!TI)
        ;
    TI = 0;
    SBUF = c;
}

void PutString(const char *s)
{
    while(*s != 0)
        PutChar(*s++);
}

void WriteRTC(unsigned int w)
{
    while(REGX_CTRL & 0x10)             // Wait for the interface to be ready
        ;
    REGX_MSB = w >> 8;
    REGX_LSB = w & 0xff;
    REGX_CTRL = 0x0a;
    while(REGX_CTRL & 0x10)             // Wait for the interface to be ready
        ;
}

unsigned int Timer2Capture(void)
{
    unsigned int cap;

    while(EXF2 == 0)
        ;
    EXF2 = 0;
    cap = RCAP2H;
    cap <<= 8;
    cap |= RCAP2L;
    return cap;
}

void Calibrate(void)
{
    unsigned long cap0, cap1, xotick;

    TL2 = 0x00;
    TH2 = 0x00;
    CKCON |= 0x20;                      // CLK/4
    T2CON = 0x0D;
    cap0 = Timer2Capture();
    cap1 = Timer2Capture();
    xotick = (cap1 - cap0) * 2 * TICK_DV;

    // xotick is now the measured number of XO cycles in one TICK period

    if ((xotick > NTXOLP) && (TICK_DV > 1))
    {
        TICK_DV--;
    }
    else if ((xotick < NTXOLP) && (TICK_DV < 255))
    {
        TICK_DV++;
    }
}

void main(void)
{    
    unsigned char i;

    Init();

    // Run a few times with 5 sec intervals:
    WriteRTC(5/TICK);
    for (i=0;i<5;i++)
    {
        Calibrate();
        CK_CTRL = 0x01;                 // Stop clock
    }

    // Run forever with 2 min intervals:
    WriteRTC(TRTC/TICK);
    while(1)
    {
        PutString("Hello World!\r\n");
        Calibrate();
        CK_CTRL = 0x01;                 // Stop clock
    }
}

⌨️ 快捷键说明

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