Single chip microcomputer source code of simple co-location lifting system (implemented by ultrasonic ranging)

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

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

/*******************Ultrasonic rangefinder************************************/

/*******************(LCD display)****************************************/

/*******************Crystal oscillator 11.0592MHz********************************/

#include

#include "12864.h"

#include

typedef unsigned char U8; /* defined for unsigned 8-bits integer variable unsigned 8-bit integer variable */

typedef signed char S8; /* defined for signed 8-bits integer variable */

typedef unsigned int U16; /* defined for unsigned 16-bits integer variable unsigned 16-bit integer variable*/

typedef signed int S16; /* defined for signed 16-bits integer variable */

typedef unsigned long U32; /* defined for unsigned 32-bits integer variable unsigned 32-bit integer variable*/

//typedef signed long S32; /* defined for signed 32-bits integer variable */

//typedef float F32; /* single precision floating point variable (32bits) single precision floating point number (32-bit length) */

//typedef double F64; /* double precision floating point variable (64bits) double precision floating point number (64-bit length) */

//The timing value of timer 0 is 1ms, that is, 11059/12=922 clock pulses, which is complemented by 65536-922=64614

#define SYSTEMCLK      921600       //11059200/12

#define T0CLK          921600       //11059200/12

#define T1CLK          921600       //11059200/12

#define T1PERIOD 1000000/921600 //T1 cycle time, in microseconds, about 1.085uS

#define TIMER0H        0xFC   //64614/256=252

#define TIMER0L        0x66   //54447%256=102


//Pin definition

sbit Trig = P1^3;

sbit Echo = P3^2; //Echo must be connected to the external interrupt pin

sbit Beep = P2^6; //Alarm buzzer

sbit k1=P3^0;

sbit k2=P3^1;

sbit k3=P3^3;

sbit k4=P3^4;

sbit k5=P3^5;

sbit k6=P2^5;

sbit k7=P2^7;

sbit hy=P1^1;

unsigned char code tab[]="0123456789 :.%";

uint h=0,v=0;

uint V1=0,s=0;

uchar time1,puk1=100,k=1,w=0,m=0,puk=0;


//Define flags

volatile bit FlagSucceed = 0; //Measurement success flag

volatile bit FlagDisplay = 0; //Display flag


//Define global variables

U16 DisplayCount=0;

U32 time=0;

U32 distance=0;



// Function declaration

void delay_20us();

void Start_Module();

void INT0_Init(void);

void Data_Init();

void Timer0_Init();

void Timer1_Init();


//20us delay program, not necessarily very accurate

void delay_20us()

{  

    U16 bt ;

    for(bt=0;bt<190;bt++); //8M crystal is 100

}

void delays(fly t)

{

        fly a,b;

        for(a=t;a>0;a--)

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

}

void speak()

{

        Beep=0;

        delayms(200);

        Beep=1;

}

//Data initialization

void Data_Init()

{

        Trig = 0;

        distance = 0;

        DisplayCount = 0;


}


//External interrupt initialization function

void INT0_Init(void)

{

   IT0 = 0; //Negative edge trigger interrupt

   EX0=0; //Disable external interrupt

}


//External interrupt processing is used to judge the echo level

void INT0_ISR (void) interrupt 0

{      

     time =TH1*256+TL1; //Get the value of the timer

     FlagSucceed = 1; //Set the flag of successful measurement

         if(s==1)

         V1=3*puk1;

         else

         V1=0;

         co_display (3,5,V1/100,V1%100/10,tab);

         

           EX0=0; //Disable external interrupt

}

//////////////Initialize timer///////////////////

void InitTimer0(void)

{

    TMOD = 0x11;

    TH0=0xff;

        TL0=0xfe;

    EA = 1;

    ET0 = 1;

    TR0 = 1;

}

///////////////////PWM generation function//////////////////////////////

void Timer0Interrupt(void) interrupt 1

