找回密码
 立即注册

QQ登录

只需一步,快速开始

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

跷跷板小车51单片机源码和proteus仿真

[复制链接]
跳转到指定楼层
楼主
跷跷板小车仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include <AT89X51.H>

  2. unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,
  3.                                0x66,0x6d,0x7d,0x07,
  4.                                0x7f,0x6f,0x77,0x7c,
  5.                                0x39,0x5e,0x79,0x71,0x00};
  6. unsigned char second;
  7. unsigned char tcount,a,temp,b,c,zongshijian;
  8. void motor11(void)
  9. {
  10.   P1_4=1;
  11.   P1_5=0;
  12.   P1_6=1;
  13.   P1_7=0;
  14. }
  15. void motor01(void)
  16. {
  17.   P1_4=0;
  18.   P1_5=0;
  19.   P1_6=1;
  20.   P1_7=0;
  21. }
  22. void motor10(void)
  23. {
  24.   P1_4=1;
  25.   P1_5=0;
  26.   P1_6=0;
  27.   P1_7=0;
  28. }
  29. void motor00(void)
  30. {
  31.   P1_4=0;
  32.   P1_5=0;
  33.   P1_6=0;
  34.   P1_7=0;
  35. }
  36. void motor22(void)
  37. {
  38.   P1_4=0;
  39.   P1_5=1;
  40.   P1_6=0;
  41.   P1_7=1;
  42. }
  43. void motor02(void)
  44. {
  45.   P1_4=0;
  46.   P1_5=0;
  47.   P1_6=0;
  48.   P1_7=1;
  49. }
  50. void motor20(void)
  51. {
  52.   P1_4=0;
  53.   P1_5=1;
  54.   P1_6=0;
  55.   P1_7=0;
  56. }
  57. void delay1s(void)
  58. {
  59.   unsigned char i,j,k;
  60.   for(k=100;k>0;k--)
  61.   for(i=20;i>0;i--)
  62.   for(j=248;j>0;j--);
  63. }
  64. //------------------------------------------------------------------------------------------------------------//
  65. delay(unsigned int dd)
  66. {
  67.         while(--dd);
  68. }
  69. //-------------------------------------------------------------------------------------------------------------//
  70. void main(void)
  71. {
  72. // P1=0xff;
  73.   TMOD=0x01;
  74.   TH0=(65536-50000)/256;
  75.   TL0=(65536-50000)%256;
  76.   TR0=1;
  77.   ET0=1;
  78.   EX0=1;
  79.   IT0=1;
  80.   EX1=1;
  81.   IT1=1;
  82.   temp=0;
  83.   a=0;
  84.   b=0;
  85.   c=0;
  86.   zongshijian=0;
  87.   tcount=0;
  88.   second=0;
  89.   delay1s();
  90.   motor00();
  91.   while(1)
  92.   {
  93.         if(P1_0==0)
  94.                    {
  95.                  EA=1;
  96.                  motor11();
  97.                  temp=1;
  98.                 }
  99.         if(P1_1==0&&P1_2==0&&temp==1&&b==0)
  100.                   motor11();
  101.         if(P1_1==1&&P1_2==0&&temp==1&&b==0)
  102.                   motor01();
  103.         if(P1_1==0&&P1_2==1&&temp==1&&b==0)
  104.                   motor10();
  105.         if(P1_1==1&&P1_2==1&&temp==1&&b==0)
  106.                   motor11();

  107.         if(P1_1==0&&P1_2==0&&temp==1&&b==1)
  108.                   motor22();
  109.         if(P1_1==1&&P1_2==0&&temp==1&&b==1)
  110.                   motor02();
  111.         if(P1_1==0&&P1_2==1&&temp==1&&b==1)
  112.                   motor20();
  113.         if(P1_1==1&&P1_2==1&&temp==1&&b==1)
  114.                   motor22();
  115.         if(P1_3==1&&b==1&temp==1)
  116.         {
  117.                   motor00();
  118.                  c=1;
  119.                  zongshijian=second;
  120.         }
  121.         if(c==1)
  122.         {
  123.                   motor00();
  124.         }
  125.   }
  126. }
  127. //--------------------------------------------------------------------------------------------------------//
  128. void t0(void) interrupt 1 using 0
  129. {
  130.   tcount++;a++;
  131.   
  132.   if(a>=2)
  133.   {P2=0xff;
  134.      P0=dispcode[second/10];
  135.          P2=0xfe;}
  136.   if(a<2)
  137.   {P2=0xff;
  138.            P2=0xfd;
  139.     P0=dispcode[second%10];
  140.         }
  141.         if(a>=4)
  142.   {a=0;}
  143.   if(tcount==200)
  144.     {
  145.       tcount=0;
  146.           if(c==0)
  147.           {
  148.                   second++;
  149.           }
  150.           if(c==1)
  151.           {
  152.                   second=zongshijian;
  153.           }          
  154.       if(second==100)
  155.         {
  156.           second=0;
  157.         }
  158.     }
  159.   TH0=(65536-5000)/256;
  160.   TL0=(65536-50000)%256;
  161. }

  162. ///////////////-------------------------------------------------------------------------//
  163. int0()interrupt 0
  164. {EX0=0;
  165.   motor00();
  166.   P2=0xfd;P0=0x3f;
  167.   delay1s();P0=0x06;            
  168.   delay1s();P0=0x5b;
  169.   delay1s();P0=0x4f;
  170.   delay1s();P0=0x66;
  171.   delay1s();P0=0x6d;
  172.   delay1s();P0=0x7d;
  173.   second+=6;
  174.   EX0=1;
  175.   motor00();
  176. }
  177. //--------------------------------------------------------------------------------------------//
  178.   int1()interrupt 2
  179. {EX1=0;
  180.   EX0=0;
  181.    motor00();
  182.   P2=0xfd;P0=0x3f;
  183.   delay1s();P0=0x06;            
  184.   delay1s();P0=0x5b;
  185.   delay1s();P0=0x4f;
  186.   delay1s();P0=0x66;
  187.   delay1s();P0=0x6d;
  188.   delay1s();P0=0x7d;
  189.   second+=6;
  190.   b=1;
  191. // EX1=1;
  192. }
  193. //--------------------------------------------------------------------------------------------//
复制代码

所有资料51hei提供下载:
跷跷板小车.rar (56.75 KB, 下载次数: 11)


评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:88606 发表于 2018-8-12 17:57 | 只看该作者
“if”变“如果”?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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