51 single chip 1602 display ultrasonic ranging source program

Test - lowercase jpg
Kaixin micro test
Test probe P100-M3
Filter 18.432M

The production of ultrasonic range finder is a practical project for many students and microcontroller enthusiasts to learn the practice of single-chip microcomputer. This ultrasonic ranging program is designed for the widely used HC-SR04 ultrasonic module. This 1602 display Ultrasonic ranging distance source program, single-chip microcomputer is 51 series single-chip microcomputer, ultrasonic ranging module first uses HC-SRO4 ultrasonic ranging module, single-chip crystal oscillator is 12M, wiring: module TRIG connects P2.5 ECH0 to P3.2, when When the distance exceeds the measurement, the range displays “-” and the display unit is CM. Due to the relatively simple hardware, this program has a small amount of soldering work and is almost 100% successful. It can also be directly executed with PROTEUS software.

#include[reg52.h> //Please pay attention to '[ 'Change to"
#include [intrins.h> //
#define uint unsigned int
#define uchar unsigned char
Sbit rs=P2^0; //1602 data/instruction selection control line
Sbit rw=P2^1; //1602 read/write control line
Sbit en=P2^2; //1602 enable control line
Sbit trig=P2^5; //ultrasonic ranging module Trig
Sbit echo=P3^2; //ultrasonic ranging module Echo
Bit flag1; //trigger signal flag //
Uchar count; / / interrupt accumulation variable
Long int distance; //measured distance
Unsigned char code table[ ]={"0123456789"}; //Define the character array display number
Void delay(uint n)
{
Uint x,y;
For(x=n;x>0;x--)
For(y=110;y>0;y--);
}
Void delayt(uint x)
{
Uchar j;
While(x-- > 0)
{
For(j = 0;j
{
;
}
}
}
Void lcd_wcom(uchar com)
{
Rs=0; //Select instruction register
Rw=0; //Select write
P0=com; //Put the command word into P0
Delay(5); //Time delay for a while, let 1602 prepare to receive data
En=1; //Enable line level change, command to send 1602 data port of 1602, this is very important
En=0;
}
Void lcd_wdat(uchar dat)
{
Rs=1; //Select data register
Rw=0; //Select write
P0=dat; //Put the data to be displayed into P0
Delay(5); // Delay for a little while, let 1602 prepare to receive data, that is, detect busy signals, which is very important.
En=1; //Enable line level change, data is sent to the 8-bit data port of 1602
En=0;
}
Void lcd_init()
{
Lcd_wcom(0x38); //8-bit data, double-column, 5*7-shaped, using function setting instructions
Lcd_wcom(0x0c); //Open the display, turn off the cursor, the cursor does not flash, use the display switch control command
Lcd_wcom(0x06); //Display address increment, that is, after writing a data, the display position is shifted to the right by one bit, and the write mode setting instruction is used.
Lcd_wcom (0x01); / / clear screen, used to clear the screen command
}
Void lcd_xianshi()
{
Lcd_wcom(0x80+0x40);
Lcd_wdat("D");
Lcd_wdat("i");
Lcd_wdat("s");
Lcd_wdat("t");
Lcd_wdat("a");
Lcd_wdat("n");
Lcd_wdat("c");
Lcd_wdat("e");
Lcd_wdat(":");
Lcd_wcom(0x80+0x4c);
Lcd_wdat(".");
Lcd_wcom (0x80+0x4e); / / unit is cm / /
Lcd_wdat("c");
Lcd_wdat("m");
}
Void init_t0()
{
TMOD=0x01;
TL0=0x66;
TH0=0xfc; //1ms
ET0=1;
EA=1;
}
Void trigger()
{
Trig=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Trig=1;
}
Void init_measuring()
{
Trig=1;
Echo=1;
Count=0;
}
Void measuring()
{
Uchar l;
Uint h,y;
TR0 = 1;
While(echo==1)
{
;
}
TR0 = 0;
l = TL0;
h = TH0;
y = (h
y = y - 0xfc66; / / us part
Distance = y + 1000 * count; / / calculate the total time in microseconds
TL0 = 0x66;
TH0 = 0xfc;
Delayt(30);
Distance = 3453* distance / 20000; / / original: (0.34 mm / us) * time / 2 / /
}
Void display(uint x)
{
Uchar qian, bai, shi, ge;
Qian=x/1000;
Bai=(x/100);
Shi=(x/10);
Ge=x;
Lcd_wcom (0x80+0x49); / / unit is cm / /
Lcd_wdat(table[qian]);
Lcd_wdat(table[bai]);
Lcd_wdat(table[shi]);
Lcd_wcom(0x80+0x4d);
Lcd_wdat(table[ge]);
}
Void main()
{ lcd_init(); //LCD initialization
Init_t0(); //Timer 0 initialization
Init_measuring(); //ultrasound corresponding port initialization
While(1)
{
Lcd_xianshi (); / / LCD display specific characters
Trigger(); //trigger ultrasonic start
While(echo==0) //waiting for echo
{
;
}
Measuring(); //Measure the distance
Display(distance); //Show the measurement results
Init_measuring(); //ultrasound corresponding port initialization
Delayt(600); //60ms per measurement interval
}
}
//............................................. Interrupt service function......................................................//
Void T_0()interrupt 1
{
TF0 = 0;
TL0 = 0x66;
TH0 = 0xfc;
Count++;
If(count==18)
{
TR0 =0;
TL0 = 0x66;
TH0 = 0xfc;
Count = 0;
}
}

Angled Finish Nail

Angled Finish Nail,Resists Moisture Nail,Hardware Angled Finish Staple,Reliable 0.087 Inch Nail

Zhejiang Best Nail Industrial Co., Ltd. , https://www.beststaple.com