📄 f93x_sleepmode_smartclockwake.c
字号:
//-----------------------------------------------------------------------------
// F93x_SleepMode_smaRTClockWake.c
//-----------------------------------------------------------------------------
// Copyright (C) 2007 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the red LED on the C8051F930 target board about
// five times a second using the interrupt handler for Timer2 when the
// MCU is awake.
//
// The smaRTClock is configured to generate a wake-up event every 2 seconds.
// The device will toggle between Sleep/Suspend mode and Normal mode. In
// the low power state, the Yellow LED will be turned on.
//
// If a smaRTClock oscillator failure occurs, both LEDs will remain on
// and the device will be in a high power state.
//
// How To Test:
//
// 1) Download code to the 'F93x target board
// 2) Ensure that pins 1-2, 3-4, 5-6, 7-8 are shorted together on the
// J8 header
// 3) Run the program. The Red LED will blink when the device is awake.
//
//
// Target: C8051F93x-C8051F92x
// Tool chain: Generic
// Command Line: None
//
// Release 1.0
// -Initial Revision (FB)
// -15 JAN 2007
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <compiler_defs.h> // compiler declarations
#include <C8051F930_defs.h> // SFR declarations
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------
SBIT (RED_LED, SFR_P1, 5); // '0' means ON, '1' means OFF
SBIT (YELLOW_LED, SFR_P1, 6); // '0' means ON, '1' means OFF
SBIT (SW2, SFR_P0, 2); // SW2 == 0 means switch pressed
SBIT (SW3, SFR_P0, 3); // SW3 == 0 means switch pressed
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 20000000 // SYSCLK frequency in Hz
#define RTCCLK 32768 // smaRTClock frequency in Hz
#define WAKE_INTERVAL 2000 // Wakeup-interval in milliseconds
#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. When debugging, if the
// MCU is stopped/halted while
// in Sleep Mode, the connection
// to the IDE will be lost. The
// IDE connection will not be lost
// if the MCU is stopped/halted
// while in suspend mode.
#define LED_ON 0
#define LED_OFF 1
#define CAPTURE0 0x00 // RTC address of CAPTURE0 register
#define CAPTURE1 0x01 // RTC address of CAPTURE1 register
#define CAPTURE2 0x02 // RTC address of CAPTURE2 register
#define CAPTURE3 0x03 // RTC address of CAPTURE3 register
#define RTC0CN 0x04 // RTC address of RTC0CN register
#define RTC0XCN 0x05 // RTC address of RTC0XCN register
#define RTC0XCF 0x06 // RTC address of RTC0XCF register
#define RTC0PIN 0x07 // RTC address of RTC0PIN register
#define ALARM0 0x08 // RTC address of ALARM0 register
#define ALARM1 0x09 // RTC address of ALARM1 register
#define ALARM2 0x0A // RTC address of ALARM2 register
#define ALARM3 0x0B // RTC address of ALARM3 register
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void Timer2_Init (int counts);
void RTC_Init (void);
unsigned char RTC_Read (unsigned char);
void RTC_Write (unsigned char, unsigned char);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
U8 wakeup_source;
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init (); // Initialize Port I/O
OSCILLATOR_Init (); // Initialize Oscillator
RTC_Init (); // Initialize RTC
Timer2_Init (SYSCLK / 12 / 10); // Init Timer2 to generate interrupts
// at a 10 Hz rate.
EA = 1; // Enable global interrupts
//----------------------------------
// Main Application Loop
//----------------------------------
while (1)
{
if(PMU0CF & 0x0C) // Check if a smaRTClock Alarm or
{ // clock fail event has occured
// Check for clock failure while the device is awake
if(PMU0CF & 0x08)
{
EA = 0;
YELLOW_LED = LED_ON;
RED_LED = LED_ON;
while(1);
}
// Configure the Port I/O for Sleep Mode
RED_LED = LED_OFF; // Turn off the LED or other
// high-current devices
// Place the device in Sleep Mode
PMU0CF = 0x20; // Clear all wake-up flags
// Typically, a check on each non-persistent wake-up event source
// should be performed to ensure no transient event has occurred
// while the wake-up flags were being cleared. In this example,
// we have 2 wake-up sources:
// A. smaRTClock Alarm - no need to check since they are spaced
// several seconds apart.
// B. smaRTClock Oscillator Fail - no need to check since this
// condition is persistent
// Turn on Yellow LED to indicate that we are in
// the low power mode
YELLOW_LED = LED_ON;
// Enable smaRTClock alarm and clock fail as wake-up sources
// and place the device in a low power mode
PMU0CF = (POWER_MODE | 0x0C);
//--------------------------------------------------------------------
// Device Sleeping until the next smaRTClock alarm
//--------------------------------------------------------------------
// Turn off the Yellow LED to indicate that we are in the
// high power mode
YELLOW_LED = LED_OFF;
// Read the wake-up source flags
wakeup_source = PMU0CF & 0x1F;
// Clear the wake-up source flags
PMU0CF = 0x20;
//---------------------------
// Decode the wakeup source
//---------------------------
if(wakeup_source & 0x10)
{
// We have been awaken by the reset pin (most likely C2 traffic)
EA = 0;
YELLOW_LED = LED_ON;
RED_LED = LED_ON;
while(1);
}
if(wakeup_source & 0x08)
{
// We have been awaken by a smaRTClock oscillator failure
EA = 0;
YELLOW_LED = LED_ON;
RED_LED = LED_ON;
while(1);
}
if(wakeup_source & 0x04)
{
// We have been awaken by a smaRTClock alarm
}
}
}
}
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// RTC_Read ()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -