f31x_timer2_16bitreloadtimer.lst

来自「C8051F31系列单片机的例子」· LST 代码 · 共 244 行

LST
244
字号
C51 COMPILER V8.08   F31X_TIMER2_16BITRELOADTIMER                                          12/22/2008 11:14:19 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE F31X_TIMER2_16BITRELOADTIMER
OBJECT MODULE PLACED IN F31x_Timer2_16bitReloadTimer.OBJ
COMPILER INVOKED BY: E:\Apps\Keil\C51\BIN\c51.exe F31x_Timer2_16bitReloadTimer.c DB OE

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F31x_Timer2_16bitReloadTimer.c
   3          //-----------------------------------------------------------------------------
   4          // Copyright 2005 Silicon Laboratories, Inc.
   5          // http://www.silabs.com
   6          //
   7          // Program Description:
   8          //
   9          // This program presents an example of use of the Timer2 of the C8051F31x's in
  10          // 16-bit reload counter/timer mode. It uses the 'F31xDK as HW platform.
  11          //
  12          // The timer reload registers are initialized with a certain value so that
  13          // the timer counts from that value up to FFFFh and when it overflows an
  14          // interrupt is generated and the timer is automatically reloaded.
  15          // In this interrupt the ISR toggles the LED.
  16          //
  17          // Pinout:
  18          //
  19          //    P3.3 -> LED
  20          //
  21          //    all other port pins unused
  22          //
  23          // How To Test:
  24          //
  25          // 1) Open the F31x_Timer2_16bitReloadTimer.c file in the Silicon Labs IDE.
  26          // 2) If a change in the interrupt period is required, change the value of
  27          //    LED_TOGGLE_RATE.
  28          // 3) Compile and download the code.
  29          // 4) Verify the LED pins of J3 are populated on the 'F31x TB.
  30          // 5) Run the code.
  31          // 6) Check that the LED is blinking
  32          //
  33          //
  34          // FID:            31X000023
  35          // Target:         C8051F31x
  36          // Tool chain:     KEIL C51 7.20 / KEIL EVAL C51
  37          // Command Line:   None
  38          //
  39          // Release 1.0
  40          //    -Initial Revision (CG)
  41          //    -09 NOV 2005
  42          //
  43          
  44          //-----------------------------------------------------------------------------
  45          // Includes
  46          //-----------------------------------------------------------------------------
  47          
  48          #include "C8051F310.h"                 // SFR declarations
  49          
  50          //-----------------------------------------------------------------------------
  51          // Global Constants
  52          //-----------------------------------------------------------------------------
  53          
  54          #define SYSCLK             24500000/8  // SYSCLK in Hz (24.5 MHz internal
  55                                                 // oscillator / 8)
C51 COMPILER V8.08   F31X_TIMER2_16BITRELOADTIMER                                          12/22/2008 11:14:19 PAGE 2   

  56                                                 // the internal oscillator has a
  57                                                 // tolerance of +/- 2%
  58          
  59          #define TIMER_PRESCALER            1   // Based on Timer2 CKCON and TMR2CN
  60                                                 // settings
  61          
  62          #define LED_TOGGLE_RATE            10 // LED toggle rate in milliseconds
  63                                                 // if LED_TOGGLE_RATE = 1, the LED will
  64                                                 // be on for 1 millisecond and off for
  65                                                 // 1 millisecond
  66          
  67          // There are SYSCLK/TIMER_PRESCALER timer ticks per second, so
  68          // SYSCLK/TIMER_PRESCALER/1000 timer ticks per millisecond.
  69          #define TIMER_TICKS_PER_MS  SYSCLK/TIMER_PRESCALER/(1000)
  70          #define TIMER_TICKS_PER                 TIMER_TICKS_PER_MS/1000
  71          
  72          // Note: LED_TOGGLE_RATE*TIMER_TICKS_PER_MS should not exceed 65535 (0xFFFF)
  73          // for the 16-bit timer
  74          
  75          #define AUX1     TIMER_TICKS_PER*LED_TOGGLE_RATE
  76          #define AUX2     -AUX1
  77          
  78          #define TIMER2_RELOAD            AUX2  // Reload value for Timer2
  79          
  80          sbit LED = P3^3;                       // LED='1' means ON
  81          
  82          
  83          //-----------------------------------------------------------------------------
  84          // Function Prototypes
  85          //-----------------------------------------------------------------------------
  86          
  87          void Port_Init (void);                 // Port initialization routine
  88          void Timer2_Init (void);               // Timer2 initialization routine
  89          
  90          //-----------------------------------------------------------------------------
  91          // Global Variables
  92          //-----------------------------------------------------------------------------
  93          
  94          sfr16 TMR2RL = 0xCA;                   // Timer2 Reload Register
  95          sfr16 TMR2 = 0xCC;                     // Timer2 Register
  96          
  97          //-----------------------------------------------------------------------------
  98          // main() Routine
  99          //-----------------------------------------------------------------------------
 100          
 101          void main (void)
 102          {
 103   1         PCA0MD &= ~0x40;                    // Clear watchdog timer enable
 104   1      
 105   1         Timer2_Init ();                     // Initialize the Timer2
 106   1         Port_Init ();                       // Init Ports
 107   1         EA = 1;                             // Enable global interrupts
 108   1      
 109   1         while (1);                          // Loop forever
 110   1      }
 111          
 112          //-----------------------------------------------------------------------------
 113          // Initialization Subroutines
 114          //-----------------------------------------------------------------------------
 115          
 116          //-----------------------------------------------------------------------------
 117          // Port_Init
