Bicycle speed and distance measurement program based on 51 single chip microcomputer

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

The actual picture is as follows:

The circuit diagram is as follows:

The microcontroller source program is as follows:
#include
#include
#define CIRCLE 2.0 //Macro definition of the circumference of the wheel (this should be set according to the actual wheel)
sbit Signal = P1^0; //The Signal here represents the signal pin of the Hall sensor
int m_second=0; //Define variable m_second to record time (in milliseconds)
float speed=0.0 ; //Define speed variable
float length=0.0 ;//Define distance variable
void main()
{
        lcd_init(); //Initialize LCD function
        TMOD = 0x01; //Turn on timer 0, and set its working mode to 16-bit timing mode.
        TH0=(65536-10000)/ 256;
        TL0=(65536-10000)% 256; //Set the initial value of the timer so that it interrupts every 10ms
        EA = 1; //Allow total interrupt
        ET0 = 1; //Allow timer 0 terminal
        TR0 = 1; //Start timer 0
while(1) //Large loop
{
    while(Signal); //Wait for the Hall sensor signal line to be pulled low;
        speed = CIRCLE *1000/ m_second ; //Calculate speed.
        m_second = 0; //Reset the timer
        length+=CIRCLE ; //Distance plus one wheel cycle
                        //First line, display speed
        lcd_pos(0x00); //Set the LCD writing position to the first line and the first grid
        lcd_wdat('S');
        lcd_wdat('p');
        lcd_wdat('e');
        lcd_wdat('e');
        lcd_wdat('d');
        lcd_wdat(':');
        lcd_wdat((int)speed/10+0x30); //Display the integer part of the speed
        lcd_wdat((int)speed%10+0x30);
        lcd_wdat('.');
        lcd_wdat((int)(speed*10)%10+0x30 ); //Display the first decimal place of the speed lcd_wdat
        ('m');
        lcd_wdat('/');
        lcd_wdat('s');
         
                        //The second line, display mileage
        lcd_pos(0x40); //Set the writing position of the LCD to the first grid of the second line
        lcd_wdat('L');
        lcd_wdat('e')
        ;
        lcd_wdat('n'); lcd_wdat('g');
        lcd_wdat('t')
        ; lcd_wdat('h');
        lcd_wdat(':');
        lcd_wdat((int)length /10000+0x30); //Display the ten thousandth place of mileage;
        lcd_wdat((int)length %10000/1000+0x30);//Display the thousandth place of mileage;
        lcd_wdat((int)length %1000/100+0x30); //Display the hundredth place of mileage;
        lcd_wdat((int)length %100/10+0x30); //Display the tens digit of mileage;
        lcd_wdat((int)length %10+0x30); //Display the ones digit of mileage;
        lcd_wdat('m');
        while(!Signal);
}

}
void timer0_intt() interrupt 1 //
{
        TH0=(65536-10000)/ 256;
        TL0=(65536-10000)% 256; //Set the initial value of the timer so that it interrupts every 10ms
        m_second += 10; //Because the interrupt occurs every 10 milliseconds, 10 is added each time here;
}
#include
#include

/*********************************************************************************
Function:LCD delay subroutine
Entry parameter:ms
Exit parameter:
*********************************************************************************/
static void delay(unsigned char ms)
{
        unsigned char i;
        while(ms--)
                {
                for(i = 0; i< 5; i++);
                }
}

/********************************************************************************
Function: Test LCD busy state
Entry parameter:
Exit parameter: result
*****************************************************************************/
static bit lcd_bz()
{
        bit result;
        rs = 0;
        rw = 1;
        ep = 1;
        delay(5);
        result = (bit)(P0 & 0x80);
        ep = 0;
        return result;
}

/*****************************************************************************
Function: Write instruction data to LCD Subroutine
Entry parameter: cmd
Exit parameter:
*****************************************************************************/
static void lcd_wcmd(unsigned char cmd)
{
        while(lcd_bz()); //Judge whether LCD is busy
                rs = 0;
                rw = 0;
                ep = 0;
                delay(5);
                P0 = cmd;
                        delay(5);
                ep = 1;
                        delay(5);
                ep = 0;
}

/********************************************************************************
Function: Set display position Subroutine
entry parameter: pos
Exit parameter:
*****************************************************************************/
void lcd_pos(unsigned char pos)
{
        lcd_wcmd(pos | 0x80);
}

/*********************************************************************************
Function: Write display data to LCD Subroutine
entry parameter: dat
Exit parameter:
*****************************************************************************/
void lcd_wdat(unsigned char dat)
{
        while(lcd_bz()); //Judge whether LCD is busy
                rs = 1;
                rw = 0;
                ep = 0;
                P0 = dat;
                        delay(5);
                ep = 1;
                        delay(5);
                ep = 0;
}

/********************************************************************************
Function: LCD initialization subroutine
entry parameter:
exit parameter:
*****************************************************************************/
void lcd_init()
{
        lcd_wcmd(0x38);
        delay(100);
        lcd_wcmd(0x0c);
        delay(100);
        lcd_wcmd(0x06);
        delay(100);
        lcd_wcmd(0x01);
        delay(100);
}
/********************************************************************************
Function: LCD writes an integer data
entry parameter: int x
*****************************************************************************/
void lcd_write_int(unsigned int x)  
{
        unsigned char x1,x2,x3,x4,x5;
        x1 = x/10000;
        x2=x%10000/1000;
        x3=x%1000/100;
        x4=x%100/10;
        x5=x%10;
        lcd_wdat(x1+0x30);
        lcd_wdat(x2+0x30);
        lcd_wdat(x3+0x30);
        lcd_wdat(x4+0x30);
        lcd_wdat(x5+0x30);
}
#ifndef __STAR1602_H__
#define __STAR1602_H__

sbit rs= P2^6; //
sbit rw = P2^5; //     
sbit ep = P2^7; //


void lcd_init(); //LCD initialization function
void lcd_pos(unsigned char pos); //LCD display position setting function
void lcd_wdat(unsigned char dat); //LCD writes characters
void lcd_write_int(unsigned int x); //LCD displays an integer variable

Reference address:Bicycle speed and distance measurement program based on 51 single chip microcomputer

Previous article:MCU high efficiency code reading 18B20 displayed on LCD1602
Next article:51 MCU OLED12864 I2C interface usage tutorial

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号