找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2270|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

紅外遙控小車

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:290235 發(fā)表于 2018-3-10 22:55 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#ifndef _CONFIG_H
#define _CONFIG_H

/*通用頭文件包含*/
#include <STC12C5A60S2.h>
#include<intrins.h>

/*循跡模塊相關(guān)引腳定義*/
#define P0_PORT P0
sbit D1=P0^0;
sbit D2=P0^1;
sbit D3=P0^2;
sbit D4=P0^3;
sbit D5=P0^4;
sbit D6=P0^5;
sbit D7=P0^6;
sbit D8=P0^7;

/*電機驅(qū)動模塊相關(guān)引腳定義*/
sbit ENA=P1^3;                          //右側(cè)電機使能
sbit IN1=P1^0;                          //右側(cè)電機控制端口
sbit IN2=P1^1;
sbit ENB=P1^4;                          //左側(cè)電機使能
sbit IN3=P1^5;                          //左側(cè)電機控制端口
sbit IN4=P1^6;

/*紅外接收頭引腳定義*/
sbit Ird=P3^3;

/*晶振頻率*/
#define FOSC 11059200UL

/*類型替換*/
typedef unsigned char      uint8;
typedef unsigned short int uint16;
typedef unsigned long int  uint32;
typedef signed char        int8;
typedef signed short int   int16;
typedef signed long int           int32;

#endif

#define _DELAY_C
#include"Config.h"
#include"Delay.h"

/*10ms延時函數(shù)*/
void Delay10ms(uint8 tt)
{
  uint8 i, j;
        while(tt--)
        {
                _nop_();
                _nop_();
                i = 108;
                j = 144;
                do
                {
                        while (--j);
                } while (--i);
        }
}

/*100ms延時函數(shù)*/
void Delay100ms(uint8 tt)                //@11.0592MHz
{
        uint8 i, j, k;
        while(tt--)
        {
                i = 5;
                j = 52;
                k = 195;
                do
                {
                        do
                        {
                                while (--k);
                        } while (--j);
                } while (--i);
  }
}

/*10us延時函數(shù)*/
void Delay10us(void)                //@11.0592MHz
{
        uint8 i;

        _nop_();
        _nop_();
        _nop_();
        i = 24;
        while (--i);
}

#ifndef _DELAY_H
#define _DELAY_H

extern void Delay10ms(uint8 tt);
extern void Delay100ms(uint8 tt);
extern void Delay10us(void);

#ifndef _DELAY_C

#endif
#endif

#define _INFRARED_C
#include"Config.h"
#include"Infrared.h"
#include"main.h"

uint8 IrdCode[4];
bit Irdflag=0;

/*紅外按鍵掃描函數(shù)*/
void IrdScan (void)
{
        if (Irdflag)
        {
                Irdflag=0;
                IrdAction (IrdCode[2]);
        }
}
/*配置紅外函數(shù)*/
void ConfigIrd (void)
{
        Ird=1;               //初始化引腳
        TMOD&=0x0F;         
        TMOD|=0x10;
        TL1=0;               
        TH1=0;
        TR1=0;               
        ET1=0;               //關(guān)閉T1中斷
        IT1=1;               //設(shè)置外部中斷1為下降沿觸發(fā)方式
        EX1=1;               //使能外部1中斷
}

/*獲取低電平時間函數(shù)*/
uint16  GetLowTime (void)
{
        TH1=0;
        TL1=0;
        TR1=1;
        while (!Ird)
        {
                if (TH1>0x40)                              //防止因信號異常導(dǎo)致程序假死在這里
                {
                        break;
                }
        }
        TR1=0;
        return (TH1*256+TL1);
}

/*獲取高電平時間函數(shù)*/
uint16 GetHighTime (void)
{
        TH1=0;
        TL1=0;
        TR1=1;
        while (Ird)
        {
                if (TH1>0x40)
                {
                        break;
                }
        }
        TR1=0;
        return (TH1*256+TL1);
}

/*外部中斷1中斷服務(wù)函數(shù)*/
void ExternalSubroutine (void) interrupt 2
{
        uint16 time;
        uint8 i;
        uint8 j;
       
        time=GetLowTime ();
        if ((time>8755)||(time<7833))
        {
                IE1=0;                                       //清零外部中斷1中斷標(biāo)志,防止重復(fù)進(jìn)入中斷
                return ;
        }
        time=GetHighTime ();
        if ((time>4608)||(time<3686))
        {
                IE1=0;                                       //清零外部中斷1中斷標(biāo)志,防止重復(fù)進(jìn)入中斷
                return ;
        }
        for (i=0;i<4;++i)
        {
                for (j=0;j<8;++j)
                {
                        time=GetLowTime ();
                        if ((time>718)||(time<313))
                        {
                                IE1=0;                              //清零外部中斷1中斷標(biāo)志,防止重復(fù)進(jìn)入中斷
                                return ;
                        }
                        time=GetHighTime ();
                        if ((time<718)&&(time>313))
                        {
                                IrdCode[i]>>=1;
                        }
                        else if ((time>1345)&&(time<1751))
                        {
                                IrdCode[i]>>=1;
                                IrdCode[i]|=0x80;
                        }
                        else
                        {
                                IE1=0;                             //清零外部中斷1中斷標(biāo)志,防止重復(fù)進(jìn)入中斷
                                return;
                        }
                }
        }
        Irdflag=1;
        IE1=0;                                        //清零外部中斷1中斷標(biāo)志,防止重復(fù)進(jìn)入中斷
}

