//Wifi 智能小车 第二版
//小车增加调速功能
//Wonder Technology
//单片机采用22.1184M晶振
//加入控制摄像头的直流驱动
#include <STC12C5A.h> //包含STC12C5A系列单片机的头文件
#include"1602.h"
sbit LED1=P3^0; //定义两只LED到P3^0,P3^1
sbit LED2=P3^1;
sbit Light_F=P3^4;
sbit Light_B=P2^3;
sbit beed=P2^7;
sbit KEY1=P3^6;
sbit KEY2=P3^7;
sbit EN1=P2^0;
sbit IN1=P2^1;
sbit IN2=P2^2;
sbit EN2=P2^4;
sbit IN3=P2^5;
sbit IN4=P2^6;
// 舵机直流电机
sbit EN1_S=P1^0;
sbit IN1_S=P1^1;
sbit IN2_S=P1^2;
sbit EN2_S=P1^3;
sbit EN3_S=P1^4;
sbit EN4_S=P1^5;
sbit SPWM1=P1^6;
unsigned long PWM_cnt=0;
unsigned int PWM_G1=5;
int speed_R=0; //定义两个电机的速度,有正负之分,为正是,正转。(范围-100~100)
int speed_L=0;
int pre_RS=0;
int pre_LS=0;
int set_RS=0;
int set_LS=0;
int speed_A=0;
unsigned int cnt_s=0; //用于计数PWM的点空比0=100;
unsigned long cnt_b=0;
unsigned long cnt_beed=0; //用于控制蜂鸣器
unsigned int flag_beed=1;
unsigned int B_counter=0;
unsigned char R_date[3]={0};
unsigned char RBuff_date[3]={0};
unsigned char pattern=0; //0:stop 1:start
unsigned long cnt_ser=0; //用于舵机直流电机控制
unsigned char flag_ser=0;
/*
void delay(unsigned long t) //软件延时带参数t
{
unsigned int j,k;
for(t;t>0;t--)
for(j=20;j>0;j--)
for(k=100;k>0;k--);
}
*/
void beed_ctrl(unsigned long time , unsigned int num)
{
beed=0;
cnt_beed=time;
B_counter=2*num;
beed=1;
cnt_b=0;
}
void display_1602(unsigned int addr,unsigned char temp)
{
unsigned int i,w[2];
w[0]=temp/16;
w[1]=temp%16;
for(i=0;i<2;i++)
{
switch(w[i])
{
case 0: print(addr+i,"0"); break;
case 1: print(addr+i,"1"); break;
case 2: print(addr+i,"2"); break;
case 3: print(addr+i,"3"); break;
case 4: print(addr+i,"4"); break;
case 5: print(addr+i,"5"); break;
case 6: print(addr+i,"6"); break;
case 7: print(addr+i,"7"); break;
case 8: print(addr+i,"8"); break;
case 9: print(addr+i,"9"); break;
case 10: print(addr+i,"A"); break;
case 11: print(addr+i,"B"); break;
case 12: print(addr+i,"C"); break;
case 13: print(addr+i,"D"); break;
case 14: print(addr+i,"E"); break;
case 15: print(addr+i,"F"); break;
default: break;
}
}
}
void display_1602_10(unsigned int addr,unsigned char temp)
{
unsigned int i,w[3];
w[0]=temp/100;
w[1]=temp/10%10;
w[2]=temp%10;
for(i=0;i<3;i++)
{
print2(addr+i,w[i]);
}
}
/****************servo up***********/
void ser_up()
{
EN1_S=1;
IN1_S=1;
IN2_S=0;
flag_ser=1;
}
void ser_down()
{
EN1_S=1;
IN1_S=0;
IN2_S=1;
flag_ser=1;
}
/*************init To************/
void init_T0(void)
{
TMOD|=0x01; //定时器工作在模式1
TH0=0xff; //定时初值TH0=0xff TL0=0x49 定时100uS
TL0=0x49;
TR0=1;
ET0=1;
}
/************TO interrupt********/
void timer0(void) interrupt 1
{
TH0=0xff;
TL0=0x49;
cnt_s++; //PWM占空比计数 0-100
if(cnt_s>=speed_R)
{
EN1=0;
}
if(cnt_s>=speed_L)
{
EN2=0;
}
if(cnt_s>=100)
{
EN1=1;
EN2=1;
cnt_s=0;
}
}
/*************motor control***************/
void motor( int m_R, m_L)
{
pre_RS=m_R;
pre_LS=m_L;
//right motor
if(m_R>0)
{
IN1=0;
IN2=1;
speed_R=m_R;
}
else if (m_R<0)
{
IN1=1;
IN2=0;
m_R=-m_R;
speed_R=m_R;
}
else
{
IN1=0;
IN2=0;
}
//left motor
if(m_L>0)
{
IN3=0;
IN4=1;
speed_L=m_L;
}
else if(m_L<0)
{
IN3=1;
IN4=0;
m_L=-m_L;
speed_L=m_L;
}
else
{
IN3=0;
IN4=0;
}
}
/***********初始化系统所有的参数********************/
void init_system()
{
unsigned int i;
motor(0,0);
beed=1;
Light_F=1;
Light_F=1;
speed_A=0;
for(i=0;i<3;i++)
{
R_date[i]=0;
RBuff_date[i]=0;
}
}
/****************速度记录****************************/
int Mark_Speed(int set)
{
int s_temp;
if(set==0)
{
s_temp=0;
}
else if(set<0)
{
s_temp=-speed_A;
}
else
{
s_temp=speed_A;
}
return s_temp;
}
/**********UART***************采用独立波特率发生器******/
void initUart(void) //4800 波特率
{
SCON=0x50;
PCON=0xB0;
BRT=0xfa;
AUXR=0x11;
ES=1;
}
void SCI_TX(unsigned char date)
{
SBUF=date;
while(!TI);
TI=0;
}
void uart(void) interrupt 4
{
if(RI)
{
RI=0;
//add your code here.
RBuff_date[0]= RBuff_date[1];
RBuff_date[1]= RBuff_date[2];
RBuff_date[2]=SBUF;
if(RBuff_date[0]==0x7B && RBuff_date[1]==0x31 && RBuff_date[2]==0x7D)
{
pattern=1;
print(0x4f,"1");
beed_ctrl(1500,1);
}
else if(RBuff_date[0]==0x7B && RBuff_date[1]==0x30 && RBuff_date[2]==0x7D)
{
pattern=0;
print(0x4f,"0");
init_system();
beed_ctrl(500,2);
}
else if( RBuff_date[0]==0x50 && RBuff_date[2]==0x00 )
{
speed_A=RBuff_date[1];
set_RS=Mark_Speed(pre_RS);
set_LS=Mark_Speed(pre_LS);
motor( set_LS, set_RS);
}
else
{
R_date[0]=RBuff_date[0];
R_date[1]=RBuff_date[1];
R_date[2]=RBuff_date[2];
}
}
else
TI=0;
}
/**************interrupt T1***********/
void timer1(void) interrupt 3
{
TH1=0xff;
TL1=0x49;
//add your code here.
PWM_cnt++;
if(PWM_cnt >=PWM_G1 )
{
SPWM1=0;
}
if(PWM_cnt >= 20)
{
PWM_cnt=0;
SPWM1=1;
}
cnt_b++;
if(flag_beed==1)
{
if(B_counter>0)
{
if(cnt_b>cnt_beed)
{
beed=!beed;
cnt_b=0;
B_counter--;
}
}
}
if(flag_ser==1)
{
cnt_ser++;
if(cnt_ser>100)
{
EN1_S=0;
flag_ser=0;
cnt_ser=0;
}
}
}
/**************init T1****************/
void init_T1(void)
{
TMOD|=0x10;
TH1=0xff;
TL1=0x49;
TR1=1;
ET1=1;
}
main(void)
{
LED1=1; //高电平LED不亮
LED2=1; //
P4SW=0xff; //定义ALE PSEN 。。三个引脚为P4.4 P4.5 P4.6 用于控制1602显示屏
init_T0(); //初始化定时器。
init_T1();
CLK_DIV=0x01; //系统分频工作,速度太快,1602控制不了。
LCM2402_Init(); // 初如化1602显示屏
initUart();
init_system();
EA=1;
beed_ctrl(800,3);
print(0x00,"WT"); //在第一行第一位处从左向右打印"WonderTechnology" 字符串
print(0x40,"RX:");
print(0x0B,"S:");
print(0x4f,"0");
while(1)
{
if(KEY1==0) //按下第一按键发送“0x55“
{
while(!KEY1);
ser_up();
}
if(KEY2==0) //按下第二按键发送“0xf0“
{
while(!KEY2);
// SCI_TX(0xf0);
ser_down();
}
// display_1602_10(0x4D,PWM_G1);
display_1602_10(0x0D,speed_A);
display_1602(0x43,RBuff_date[0]);
display_1602(0x46,RBuff_date[1]);
display_1602(0x49,RBuff_date[2]);
if(pattern==1)
{
switch(R_date[0])
{
case 0x44:
switch(R_date[1])
{
case 0x4c:
switch(R_date[2])
{
case 0x31: motor(0,speed_A); break;
case 0x32: motor(-speed_A,speed_A); break;
case 0x33: motor(0,-speed_A); break;
default:
break;
}
break;
case 0x44:
switch(R_date[2])
{
case 0x31: motor(speed_A,speed_A); break;
case 0x32: motor(0,0); break;
case 0x33: motor(-speed_A,-speed_A); break;
default:
break;
}
break;
case 0x52:
switch(R_date[2])
{
case 0x31: motor(speed_A,0); break;
case 0x32: motor(speed_A,-speed_A); break;
case 0x33: motor(-speed_A,0); break;
default:
break;
}
break;
default:
break;
}
break;
case 0x46:
switch(R_date[1])
{
case 0x4c:
switch(R_date[2])
{
case 0x31:Light_F=0; break;
case 0x32:Light_F=1; break;
case 0x33:Light_B=0; break;
case 0x34:Light_B=1; break;
default:
break;
}
break;
case 0x42:
switch(R_date[2])
{
case 0x31: beed=0; break;
case 0x30: beed=1; break;
- 1
- 2
前往页