f360_blinky.c

来自「8051试验程序 基础教材」· C语言 代码 · 共 155 行

C
155
字号
//-----------------------------------------------------------------------------
// F360_Blinky.c
//-----------------------------------------------------------------------------
// Copyright (C) 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the green LED on the C8051F36x target board about
// five times a second using the interrupt handler for Timer2.
//
//
// How To Test:
//
// 1) Download code to a 'F36x target board
// 2) Ensure that pins 5 and 6 are shorted together on the J12 header
// 3) Run the program.  If the LED flashes, the program is working
//
//
// FID:            36X000045
// Target:         C8051F36x/F37x
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (TP)
//    -12 DEC 2006
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <ioc8051F360.h>                 // SFR declarations

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F36x
//-----------------------------------------------------------------------------

__sfr __no_init volatile unsigned short TMR2RL   @ 0xca;                 // Timer2 reload value
__sfr __no_init volatile unsigned short TMR2     @ 0xcc;                 // Timer2 counter

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK 3062500                 // Approximate SYSCLK frequency in Hz

#define LED P3_bit.P3_2                // Green LED: '1' = ON; '0' = OFF

#define CONFIG_PAGE       0x0F         // SYSTEM AND PORT CONFIGURATION PAGE

#define LEGACY_PAGE       0x00         // LEGACY SFR PAGE

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void PORT_Init (void);
void Timer2_Init (int counts);
__interrupt void Timer2_ISR (void);

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

   Timer2_Init (SYSCLK / 12 / 10);     // Init Timer2 to generate interrupts
                                       // at a 10 Hz rate.

   IE_bit.EA = 1;                             // Enable global interrupts

   SFRPAGE = LEGACY_PAGE;              // Page to sit in for now

   while (1) {}                         // Spin forever
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
   unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

   SFRPAGE = CONFIG_PAGE;              // Switch to the necessary SFRPAGE

   XBR1    = 0x40;                     // Enable crossbar and weak pull-ups
   P3MDOUT |= 0x04;                    // enable P3.2 (LED) as push-pull output

   SFRPAGE = SFRPAGE_save;             // Restore the SFRPAGE
}

//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1)  int counts - calculated Timer overflow rate
//                    range is postive range of integer: 0 to 32767
//
// Configure Timer2 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
//
void Timer2_Init (int counts)
{
   unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

   SFRPAGE = CONFIG_PAGE;              // Switch to the necessary SFRPAGE

   TMR2CN = 0x00;                      // Stop Timer2; Clear TF2;
                                       // Use SYSCLK/12 as timebase
   TMR2RL = -counts;                   // Init reload values
   TMR2 = 0xFFFF;                      // Set to reload immediately

   IE_bit.ET2     = 1;                        // Enable Timer2 interrupts

   TMR2CN_bit.TR2     = 1;                        // Start Timer2

   SFRPAGE = SFRPAGE_save;             // Restore the SFRPAGE
}

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

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer2 overflows.
//
// NOTE: The SFRPAGE register will automatically be switched to the Timer 2
// Page when an interrupt occurs.  SFRPAGE will return to its previous setting
// on exit from this routine.
//
#pragma vector=0x2B
__interrupt void Timer2_ISR (void)
{
   TMR2CN_bit.TF2H = 0;                           // Clear Timer2 interrupt flag
   LED = ~LED;                       // Change state of LED
}

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

⌨️ 快捷键说明

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