找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2021|回复: 3
收起左侧

自行车里程计算单片机仿真程序遇到问题求大神帮助!

[复制链接]
ID:251924 发表于 2017-11-21 16:18 | 显示全部楼层 |阅读模式
自行车速度已经显示好了,就是里程不会写程序里没有,求助

QQ图片20171121161817.png
程序在下面 求大神帮助


  1. #include <reg51.h>
  2. #include <intrins.h>

  3. sbit LCM_RS=P3^0; //液晶显示模块
  4. sbit LCM_RW=P3^1;//P3口来控制LCM的值
  5. sbit LCM_EN=P3^7;

  6. #define BUSY                  0x80              //常量定义
  7. #define DATAPORT         P0 //如上
  8. #define uchar                 unsigned char
  9. #define uint                   unsigned int
  10. #define L                        50 //50就是L

  11. uchar str0[16],str1[16],count;// 数组
  12. uint speed; //速度显示
  13. unsigned long time;//

  14. void ddelay(uint);
  15. void lcd_wait(void);
  16. void display();
  17. void initLCM();
  18. void WriteCommandLCM(uchar WCLCM,uchar BusyC);
  19. void STR();
  20. void account();


  21. /*********延时K*1ms,12.000mhz**********/

  22. void int0_isr(void) interrupt 0         /*遥控使用外部中断0,接P3.2口*/
  23. {
  24.     unsigned int temp;
  25.         time=count;
  26.                                 TR0=0;
  27.         temp=TH0;
  28.         temp=((temp << 8) | TL0);
  29.     TH0=0x3c;
  30.     TL0=0xaf;
  31.         count=0;
  32.     TR0=1;
  33.         time=time*50000+temp;
  34. }

  35. void time0_isr(void) interrupt 1        /*遥控使用定时计数器1 */
  36. {
  37.    TH0 =0x3c;
  38.    TL0 =0xaf;
  39.    count++;
  40. }

  41. void main(void)
  42. {
  43.            TMOD=0x01;                       /*TMOD T0选用方式1(16位定时) */
  44.     IP|=0x01;                           /*INT0 中断优先*/
  45.     TCON|=0x11;                         /*TCON  EX0下降沿触发,启动T0*/
  46.     IE|=0x83;  
  47.     TH0=0x3c;
  48.     TL0=0xaf;
  49.   
  50.         initLCM();
  51.            WriteCommandLCM(0x01,1);                    //清显示屏
  52.         for(;;)
  53.         {
  54.                 account();
  55.                 display();
  56.         }
  57. }

  58. void account()
  59. {
  60.         unsigned long a;
  61.         if (time!=0)
  62.         {
  63.                 a=L*360000000/time;
  64.         }
  65.         speed=a;
  66. }



  67. void STR() //速度LCD设计
  68. {
  69.         str0[0]='S';
  70.         str0[1]='p';
  71.         str0[2]='e';
  72.     str0[3]='e';
  73.         str0[4]='d';
  74.         str0[5]=' ';        
  75.         str0[6]=(speed%100000)/10000+0x30;
  76.         str0[7]=(speed%10000)/1000+0x30;
  77.         str0[8]=(speed%1000)/100+0x30;
  78.         str0[9]='.';
  79.         str0[10]=(speed%100)/10+0x30;
  80.         str0[11]=speed%10+0x30;
  81.         str0[12]='k';
  82.         str0[13]='m';
  83.         str0[14]='/';
  84.         str0[15]='h';
  85. }

  86. void ddelay(uint k)
  87. {
  88.     uint i,j;
  89.     for(i=0;i<k;i++)
  90.     {
  91.         for(j=0;j<60;j++)
  92.                 {;}
  93.     }
  94. }
  95. /**********写指令到lcd子函数************/

  96. void WriteCommandLCM(uchar WCLCM,uchar BusyC)
  97. {
  98.     if(BusyC)lcd_wait();
  99.         DATAPORT=WCLCM;
  100.     LCM_RS=0;                   /* 选中指令寄存器*/
  101.     LCM_RW=0;                       // 写模式
  102.     LCM_EN=1;
  103.         _nop_();
  104.         _nop_();
  105.         _nop_();
  106.     LCM_EN=0;

  107. }

  108. /**********写数据到LCD子函数************/

  109. void WriteDataLCM(uchar WDLCM)
  110. {
  111.     lcd_wait( );            //检测忙信号
  112.         DATAPORT=WDLCM;
  113.     LCM_RS=1;               /* 选中数据寄存器  */
  114.     LCM_RW=0;                   // 写模式
  115.     LCM_EN=1;
  116.     _nop_();
  117.         _nop_();
  118.         _nop_();
  119.     LCM_EN=0;
  120. }

  121. /***********lcd内部等待函数*************/

  122. void lcd_wait(void)
  123. {
  124.     DATAPORT=0xff;             //读LCD前若单片机输出低电平,而读出LCD为高电平,则冲突,Proteus就会显示逻辑黄色
  125.         LCM_EN=1;
  126.     LCM_RS=0;
  127.     LCM_RW=1;
  128.     _nop_();
  129.     _nop_();
  130.         _nop_();
  131.     while(DATAPORT&BUSY)
  132.         {  LCM_EN=0;
  133.            _nop_();
  134.            _nop_();
  135.            LCM_EN=1;
  136.            _nop_();
  137.            _nop_();
  138.         }
  139.            LCM_EN=0;

  140. }

  141. /**********LCD初始化子函数***********/
  142. void initLCM( )
  143. {
  144.         DATAPORT=0;
  145.         ddelay(15);
  146.         WriteCommandLCM(0x38,0);    //三次显示模式设置,不检测忙信号
  147.     ddelay(5);
  148.     WriteCommandLCM(0x38,0);
  149.     ddelay(5);
  150.     WriteCommandLCM(0x38,0);
  151.     ddelay(5);

  152.     WriteCommandLCM(0x38,1);    //8bit数据传送,2行显示,5*7字形,检测忙信号
  153.     WriteCommandLCM(0x08,1);    //关闭显示检测忙信号
  154.     WriteCommandLCM(0x01,1);    //清屏检测忙信号
  155.     WriteCommandLCM(0x06,1);    //显示光标右移设置,检测忙信号
  156.     WriteCommandLCM(0x0c,1);    //显示屏打开,光标不显示,不闪烁,检测忙信号
  157. }

  158. /****显示指定坐标的一个字符子函数****/

  159. void DisplayOneChar(uchar X,uchar Y,uchar DData)
  160. {
  161.     Y&=1;
  162.     X&=15;
  163.     if(Y)X|=0x40;               //若y为1(显示第二行),地址码+0X40
  164.     X|=0x80;                    //指令码为地址码+0X80
  165.     WriteCommandLCM(X,0);
  166.     WriteDataLCM(DData);
  167. }

  168. /*******显示指定坐标的一串字符子函数*****/

  169. void DisplayListChar(uchar X,uchar Y,uchar *DData)
  170. {
  171.     uchar ListLength=0;
  172.     Y&=0x01;
  173.     X&=0x0f;
  174.     while(X<16)
  175.     {
  176.         DisplayOneChar(X,Y,DData[ListLength]);
  177.         ListLength++;
  178.         X++;
  179.     }
  180. }


  181. void display()
  182. {

  183.         STR();
  184.         DisplayListChar(0,0,str0);
  185.            DisplayListChar(0,1,str1);
  186. }
复制代码



回复

使用道具 举报

ID:251924 发表于 2017-11-21 16:20 | 显示全部楼层
刚刚程序有乱码我重新发一个


回复

使用道具 举报

ID:164602 发表于 2017-11-22 09:26 | 显示全部楼层
给你两个建议:
第一:你的速度已经有了,那么,里程就太太......太简单了,找时间啊,S=Vt,初中就知道的。
这个时间可不能用定时器啊,定时器的时间太短了,用1302之类的时间芯片好了——确定计时开始时刻,确定扫描时间,即多久显示更新一次里程,就可以算里程了。
第二:如果上面方法的程序太难(我想想都觉得麻烦),那就用简单方法了。在自行车轮子了装一个光电传感器,轮子转动一周,计数一次,这个里程就是轮子周长,确定转动几周显示更新一次,就可以有里程数据了。可以用外部中断接收这个光电传感器的数据,这样可以计数的最大值,可以由程序设定,而不是定时/计数器,定时器的最大值有限制。
回复

使用道具 举报

ID:249916 发表于 2017-11-22 20:50 | 显示全部楼层
我也在做这个,才开始,请教一下霍尔元件怎么产生高低电平的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表