Microcontroller measuring bicycle speed program - with simulation

Publisher:cyzceeLatest update time:2015-06-26 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This is a small project using 51 single chip microcomputer to measure the speed of bicycle.

 Please download the circuit diagram and simulation files from  http://www.51hei.com/bbs/dpj-20396-1.html
The following is the program source code:
#include "d:c51 eg51.h"
#include "d: c51 intrins.h"


sbit LCM_RS=P3^0;
sbit LCM_RW=P3^1;
sbit LCM_EN=P3^7;

#define BUSY 0x80 //constant definition
#define DATAPORT P1
#define uchar unsigned char
#define uint unsigned int
#define L 50

uchar str0[16],str1[16],count;
uint speed;
unsigned long time;

void ddelay(uint);
void lcd_wait(void);
void display();
void initLCM();
void WriteCommandLCM(uchar WCLCM,uchar BusyC);
void STR();
void account();


/*********Delay K*1ms,12.000mhz**********/

void int0_isr(void) interrupt 0 /*Remote control uses external interrupt 0, connected to P3.2 port*/
{
    unsigned int temp;
 time=count;
    TR0=0;
 temp=TH0;
 temp=((temp << 8) | TL0);
    TH0=0x3c;
    TL0=0xaf;
 count=0;
    TR0=1;
 time=time*50000+temp;
}

void time0_isr(void) interrupt 1 /* Remote control uses timer counter 1 */
{
   TH0 =0x3c;
   TL0 =0xaf;
   count++;
}

void main(void)
{
    TMOD=0x01; /*TMOD T0 selects mode 1 (16-bit timing) */
    IP|=0x01; /*INT0 interrupt priority*/
    TCON|=0x11; /*TCON EX0 falling edge trigger, start T0*/
    IE|=0x83;  
    TH0=0x3c;
    TL0=0xaf;
  
 initLCM();
    WriteCommandLCM(0x01,1); //Clear the display
 for(;;)
 {
  account();
  display();
 }
}

void account()
{
 unsigned long a; 
 if (time!=0)
 {
  a=L*360000000/time;
 }
 speed=a;
}

 

void STR()
{
 str0[0]='S';
 str0[1]='p';
 str0[2]='e';
    str0[3]='e';
 str0[4]='d';
 str0[5]=' '; 
 str0[6]=(speed%100000)/10000+0x30;
 str0[7]=(speed%10000)/1000+0x30;
 str0[8]=(speed%1000)/100+0x30;
 str0[9]='.';
 str0[10]=(speed%100)/10+0x30;
 str0[11]=speed%10+0x30;
 str0[12]='k';
 str0[13]='m';
 str0[14]='/';
 str0[15]='h';
}

void ddelay(uint k)
{
    uint i,j;
    for(i=0;i     {
        for(j=0;j<60;j++)
         {;}
    }
}[page]
/**********Write instructions to LCD sub-function************/

void WriteCommandLCM(uchar WCLCM,uchar BusyC)
{
    if(BusyC)lcd_wait();
 DATAPORT=WCLCM;
    LCM_RS=0; /* Select command register*/
    LCM_RW=0; // Write mode
    LCM_EN=1;
 _nop_();
 _nop_();
 _nop_();
    LCM_EN=0;

}

/**********Write data to LCD sub-function************/

void WriteDataLCM(uchar WDLCM)
{
    lcd_wait() ); // Detect busy signal
 DATAPORT=WDLCM;
    LCM_RS=1; /* Select data register   */
    LCM_RW=0; // Write mode
    LCM_EN=1;
    _nop_();
 _nop_();
 _nop_();
    LCM_EN=0;
}

/***********lcd internal waiting function*************/

void lcd_wait(void)
{ DATAPORT=0xff; //If the MCU outputs a low level
    before reading LCD , and the LCD is read as a high level, there will be a conflict, and the Proteus simulation will display the logic yellow  LCM_EN=1;     LCM_RS=0;     LCM_RW=1;     _nop_();     _nop_();  _nop_();     while(DATAPORT&BUSY)  { LCM_EN=0;     _nop_();     _nop_();     LCM_EN=1;     _nop_();     _nop_();  }     LCM_EN=0;














}

/**********LCD initialization sub-function***********/
void initLCM()
{
 DATAPORT=0;
 ddelay(15);
 WriteCommandLCM(0x38,0); //Three display mode settings, do not detect busy signal
    ddelay(5);
    WriteCommandLCM(0x38,0);
    ddelay(5);
    WriteCommandLCM(0x38,0);
    ddelay(5);

    WriteCommandLCM(0x38,1); //8-bit data transmission, 2-line display, 5*7 font, detect busy signal
    WriteCommandLCM(0x08,1); //Display off, detect busy signal
    WriteCommandLCM(0x01,1); //Clear screen, detect busy signal
    WriteCommandLCM(0x06,1); //Display cursor right shift setting, detect busy signal
    WriteCommandLCM(0x0c,1); // Display screen is turned on, cursor is not displayed, does not flash, detect busy signal
}

/****Display a character subfunction at the specified coordinate****/

void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
    Y&=1;
    X&=15;
    if(Y)X|=0x40; //If y is 1 (display the second line), address code + 0X40
    X|=0x80; //The instruction code is address code + 0X80
    WriteCommandLCM(X,0);
    WriteDataLCM(DData);
}

/*******Display a string of characters at the specified coordinates subfunction*****/

void DisplayListChar(uchar X,uchar Y,uchar *DData)
{
    uchar ListLength=0;
    Y&=0x01;
    X&=0x0f;
    while(X<16)
    {
        DisplayOneChar(X,Y,DData[ListLength]);
        ListLength++;
        X++;
    }
}


void display()
{

 STR(); 
 DisplayListChar(0,0,str0);
    DisplayListChar(0,1,str1);
}

Reference address:Microcontroller measuring bicycle speed program - with simulation

Previous article:ds1302 clock program controlled by microcontroller with alarm function
Next article:51 MCU drives ds1302 program (12864 LCD display)

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号