#ifndef _INFRARED_H
#define _INFRARED_H

extern void IrdScan (void);
extern void ConfigIrd (void);

#ifndef _INFRARED_C

#endif
#endif

#define _MAIN_C
#include"Config.h"
#include"main.h"
//#include"Delay.h"
#include"PWM.h"

void Self_Tracing (void);
void Stop (void);

/*主函數(shù)*/
void main (void)
{       
        InitPCA ();                                          //初始化PWM模塊
        EA=1;
        while (1)
        {
                Self_Tracing ();
        }
}

/*慢左轉(zhuǎn)函數(shù)*/
void SlowTurnLeft (void)
{
        IN3=0;                                               //左側(cè)停
        IN4=0;
        IN1=0;                                               //右側(cè)運動       
        IN2=1;
        CCAP0L=CCAP0H=0xFF;                                  //占空比
        PCA_PWM0=0x03;
        CCAP1L=CCAP1H=0;                                     //占空比
        PCA_PWM1=0;
}

/*慢右轉(zhuǎn)函數(shù)*/
void SlowTurnRight (void)
{
        IN3=0;                                               //左側(cè)運動
        IN4=1;
        IN1=0;                                               //右側(cè)停
        IN2=0;
        CCAP0L=CCAP0H=0;                                     //占空比
        PCA_PWM0=0;
        CCAP1L=CCAP1H=0xFF;                                  //占空比
        PCA_PWM1=0x03;
}

/*快左轉(zhuǎn)函數(shù)*/
void FastTurnLeft (void)
{
        IN3=1;                                               //左側(cè)向相反方向運動
        IN4=0;
        IN1=0;                                               //右側(cè)運動       
        IN2=1;
        CCAP0L=CCAP0H=70;                                     //占空比
        PCA_PWM0=0;
        CCAP1L=CCAP1H=70;                                     //占空比
        PCA_PWM1=0;
}

/*快右轉(zhuǎn)函數(shù)*/
void FastTurnRight (void)
{
        IN3=0;                                               //左側(cè)運動
        IN4=1;
        IN1=1;                                               //右側(cè)向相反方向運動
        IN2=0;
        CCAP0L=CCAP0H=70;                                     //占空比
        PCA_PWM0=0;
        CCAP1L=CCAP1H=70;                                     //占空比
        PCA_PWM1=0;
}

/*后退函數(shù)*/
void Backward (void)
{
        IN1=1;
        IN2=0;
        IN3=1;
        IN4=0;
        CCAP0L=CCAP0H=70;                                     //占空比
        PCA_PWM0=0x00;
        CCAP1L=CCAP1H=70;                                     //占空比
        PCA_PWM1=0x00;
}       

/*前進(jìn)函數(shù)*/
void Forward (void)
{
        IN1=0;
        IN2=1;
        IN3=0;
        IN4=1;
        CCAP0L=CCAP0H=70;                                     //占空比
        PCA_PWM0=0x00;
        CCAP1L=CCAP1H=70;                                     //占空比
        PCA_PWM1=0x00;
}

/*停止函數(shù)*/
void Stop (void)
{
        IN1=0;
        IN2=0;
        IN3=0;
        IN4=0;
        CCAP0L=CCAP0H=0xFF;                                    //占空比0%,即低電平,失能
        PCA_PWM0=0x03;
        CCAP1L=CCAP1H=0xFF;                                    //占空比0%,即低電平,失能
        PCA_PWM1=0x03;
}

