非精准定时器 延时器

非精准定时,延时器

#include”reg51.h”

#include”intrins.h”

 

#define ST_0_5S 2601

#define ST_1S 5201

 

bit Delay0Open=0;

unsigned long Delay0Counter=0;

bit Delay0Over=0;

 

void main()

{

    Delay0Counter=ST_0_5S;

    Delay0Open=1;

 

    while(1)

    {

        if (Delay0Open)

        {

            if(Delay0Counter)

            {

                Delay0Counter–;

                if(!Delay0Counter)

                {

                    Delay0Over=1;

                }

            }

        }

        if(_testbit_(Delay0Over))

        {

            Delay0Counter=ST_0_5S;

            P2=~P2;

        }

    }

}

延时器的扩展

#include”reg51.h”

#include”intrins.h”

 

#define ST_0_5S 2601

#define ST_1S (ST_0_5S+ST_0_5S)

 

bit Delay0Open=0;

unsigned long Delay0Counter=0;

bit Delay0Over=0;

 

bit Delay1Open=0;

unsigned long Delay1Counter=0;

bit Delay1Over=0;

 

void main()

{

    Delay0Counter=ST_0_5S;

    Delay0Open=1;

 

    Delay1Counter=ST_1S;

    Delay1Open=1;

 

    while(1)

    {

        if (Delay0Open)

        {

            if(Delay0Counter)

            {

                Delay0Counter–;

                if(!Delay0Counter)

                {

                    Delay0Over=1;

                }

            }

        }

        if(_testbit_(Delay0Over))

        {

            Delay0Counter=ST_0_5S;

            P2=~P2;

        }

 

        if (Delay1Open)

        {

            if(Delay1Counter)

            {

                Delay1Counter–;

                if(!Delay1Counter)

                {

                    Delay1Over=1;

                }

            }

        }

        if(_testbit_(Delay1Over))

        {

            Delay1Counter=ST_1S;

            P3=~P3;

        }

    }

}