51 single chip microcomputer with ultrasonic ranging and 1602 LCD display

Publisher:PeacefulSoulLatest update time:2018-06-25 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*Ultrasonic distance measurement program, using the external interrupt of 51 MCU. If it is not working well, it is recommended to use Dupont wire to connect the pins directly*/  

#include  

#include  

  

typedef unsigned char uchar;  

typedef unsigned int uint;  

/*The ultrasonic module is HC-SR04. The trigger signal is a high level of at least 10us. After that, the module will automatically send 8 40kHz square wave signals.  

Once the module receives the echo signal, the Echo pin will output a high level until it stops receiving the signal.  

The calculation is based on the duration of the high level of the Echo pin. However, shouldn't the sound wave's journey start when the sound wave is emitted?  

For the second calculation, if you want to change it, please change the start time of timer 0. */  

sbit Echo = P3^2; //The echo signal output of the ultrasonic module, P3^2 is also the input terminal of the 51 MCU external interrupt 0, using the falling edge to trigger the interrupt  

sbit Trig=P1^4; //trigger signal pin  

  

  

sbit lcdrs = P1^0; //Data command selection terminal, write command when 0, write data when 1  

sbit lcdrw = P1^1; // read and write command selection terminal, write data when 0, read data when 1, set to 1 when reading status, and 0 at other times;  

sbit lcden=P2^5; //enable terminal  

sbit dula=P2^6; //Not useful in this program  

sbit wela=P2^7;  

sbit STA7=P0^7; //D0~D7 data ports correspond to P0^0~P0^7. When reading LCD data, STA7 corresponds to P0^7. When STA7 is 1, it means the LCD is busy and cannot receive data.  

  

float dis; //distance cache  

uchar flag; //interrupt flag  

char code table[]="distance:"; //Display characters at specified positions  

char code table1[]="cm";  

  

void delayms(uchar x)  