///*開關(guān)函數(shù)*/
//void Off_On (void)
//{        
//        if (Switch_Flag)
//        {
//                 Stop ();
//        }
//        Switch_Flag=~Switch_Flag;
//}
//
//void Continue (void)
//{
//        switch (Backup)
//        {
//                case 0x40: SlowTurnLeft ();break;
//                case 0x43: SlowTurnRight ();break;
//                case 0x15: Backward ();break;
//                case 0x09: Forward ();break;
//                case 0x08: FastTurnLeft ();break;
//                case 0x5A: FastTurnRight ();break;
//        }
//}
//
//void IrdAction (uint8 IrdCode)
//{
//        switch (IrdCode)
//        {
//                case 0x45: Off_On ();break;
//                case 0x40: if (Switch_Flag)
//                                   {
//                                                SlowTurnLeft ();
//                                                Backup=IrdCode;
//                                                Pause_Flag=0;
//                                   };break;
//                case 0x43: if (Switch_Flag)
//                                   {
//                                                SlowTurnRight ();
//                                                Backup=IrdCode;
//                                                Pause_Flag=0;
//                                   };break;
//                case 0x15: if (Switch_Flag)
//                                   {
//                                                Backward ();
//                                                Backup=IrdCode;
//                                                Pause_Flag=0;
//                                   };break;
//                case 0x09: if (Switch_Flag)
//                                   {
//                                                Forward ();
//                                                Backup=IrdCode;
//                                                Pause_Flag=0;
//                                   };break;
//                case 0x08: if (Switch_Flag)
//                                   {
//                                                FastTurnLeft ();
//                                                Backup=IrdCode;
//                                                Pause_Flag=0;
//                                   };break;
//                case 0x5A: if (Switch_Flag)
//                                   {
//                                                FastTurnRight ();
//                                                Backup=IrdCode;
//                                                Pause_Flag=0;
//                                   };break;
//                case 0x044: if (Switch_Flag)
//                                   {
//                                                   Pause_Flag=~Pause_Flag;
//                                                if (Pause_Flag)
//                                                {
//                                                        Stop ();
//                                                }
//                                                else
//                                                {
//                                                         Continue ();
//                                                }
//                                   };break;
//                default :break;
//        }
//}

/*循跡函數(shù)*/
void Self_Tracing (void)
{       
        switch (P0_PORT)
        {
                case 0x0F:FastTurnLeft ();break;
                case 0x1F:FastTurnLeft ();break;
                case 0x2F:FastTurnLeft ();break;
                case 0x3F:FastTurnLeft ();break;
                case 0x4F:FastTurnLeft ();break;
                case 0x5F:FastTurnLeft ();break;
                case 0x6F:FastTurnLeft ();break;
                case 0x7F:FastTurnLeft ();break;
                case 0x8F:FastTurnLeft ();break;
                case 0x9F:FastTurnLeft ();break;
                case 0xAF:FastTurnLeft ();break;
                case 0xBF:FastTurnLeft ();break;
                case 0xCF:FastTurnLeft ();break;
                case 0xDF:FastTurnLeft ();break;
                case 0xEF:FastTurnLeft ();break;
               
                case 0xF0:FastTurnRight();break;
                case 0xF1:FastTurnRight();break;
                case 0xF2:FastTurnRight();break;
                case 0xF3:FastTurnRight();break;
                case 0xF4:FastTurnRight();break;
                case 0xF5:FastTurnRight();break;
                case 0xF6:FastTurnRight();break;
                case 0xF7:FastTurnRight();break;
                case 0xF8:FastTurnRight();break;
                case 0xF9:FastTurnRight();break;
                case 0xFA:FastTurnRight();break;
                case 0xFB:FastTurnRight();break;
                case 0xFC:FastTurnRight();break;
                case 0xFD:FastTurnRight();break;
                case 0xFE:FastTurnRight();break;
               
                case 0xE7:Forward();break;
                case 0xFF:Stop ();break;
                default :Forward();break;
        }
}

#ifndef _MAIN_H
#define _MAIN_H

//extern void IrdAction (uint8 IrdCode);

#ifndef _MAIN_C

#endif
#endif

#define _PWM_C
#include"Config.h"
#include"PWM.h"

/*配置PCA模塊函數(shù),使其工作在PWM模式*/
void InitPCA (void)
{
        CMOD=0x0C;                                    //PCA 計數(shù)器脈沖源為11.0592MHz/6溢出
        CCAPM0=0x42;                                  //PWM模塊1為普通8位PWM, 無中斷
        CCAPM1=0x42;                                  //PWM模塊2為普通8位PWM, 無中斷
        CCAP0L=CCAP0H=0xFF;                           //初始化時使其(P1^3)輸出恒為0
        PCA_PWM0=0x03;
        CCAP1L=CCAP1H=0xFF;                           //初始化時使其(P1^4)輸出恒為0
        PCA_PWM1=0x03;
        CR=1;                                         //允許 PCA 計數(shù)器計數(shù)
}

#ifndef _PWM_H
#define _PWM_H

extern void InitPCA (void);

#ifndef _PWM_C

#endif
#endif

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:291594 發(fā)表于 2018-3-14 08:38 | 只看該作者
新來的,看到這個資料,很實用,雖然不懂,但給你點贊!最好能注解一下,謝謝
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表