C51 COMPILER V8.08   F31X_TIMER2_16BITRELOADTIMER                                          12/22/2008 11:14:19 PAGE 3   

 118          //-----------------------------------------------------------------------------
 119          //
 120          // Return Value : None
 121          // Parameters   : None
 122          //
 123          // This function configures the crossbar and GPIO ports.
 124          //
 125          // Pinout:
 126          //
 127          //    P3.3 -> LED
 128          //
 129          //    all other port pins unused
 130          //
 131          //-----------------------------------------------------------------------------
 132          void Port_Init (void)
 133          {
 134   1         XBR1 = 0x40;                        // Enable crossbar
 135   1         P3MDOUT = 0x08;                     // Set LED to push-pull
 136   1      }
 137          
 138          //-----------------------------------------------------------------------------
 139          // Timer2_Init
 140          //-----------------------------------------------------------------------------
 141          //
 142          // Return Value : None
 143          // Parameters   : None
 144          //
 145          // This function configures Timer2 as a 16-bit reload timer, interrupt enabled.
 146          // Using the SYSCLK at 16MHz/8 with a 1:12 prescaler.
 147          //
 148          // Note: The Timer2 uses a 1:12 prescaler.  If this setting changes, the
 149          // TIMER_PRESCALER constant must also be changed.
 150          //-----------------------------------------------------------------------------
 151          void Timer2_Init(void)
 152          {
 153   1         int s = AUX1;
 154   1         unsigned int t = TIMER_TICKS_PER;
 155   1       /* 
 156   1         CKCON &= ~0x60;                     // Timer2 uses SYSCLK/12
 157   1         TMR2CN = 0x00;
 158   1      
 159   1         TMR2RL = TIMER2_RELOAD;             // Reload value to be used in Timer2
 160   1         TMR2 = TMR2RL;                      // Init the Timer2 register
 161   1      
 162   1         TMR2CN = 0x04;                      // Enable Timer2 in auto-reload mode
 163   1         ET2 = 1;                            // Timer2 interrupt enabled
 164   1      */   
 165   1         TMR3CN = 0x00;                      // STOP Timer3; Clear TF3H and TF3L;
 166   1                                                                                 // Timer3 operates in 16 bits auto reload mode
 167   1                                             // disable low-byte interrupt; disable
 168   1                                           // split mode; select internal timebase
 169   1         CKCON |= 0xc0;                      // Timer3 uses SYSCLK as its timebase
 170   1      
 171   1         //TMR3RLL  = -counts>>8;                  // Init Timer3 (Hi&Lo) reload register values
 172   1         //TMR3L  = TMR3RLL;                    // Init Timer3 (Hi&Lo) load register values
 173   1      
 174   1              TMR3RLH = TIMER2_RELOAD>>8;
 175   1              TMR3RLL = TIMER2_RELOAD;   
 176   1              TMR3L =  TMR3RLL;
 177   1      
 178   1         EIE1 |= 0x80;                        // Enable Timer3 interrupts
 179   1         TMR3CN |= 0x04;                      // start Timer3, and set low bytes overflow flags
C51 COMPILER V8.08   F31X_TIMER2_16BITRELOADTIMER                                          12/22/2008 11:14:19 PAGE 4   

 180   1      
 181   1      }
 182          
 183          
 184          //-----------------------------------------------------------------------------
 185          // Interrupt Service Routines
 186          //-----------------------------------------------------------------------------
 187          
 188          //-----------------------------------------------------------------------------
 189          // Timer2_ISR
 190          //-----------------------------------------------------------------------------
 191          //
 192          // Here we process the Timer2 interrupt and toggle the LED
 193          //
 194          //-----------------------------------------------------------------------------
 195          void Timer2_ISR (void) interrupt 14
 196          {
 197   1         static unsigned int n = 0;
 198   1         static int s = 0;
 199   1         if (n++ >= (1000*2))
 200   1         {
 201   2              n = 0;
 202   2                  if (s++>=100)
 203   2                      {
 204   3                  s = 0;
 205   3                      LED = ~LED;                         // Toggle the LED
 206   3                      }
 207   2         }
 208   1        // TF2H = 0;                           // Reset Interrupt
 209   1        TMR3CN &= 0x7F;
 210   1      }
 211          
 212          //-----------------------------------------------------------------------------
 213          // End Of File
 214          //-----------------------------------------------------------------------------


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    126    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      4       4
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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