定时器扩展

定时器的扩展

#include”reg51.h”

#include”intrins.h”

 
 

#define ST_0_5S     2500

#define ST_1S     5000

#define ST_1_5S     7500

#define ST_2S     10000

#define ST_2_5S     12500

#define ST_3S     15000

#define ST_3_5S     17500

#define ST_4S     20000

 
 

sbit LED1=P2^0;

sbit LED2=P2^1;

sbit LED3=P2^2;

sbit LED4=P2^3;

sbit LED5=P2^4;

sbit LED6=P2^5;

sbit LED7=P2^6;

sbit LED8=P2^7;

 
 

bit SoftTimer0Enable=0;

unsigned int SoftTimer0Counter=0;

bit SoftTimer0Over=0;

 
 

bit SoftTimer1Enable=0;

unsigned int SoftTimer1Counter=0;

bit SoftTimer1Over=0;

 
 

bit SoftTimer2Enable=0;

unsigned int SoftTimer2Counter=0;

bit SoftTimer2Over=0;

 
 

bit SoftTimer3Enable=0;

unsigned int SoftTimer3Counter=0;

bit SoftTimer3Over=0;

 
 

bit SoftTimer4Enable=0;

unsigned int SoftTimer4Counter=0;

bit SoftTimer4Over=0;

 
 

bit SoftTimer5Enable=0;

unsigned int SoftTimer5Counter=0;

bit SoftTimer5Over=0;

 
 

bit SoftTimer6Enable=0;

unsigned int SoftTimer6Counter=0;

bit SoftTimer6Over=0;

 
 

bit SoftTimer7Enable=0;

unsigned int SoftTimer7Counter=0;

bit SoftTimer7Over=0;

 
 

void main(void)

{

    TMOD=0x02;//定时器工作方式2,自装

    TH0=TL0=0x38;//设定硬件时基0.2ms

    ET0=1;

    TR0=1;

    EA=1;

 
 

    SoftTimer0Counter=ST_0_5S;

    SoftTimer0Enable=1;

    SoftTimer1Counter=ST_1S;

    SoftTimer1Enable=1;

    SoftTimer2Counter=ST_1_5S;

    SoftTimer2Enable=1;

    SoftTimer3Counter=ST_2S;

    SoftTimer3Enable=1;

    SoftTimer4Counter=ST_2_5S;

    SoftTimer4Enable=1;

    SoftTimer5Counter=ST_3S;

    SoftTimer5Enable=1;

    SoftTimer6Counter=ST_3_5S;

    SoftTimer6Enable=1;

    SoftTimer7Counter=ST_4S;

    SoftTimer7Enable=1;

 
 

    while(1)

    {

        if(_testbit_(SoftTimer0Over))

        {

             SoftTimer0Counter=ST_0_5S;

             LED1=~LED1;

        }

        if(_testbit_(SoftTimer1Over))

        {

             SoftTimer1Counter=ST_1S;

             LED2=~LED2;

        }

        if(_testbit_(SoftTimer2Over))

        {

             SoftTimer2Counter=ST_1_5S;

             LED3=~LED3;

        }

        if(_testbit_(SoftTimer3Over))

        {

             SoftTimer3Counter=ST_2S;

             LED4=~LED4;

        }

        if(_testbit_(SoftTimer4Over))

        {

             SoftTimer4Counter=ST_2_5S;

             LED5=~LED5;

        }

        if(_testbit_(SoftTimer5Over))

        {

             SoftTimer5Counter=ST_3S;

             LED6=~LED6;

        }

        if(_testbit_(SoftTimer6Over))

        {

             SoftTimer6Counter=ST_3_5S;

             LED7=~LED7;

        }

        if(_testbit_(SoftTimer7Over))

        {

             SoftTimer7Counter=ST_4S;

             LED8=~LED8;

        }

    }

}

 
 

void TimerBaseByTimer0(void) interrupt 1

{

    if(SoftTimer0Enable)

    {

        if(SoftTimer0Counter)

        {

            SoftTimer0Counter–;

            if(!SoftTimer0Counter)

            {

                SoftTimer0Over=1;

            }

        }

    }

    if(SoftTimer1Enable)

    {

        if(SoftTimer1Counter)

        {

            SoftTimer1Counter–;

            if(!SoftTimer1Counter)

            {

                SoftTimer1Over=1;

            }

        }

    }

    if(SoftTimer2Enable)

    {

        if(SoftTimer2Counter)

        {

            SoftTimer2Counter–;

            if(!SoftTimer2Counter)

            {

                SoftTimer2Over=1;

            }

        }

    }

    if(SoftTimer3Enable)

    {

        if(SoftTimer3Counter)

        {

            SoftTimer3Counter–;

            if(!SoftTimer3Counter)

            {

                SoftTimer3Over=1;

            }

        }

    }

    if(SoftTimer4Enable)

    {

        if(SoftTimer4Counter)

        {

            SoftTimer4Counter–;

            if(!SoftTimer4Counter)

            {

                SoftTimer4Over=1;

            }

        }

    }

    if(SoftTimer5Enable)

    {

        if(SoftTimer5Counter)

        {

            SoftTimer5Counter–;

            if(!SoftTimer5Counter)

            {

                SoftTimer5Over=1;

            }

        }

    }

    if(SoftTimer6Enable)

    {

        if(SoftTimer6Counter)

        {

            SoftTimer6Counter–;

            if(!SoftTimer6Counter)

            {

                SoftTimer6Over=1;

            }

        }

    }

    if(SoftTimer7Enable)

    {

        if(SoftTimer7Counter)

        {

            SoftTimer7Counter–;

            if(!SoftTimer7Counter)

            {

                SoftTimer7Over=1;

            }

        }

    }

}

 
 

 
 

 
 

 
 

 
 

