找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 309|回复: 0
打印 上一主题 下一主题
收起左侧

单片机出租车计价器程序

[复制链接]
跳转到指定楼层
楼主
ID:1109011 发表于 2024-1-12 10:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是我实习做的出租车计价器,能够实现显示路程,车价等功能。
单片机源程序如下:
  1. /*
  2. 基于51单片机的出租车计价器
  3. */
  4. #define uchar unsigned char
  5. #define uint unsigned int

  6. //头函数声明
  7. #include <reg52.h>
  8. #include "Lcd.h"
  9. #include "Data.h"
  10. #include "DS1302.h"
  11. #include "AT24C02.h"


  12. //键盘定义
  13. sbit K1=P3^4;        //模式切换
  14. sbit K2=P3^6;        //减按键
  15. sbit K3=P3^5;        //加按键
  16. sbit K4=P3^7;        //万年历模式时:设置时间信息。计价器模式时:设置单价

  17. //模式0时钟 1时钟调整                                                   
  18. uchar Mode=0;
  19. //时钟屏幕调整
  20. uchar Wei=0;
  21. uchar SS=0;
  22. //价格调整
  23. uchar JGWei=0;

  24. //计价器参数
  25. uint Day=0;
  26. uint Night=0;
  27. uint DanJia=0;
  28. uint DJ_min=0;
  29. uint  ZJ=0;
  30. unsigned long  GL=0;
  31. uint  Time=0;
  32. uint count=0;
  33. uchar m=0;
  34. //白天/晚上
  35. uchar DN=0;
  36. //公里/时间
  37. uchar GT=0;

  38. //初始化函数
  39. void InitTimer0();
  40. //写初值
  41. void SETS();
  42. //读初值
  43. void READS();

  44. //主函数
  45. void main()
  46. {
  47.         //初始化
  48.         Ds1302_Init();
  49.         Init_LCD();
  50.         initeeprom();
  51.         //SETS();
  52.         //读取初始参数
  53.         READS();
  54.         //定时器初始化
  55.         InitTimer0();
  56.         //循环
  57.         while(1)
  58.         {
  59.                 //时钟模式
  60.                 if(Mode==0)
  61.                 {
  62.                         //读时间
  63.                         if(Wei==0)
  64.                         {
  65.                                 Ds1302_Read_Time();
  66.                                 //显示时间
  67.                                 display_NYR(time_buf1,Wei,0);
  68.                         }
  69.                 }
  70.                 else if(Mode==1)
  71.                 {
  72.                         if(time_buf1[4]>=23||time_buf1[4]<7)  
  73.                         DN=1;
  74.                         else
  75.                         DN=0;
  76.                         //显示计价器信息
  77.                         
  78.                         display_S(Day,Night,DanJia,ZJ,GL/10,Time,GT,DN);
  79.                 }


  80.                 if(K4==0)
  81.                 {
  82.                         delay(100);
  83.                         //调时
  84.                         if(Mode==0)
  85.                         {
  86.                                 Wei++;
  87.                                 if(Wei==1)
  88.                                 {
  89.                                         SS=1;
  90.                                 }
  91.                                 //闪烁
  92.                                 display_NYR(time_buf1,Wei,SS);
  93.                                 if(Wei==7)
  94.                                 {
  95.                                         Wei=0;
  96.                                         //保存时间
  97.                                         Ds1302_Write_Time();
  98.                                         Init_LCD();
  99.                                 }
  100.                         }
  101.                         else if(Mode==1)
  102.                         {
  103.                                 JGWei++;
  104.                                 if(JGWei==1)
  105.                                 {
  106.                                         Mode=2;
  107.                                         //显示计价器价格
  108.                                         Init_LCD_JGTZ();
  109.                                         display_D(Day,Night,DanJia,JGWei,DJ_min);
  110.                                 }
  111.                         }
  112.                         else if(Mode==2)
  113.                         {        
  114.                                 JGWei++;        
  115.                                 display_D(Day,Night,DanJia,JGWei,DJ_min);
  116.                                 if(JGWei==5)
  117.                                 {
  118.                                         Mode=1;
  119.                                         JGWei=0;
  120.                                         //参数初始化
  121.                                         //保存
  122.                                         SETS();
  123.                                         //读初值
  124.                                         READS();
  125.         
  126.                                         ZJ=0;
  127.                                         GL=0;
  128.                                         Time=0;
  129.                                         //开启定时器
  130.                                         TR0=1;
  131.                                         //显示计价器
  132.                                         Init_LCD_JJQ();
  133.                                 }
  134.                         }
  135.                         while(K4==0);
  136.                 }
  137.                 //+
  138.                 if(K3==0)
  139.                 {
  140.                         delay(100);
  141.                         //调时
  142.                         if(Mode==0)
  143.                         {
  144.                                 //年
  145.                                 if(Wei==1)
  146.                                 {
  147.                                         time_buf1[1]++;
  148.                                         if(time_buf1[1]>=100)
  149.                                                 time_buf1[1]=0;
  150.                                 }
  151.                                 //月
  152.                                 else if(Wei==2)
  153.                                 {
  154.                                         time_buf1[2]++;
  155.                                         if(time_buf1[2]>=13)
  156.                                                 time_buf1[2]=1;
  157.                                 }
  158.                                 //日
  159.                                 else if(Wei==3)
  160.                                 {
  161.                                         time_buf1[3]++;
  162.                                         if(time_buf1[3]>=YDay(time_buf1[1],time_buf1[2])+1)
  163.                                                 time_buf1[3]=1;
  164.                                 }        
  165.                                 //时
  166.                                 else if(Wei==4)
  167.                                 {
  168.                                         time_buf1[4]++;
  169.                                         if(time_buf1[4]>=24)
  170.                                                 time_buf1[4]=0;
  171.                                 }        
  172.                                 //分
  173.                                 else if(Wei==5)
  174.                                 {
  175.                                         time_buf1[5]++;
  176.                                         if(time_buf1[5]>=60)
  177.                                                 time_buf1[5]=0;
  178.                                 }
  179.                                 //秒
  180.                                 else if(Wei==6)
  181.                                 {
  182.                                         time_buf1[6]++;
  183.                                         if(time_buf1[6]>=60)
  184.                                                 time_buf1[6]=0;
  185.                                 }
  186.                                 //显示
  187.                                 if(Wei!=0)
  188.                                         display_NYR(time_buf1,Wei,SS);                        
  189.                         }
  190.                         //调价
  191.                         else if(Mode==2)
  192.                         {
  193.                                 //单价
  194.                                 if(JGWei==1)
  195.                                 {
  196.                                         DanJia++;
  197.                                         if(DanJia>999)
  198.                                         {
  199.                                                 DanJia=1;
  200.                                         }
  201.                                 }
  202.                                 //等待时间单价
  203.                                 else if(JGWei==2)
  204.                                 {
  205.                                         DJ_min++;
  206.                                         if(DJ_min>999)
  207.                                         {
  208.                                                 DJ_min=1;
  209.                                         }
  210.                                 }
  211.                                 //夜间
  212.                                 else if(JGWei==3)
  213.                                 {
  214.                                         Night++;
  215.                                         if(Night>999)
  216.                                         {
  217.                                                 Night=1;
  218.                                         }
  219.                                 }
  220.                                 //白天
  221.                                 else if(JGWei==4)
  222.                                 {
  223.                                         Day++;
  224.                                         if(Day>999)
  225.                                         {
  226.                                                 Day=1;
  227.                                         }
  228.                                 }
  229.                                 display_D(Day,Night,DanJia,JGWei,DJ_min);
  230.                         }

  231.                         while(K3==0);
  232.                 }

  233.                 //-
  234.                 if(K2==0)
  235.                 {
  236.                         delay(100);
  237.                         //调时
  238.                         if(Mode==0)
  239.                         {
  240.                                 //年
  241.                                 if(Wei==1)
  242.                                 {
  243.                                         time_buf1[1]--;
  244.                                         if(time_buf1[1]<0)
  245.                                                 time_buf1[1]=99;
  246.                                 }
  247.                                 //月
  248.                                 else if(Wei==2)
  249.                                 {
  250.                                         time_buf1[2]--;
  251.                                         if(time_buf1[2]<=0)
  252.                                                 time_buf1[2]=12;
  253.                                 }
  254.                                 //日
  255.                                 else if(Wei==3)
  256.                                 {
  257.                                         time_buf1[3]--;
  258.                                         if(time_buf1[3]<=0)
  259.                                                 time_buf1[3]=YDay(time_buf1[1],time_buf1[2]);
  260.                                 }        
  261.                                 //时
  262.                                 else if(Wei==4)
  263.                                 {
  264.                                         time_buf1[4]--;
  265.                                         if(time_buf1[4]<0)
  266.                                                 time_buf1[4]=23;
  267.                                 }        
  268.                                 //分
  269.                                 else if(Wei==5)
  270.                                 {
  271.                                         time_buf1[5]--;
  272.                                         if(time_buf1[5]<0)
  273.                                                 time_buf1[5]=59;
  274.                                 }
  275.                                 //秒
  276.                                 else if(Wei==6)
  277.                                 {
  278.                                         time_buf1[6]--;
  279.                                         if(time_buf1[6]<0)
  280.                                                 time_buf1[6]=59;
  281.                                 }
  282.                                 //显示
  283.                                 if(Wei!=0)
  284.                                         display_NYR(time_buf1,Wei,SS);                        
  285.                         }
  286.                         //调价
  287.                         else if(Mode==2)
  288.                         {
  289.                                 //单价
  290.                                 if(JGWei==1)
  291.                                 {
  292.                                         DanJia--;
  293.                                         if(DanJia==0)
  294.                                         {
  295.                                                 DanJia=999;
  296.                                         }
  297.                                 }
  298.                                 //等待时间单价
  299.                                 else if(JGWei==2)
  300.                                 {
  301.                                         DJ_min--;
  302.                                         if(DJ_min==0)
  303.                                         {
  304.                                                 DJ_min=999;
  305.                                         }
  306.                                 }
  307.                                 //夜间
  308.                                 else if(JGWei==3)
  309.                                 {
  310.                                         Night--;
  311.                                         if(Night==0)
  312.                                         {
  313.                                                 Night=999;
  314.                                         }
  315.                                 }
  316.                                 //白天
  317.                                 else if(JGWei==4)
  318.                                 {
  319.                                         Day--;
  320.                                         if(Day==0)
  321.                                         {
  322.                                                 Day=999;
  323.                                         }
  324.                                 }
  325.                                 display_D(Day,Night,DanJia,JGWei,DJ_min);
  326.                         }
  327.                         while(K2==0);
  328.                 }        
  329.                 //切换
  330.                 if(K1==0)
  331.                 {
  332.                         delay(100);
  333.                         //计价器模式
  334.                         if(Mode==1)
  335.                         {
  336.                                 Mode=0;
  337.                                 ZJ=0;
  338.                                 GL=0;
  339.                                 Time=0;
  340.                                 //开启定时器
  341.                                 TR0=0;
  342.                                 EX0=0;
  343.                                 //显示计价器
  344.                                 Init_LCD();
  345.                         }
  346.                         else if(Mode==0)
  347.                         {
  348.                                 Mode=1;
  349.                                 //参数初始化
  350.                                 //读初值
  351.                                 READS();
  352.                                 ZJ=0;
  353.                                 GL=0;
  354.                                 Time=0;
  355.                                 //开启定时器
  356.                                 m=0;
  357.                                 TR0=1;
  358.                                 EX0=1;
  359.                                 //显示计价器
  360.                                 Init_LCD_JJQ();
  361.                         }
  362.                         while(K1==0);
  363.                 }
  364.         }
  365. }

  366. //初始化函数
  367. void InitTimer0(void)
  368. {
  369.     TMOD = 0x01;
  370.     TH0 = 0x3C;
  371.     TL0 = 0xB0;                //10ms
  372.     EA = 1;
  373.     ET0 = 1;
  374.     TR0 = 1;
  375.         IT0=1;        //INT0负跳变触发
  376.         EX0=0;//开外部INTO中断
  377. }

  378. //定时器0中断10ms
  379. void Timer0Interrupt(void) interrupt 1
  380. {
  381.     TH0 = 0x3C;
  382.     TL0 = 0xB0;   //赋初值
  383.         m++;
  384.         if(m>=20)
  385.         {
  386.                 m=0;
  387.                 if(count>=60)
  388.                 {
  389.                         GT=0;
  390.                         GL=GL+count/10;
  391.                         count=0;
  392.                 }
  393.                 else
  394.                 {
  395.                         count=0;
  396.                         GT=1;
  397.                         Time++;         //时间++
  398.                 }
  399.         }
  400.          //计算钱数
  401.          //公里数小于3
  402.          if(GL<=300)
  403.          {
  404.                 if(DN==0)
  405.                 {
  406.                         if(Time>60)
  407.                                 ZJ=Day+Time/60*DJ_min;
  408.                         else
  409.                                 ZJ=Day;
  410.                 }        
  411.                 else
  412.                 {
  413.                         if(Time>60)
  414.                                 ZJ=Night+Time/60*DJ_min;
  415.                         else
  416.                                 ZJ=Night;
  417.                 }
  418.          }
  419.          else
  420.          {
  421.                 if(DN==0)
  422.                 {
  423.                         if(Time>60)
  424.                                 ZJ=Day+(GL/10-20)/10*DanJia+Time/60*DJ_min;
  425.                         else
  426.                                 ZJ=Day+(GL/10-20)/10*DanJia;

  427.                 }        
  428.                 else
  429.                 {
  430.                         if(Time>60)
  431.                                 ZJ=Night+(GL/10-20)/10*DanJia+Time/60*DJ_min;
  432.                         else
  433.                                 ZJ=Night+(GL/10-20)/10*DanJia;
  434.                 }
  435.          }
  436. }
  437. void EXINT0() interrupt 0
  438. {
  439.         count++;
  440. }

  441. //读初值
  442. void READS()
  443. {
  444.         uchar SH=0,SL=0;
  445.         uint temp=0;

  446.         delay(10);
  447.         SL=read_add(0x01);
  448.         delay(10);
  449.         SH=read_add(0x02);
  450.         temp=SH;
  451.         temp=temp<<8;
  452.         temp=temp+SL;
  453.         Day=temp;

  454.         delay(10);
  455.         SL=read_add(0x03);
  456.         delay(10);
  457.         SH=read_add(0x04);
  458.         temp=SH;
  459.         temp=temp<<8;
  460.         temp=temp+SL;
  461.         Night=temp;

  462.         delay(10);
  463.         SL=read_add(0x05);
  464.         delay(10);
  465.         SH=read_add(0x06);
  466.         temp=SH;
  467.         temp=temp<<8;
  468.         temp=temp+SL;
  469.         DanJia=temp;

  470.         delay(10);
  471.         SL=read_add(0x07);
  472.         delay(10);
  473.         SH=read_add(0x08);
  474.         temp=SH;
  475.         temp=temp<<8;
  476.         temp=temp+SL;
  477.         DJ_min=temp;
  478. }


  479. //写初值
  480. void SETS()
  481. {
  482.         uchar SH=0,SL=0;
  483.         uint temp=0;

  484.         temp=Day;
  485.         SL=temp&0x00ff;
  486.         SH=temp>>8;
  487.         delay(10);
  488.         write_add(0x01,SL);
  489.         delay(10);
  490.         write_add(0x02,SH);

  491.         temp=Night;
  492.         SL=temp&0x00ff;
  493.         SH=temp>>8;
  494.         delay(10);
  495.         write_add(0x03,SL);
  496.         delay(10);
  497.         write_add(0x04,SH);

  498.         temp=DanJia;
  499.         SL=temp&0x00ff;
  500.         SH=temp>>8;
  501.         delay(10);
  502.         write_add(0x05,SL);
  503.         delay(10);
  504.         write_add(0x06,SH);

  505.         temp=DJ_min;
  506.         SL=temp&0x00ff;
  507.         SH=temp>>8;
  508.         delay(10);
  509.         write_add(0x07,SL);
  510.         delay(10);
  511.         write_add(0x08,SH);
  512. }
复制代码

原理图: 无
仿真: 无
代码(仅供参考): 出租车计价器.7z (35.68 KB, 下载次数: 4)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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