找回密码
 立即注册

QQ登录

只需一步,快速开始

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

51单片机3红外传感器2轮驱动避障小车

[复制链接]
跳转到指定楼层
楼主
ID:680274 发表于 2020-1-8 09:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

sbit c0=P2^0;//c0右避障---红外传感器
sbit c1=P2^1;//c1左避障
sbit c2=P2^2;//c2中间避障
sbit D1A=P1^0;//右轮电机输出
sbit D1B=P1^1;
sbit D2A=P1^2;//左轮电机输出
sbit D2B=P1^3;

int t=0;
int PWM_T1=0;//右轮电机占空比
int PWM_T2=0;//左轮电机占空比

//延时函数
void delay_ms(int x)
{
        int i,j;
        for(i=0;i<x;i++)
        for(j=0;j<110;j++);
}
/********************************************
                            主程序
********************************************/
void main(void)
{
  D1B=0;
  D2B=0;
  TMOD=0x02;//定时器0,工作模式2,8位定时模式
  TH0=210;        //写入预置初值(取值1-255,数值越大PWM频率越高)
  TL0=210;        //写入预置值(取值1-255,数值越大PWM频率越高)
  TR0=1;        //启动定时器
  ET0=1;        //允许定时器0中断
  EA=1;                //允许总中断
  PWM_T1=150;
  PWM_T2=150;
  delay_ms(10);       
  while(1)
  {
    if(c0==0&&c1==1)
        {
         PWM_T1=0;
     PWM_T2=100;
         delay_ms(10);
        }
        if(c0==1&&c1==0)
        {
         PWM_T1=100;
     PWM_T2=0;
         delay_ms(10);
        }
        if(c2==0)
        {
         PWM_T1=0;
     PWM_T2=0;
         delay_ms(10);
        }
        if(c2==1)
        {
         PWM_T1=100;
     PWM_T2=100;
         delay_ms(10);
        }
        if(c0==1&&c1==1)
        {
         PWM_T1=100;
     PWM_T2=100;
         delay_ms(10);
         }
        if(c0==0&&c1==0)
        {
         PWM_T1=0;
     PWM_T2=0;
         delay_ms(10);
        }
  }
}         
//部分程序见附件

避障.zip

856 Bytes, 下载次数: 18, 下载积分: 黑币 -5

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

使用道具 举报

沙发
ID:680274 发表于 2020-1-8 09:31 | 只看该作者
定时器中断模拟PWM调速在附件
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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