{  

    uint i,j;  

    for(i=0;i

    {  

        for(j=0;j<110;j++);  

    }  

}  

void nops() //Delay 10.9us, each machine cycle is about 1.09us, when the crystal oscillator is 11.0592MHz  

{  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

    _nop_();  

}  

  

void distance()//Function to calculate distance  

{  

    Trig=0; //First pull the trigger low  

    nops();  

    Trig=1; //Give a high level of at least 10us to start the module  

    nops();  

    nops();  

    Trig=0; //The module has been triggered at this time, and the timer count will be started immediately  

    TR0=1; //Turn on timer 0  

    EX0=1; //Open external interrupt, external interrupt input is P3^2, falling edge is valid, trigger interrupt  

    delayms(1); //Wait a moment. One machine cycle is about 1.09us. If you don't wait, you may miss the calculation of dis and fall into an infinite loop, and you will never get the value.  

    if(flag==1) //If the flag is set to 1, it means that the Echo output is at a falling edge, i.e. the reception is finished  

    {  

  

        flag=0;  

    }  

  

  

}  

  

void wait()//Wait for LCD to be idle  

{  

    P0=0xff;  

    do  

    {  

        lcdrs=0; //These statements in the loop are the operation timing when reading the status  

        lcdrw=1;  

        lcden=0;  

        lcden=1;  

    }while(STA7==1); //Wait until the LCD is idle before looping  

    lcden=0; //enable to 0  

}  

  

void write_com(uchar com) //write command program  

{  

    wait();  

    lcdrs=0; // Set RS to 0, the data sent will be regarded as a command by the LCD instead of real data, which is the only difference from the data writing program  

    P0=com;  

    lcdrw=0; //Always write, so it is 0  

    delayms(5); // Make the data stable, so that the LCD has time to read  

    lcden=1; //According to the operation sequence, enable to output a high pulse. This sentence and the following sentence together constitute a high pulse  

    delayms(5);  

    lcden=0;  

}  

  

void write_date(uchar date) //Write data program  

{  

    wait();  

    lcdrs=1; //Note that this is writing data  

    P0=date;  

    lcdrw=0;  

    delayms(5);  

    lcden=1;  

    delayms(5);  

    lcden=0;  

}  

  

/* Initialization function */  

void init()  

{  

    wait();  

    dula=0;//useless  

    wela=0;  

    lcden=0;  

    write_com(0x38); //Set 16*2 display, 5*7 dot matrix, 8-bit data port. This sentence is unchanged  

    write_com(0x0c);//Status word set according to operation sequence. Same as below  

    write_com(0x06);  

    TMOD=0x01; //16-bit counter, there is no need to enable the timer interrupt here, because the timer interrupt has nothing to do, as long as the value in the timer is OK, so there is no need to set TF0 to 1  

    TH0=0; //Set all to 0  

    TL0=0;  

    IT0=1; //Set external interrupt falling edge to be effective  

    EA=1; //Open the general interrupt  

}  

/*Display function*/  

void display(float dis)  

{  

    uint bai,shi,ge,p1,p2; // respectively, the hundreds place down to two decimal places  

    bai=dis/100;  

    shi=(dis-bai*100)/10;  

    ge=dis-bai*100-shi*10;  

    p1=(dis*10)-bai*1000-shi*100-ge*10;  

    p2=(dis*100)-bai*10000-shi*1000-ge*100-p1*10;  

    write_date(0x30+bai); //Convert numbers to characters, must be +0x30, in addition, 1602 LCD can only receive character data  

    write_date(0x30+shi);  

    write_date(0x30+ge);  

    write_date('.');  

    write_date(0x30+p1);  

    write_date(0x30+p2);  

  

}  

  

  

void main()//main function  

{  

    uchar i=0;  

    init();  

    write_com(0x80); //Set the address, display distance in the first row and first column: 0x80 is the starting address of the first row  

    while(table[i]!='\0')  

    {  

        write_date(table[i]);  

        i++;  

        delayms(5);  

    }  

    i=0;  

    write_com(0x80+0x40+6); //Set the address, display cm in the 7th position of the second line, 0x80+0x40 is the starting address of the second line  

    while(table1[i]!='\0')  

    {  

        write_date(table1[i]);  

        i++;  

        delayms(5);  

    }  

    while(1)  

    {  

        distance();  

        write_com(0x80+0x40); //Set the display address of the data to the start position of the second line, and then refresh the display at this place during the loop  

        display(dis);  

        delayms(60);  

    }  

  

  

}  

  

void ex() interrupt 0 //interrupt function of external interrupt  

{  

    TR0=0; //Once receiving a falling edge, the timer will stop counting immediately  

    dis=(TH0*256+TL0)*1.09/58; //First take out the time value in the timer, then set the timer to 0  

    flag=1; //Set the flag position to 0  

    TH0=0;  

    TL0=0;  

}  


Reference address:51 single chip microcomputer with ultrasonic ranging and 1602 LCD display

Previous article:Electronic clock (51 single chip timer, 1602 LCD)
Next article:51 MCU 1602 LCD mobile display

Recommended ReadingLatest update time:2024-11-16 14:59

51 single chip GPS + sim800c GSM positioning SMS LCD1602 LCD display program
sim800c+GPS chip is a small design for practice, a little ugly. Attached is the program and the principle PCB diagram. It is   my first post, please forgive me if it is not good. Button function: one button alarms, buzzer sounds, another button sends a text message to the specified mobile phone, the mobile phone num
[Microcontroller]
51 single chip GPS + sim800c GSM positioning SMS LCD1602 LCD display program
Detailed explanation of 51 microcontroller driver LCD1602 program
  Detailed explanation of 51 single chip LCD1602 program   LCD1602   Industrial character LCD. 1602 means that the content displayed by the LCD is 16X2, that is, it can display two lines of 16 characters each.      Special interface description   RS: register selection input   RS=1: Point to data register   RS=0: p
[Microcontroller]
Detailed explanation of 51 microcontroller driver LCD1602 program
Single chip microcomputer learning - LCD1602 display experiment one
Title: Displayed on the LCD1602 display    "I LIKE MCU!        XIAO WO ”    And the display method is to dynamically move from the right screen to the left in sequence. Solution: (C language programming) #include reg52.h #define uchar unsigned char #define uint unsigned int uchar code table ="I LIKE MCU!";
[Microcontroller]
[C51 code] Menu function [for LCD1602]
#ifndef __menu_h__ #define __menu_h__ #include "head.h" #include "LCD1602_2.h" #include "DS18B20.h" #define Null 0 /*********************** * Function declaration * ***********************/ void ShowMenu(void); void Menu_Change(volatile KeyNum); /*********************** * Key function key macro defi
[Microcontroller]
LCD1602 dynamic display
The schematic diagram of the simulation element is as follows: /*------------------------------------- Function: LCD1602 local dynamic display Description: Display count at a fixed position on the second line of the screen: Then the number of key presses will be displayed dynamically (00 ~ 99) Author: Zhang Kaizhou D
[Microcontroller]
LCD1602 dynamic display
Realization of LCD1602 Liquid Crystal Driver Based on 51 Single Chip Microcomputer
The main contents of this article are as follows: 1. Basic introduction of LCD1602 2. Implementation of LCD1602 liquid crystal driver     According to the analysis of the existing LCD1602 liquid crystal driver, I always feel that it is a bit messy and troublesome. To put it bluntly, it is not easy to use. Here I pla
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号