f53x_linsnooper_timer.c

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

C
166
字号
//-----------------------------------------------------------------------------
// F53x_Timer.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// File contains functions for Timers. These functions are using to set, start,
// and use timers. This module contains functions for all used timers.
//
// FID:
// Target:          C8051F53x
// Tool chain:      KEIL C51 7.20
// Command Line:    None
// Project Name:    LIN Snooper
//
// Release 1.0
//    -Initial Revision
//    -01 NOV 2006
//
//


//-----------------------------------------------------------------------------
// Header File Preprocessor Directive
//-----------------------------------------------------------------------------

#ifndef __TIMER_C__
#define __TIMER_C__

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include  <ioc8051f530.h>                // SFR declarations
#include "F53x_LINSnooper_Main.h"      // Include common specific definitions
#include "F53x_LINSnooper_UART.h"      // Include UARTs specific definitions
#include "F53x_LINSnooper_Timer.h"     // Include Timers specific definitions

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions
//-----------------------------------------------------------------------------
__sfr __no_init volatile unsigned short TMR2R         @ 0xCA;
__sfr __no_init volatile unsigned short TMR2          @ 0xCC;

//-----------------------------------------------------------------------------
// Constant Definitions
//-----------------------------------------------------------------------------

#define  F53x_TIMERS_SOURCE

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
uint    LED_TICK    = 0;             	// Number of Timer2's tick
uchar   STATUS_FLAG;                   // Status flag
uchar   TIMEOUT;                       // Timeout flag


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

//----------------------------------------------------------------------------
// Timer1_Initialize
//----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  : This function initializes the Timer1 and starts it
//                for the UART
//
//----------------------------------------------------------------------------
void Timer1_Initialize(void)
{
#if BAUDRATE < 28800

    CKCON &= ~0x08;                    // Timer1 uses the system clock
    CKCON   &= ~0x03;                  // divided by 12

    TH1 = -(SYSCLK/12/BAUDRATE/2);     // Time for BAUDRATE
    TL1 = TH1;

#else
#if BAUDRATE < 57600

    CKCON &= ~0x08;                    // Timer1 uses the system clock
    CKCON   |= 0x01;                   //  divided by 4

    TH1 = -(SYSCLK/4/BAUDRATE/2);      // Time for BAUDRATE
    TL1 = TH1;

#else

    CKCON |= 0x08;                     // Timer1 uses the system clock

    TH1 = (0xFF-(SYSCLK/BAUDRATE/2))+1;        // Time for BAUDRATE
    TL1 = TH1;


#endif
#endif

    TCON |= 0x40;                      // Timer1 enabled
    TMOD |= 0x20;                      // Timer1 Mode2: 8-bit counter/timer
                                       // with auto-reload
//    IE |= 0x08;                      // Enable Timer1 interrupt
}

//-----------------------------------------------------------------------------
// Timer2_Initialize
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  : Function provided by the designer to initialize a timer and
//                therefore create a time-basis for the system
//
//-----------------------------------------------------------------------------
void Timer2_Initialize(void)
{
    CKCON |= 0x30;                      // Timer2 high and low bytes uses the
                                        //  system clock
    TMR2R = (0xFFFF-((SYSCLK+500L)/1000))+1;      // Time for 1msec (24.5MHz SYSCLOCK)
    TMR2 = TMR2R;                       //  and timer register
    TMR2CN = 0x04;                      // Timer2 in 16-bit reload timer mode
    IE |= 0x20;                         // Enable Timer2 interrupt
}


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

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// Return Value :   None
// Parameters   :   None
// Description  :   This is interrupt service
//                      This function is calling every 1ms
// Note:
//-----------------------------------------------------------------------------
#pragma vector = TF2H_int
__interrupt void Timer2_ISR(void)
{
    TMR2CN_bit.TF2H=0;                            // Reset Timer2 interrupt flag
    LED_TICK++;                        // Increase
    if ( LED_TICK == 1000 )            // After 1000ms (Timer2 ticks)
    {
        LED_TICK = 0;
        P1 ^= LED;                     // Toggle LED
        STATUS_FLAG = 0xFF;            // Set Status flag
        if (TIMEOUT)
            TIMEOUT--;                 // Decrease timeout at every 0.5s
    }
}

#endif                                 // endif definition of __TIMER_C__

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

⌨️ 快捷键说明

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