f41x_rtc_suspend.c
来自「芯科原厂所有c8051fxx程序的例子。」· C语言 代码 · 共 468 行 · 第 1/2 页
C
468 行
//-----------------------------------------------------------------------------
// F41x_RTC_Suspend.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// The program demonstrates the SmaRTClock oscillator and power
// savings mode operation on the F410. The MCU will sleep in low power suspend
// mode with only the RTC running. When the MCU wakes up it will switch
// to a 24.5 MHz internal oscillator & turn on an LED. It will then switch
// to the 32kHz RTC clock and wait 10 ms. The 10 ms delay also uses T0
// and the RTC clock oscillator but does not use idle mode or interrupts.
//
// How To Test:
//
// 1) Download code to a 'F410 target board.
// 2) Make sure J5 has a jumper connecting P2.3 to D5.
// 3) Run the code.
// D5 should blink once a secoond.
//
// Target: C8051F41x
// Tool chain: Keil C51 7.05 / Keil EVAL C51
// Command Line: None
//
//
// Release 1.0
// -Initial Revision (KAB)
// -07 AUG 2006
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051F410.h>
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define RTC0CN 0x06 // RTC address of RTC0CN register
#define RTC0XCN 0x07 // RTC address of RTC0XCN register
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void main (void);
void OSC_Init (void);
void PORT_Init (void);
void RTC_Init (void);
unsigned char RTC_Read (unsigned char);
void RTC_Write (unsigned char, unsigned char);
void RTC_SetBits (unsigned char, unsigned char);
void RTC_ClearBits (unsigned char, unsigned char);
void RTC_ClearCapture (void);
void RTC_ClearAlarm (void);
void RTC_BumpAlarm (unsigned char code *p);
void configSleep (void);
void configWake (void);
void delay (unsigned int);
void RTC_ISR (void); // interrupt key word not permitted.
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
sbit LED = P2^3; // port pin used for LED
// RTC_Interval - little endian data added to alarm register = 1 second
const unsigned char code RTC_Interval[6]= {0x00,0x00,0x01,0x00,0x00,0x00};
//-----------------------------------------------------------------------------
// main () Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // disable watchdog timer
OSC_Init (); // initialize 24.5 MHz oscillator
PORT_Init (); // initialize IO ports
RTC_Init (); // initiralize RTC
EA = 1; // set global interrupt enable
while (1)
{
configSleep (); // configure MCU for low power sleep
OSCICN |= 0x20; // set suspend mode bit
configWake (); // configure MCU for wake mode
LED = 1; // turn on LED
delay (164); // wait 50 ms
LED = 0; // turn off LED
}
}
//-----------------------------------------------------------------------------
// PORT_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
// P1.3 push-pull output LED
//-----------------------------------------------------------------------------
void Port_Init ()
{
// configure Port IO here
P2MDOUT = 0x08; // enable P2.3 output
XBR1 |= 0x40; // enable crossbar
}
//-----------------------------------------------------------------------------
// OSC_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the internal oscilloator.
// Note that the OSCICN setting for 24.5MHz is different than F12x and F3xx
//
//-----------------------------------------------------------------------------
void OSC_Init ()
{
OSCICN = 0x87; // enable 24.5 MHz int osc
}
//-----------------------------------------------------------------------------
// RTC_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function will initialize the RTC. First it unlocks the RTC interface,
// enables the RTC, clears ALRMn and CAPTUREn bits. Then it sets up the RTC
// to operate using a 32.768khZ crystal. Lastly it enables the alarm and
// interrupt. This function uses the RTC primitive functions to access
// the internal RTC registers.
//
//-----------------------------------------------------------------------------
void RTC_Init (void)
{
unsigned char i;
RTC0KEY = 0xA5; // unlock the RTC interface
RTC0KEY = 0xF1;
RTC_Write (RTC0CN, 0x80); // enable RTC
RTC_ClearCapture (); // clear CAPTUREn registers
RTC_ClearAlarm (); // clear ALARMn registers
RTC_SetBits (RTC0XCN, 0x40); // set crystal mode
OSCICN = 0x80; // switch to 192 kHz int osc
for (i=0xFF;i!=0;i--); // wait > 1 ms
OSCICN = 0x87; // switch back to 24.5 MHz int osc
// wait for clock valid
while ((RTC_Read (RTC0XCN) & 0x10)== 0x00);
RTC_SetBits (RTC0CN, 0x40); // enable missing clock detector
RTC_SetBits (RTC0XCN, 0x80); // enable ACG to save current
RTC_SetBits (RTC0CN, 0x02); // transfer capture to clock
RTC_SetBits (RTC0CN, 0x10); // enable RTC run
RTC_SetBits (RTC0CN, 0x08); // enable RTC alarm
EIE1 |= 0x02; // enable RTC interrupt
}
//-----------------------------------------------------------------------------
// configSleep ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the MCU for low power sleep disabling the crossbar,
// enabling weak pull-ups, and turning off MCU peripherals. The lowest possible
// sleep current can be obtained by turning off the Voltage monitor.
//
//-----------------------------------------------------------------------------
void configSleep (void)
{
XBR1 &= ~0xC0; // disable crossbar, enabled weak pull ups
// disable all peripherals for low power sleep
SCON0 &= ~0x10; // disable UART0
TCON &= ~0x10; // disable T0
TCON &= ~0x40; // disable T1
TMR2CN &= ~0x04; // disable T2
// disable any additional peripherals here
VDM0CN &= ~0x80; // disable voltage monitor
}
//-----------------------------------------------------------------------------
// configWake
//
// Return Value : None
// Parameters : None
//
// This function will configure the MCU for wake mode operation.
// Enable additional peripherals as needed.
//-----------------------------------------------------------------------------
void configWake (void)
{
VDM0CN |= 0x80; // enable voltage monitor
TCON |= 0x10; // enable T0
// enable any additional peripherals here
XBR1 |= 0x40; // enable crossbar
}
//-----------------------------------------------------------------------------
// delay ()
//
// Return Value : None
// Parameters : None
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?