1、智能小车源代码#include #include /#includelcd.hsbit TX=P27;sbit RX=P26;sbit PWM=P14;sbit Beep=P37;sbit led1=P33;sbit led2=P34;sbit led3=P35;/*蓝牙切换按键*/sbit k1=P36;/*自定义一些数据,注意这些数据对应“51智能车蓝牙无线遥控.exe”软件上的数据设置*/#define leftdata 0x11#define rightdata 0x22#define forwarddata 0x33#define backdata 0x44#define stopd
2、ata 0x55#define uchar unsigned char#define uint unsigned intunsigned char receiveData,high_velosity,low_velosity; void UsartConfiguration();/*电机端口定义*/sbit IN1=P10; /为高电平时,左电机后转 sbit IN2=P11; /为高电平时,左电机正转sbit IN3=P12; /为高电平时,右电机正转sbit IN4=P13; /为高电平时,右电机后转/*无线遥控模块定义*/sbit Key_A = P20; /B键信号端 对应D0sbit
3、 Key_B = P21; /D键信号端 对应D1sbit Key_C = P22; /C键信号端 对应D2sbit Key_D = P23; /C键信号端 void delay0(int a);void delay15us(void);void delay1ms(int z);void di(); /蜂鸣器函数声明unsigned int time;/用于存放定时器时间值unsigned int S; /用于存放距离的值char flag =0; /量程溢出标志位char f1,f2,f3,f4,n0;int a;int n;/运行次数判断标志int s_left,s_right; void
4、 Delay10us(unsigned char i) unsigned char j; do j = 10; do _nop_(); while(-j); while(-i); /*/ /*电机程序*/*/void loop() IN1=1; /左电机 IN2=0; IN3=1; /右电机 IN4=0;void runfront() IN1=1; /左电机 IN2=0; IN3=0; /右电机 IN4=1;/小车后退函数void runback() IN1=0; /左电机 IN2=1; IN3=1; /右电机 IN4=0;/小车左转void runleft() IN1=0; /左电机 IN2
5、=0; IN3=0; /右电机 IN4=1;/小车右转void runright() IN1=1; /左电机 IN2=0; IN3=0; /右电机 IN4=0;/小车左后转/void runbackleft()/ IN1=0; /左电机/ IN2=0;/ IN3=0; /右电机/ IN4=1;/小车右后转/void runbackright()/ IN1=1; /左电机/ IN2=0;/ IN3=0; /右电机/ IN4=0;/小车停止函数void stop() IN1=0; /左电机 IN2=0; IN3=0; /右电机 IN4=0;/*/ /*蓝牙程序*/*/void delay(unsi
6、gned int a) unsigned int b; for(;a0;a-) for(b=3;b0;b-);/左电机转void left_motor_runfront(unsigned int a) IN1=1; /左电机 IN2=0; delay(a); IN1=0; IN2=0; delay(100-a); void right_motor_runfront(unsigned int a) IN3=0; /右电机 IN4=1; delay(a); IN3=0; IN4=0; delay(100-a); void left_motor_back(unsigned int a) IN1=0;
7、 /左电机 IN2=1; delay(a); IN1=0; IN2=0; delay(100-a); void right_motor_back(unsigned int a) IN3=1; /右电机 IN4=0; delay(a); IN3=0; IN4=0; delay(100-a); void left_motor_stop() IN1=0; /左电机 IN2=0; void right_motor_stop() IN3=0; /左电机 IN4=0; /PWM调速void PWM_RUNFRONT(unsigned int a,unsigned int b) /a属于0100 left_
8、motor_runfront(a); right_motor_runfront(b); void PWM_RUNBACK(unsigned int a,unsigned int b) /a属于0100 left_motor_back(a); right_motor_back(b); void PWM_RUNLEFT(unsigned int a) /a属于0100 right_motor_runfront(a); left_motor_stop(); void PWM_RUNRIGHT(unsigned int a) /a属于0100 right_motor_stop(); left_moto
9、r_runfront(a); void bluestop() left_motor_stop() ; right_motor_stop() ; void UsartConfiguration() SCON=0X50; /设置为工作方式1 TMOD=TMOD|0x21; /设置计数器工作方式2 PCON=0X80; /波特率加倍 TH1=0XFA; /计数器初始值设置,注意波特率是4800的 TL1=0XFA; ES=1; /打开接收中断 EA=1; /打开总中断 TR1=1; TH0=0; TL0=0; ET0=1; /打开计数器/* 函数名 :Delay(unsigned int i)* 函
10、数功能 : 延时函数* 输入 : 无* 输出 : 无*/void Usart() interrupt 4 receiveData=SBUF;/出去接收到的数据 RI = 0;/清除接收中断标志位 if(receiveData=0x66) low_velosity=1; high_velosity=0;/ write_com(0x80); if(receiveData=0x77) low_velosity=0; high_velosity=1; /*/ /*舵机程序*/*/void delayx(int t) char j; for(;t0;t-) for(j=19;j0;j-);void de
11、gree0() int i; for(i=0;i10;i+) PWM=1; delayx(10);/1ms PWM=0; delayx(390);/19ms void degree90() int i; for(i=0;i10;i+) PWM=1; delayx(24);/50 PWM=0; delayx(376);/18.5ms void degree180() int i; for(i=0;i15;i+) PWM=1; delayx(40);/1.5ms PWM=0; delayx(360);/18.5ms /*/ /*超声波程序*/*/*A键被按下后,前进*/void Count1(vo
12、id) time=TH1*256+TL1; TH1=0; TL1=0; S=(time*1.7)/100; n+; if(S30&n=1) led1=1; runfront(); n=0; if(n=2) s_left=S; degree180();di(); if(n=3) s_right=S; degree90();di(); n=0; /标志位清零 if(s_lefts_right) led2=1;delay1ms(50);led2=0;delay1ms(50); runright(); delayx(1250); else led3=1;delay(800);led3=0;delay(
13、500); runleft(); delayx(1250); /*超声波高电平脉冲宽度计算程序*/void Timer_Count(void) TR1=1; /开启计数 while(RX); /当RX为1计数并等待 TR1=0; /关闭计数 /Count1(); /计算/*/void StartModule() /启动模块 TX=1; /启动一次模块 Delay10us(2); TX=0;/*/ /*主程序*/*/void main(void) UsartConfiguration(); while(1) if(k1=0&n0=0) delay1ms(20); if(k1=0) n0+; el
14、se if(k1=0&n0=1) delay1ms(20); if(k1=0) n0-; if(n0=0) if(Key_B=0&Key_D=0&Key_C=0&Key_A=1) /A键被按下 f1=1;f2=0;f3=0;f4=0;di(); else if(Key_A=0&Key_D=0&Key_B=1&Key_C=0) /B键被按下 f1=0;f2=1;f3=0;f4=0;di(); else if(Key_B=0&Key_D=0&Key_A=0&Key_C=1) /C键被按下 f1=0;f2=0;f3=1;f4=0;di(); else if(Key_D=1&Key_B=0&Key_A
15、=0&Key_C=0) /D键被按下 f1=0;f2=0;f3=0;f4=1;di(); if(f1) StartModule(); for(a=951;a0;a-) if(RX=1) Timer_Count(); Count1(); if(f2) runback(); if(f3) loop(); if(f4) stop(); if(n0=1) if(low_velosity) switch(receiveData) case leftdata: PWM_RUNLEFT(30); break; case rightdata: PWM_RUNRIGHT(30); break; case for
16、warddata: PWM_RUNFRONT(64,70); break; case backdata: PWM_RUNBACK(64,70); break; case stopdata: bluestop(); break; if(high_velosity) switch(receiveData) case leftdata: PWM_RUNLEFT(50); break; case rightdata: PWM_RUNRIGHT(50); break; case forwarddata: PWM_RUNFRONT(94,100); break; case backdata: PWM_RUNBACK(94,100); break; case stopdata: stop(); break; /*蜂鸣器*/void di() int i; for(i=0;i 0; i-) for(j = 200; j 0; j-);void delay1ms(int z) /延时1ms int x,y; for(x=z;x0;x-) for(y=110;y0;y-);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1