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

📄 main.c

📁 PHILIPS LPC76X Timer 实例程序
💻 C
字号:
    #include <Reg768.h>
    #include <stdio.h>

    #define TIMER0_COUNT 0xD906     //10000h-((6,000,000Hz / (6 * FREQ))-12)
    static unsigned timer0_tick;    // Tick timer for funct0();
    static unsigned timer1_tick;    // Tick timer for funct1();
    static bit bzy_flag0, bzy_flag1;
    
    /*******************************************
            Function Prototypes
    ********************************************/
    void timer0_initialize (void);
    void serial_init(void);
    void funct0(void);
    void funct1(void);


    void main (void)
    {
        serial_init();
        timer0_initialize ();
        bzy_flag0 = bzy_flag1 = 0;

        while(1)                // Call functions Round Robin
            {
                funct0();         
                funct1();
            }
    }

    void funct0(void)
    {
        if(!bzy_flag0)           // If task not active make task ready
        {
            bzy_flag0 = 1;       // Set busy flag bit
            timer0_tick = 0;     // Clear timer0_tick count
        }

        if(timer0_tick >= 100)   // Suspend task for delay
        {
            printf("In funct0 task.\n");
            bzy_flag0 = 0;
        }
    }

    void funct1(void)
    {
        if(!bzy_flag1)           // If task not active make task ready
        {
            bzy_flag1 = 1;       // Set busy flag bit
            timer1_tick = 0;     // Clear timer1_tick count
        }

        if(timer1_tick >= 400)   // Suspend task for delay
        {
            printf("In funct1 task.\n");
            bzy_flag1 = 0;      // Task is complete - Make not active
        }
    }

    /******************************************************************************
    static void timer0_isr (void);
    This function is an interrupt service routine for TIMER 0. It should never
    be called by a C or assembly function. It will be executed automatically
    when TIMER 0 overflows.
    *******************************************************************************/
    static void timer0_isr (void) interrupt 1 using 1
    {
    /*********************************************************
    Stop Timer 0, adjust the timer 0 counter so that
    we get another interrupt in 10ms, and restart the
    timer.
    **********************************************************/

    TR0 = 0;        // stop timer 0 
    TL0 = TL0 + (TIMER0_COUNT & 0x00FF);    
    TH0 = TH0 + (TIMER0_COUNT >> 8);
    TR0 = 1;        // start timer 0
 
    /********************************************************
    Increment the timer tick. This interrupt should
    occur approximately every 10ms.
    *********************************************************/
    timer0_tick++;
    timer1_tick++;
    }

    /******************************************************************************
    void timer0_initialize (void);
    This function enables TIMER 0. TIMER 0 generates an interrupt every 100Hz.
    ******************************************************************************/
    void timer0_initialize (void)
    {
    EA = 0;             // disable all interrupts 
    TR0 = 0;            // stop timer 0
    TMOD &= ~0x0F;      // clear timer 0 mode bits
    TMOD |= 0x01;       // put timer 0 into 16-bit no prescale
    TL0 = (TIMER0_COUNT & 0x00FF);
    TH0 = (TIMER0_COUNT >> 8);
    PT0 = 0;            // set low priority for timer 0
    ET0 = 1;            // enable timer 0 interrupt
    TR0 = 1;            // start timer 0
    EA = 1;             // enable interrupts
    }

    /*****************************************************
    Configure the serial port to run at 1200 Baud using
    timer 1 as an 8 bit auto reload baud rate generator
    *****************************************************/    
    
    void serial_init(void)
    {
    
        SCON  = 0x50;   // set UART as 8-bit mode 1
        TMOD |= 0x20;   // put timer 1 into 8-bit autoload mode 2
        TH1   = 0xA9;   // reload value for 1200 baud
        TR1   = 1;      // enable timer 1
        TI    = 1;      // enable first transmitt
    }
   




⌨️ 快捷键说明

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