{

    TH0=0xff;

        TL0=0xfe; //Reassign value

    time1++;                                                  

        if(time1        else

                if((time1>=puk1)&&(time1<=100))pwm1=0; // If puk1 reaches 100, it is low level

                else

                        if(time1>=100)time1=0; //clear to 0 if time is greater than 100                                 

}                              




//Timer 1 initialization, 16-bit counting mode, clock is 11059200/12=921600Hz

//60ms count is 55296, which is 0xD800

void Timer1_Init()

{

        TMOD = 0x11; //Timer 0 and 1 work in 16-bit mode

        TH1 =0;

        TL1= 0;

        ET1 = 1;

}




//Start the module, Trig pin 20us positive pulse

void Start_Module() //Start module

{

   Trig=1; //Start the module once

   delay_20us();

   Trig=0;

}



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

* Name: Main()

* Function: Main function

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

void key()

{

  if(k1==0||he==0)

  {

          pwm(w);

// puk1=puk;

        cn_display(1,6,0,"启");

        s=1;

  }

  if(k2==0)

  {

    delay_20us();

        delay_20us();

        mold(2);

        s=0;

        {

          if(k==1)

          cn_display(1,4,0,"伸"),w=1,k=0;

          else if(k==0)

          cn_display(1,4,0,"缩"),w=0,k=1;

         // else

         // cn_display(1,4,0,"停"),pwm(w),k=1;      

        }

  }      

  if(k3==0)

  {

        h++;

        if(h>=30)

        h=30;

  }

   if(k4==0)

  {

        h--;

        if(h==0)

        h=30;

  }

  if(k5==0)

  {

        in++;

        if(v>=30)

        v=30;

  }

   if(k6==0)

  {

        in--;

        if(v==0)

        v=30;

  }      

  if(k7==0)

  {

    m++;

    if(m>6)

        m=0;

  }

}

void chaosheng()

{

        EA = 0;        

            //The following is a detection process: first send the Trig level, turn on the external interrupt, clear T1,

                //Finally, when the external interrupt falling edge is triggered, take out the current value of T1 and calculate the Trig pulse width.

        while (Echo == 1) ;

                Start_Module();

        while(Echo==0) ;//Wait for the Echo pin to become high level

                FlagSucceed = 0;

                EX0=1;

                TH1= 0;

                TL1= 0;

                TF1= 0;

        TR1=1; //Start timer 1 to start counting

                EA = 1;

                while (TH1<83) ; //blind area

                TR1 = 0; // Turn off timer 1

                EX0 = 0; //Disable external interrupt

        if(FlagSucceed==1) //If a test succeeds, calculate the distance in centimeters

            {        

           distance = time * 1.085; //Calculate the pulse time (in microseconds)

                   //Algorithm to convert microseconds into centimeters: Y meters = (X seconds * 344) / 2

                   // X seconds = (2*Y meters)/344 ==》X seconds = 0.0058*Y meters ==》Centimeters = microseconds/58

           distance /=58;

                   //If the distance is less than 20cm, the sound and light alarm will sound

                   distance*=10;

                   distance-=35;

                   distance-=520;

                   co_display (2,6,distance%1000/100,distance%100/10,tab);         

                }

}

void init()

{

    U16 and;

        init_lcd ();

    InitTimer0();

        for(i=0;i<8;i++)

        cn_display(1,i,i," Shaanxi's first college");

        for(i=0;i<8;i++)

        cn_display(2,i,i,"Teacher Electronic Design Competition");

        delayms(2000);

        clrram ();

    EA = 0;

    INT0_Heat();

        Timer1_Init(); //Timer 1 initialization

    Data_Init();

        EA = 1;

        for(i=0;i<8;i++)

        cn_display(0,i,i,"Co-location telescopic control system");

        for(i=0;i<8;i++)

        cn_display(1,i,i," A状态:        ");

        mold(2);

        for(i=0;i<2;i++)

        cn_display(2,i,i,"SET:");

        cn_display(2,3,0,"cm");

        cn_display(2,4,0,"RU");

        cn_display(2,5,0,"N:");

        cn_display(2,7,0,"cm");

        cn_display(3,0,0,"V:");

        cn_display(3,2,0,"mm");

        cn_display(3,3,0,"/s");

        cn_display(3,4,0,"V:");

        cn_display(3,6,0,"mm");

        cn_display(3,7,0,"/s");      

}

void main()

{

   uint l=1,pt=1;

    heat();

        while(1)

        {

        chaosheng();

        co_display (1,3,11,m%10,tab);

        co_display (2,2,h/10,h%10,tab);

        co_display (3,1,v/10,v%10,tab);

      

        if(m==0)

        {

        if((distance/10==h))      

        {

        pwm(2),w=2;

        speak();

        }                                                                          

        }

        if(m==1)

        {

          if((v*10)>V1)

          {

           puk1+=1;

           if(puk1>=100)

           puk1=100;

          }

          else

          if((v*10)          {

           puk1-=1;

           if(puk1==40)

           puk1=40;

          }

      else

          {

           speak();

          }         

        }

        if(m==2)

        {

           if(distance/10==20)

             pwm(0),pt=0;

                 else if((distance/10<=1) &&pt==0)

                 pwm(2),speak(),pt=1;

        }

        key();

        }      

}


Reference address:Single chip microcomputer source code of simple co-location lifting system (implemented by ultrasonic ranging)

Previous article:1.44 inch TFT LCD screen test code (screen driver: ST7735)
Next article:Share a header file of STC89C52

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号