找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机自行车测速系统的设计 12232液晶显示

[复制链接]
ID:419294 发表于 2018-11-22 18:47 | 显示全部楼层 |阅读模式
主要测量速度、里程、行驶时间及温度。利用的是STC89C52、霍尔传感器、矩阵键盘。

电路原理图如下:
0.jpg
单片机源程序:
  1. #include <REGX52.H>
  2. #include "12232.h"
  3. #include "stc_eep.h"

  4. sbit LCD_bk = P2^4;
  5. sbit KEY1 = P1^3;
  6. sbit KEY2 = P1^4;
  7. sbit KEY3 = P1^5;

  8. volatile unsigned int t_counter=0;
  9. volatile unsigned char int_flag=0,t0_flag=0;

  10. void t0_srv(void) interrupt 1 using 1
  11. {
  12.         TR0 = 0;
  13.         ET0 = 0;

  14.         t_counter++;
  15.         t0_flag = 1;

  16.         if(t_counter<5)
  17.         {
  18.                 IE0 = 0;
  19.         }
  20.         if(t_counter==5)
  21.         {
  22.                 EX0 = 1;         //50ms后开int0中断, 防抖动.
  23.         }

  24.         TH0 = 216;                 //(65536-10000)/256
  25.         //TL0 = 240;                 //(65536-10000)%256
  26.         TL0 = 255;

  27.         TR0 = 1;
  28.         ET0 = 1;
  29. }

  30. void int0_srv(void) interrupt 0        using 0
  31. {
  32.         EX0 = 0;
  33.         int_flag = 1;
  34. }

  35. void delay_nms(unsigned int count)
  36. {
  37.         unsigned int i,j;
  38.         for(i=0;i<count;i++)
  39.         for(j=0;j<45;j++);        
  40. }

  41. void main(void)
  42. {
  43.         unsigned char buff[]=(" 0.000km");
  44.         unsigned char buff_2[]=(" 000.0km");
  45.         unsigned char buff_1[]=("11");
  46.         unsigned char sec=0,min=0,hour=0,counter=0;
  47.         unsigned char time_buff[]=("00:00:00");
  48.         unsigned int temp=0;
  49.         unsigned int speed=0;
  50.         unsigned int once_counter=0;        //单次骑行计数
  51.         unsigned char all_counter_1,all_counter_2,all_counter_3;
  52.         unsigned long all_counter=0,temp1=0;

  53.         EA  = 0;               
  54.         IT0 = 1;                //int0 下降沿中断
  55.         EX0 = 1;                //允许外部中断0
  56.         TMOD = 0x09;        //timer0 工作方式1 10ms溢出
  57.         TH0 = 216;                 //(65536-10000)/256
  58.         TL0 = 240;                 //(65536-10000)%256
  59.         TR0 = 1;
  60.         ET0 = 1;
  61.         EA         = 1;
  62.                         
  63.     LCD_init();

  64.         LCD_bk = 0;
  65.     clrscr(0x00);

  66.     //drawascii(10,0,"speed meater v0.1");
  67. //----------------------------------------------
  68. //--大字符显示格式------------------------------
  69. //*
  70.         draw_very_big_ascii(0,"12");
  71.         draw_big_ascii(47,2,".3");
  72.         drawascii(73,3," KM/H");
  73.         drawascii(73,0,"00.000km");
  74.         drawascii(73,1,"0000.0km");
  75.         drawascii(73,2,"00:00:00");
  76. //*/
  77. //----------------------------------------------
  78. //--小字符显示格式------------------------------
  79. /*
  80.         drawascii(0,0,"SPD 12.3KM/H");
  81.         drawascii(48,0," KM/H");
  82.         drawascii(0,2,"max 29.8 | avg 13.2 ");
  83.         drawascii(0,3,"12.345KM | 1234.56KM");
  84. */
  85.         //STC_BYTE_program(0x20,0x20,0x1f);

  86. //        if(STC_BYTE_read(0x20,0x20)==0x1f)
  87. //        P1=0xf0;
  88. /*
  89. STC_SECTOR_erase(0x20,0x00);

  90. STC_BYTE_program(0x20,0x21,00);
  91. STC_BYTE_program(0x20,0x22,00);
  92. STC_BYTE_program(0x20,0x23,00);
  93. */

  94.                 if((KEY1 == 0)&&(KEY2 == 0))
  95.                 {
  96.                         delay_nms(20);
  97.                         if((KEY1 == 0)&&(KEY2 == 0))
  98.                         {
  99.                                 drawascii(73,3,"CLEAR");
  100.                                 while((KEY1 == 0)&&(KEY2 == 0));
  101.                                 STC_SECTOR_erase(0x20,0x00);
  102.                                 STC_BYTE_program(0x20,0x21,0);
  103.                                 STC_BYTE_program(0x20,0x22,0);
  104.                                 STC_BYTE_program(0x20,0x23,0);

  105.                                 delay_nms(4000);
  106.                                 drawascii(73,3," KM/H  ");
  107.                         }
  108.                 }

  109.         all_counter_1 = STC_BYTE_read(0x20,0x21);
  110.         all_counter_2 = STC_BYTE_read(0x20,0x22);
  111.         all_counter_3 = STC_BYTE_read(0x20,0x23);

  112.         all_counter = (unsigned long)all_counter_1<<16 | (unsigned long)all_counter_2<<8 | (unsigned long)all_counter_3 ;


  113.                         temp1 = all_counter *2;               
  114.                         buff_2[0] = temp1/1000000 + '0';
  115.                         buff_2[1] = temp1%1000000/100000 + '0';         
  116.                         buff_2[2] = temp1%100000/10000 + '0';
  117.                         buff_2[3] = temp1%10000/1000 + '0';
  118.                         buff_2[5] = temp1%1000/100 + '0';
  119.                         drawascii(73,1,buff_2);

  120.     while(1)
  121.         {
  122.                 if(t0_flag == 1)
  123.                 {
  124.                         t0_flag = 0;
  125.                         counter++;
  126.                         if(counter == 100)
  127.                         {
  128.                                 counter = 0;
  129.                                 sec ++;
  130.                                 if(sec == 60)
  131.                                 {
  132.                                         sec = 0;
  133.                                         min ++;
  134.                                         if(min == 60)
  135.                                         {
  136.                                                 min = 0;
  137.                                                 hour ++;
  138.                                         }
  139.                                 }
  140.                                 time_buff[0]=hour/10+'0';
  141.                                 time_buff[1]=hour%10+'0';
  142.                                 time_buff[3]=min/10+'0';
  143.                                 time_buff[4]=min%10+'0';
  144.                                 time_buff[6]=sec/10+'0';
  145.                                 time_buff[7]=sec%10+'0';
  146.                                 drawascii(73,2,time_buff);
  147.                         }
  148.                 }

  149.                 if(t_counter>1000)
  150.                 {
  151.                         t_counter = 0;
  152.                         //drawascii(0,1,"time out");
  153.                 }
  154.                 if(KEY3 == 0)
  155.                 {
  156.                         delay_nms(20);
  157.                         if(KEY3 == 0)
  158.                         {
  159.                                 while(KEY3==0);
  160.                                 LCD_bk = !LCD_bk;
  161.                         }
  162.                 }
  163.                 if(KEY1 == 0)
  164.                 {
  165.                         delay_nms(20);
  166.                         if(KEY1 == 0)
  167.                         {
  168.                                 while(KEY1==0);
  169.                                 drawascii(73,3,"WRITED");

  170.                                 STC_SECTOR_erase(0x20,0x00);
  171.                                 STC_BYTE_program(0x20,0x21,(unsigned char)all_counter>>16);
  172.                                 STC_BYTE_program(0x20,0x22,(unsigned char)all_counter>>8);
  173.                                 STC_BYTE_program(0x20,0x23,(unsigned char)all_counter);

  174.                                 delay_nms(2000);
  175.                                 drawascii(73,3," KM/H  ");
  176.                         }
  177.                 }
  178.                         
  179.                 if(int_flag==1)
  180.                 {
  181.                         int_flag = 0;
  182.                         temp = t_counter;
  183.                         t_counter = 0;
  184.                         /*
  185.                         buff[0] = temp/1000 + '0';
  186.                         buff[1] = temp%1000/100 + '0';         
  187.                         buff[2] = temp%100/10 + '0';
  188.                         buff[3] = temp%10 + '0';
  189.                         //drawascii(0,1,buff);
  190.                         */

  191.                         speed = 7351/temp;                                                                           //显示速度
  192.                         buff_1[0] = speed/100 + '0';
  193.                         buff_1[1] = speed%100/10 + '0';         
  194.                         draw_very_big_ascii(0,buff_1);
  195.                         buff_1[0] = '.';
  196.                         buff_1[1] = speed%10 + '0';         
  197.                         draw_big_ascii(47,2,buff_1);

  198.                         once_counter++;        
  199.                         all_counter++;                                                                                    //显示距离
  200.                         temp = (unsigned long)once_counter * 2040 /1000;                //轮胎周长2040mm
  201.                         buff[0] = temp/10000 + '0';
  202.                         buff[1] = temp%10000/1000 + '0';         
  203.                         buff[3] = temp%1000/100 + '0';
  204.                         buff[4] = temp%100/10 + '0';
  205.                         buff[5] = temp%10 + '0';
  206.                         drawascii(73,0,buff);

  207.                         temp1 = all_counter *2;               
  208.                         buff_2[0] = temp1/1000000 + '0';
  209.                         buff_2[1] = temp1%1000000/100000 + '0';         
  210.                         buff_2[2] = temp1%100000/10000 + '0';
  211.                         buff_2[3] = temp1%10000/1000 + '0';
  212.                         buff_2[5] = temp1%1000/100 + '0';
  213.                         drawascii(73,1,buff_2);
  214.                 }
  215.         }


  216. }

复制代码
全部资料51hei下载地址:
自行车测速(霍尔).rar (90.44 KB, 下载次数: 81)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:489118 发表于 2019-3-12 12:03 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:98992 发表于 2019-12-6 16:19 | 显示全部楼层
我也想玩玩看 怎么弄的
回复

使用道具 举报

ID:65237 发表于 2020-6-17 20:06 | 显示全部楼层
谢谢分享
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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