找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7231|回复: 4
收起左侧

智能寻铁丝寻迹小车单片机程序 电感式接近开关做的

[复制链接]
ID:252449 发表于 2017-12-4 19:54 | 显示全部楼层 |阅读模式
用电感式接近开关做的寻铁丝小车

单片机源程序如下:
  1.         #include<reg52.h>
  2.         #define uchar unsigned char
  3.         #define uint unsigned int
  4.         sbit P0_4 = P0^4;                 //后轮输入口
  5.         sbit P0_5 = P0^5;
  6.         sbit P0_6 = P0^6;
  7.         sbit P0_7 = P0^7;

  8.         sbit P0_0 = P0^0;                //前轮输入口
  9.         sbit P0_1 = P0^1;
  10.         sbit P0_2 = P0^2;
  11.         sbit P0_3 = P0^3;
  12.                
  13.         sbit Left_moto_pwm = P3^6;          //电机使能
  14.         sbit Right_moto_pwm = P3^7;
  15.         sbit Left_moto_pwm2 = P3^5;
  16.         sbit Right_moto_pwm2 = P3^4;

  17.         sbit P20 = P2^0;          //金属检测器I/O口                 //左
  18.         sbit P21 = P2^1;                                                        // 中
  19.         sbit P22 = P2^2;                                                        //右
  20.                                                                                        
  21.         sbit rw=P2^5;         //1602
  22.         sbit rs=P2^6;
  23.         sbit en=P2^7;

  24.         sbit beep=P2^4;//蜂鸣器

  25.         uchar code table[]="Distance";
  26.         uchar code table1[]="Time";

  27.         unsigned int motor1=0;         //计左电机码盘脉冲值         (码盘值为20)
  28.         unsigned int motor2=0;         //计右电机码盘脉冲值

  29. //        #define Left_moto_pwm     P3_6           //接驱动模块ENA        使能端,输入PWM信号调节速度         (左马达调节PWM)  前
  30. //        #define Right_moto_pwm    P3_7           //接驱动模块ENB         (右马达调节PWM)
  31. //        #define Left_moto_pwm2     P3_5           //接驱动模块ENA        使能端,输入PWM信号调节速度         (左马达调节PWM)  后
  32. //        #define Right_moto_pwm2    P3_4           //接驱动模块ENB         (右马达调节PWM)

  33.         #define Left_moto_go      {P0_4=0,P0_5=1;} //  左电机前进         前          P0_4=0,P0_5=1;
  34.         #define Left_moto_back    {P0_4=1,P0_5=0;} // 左电机后退  前        P0_4=1,P0_5=0                      
  35.         #define Right_moto_go     {P0_6=1,P0_7=0;} //右电机前转         后                P0_6=1,P0_7=0;
  36.         #define Right_moto_back   {P0_6=0,P0_7=1;} //右电机后退         后                P0_6=0,P0_7=1

  37.         #define Left_moto_go2      {P0_2=0,P0_3=1;} //左电机前进        P0_2=0,P0_3=1                           
  38.         #define Left_moto_back2    {P0_2=1,P0_3=0;} //左电机后退          P0_2=1,P0_3=0        前左               
  39.         #define Right_moto_go2     {P0_0=1,P0_1=0;} //右电机前转        P0_0=1,P0_1=0         前右
  40.         #define Right_moto_back2   {P0_0=0,P0_1=1;} //右电机后退         P0_0=0,P0_1=1

  41.         #define Left_moto_stop22    {P0_2=0,P0_3=0;} //左电机后退          P0_2=0,P0_3=0                      
  42.         #define Right_moto_stop22     {P0_0=0,P0_1=0;} //右电机前转        P0_0=0,P0_1=0
  43.         #define Left_moto_stop21    {P0_4=0,P0_5=0;} // 左电机后退  后        P0_4=0,P0_5=0                      
  44.         #define Right_moto_stop21     {P0_6=0,P0_7=0;} //右电机前转         后                P0_6=0,P0_7=0;

  45.            unsigned char pwm_val_left  =0;//变量定义
  46.         unsigned char push_val_left =0;// 左电机占空比N/20         推动                           //计算占空比时N的值(人工改变值)
  47.         unsigned char pwm_val_right =0;                                                                                      //通过它来实现PWM的改变(通过FOR循环)
  48.         unsigned char push_val_right=0;// 右电机占空比N/20

  49.         unsigned char pwm_val_left2  =0;//变量定义
  50.         unsigned char push_val_left2 =0;// 左电机占空比N/20         推动                           //计算占空比时N的值(人工改变值)
  51.         unsigned char pwm_val_right2 =0;                                                                                      //通过它来实现PWM的改变(通过FOR循环)
  52.         unsigned char push_val_right2=0;// 右电机占空比N/20

  53.         bit Right_moto_stop=1;
  54.         bit Left_moto_stop =1;

  55.         bit Right_moto_stop2=1;
  56.         bit Left_moto_stop2 =1;
  57.         unsigned  int  time=0;
  58.         uchar num2,shi=0,fen=0,miao=0;           //num2用于时钟部分  
  59.         uint num,num1,num3,sum,num7;  //num,num1,num3用于距离处理部分
  60.         unsigned long num6,S=0,S1=0,S2=0;
  61.         uchar jc; //检测
  62. /**************************1602显示**********************************************/
  63. void delayms(uint xms)//延时函数
  64. {
  65. uint i,j;
  66. for(i=xms;i>0;i--)
  67.         for(j=110;j>0;j--);
  68. }
  69. void write_com(uchar com)
  70. {
  71. rs=0;
  72. en=0;
  73. P1=com;
  74. delayms(5);
  75. en=1;
  76. delayms(5);
  77. en=0;
  78. }
  79. void write_date(uchar date)
  80. {
  81. rs=1;
  82. en=0;
  83. P1=date;
  84. delayms(5);
  85. en=1;
  86. delayms(5);
  87. en=0;
  88. }
  89. void write_sfm(uchar add,uchar date)
  90. {
  91. uchar shi,ge;
  92. shi=date/10;
  93. ge=date%10;
  94. write_com(0x80+0x00+add);                                                  
  95. write_date(0x30+shi);                                                         
  96. write_date(0x30+ge);                                                         

  97. }
  98. void write_juli(uchar add,uchar date)
  99. {
  100. uchar shi,ge;
  101. shi=date/10;
  102. ge=date%10;
  103. write_com(0x80+0x40+add);                                                  
  104. write_date(0x30+shi);                                                         
  105. write_date(0x30+ge);                                                         

  106. }
  107. void led_1602_init()  //1602初始化函数
  108. {
  109. rw=0;
  110. en=0;
  111. write_com(0x38);
  112. write_com(0x0c);
  113. write_com(0x06);
  114. write_com(0x01);

  115.                
  116. }
  117.         void timer1()interrupt 3
  118.         {
  119.          TH1=(65536-45872)/256;
  120.          TL1=(65536-45872)%256;
  121.                  num2++;
  122.                 if(num2==20)
  123.                 {
  124.                  num2=0;
  125.                  miao++;
  126.                  }
  127.                  if(miao==60)
  128.                          {
  129.                          miao=0;
  130.                          fen++;
  131.                          }
  132.                          if(fen==60)
  133.                                  {
  134.                                  fen=0;
  135.                                  shi++;
  136.                                  }
  137.                                  if(shi==24)
  138.                                          {
  139.                                          shi=0;
  140.                                         }
  141.                                 
  142.         }
  143. void deal_juli() //测距函数
  144. {
  145. // S=S1*100+S2;
  146. // uint sum;                 
  147. sum=motor1+motor2;        // 求和
  148. num=sum/2.0;          // 求平均值
  149. num1=num/160.0;//求轮子旋转圈数
  150. num3=num1*22.5;//轮子走过的距离 算出来的是cm
  151. S2=num3;
  152. S=S2/100;
  153. S1=S2%100;
  154. // write_juli(9,S);
  155. // write_juli(12,S1);
  156. }

  157. /*********************************************************************************************
  158. 蜂鸣器
  159. /********************************************************************************************/
  160. //void sound()
  161. //{
  162. // uint i;
  163. // for(i=4;i>0;i--)
  164. //        {
  165. //         beep=~beep;
  166. //        delayms(100);        //控制蜂鸣器的频率
  167. //        }
  168. //}




  169. /*********************************************************************************************
  170. 外部中断INT0计算电机1的脉冲
  171. /********************************************************************************************/
  172. void intersvr1(void) interrupt 0 using 1
  173. {
  174.         motor1++;       
  175.         if(motor1>=9999)
  176.                 motor1=0;       
  177. }
  178. /*********************************************************************************************
  179. 外部中断INT1计算电机2的脉冲
  180. /********************************************************************************************/
  181. void intersvr2(void) interrupt 2 using 3
  182. {
  183.         motor2++;
  184.         if(motor2>=9999)
  185.                 motor2=0;
  186. }
  187. /************************************************************************/
  188.      void  run(void)        //前进函数
  189. {
  190. ////         TR1=1;
  191. //     push_val_left  =4;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  192. //         push_val_right =4;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  193. //
  194. //         push_val_left2  =4;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  195. //         push_val_right2 =4;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  196.          if(P20==0&&P21==0&&P22==0)                //全测到
  197.          {
  198.          TR1=0;
  199.          Left_moto_stop21 ;                 //左电机
  200.          Right_moto_stop21 ;         //右电机

  201.          Left_moto_stop22 ;                 //左电机
  202.          Right_moto_stop22 ;         //右电机         
  203.          }

  204.      else if(P20==1&&P21==0&&P22==1)                //中间测到        //前进函数
  205. {
  206.                 TR1=1;
  207.      push_val_left  =9;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  208.          push_val_right =9;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  209.          push_val_left2  =9;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  210.          push_val_right2 =9;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  211.        Left_moto_go ;                 //停止
  212.            Right_moto_go ;         

  213.            Left_moto_go2 ;               
  214.            Right_moto_go2 ;       
  215.            push_val_left  =4;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  216.          push_val_right =4;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  217.          push_val_left2  =4;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  218.          push_val_right2 =4;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  219.        Left_moto_go ;                 //停止
  220.            Right_moto_go ;         

  221.            Left_moto_go2 ;               
  222.            Right_moto_go2 ;       

  223.          
  224. }
  225.         else if(P20==0&&P21==1&&P22==1)                 //左边测到
  226. {
  227.          TR1=1;
  228.          push_val_left  =7;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  229.          push_val_right =7;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  230.          push_val_left2  =7;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  231.          push_val_right2 =7;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  232.          Right_moto_go;
  233.          Right_moto_go2;
  234.          Left_moto_back;
  235.          Left_moto_back2;
  236.          
  237. }
  238.         else if(P20==1&&P21==1&&P22==0)                 //右边测到
  239. {
  240.          TR1=1;
  241.          push_val_left  =7;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  242.          push_val_right =7;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  243.          push_val_left2  =7;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  244.          push_val_right2 =7;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  245.          Right_moto_back;
  246.          Right_moto_back2;
  247.          Left_moto_go;
  248.          Left_moto_go2;
  249.          
  250. }
  251.             else if(P20==0&&P21==0&&P22==1)                 //左两测到
  252.          {
  253.                  TR1=1;
  254.      push_val_left  =8;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  255.          push_val_right =8;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  256.          push_val_left2  =8;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  257.          push_val_right2 =8;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  258.        Left_moto_go ;                 //停止
  259.            Right_moto_go ;         

  260.            Left_moto_go2 ;               
  261.            Right_moto_go2 ;       
  262.            push_val_left  =3;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  263.          push_val_right =3;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  264.          push_val_left2  =3;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  265.          push_val_right2 =3;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  266.        Left_moto_go ;                 //停止
  267.            Right_moto_go ;         

  268.            Left_moto_go2 ;               
  269.            Right_moto_go2 ;       
  270.          }
  271.          else if(P20==1&&P21==0&&P22==0)         //右两测到
  272.          {
  273.            TR1=1;
  274.      push_val_left  =8;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  275.          push_val_right =8;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  276.          push_val_left2  =8;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  277.          push_val_right2 =8;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  278.        Left_moto_go ;                 //停止
  279.            Right_moto_go ;         

  280.            Left_moto_go2 ;               
  281.            Right_moto_go2 ;       
  282.            push_val_left  =3;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  283.          push_val_right =3;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度

  284.          push_val_left2  =3;  //PWM 调节参数1-20   1为最慢,20是最快  改这个值可以改变其速度
  285.          push_val_right2 =3;         //PWM 调节参数1-20   1为最慢,20是最快         改这个值可以改变其速度
  286.        Left_moto_go ;                 //停止
  287.            Right_moto_go ;         

  288.            Left_moto_go2 ;               
  289.            Right_moto_go2 ;       
  290.      }
  291. }                        



  292. /************************************************************************/
  293. /*                    PWM调制电机转速                                   */
  294. /************************************************************************/
  295. /*                    左电机调速                                        */
  296. /*调节push_val_left的值改变电机转速,占空比            */
  297.                 void pwm_out_left_moto(void)
  298. {  
  299.    if(Left_moto_stop)
  300.    {
  301.     if(pwm_val_left<=push_val_left)
  302.                Left_moto_pwm=1;
  303.         else
  304.                Left_moto_pwm=0;
  305.         if(pwm_val_left>=20)
  306.         pwm_val_left=0;
  307.    }
  308.    else  Left_moto_pwm=0;
  309. }
  310. /******************************************************************/
  311. /*                    右电机调速                                  */  
  312.    void pwm_out_right_moto(void)
  313. {
  314.   if(Right_moto_stop)
  315.    {
  316.     if(pwm_val_right<=push_val_right)
  317.                Right_moto_pwm=1;
  318.         else
  319.                Right_moto_pwm=0;
  320.         if(pwm_val_right>=20)
  321.         pwm_val_right=0;
  322.    }
  323.    else    Right_moto_pwm=0;
  324. }

  325. /************************************************************************/
  326. /*    222222                2PWM调制电机转速2                                   */
  327. /************************************************************************/
  328. /*                    左电机调速                                        */
  329. /*调节push_val_left的值改变电机转速,占空比            */
  330.                 void pwm_out_left_moto2(void)
  331. {  
  332.    if(Left_moto_stop2)
  333.    {
  334.     if(pwm_val_left2<=push_val_left2)
  335.                Left_moto_pwm2=1;
  336.         else
  337.                Left_moto_pwm2=0;
  338.         if(pwm_val_left2>=20)
  339.         pwm_val_left2=0;
  340.    }
  341.    else  Left_moto_pwm2=0;
  342. }
  343. /******************************************************************/
  344. /*                    右电机调速                                  */  
  345.    void pwm_out_right_moto2(void)
  346. {
  347.   if(Right_moto_stop2)
  348.    {
  349.     if(pwm_val_right2<=push_val_right2)
  350.                Right_moto_pwm2=1;
  351.         else
  352.                Right_moto_pwm2=0;
  353.         if(pwm_val_right2>=20)
  354.         pwm_val_right2=0;
  355.    }
  356.    else    Right_moto_pwm2=0;
  357. }

  358. /***************************************************/
  359. ///*TIMER0中断服务子函数产生PWM信号*/
  360.         void timer0()interrupt 1   using 2
  361. {
  362.      TH0=0Xfe;          //0.1Ms定时
  363.          TL0=0Xa3;
  364.          time++;
  365.          pwm_val_left++;
  366.          pwm_val_right++;
  367.          pwm_out_left_moto();
  368.          pwm_out_right_moto();

  369.          pwm_val_left2++;
  370.          pwm_val_right2++;
  371.          pwm_out_left_moto2();
  372.          pwm_out_right_moto2();

  373. }

  374. /***************************************************/


  375. /**********************循迹程序*****************************/

  376.         void main(void)
  377. {

  378.         TMOD=0X11;
  379.         TH0= 0Xfe;                  //0.1ms定时
  380.         TL0= 0Xa3;

  381.         TH1=(65536-45872)/256;
  382.         TL1=(65536-45872)%256;
  383.         EA = 1;                        //中断总开关       
  384.         TR0= 1;
  385.         ET0= 1;
  386. //        TR1=1;
  387.         ET1=1;
  388.                   
  389. ……………………

  390. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
yyb.zip (3.08 KB, 下载次数: 88)

评分

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

查看全部评分

回复

使用道具 举报

ID:342305 发表于 2018-6-19 11:00 | 显示全部楼层
好资料,51黑有你更精彩!!!
回复

使用道具 举报

ID:342305 发表于 2018-6-19 11:01 | 显示全部楼层
想问一下楼主有小车的照片吗?还有这个模块用的是那种的啊?》
回复

使用道具 举报

ID:356953 发表于 2018-6-23 10:36 | 显示全部楼层
我们之前做电子产品设计大赛的时候做的就是这个
回复

使用道具 举报

ID:379274 发表于 2018-7-26 20:19 来自手机 | 显示全部楼层
这是用的ldc1000和52实现的么???
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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