上述程序的简化版,去除了使能条件

#include”reg51.h”

#include”intrins.h”

 
 

#define ST_0_5S     2500

#define ST_1S     5000

#define ST_1_5S     7500

#define ST_2S     10000

#define ST_2_5S     12500

#define ST_3S     15000

#define ST_3_5S     17500

#define ST_4S     20000

 
 

sbit LED1=P2^0;

sbit LED2=P2^1;

sbit LED3=P2^2;

sbit LED4=P2^3;

sbit LED5=P2^4;

sbit LED6=P2^5;

sbit LED7=P2^6;

sbit LED8=P2^7;

 
 

unsigned int SoftTimer0Counter=0;

bit SoftTimer0Over=0;

 
 

unsigned int SoftTimer1Counter=0;

bit SoftTimer1Over=0;

 
 

unsigned int SoftTimer2Counter=0;

bit SoftTimer2Over=0;

 
 

unsigned int SoftTimer3Counter=0;

bit SoftTimer3Over=0;

 
 

unsigned int SoftTimer4Counter=0;

bit SoftTimer4Over=0;

 
 

unsigned int SoftTimer5Counter=0;

bit SoftTimer5Over=0;

 
 

unsigned int SoftTimer6Counter=0;

bit SoftTimer6Over=0;

 
 

unsigned int SoftTimer7Counter=0;

bit SoftTimer7Over=0;

 
 

void main(void)

{

    TMOD=0x02;//定时器工作方式2

    TH0=TL0=0x38;//设定硬件时基0.2ms

    ET0=1;

    TR0=1;

    EA=1;

 
 

    SoftTimer0Counter=ST_0_5S;

    SoftTimer1Counter=ST_1S;

    SoftTimer2Counter=ST_1_5S;

    SoftTimer3Counter=ST_2S;

    SoftTimer4Counter=ST_2_5S;

    SoftTimer5Counter=ST_3S;

    SoftTimer6Counter=ST_3_5S;

    SoftTimer7Counter=ST_4S;

 
 

 
 

    while(1)

    {

        if(_testbit_(SoftTimer0Over))

        {

             SoftTimer0Counter=ST_0_5S;

             LED1=~LED1;

        }

        if(_testbit_(SoftTimer1Over))

        {

             SoftTimer1Counter=ST_1S;

             LED2=~LED2;

        }

        if(_testbit_(SoftTimer2Over))

        {

             SoftTimer2Counter=ST_1_5S;

             LED3=~LED3;

        }

        if(_testbit_(SoftTimer3Over))

        {

             SoftTimer3Counter=ST_2S;

             LED4=~LED4;

        }

        if(_testbit_(SoftTimer4Over))

        {

             SoftTimer4Counter=ST_2_5S;

             LED5=~LED5;

        }

        if(_testbit_(SoftTimer5Over))

        {

             SoftTimer5Counter=ST_3S;

             LED6=~LED6;

        }

        if(_testbit_(SoftTimer6Over))

        {

             SoftTimer6Counter=ST_3_5S;

             LED7=~LED7;

        }

        if(_testbit_(SoftTimer7Over))

        {

             SoftTimer7Counter=ST_4S;

             LED8=~LED8;

        }

    }

}

 
 

void TimerBaseByTimer0(void) interrupt 1

{

    if(SoftTimer0Counter)

    {

        SoftTimer0Counter–;

        if(!SoftTimer0Counter)

        {

            SoftTimer0Over=1;

        }

    }

 
 

    if(SoftTimer1Counter)

    {

        SoftTimer1Counter–;

        if(!SoftTimer1Counter)

        {

            SoftTimer1Over=1;

        }

    }

 
 

    if(SoftTimer2Counter)

    {

        SoftTimer2Counter–;

        if(!SoftTimer2Counter)

        {

            SoftTimer2Over=1;

        }

    }

 
 

    if(SoftTimer3Counter)

    {

        SoftTimer3Counter–;

        if(!SoftTimer3Counter)

        {

            SoftTimer3Over=1;

        }

    }

 
 

    if(SoftTimer4Counter)

    {

        SoftTimer4Counter–;

        if(!SoftTimer4Counter)

        {

            SoftTimer4Over=1;

        }

    }

 
 

    if(SoftTimer5Counter)

    {

        SoftTimer5Counter–;

        if(!SoftTimer5Counter)

        {

            SoftTimer5Over=1;

        }

    }

 
 

    if(SoftTimer6Counter)

    {

        SoftTimer6Counter–;

        if(!SoftTimer6Counter)

        {

            SoftTimer6Over=1;

        }

    }

 
 

    if(SoftTimer7Counter)

    {

        SoftTimer7Counter–;

        if(!SoftTimer7Counter)

        {

            SoftTimer7Over=1;

        }

    }

}

 
 

 
 

 
 

  

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注