找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5007|回复: 2
收起左侧

自行车测速度的单片机仿真怎么调整频率?

[复制链接]
ID:213546 发表于 2017-6-21 23:08 | 显示全部楼层 |阅读模式
自行车测速度仿真图如下:
0.png 0.png

单片机源程序如下:
  1. #include "d:\c51\reg51.h"
  2. #include "d:\c51\intrins.h"


  3. sbit LCM_RS=P3^0;
  4. sbit LCM_RW=P3^1;
  5. sbit LCM_EN=P3^7;

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

  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()
  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. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
自行车测速仿真.7z (27.34 KB, 下载次数: 52)
回复

使用道具 举报

ID:308395 发表于 2018-4-27 11:35 | 显示全部楼层
楼主,你的压缩包下载不了
回复

使用道具 举报

ID:641779 发表于 2019-11-17 19:20 | 显示全部楼层
你好,我想问下这个应该怎么打开?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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