实物图:
#include
unsigned char pwm_left_val = 210;//左电机占空比值 取值范围0-170,0最快
unsigned char pwm_right_val = 210;//右电机占空比值取值范围0-170 ,0最快
unsigned char pwm_t;//周期
/*电机驱动IO定义*/
sbit IN1 = P2^0; //为1 左电机反转
sbit IN2 = P2^1; //为1 左电机正转
sbit IN3 = P2^2; //为1 右电机正转
sbit IN4 = P2^3; //为1 右电机反转
sbit EN1 = P2^4; //为1 左电机使能
sbit EN2 = P2^5; //为1 右电机使能
sbit STBY = P2^6;//置0电机全部停止;置1电机才可按指令转动
/*红外对管IO定义*/
sbit L1=P1^0;
sbit L2=P1^1;
sbit zj=P1^4;
sbit R3=P1^2;
sbit R4=P1^3;
#define left_motor_en EN1 = 1 //左电机使能
#define left_motor_stops EN1 = 0 //左电机停止
#define right_motor_en EN2 = 1 //右电机使能
#define right_motor_stops EN2 = 0 //右电机停止
#define left_motor_go IN1 = 0, IN2 = 1//左电机正传
#define left_motor_back IN1 = 1, IN2 = 0//左电机反转
#define right_motor_go IN3 = 1, IN4 = 0//右电机正传
#define right_motor_back IN3 = 0, IN4 = 1//右电机反转
/*小车前进*/
void forward()
{
left_motor_go; //左电机前进
right_motor_go; //右电机前进
}
//定时器0中断
void timer0() interrupt 1
{
pwm_t++;
if(pwm_t == 255)
pwm_t = EN1 = EN2 = 0;
if(pwm_left_val == pwm_t)
EN1 = 1;
if(pwm_right_val == pwm_t)
EN2 = 1;
}
void main()
{
TMOD |= 0x02;//8位自动重装模块
TH0 = 220;
TL0 = 220;//11.0592M晶振下占空比最大比值是256,输出100HZ
TR0 = 1;//启动定时器0
ET0 = 1;//允许定时器0中断
EA = 1;//总中断允许
while(1)
{
forward();//前进
}
}
/* --- STC15F系列 PCA输出6/7/8位PWM举例-------------------------*/
//工作频率一般为11.0592MHz
#include "reg51.h"
#include "intrins.h"
#define FOSC 11059200L
typedef unsigned char BYTE;
typedef unsigned int WORD;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
sfr P_SW1 = 0xA2; //外设功能切换寄存器1
#define CCP_S0 0x10 //P_SW1.4
#define CCP_S1 0x20 //P_SW1.5
sfr CCON = 0xD8; //PCA控制寄存器
sbit CCF0 = CCON^0; //PCA模块0中断标志
sbit CCF1 = CCON^1; //PCA模块1中断标志
sbit CR = CCON^6; //PCA定时器运行控制位
sbit CF = CCON^7; //PCA定时器溢出标志
sfr CMOD = 0xD9; //PCA模式寄存器
sfr CL = 0xE9; //PCA定时器低字节
sfr CH = 0xF9; //PCA定时器高字节
sfr CCAPM0 = 0xDA; //PCA模块0模式寄存器
sfr CCAP0L = 0xEA; //PCA模块0捕获寄存器 LOW
sfr CCAP0H = 0xFA; //PCA模块0捕获寄存器 HIGH
sfr CCAPM1 = 0xDB; //PCA模块1模式寄存器
sfr CCAP1L = 0xEB; //PCA模块1捕获寄存器 LOW
sfr CCAP1H = 0xFB; //PCA模块1捕获寄存器 HIGH
sfr CCAPM2 = 0xDC; //PCA模块2模式寄存器
sfr CCAP2L = 0xEC; //PCA模块2捕获寄存器 LOW
sfr CCAP2H = 0xFC; //PCA模块2捕获寄存器 HIGH
sfr PCA_PWM0 = 0xf2; //PCA模块0的PWM寄存器
sfr PCA_PWM1 = 0xf3; //PCA模块1的PWM寄存器
sfr PCA_PWM2 = 0xf4; //PCA模块2的PWM寄存器
void main()
{
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
P7M0 = 0x00;
P7M1 = 0x00;
ACC = P_SW1;
ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=0 CCP_S1=0
P_SW1 = ACC; //(P1.2/ECI, P1.1/CCP0, P1.0/CCP1, P3.7/CCP2)
// ACC = P_SW1;
// ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=1 CCP_S1=0
// ACC |= CCP_S0; //(P3.4/ECI_2, P3.5/CCP0_2, P3.6/CCP1_2, P3.7/CCP2_2)
// P_SW1 = ACC;
//
// ACC = P_SW1;
// ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=0 CCP_S1=1
// ACC |= CCP_S1; //(P2.4/ECI_3, P2.5/CCP0_3, P2.6/CCP1_3, P2.7/CCP2_3)
// P_SW1 = ACC;
CCON = 0; //初始化PCA控制寄存器
//PCA定时器停止
//清除CF标志
//清除模块中断标志
CL = 0; //复位PCA寄存器
CH = 0;
CMOD = 0x02; //设置PCA时钟源
//禁止PCA定时器溢出中断
PCA_PWM0 = 0x00; //PCA模块0工作于8位PWM
CCAP0H = CCAP0L = 0x20; //PWM0的占空比为87.5% ((100H-20H)/100H)
CCAPM0 = 0x42; //PCA模块0为8位PWM模式
PCA_PWM1 = 0x40; //PCA模块1工作于7位PWM
CCAP1H = CCAP1L = 0x20; //PWM1的占空比为75% ((80H-20H)/80H)
CCAPM1 = 0x42; //PCA模块1为7位PWM模式
PCA_PWM2 = 0x80; //PCA模块2工作于6位PWM
CCAP2H = CCAP2L = 0x20; //PWM2的占空比为50% ((40H-20H)/40H)
CCAPM2 = 0x42; //PCA模块2为6位PWM模式
CR = 1; //PCA定时器开始工作
while (1);
}
其实这里只是用
模块0和模块1就好,调节成自己想要的
工作位数及
占空比即可!占空比控制着车的速度,占空比越高小车速度越快,占空比越低小车速度越慢。
//-----------------------------------------
//中断服务程序
void exint0() interrupt 0 //INT0中断入口
{
while(zj==0)
{
right_motor_go;
IN1 = 1;IN2 = 1;
}
}
当
L1检测到黑线时,小车偏离黑线靠右,进入中断服务函数左转,,中断服务函数里面使用一个
while结构,当标号为
zj的循迹模块检测到黑线时,立即跳出中断服务函数,while函数里面即为定义的左转函数!!