找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机+红外模块控制小车定点停车和转向

[复制链接]
跳转到指定楼层
楼主
ID:605843 发表于 2019-12-6 19:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
20黑币
在用红外模块TCRT5000使小车定点停车,变向,小车用l298n驱动,外加PT2262遥控调速,仿真已经实现了,为啥弄实物的时候电机不动呢?l298n的电机工作状态一直显示不工作,但电机驱动模块是新的,没问题,请大佬们指导一下,程序应该没问题吧。
#include <reg52.h>

typedef unsigned char u8;
typedef unsigned int u16;


sbit in1= P2^0;           //L298N引脚定义
sbit in2= P2^1;
sbit in3= P2^2;
sbit in4= P2^3;
sbit en1=P3^0;
sbit en2=P3^5;                                                                                                                                                                                                                                                                                                                                                                                    
//sbit key1=P3^1;//前进  ,遥控器引脚定义
//sbit key2=P3^2;//后退
sbit key3=P3^3;//加速
sbit key4=P3^4;//减速

sbit chetou=P2^4; //红外传感器引脚定义
sbit chewei=P2^5;
sbit cheshen=P2^6;

u16 M;//改变占空,一共有240
u16 Z;//用于中断程序,实时红外检测

void delay_us(u16 aa) //延时函数
{
        while(aa--);
}



void main()
{
        u16 a;
        a=120;//初始的速度
        en1=0;
        en2=0;
        TMOD = 0x01;  // T0定时器为工作方式一
        TH0 = 0x0FF;                    //设置初值
        TL0 = 0x09C;          //设置初值
        TR0 = 1; //开启定时器T0
        ET0 = 1;//开放定时器T0中断
        EA = 1;//开放总中断
    //key1 = 0;
        //key2 = 0;
        key3 = 1;//用于仿真工程,真正写入程序板的时候要改为0
        key4 = 1;

        while(1)
        {
           if(Z==240)
           {
           Z=0;
           if((chetou==0&&chewei==1&&cheshen==1)||(chewei==0&&chetou==1&&cheshen==1)||(chetou==1&&chewei==0&&cheshen==0)||(chetou==0&&chewei==1&&cheshen==0))
                {
                        delay_us(2);
           if((chetou==0&&chewei==1&&cheshen==1)||(chewei==0&&chetou==1&&cheshen==1)||(chetou==1&&chewei==0&&cheshen==0)||(chetou==0&&chewei==1&&cheshen==0))
                        {
                                en1=0;
                                en2=0;
                                in1=0;
                                in2=0;
                                in3=0;
                                in4=0;

                        }
                }
                if((chetou==0)&&(chewei==0)&&(cheshen==1))
                {
                        delay_us(2);
                if((chetou==0)&&(chewei==0)&&(cheshen==1))
                        {
                                in1=1;
                                in2=0;
                                in3=0;
                                in4=1;
                        }
                }
                if((chetou==0)&&(chewei==0)&&(cheshen==0))
                {
                        delay_us(2);
                if((chetou==0)&&(chewei==0)&&(cheshen==0))
                        {
                                in1=0;
                                in2=1;
                                in3=1;
                                in4=0;
                        }
                }               
           }

               
                if(key3==0)
                {
                        delay_us(2);
                        if(key3==0)
                        {
                                a+=40;
                                if(a>=240)
                                        a=240;
                        }
                        while(!key3);
                        delay_us(2);
                        while(!key3);
                }
                if(key4==0)
                {
                        delay_us(2);
                        if(key4==0)
                        {
                                a-=40;
                                if(a<=40)
                                        a=40;
                        }
                        while(!key4);
                        delay_us(2);
                        while(!key4);


                }
                if(M==0)
                {
                        en1=1;

                        en2=1;

                }
                if(M==a)
                {
                        en1=0;
                        en2=0;

                }

        }
}

void time0_int() interrupt 1   //T = 0.1ms * 240 = 24ms
{  
TR0 = 0;      
TH0 = 0x0FF;
TL0 = 0x09C; //0.1ms
M++;
Z++;
if(M>=240)
{
        M=0;               
}
TR0 = 1;
}

用的开关代替的PT2262个TCRT5000因为软件里没有这俩元件



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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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