Program for anti-myopia desk lamp based on 51 single chip ultrasonic distance measurement

Publisher:MagicGardenLatest update time:2020-12-09 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Program code 

simulation diagram

The microcontroller source program is as follows:

#include

#include


/*************************Macro definition************************/

#define uchar unsigned char

#define uint unsigned int


/************Bit definition********************************/

sbit Trig=P3^7; //Ultrasonic module trigger bit

sbit LEDRED=P1^0;

sbit LEDGREEN=P1^2;

sbit speaker=P1^7;

sbit person=P1^6;

sbit TLED = P1^5;

/************Global variable declaration************************/

uint H,T; //define global variables, H--distance, T--time


/***********Common cathode digital tube segment code table********************/

uchar a_code[]={ //Common cathode digital tube code 0~9

0x3f,0x06,0x5b,0x4f,0x66,

0x6d,0x7d,0x07,0x7f,0x6f,

/*0x77,0x7c,0x39,0x5e,0x79,

0x71*/};


/***************Delay function****************************/

void delay(uint z) //*1ms delay function

{

        uint x,y;

        for(x=z;x>0;x--)

                for(y=110;y>0;y--);

}


/*************Ultrasonic module startup function**************/

void start()

{

        

        Trig=1; //Pull up the trigger signal to start the ultrasonic module to emit ultrasonic waves

        _nop_(); //Delay 10us

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        Trig=0; //Pull the trigger signal low

}



/*****************Digital tube display function*****************/

void display(unsigned int a,unsigned int b,unsigned int c,unsigned int d)

{

    P2=0xfe;

    P0=a_code[a];

    delay(5);

    P2=0xfd;

    P0=a_code[b];

    delay(5);

        P2=0xfb;

    P0=a_code[c];

    delay(5);

        P2=0xf7;

    P0=a_code[d];

    delay(5);


}



/*********************Main function****************************/

void main()

{

          Trig=0;

         LEDRED=1;

         LEDGREEN=1;

         speaker=0;

     EA=1;

TMOD=0x19; //Timer 0 working mode:

               /*GATE=1 (Timer 0 is started by external interrupt 0 pin and TR0)

               C/T=0 (timing mode)

                                M1M0=01(16-bit timing)*/

        TH1=64580/256;

        TL1=65480%256;

        ET1=1;

        TR0=1; //Start timer 0

        EX0=1; //Open external interrupt 0

        IT0=1; //Set external interrupt 0 to falling edge trigger mode

        while(1)

{

start(); //Call the ultrasonic module start function

display(H/1000,H%1000/100,H%1000%100/10,H%10); //Call the digital tube display function

if(person==1)  

         {

                TLED=1;

                if(H<=25)

                {

                        LEDRED=0;

                        LEDGREEN=1;

                        TR1=1;

                }

                else

                {

                        LEDRED=1;

                        LEDGREEN=0;

                        TR1=0;

                }

        }

        else

        {

                TLED=0;

                LEDRED=1;

                LEDGREEN=1;

                TR1=0;

        }

}

}


/***************External interrupt 0 function****************************

The high level width of the echo output terminal is measured (when the rising edge arrives, timer 0 starts timing.

When the falling edge arrives, an external interrupt is triggered, timer 0 is turned off, timing is stopped, and distance is calculated.

************************************************************/

void int0() interrupt 0         

{

         TR0=0; //Turn off timer 0

        EX0=0; //Disable external interrupt 0

        T=(TH0*256+TL0); //Calculate the ultrasonic transmission time. The CPU uses a 12M clock, so the calculation unit is us.

         TH0=0; //Timer cleared

         TL0=0;

         H=T*0.017; //Calculate the distance in cm (Note: H=(T*17)/1000 cannot be used, as this will result in rounding)

         H=H+2;

        TR0=1; //Start timer 0

        EX0=1; // Enable external interrupt 0        

}

void time1() interrupt 3

{

        TR1=0;

        TH1=65480/256;

        TL1=65480%256;

        speaker=~speaker;

        TR1=1;

}


Reference address:Program for anti-myopia desk lamp based on 51 single chip ultrasonic distance measurement

Previous article:Body temperature monitoring sound and light alarm device based on DS18B20
Next article:Taxi meter based on single chip microcomputer control

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号