基于C语言的磁引导园丁机器人源程序 --单片机应用

GardenRobot.c:

#include"reg52.h"

#include"intrins.h"

#define uchar unsigned char 

#define uint unsigned int

sbit magInduction1=P2^7;

sbit magInduction2=P2^6;

sbit magInduction3=P2^5;

sbit magInduction4=P2^4;

uchar Duty_left,Duty_right,i=0,j=0;

uint  time=0,s; 

sbit in1=P1^0;

sbit in2=P1^1;

sbit in3=P1^2;

sbit in4=P1^3;

sbit e1=P2^3;

sbit e2=P2^2;

sbit RX=P0^4;

sbit TX=P0^5; 

sbit jd=P3^7;

void init()

{

  TMOD=0X01;

  TH0=(65536-50)/256;

  TL0=(65536-50)%256;

  EA=1;

  ET0=1;

  TR0=1;

  e1=1;

  e2=1;

}

void init2()

{

  TMOD=0x10;

     TH1=0;

  TL1=0;

  TR1=1;

  ET1=1;

  EA=1;

}

void startmodule()

{

  uint n; 

  TX=1;

  for(n=0;n<21;n++)

    _nop_();

  TX=0; 

}

void count()

{

  time=TH1*256+TL1;

  TH1=0;

  TL1=0;

  s=(time*1.7)/100;

}

void stop()

{

  in1=0;

  in2=0;

  in3=0;

  in4=0;

}

void delay(unsigned int k)

  unsigned int x,y;

  for(x=0;x<k;x++) 

    for(y=0;y<2000;y++);

}

void turnRight()

{

  Duty_left=29;

  Duty_right=20;

  in1=1;

  in2=0;

  in3=0;

  in4=1;

}

void turnRight2()

{

  Duty_left=29;

  Duty_right=20;

  in1=1;

  in2=0;

  in3=0;

  in4=1;

}

void turnLeft()

{

  Duty_left=18;

  Duty_right=26;

  in1=0;

  in2=1;

  in3=1;

  in4=0;

}

void turnLeft2()

{

  Duty_left=20;

  Duty_right=30;

  in1=0;

  in2=1;

  in3=1;

  in4=0;

}

void forward()

{

  Duty_left=25;

  Duty_right=25;

  in1=1;

  in2=0;

  in3=1;

  in4=0;

}

void forward2()

{

  Duty_left=45;

  Duty_right=35;

  in1=1;

  in2=0;

  in3=1;

  in4=0;

}

 

void timer() interrupt 1

{

  i++;

  j++;

  if(i<=Duty_left)

    e1=1;

  else

    e1=0;

  if(i>100)

  {

    e1=1;

    i=0;

  }

  if(j<=Duty_right)

    e2=1;

  else

    e2=0;

  if(j>100)

  {

    e2=0;

    j=0;

  }

  TH0=(65536-50)/256;

  TL0=(65536-50)%256;

}

void main()

{

  init();

  init2();

  while(1)

  {

    startmodule(); 

    while(!RX);

    TR1=1;

    while(RX);

    TR1=0;

    count();

    if(magInduction1==0&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s>40)

      turnRight();

    if(magInduction1==0&&magInduction2==0&&magInduction3==1&&magInduction4==1&&s>40)

      turnRight();

    if(magInduction1==1&&magInduction2==0&&magInduction3==0&&magInduction4==1&&s>40)

      forward();

    if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s>40)

      forward();

    if(magInduction1==1&&magInduction2==1&&magInduction3==0&&magInduction4==0&&s>40)

      turnLeft();

    if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==0&&s>40)

      turnLeft();

    if(magInduction2==0&&magInduction1==1&&magInduction3==1&&magInduction4==1&&s>40)

      turnRight();

    if(magInduction3==0&&magInduction1==1&&magInduction2==1&&magInduction4==1&&s>40)

      turnLeft();

    if(magInduction1==0&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s<40)

    { 

      stop();

      jd=0;

      delay(30);

      jd=1;

      turnRight2();

      delay(8);

    }

    if(magInduction1==0&&magInduction2==0&&magInduction3==1&&magInduction4==1&&s<40)

    { 

      stop();

      jd=0; 

      delay(30);

      jd=1;

      turnRight2();

      delay(8);

    }

    if(magInduction1==1&&magInduction2==0&&magInduction3==0&&magInduction4==1&&s<40)

    {

      stop();

      jd=0; 

      delay(30);

      jd=1;

      forward2();

      delay(8);  

    }

    if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s<40)

    {

      stop();

      jd=0;

      delay(30);

      jd=1;

      forward2();

      delay(8);

    }

    if(magInduction1==1&&magInduction2==1&&magInduction3==0&&magInduction4==0&&s<40)

    {

      stop();

      jd=0;

      delay(30);

      jd=1;

      turnLeft2();

      delay(8);

    }

    if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==0&&s<40)

    {

      stop(); 

      jd=0;

      delay(30);

      jd=1;

      turnLeft2();

      delay(8);

    }

  }

}

 

源文件下载:

链接:GardenRobot.c
提取码:iu32 

转载于:https://www.cnblogs.com/qikeyishu/p/10507519.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值