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

📄 f93x_sleepmode_portmatchwake.c

📁 芯科原厂所有c8051fxx程序的例子。
💻 C
字号:
//-----------------------------------------------------------------------------
// F93x_SleepMode_PortMatchWake.c
//-----------------------------------------------------------------------------
// Copyright (C) 2007 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the red LED on the C8051F930 target board using the 
// interrupt handler for Timer2 when the MCU is awake.
//
// Pressing the switch on P0.3 will place the device in a low power mode.
// Pressing the switch on P0.2 will wake up the device using Port Match.
//
// 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. Press P0.3 to place the device in sleep mode. 
//    Press P0.2 to wake it back up. 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)
//    -8 OCT 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 LED_ON           0
#define LED_OFF          1

#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      SLEEP          // 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.
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void Timer2_Init (int counts);

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
   U8 wakeup_event_pending;
   U8 wakeup_source;
   
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                       // enable)

   PORT_Init ();                       // Initialize Port I/O
   OSCILLATOR_Init ();                 // Initialize Oscillator
   
   Timer2_Init (SYSCLK / 12 / 32);     // Init Timer2 to generate interrupts
                                       // at a 32 Hz rate.

   // Setup the Port Match Wake-up Source to wake up on the falling
   // edge of P0.2 (when the switch is pressed)
   P0MASK = 0x04;                      // Mask out all pins except for P0.2
   P0MAT = 0x04;                       // P0.2 should be HIGH while the device
                                       // is in the low power mode

   EA = 1;                             // Enable global interrupts
   
   //----------------------------------
   // Main Application Loop
   //----------------------------------
   while (1)
   {
      if(!SW3)                         // If the P0.3 switch is pressed
      {
         

         // Configure the Port I/O for Low Power Mode 
         RED_LED = LED_OFF;            // Turn off the LED or other
                                       // high-current devices         
                  
         // Place the device in the Low Power Mode
         PMU0CF = 0x20;                // Clear all wake-up flags
         wakeup_event_pending = 0;     // Clear software event pending flag         

         if(!SW2)                      // Check each wake-up event to ensure
         {                             // it has not occured while the 
            wakeup_event_pending = 1;  // wake-up flags were being cleared
         }                             // In this example, we are only using
                                       // a single wake-up source
         
         if(!wakeup_event_pending)
         {
            YELLOW_LED = LED_ON;       // Turn on Yellow LED to indicate that
                                       // the device is entering the low power
                                       // state
            
            // Enable Port Match wake-up source and place the device in 
            // the low power state
            PMU0CF = (POWER_MODE | 0x02);
         }

         //--------------------------------------------------------------------
         // Device Sleeping until P0.2 is pressed
         //--------------------------------------------------------------------
         
         // Turn off the Yellow LED to indicate that we are in the 
         // high power mode
         YELLOW_LED = LED_OFF;          

         RED_LED = LED_ON;            // Turn on the LED or other
                                       // high-current devices   
         // 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 & 0x02)
         {
            // We have been awaken by a port match
         }
                     

      }
   }                        
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use the internal low power
// oscillator.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
   
   
         
   RSTSRC = 0x06;                      // Enable missing clock detector and
                                       // leave VDD Monitor enabled.

   CLKSEL = 0x04;                      // Select low power internal osc. 
                                       // divided by 1 as the system clock
  
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and ports pins.
//
// P0.2   digital   open-drain    Switch 2
// P0.3   digital   open-drain    Switch 3
// P1.5   digital   push-pull     Red LED
// P1.6   digital   push-pull     Yellow LED
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   P0MDIN |= 0x0C;                     // P0.2, P0.3 are digital
   P1MDIN |= 0x60;                     // P1.5, P1.6 are digital

   P0MDOUT &= ~0x0C;                   // P0.2, P0.3 are open-drain
   P1MDOUT |= 0x60;                    // P1.5, P1.6 are push-pull

   P0     |= 0x0C;                     // Set P0.2, P0.3 latches to '1'

   XBR2    = 0x40;                     // Enable crossbar and enable
                                       // weak pull-ups
}

//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at
// interval specified by <counts> using SYSCLK/48 as its time base.
//
void Timer2_Init (int counts)
{
   TMR2CN  = 0x00;                        // Stop Timer2; Clear TF2;
                                          // use SYSCLK/12 as timebase
   CKCON  &= ~0x60;                       // Timer2 clocked based on T2XCLK;

   TMR2RL  = -counts;                     // Init reload values
   TMR2    = TMR2RL;                      // initalize timer to reload value
   ET2     = 1;                           // enable Timer2 interrupts
   TR2     = 1;                           // start Timer2
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer2 overflows.
//
INTERRUPT(Timer2_ISR, INTERRUPT_TIMER2)
{
   TF2H = 0;                              // clear Timer2 interrupt flag
   RED_LED = !RED_LED;                    // change state of LED

}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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