这是个单字符的蓝牙控制的小车程序- /************************************************
- //蓝牙控制小车 加舵机 加避障 可以调速 正确的程序
- 单片机: STC12C5608AD
- // 晶振 : 11.0592M晶振
- 作者 :苏义江
- 时间 :2016-1-8
- ***************************************************/
- #include <reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- //sfr T2MOD=0xc9;
- sbit in1 =P1^0; //电机1端口
- sbit in2 =P1^1;
- sbit ina =P1^2; //电机PWM-A
- sbit in3 =P1^3;//电机2端口
- sbit in4 =P1^4;
- sbit inb =P1^5;//电机PWM-B
- sbit bz_l =P2^0; //避障左
- sbit bz_z =P2^1; //避障右
- sbit bz_r =P2^2; //避障右
- sbit moto_pwm =P3^4;//PWM输出脚控制舵机
- sbit yan =P3^5;//头灯光
- sbit landeng =P3^2;//蓝前大灯
- sbit hondeng =P3^3;//红前大灯
- sbit yinyue =P3^7;//音乐端口
- sbit wela =P1^6;//位码
- sbit dula =P1^7;//段码
- uchar code table[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,
- 0x82,0xF8,0x80,0x90,0xBF,0xff};
- uchar code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- //uchar disbuff[4] ={ 0,0,0,0,};//数码管全显示 0
- uint tt = 0;//变量定义
- uint num=0;//变量定义
- uchar pwm_lefr=0;//定义 定时器自加1变量
- //uchar pwm_lefr1=0;//定义 定时器自加1变量
- uchar duojiguizhong=14;//舵机归中1.5ms
- uchar duojiguizhong1=14;//舵机归中1.5ms
- uchar tmp,w,sudu; //变量w 定义PWM值
-
- void init() ;
- void delay1(uint z)//延时函数
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=120;y>1;y--);
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=0;x<z;x++)
- for(y=0;y<1000;y++);
-
- }
- /*void xianshi(uchar num)//显示
- {
- uchar gw,sw,bw,qianw;
- qianw=num/1000%100;
- bw=num%1000/100;
- sw=num%100/10;
- gw=num%10;
-
- P2=0xfe;
- wela=1;
- _nop_();_nop_();
- wela=0;
- P2=table[qianw];
- dula=1;
- _nop_();_nop_();
- dula=0;
- delay1(2);
- P2=0xfd;
- wela=1;
- _nop_();
- _nop_();
- wela=0;
- P2=table[bw];
- dula=1;
- _nop_();
- _nop_();
- dula=0;
- delay1(2);
-
- P2=0xfb;
- wela=1;
- _nop_();_nop_();
- wela=0;
- P2=table[sw];
- dula=1;
- _nop_();_nop_();
- dula=0;
- delay1(2);
- P2=0xf7;
- wela=1;
- _nop_();_nop_();
- wela=0;
- P2=table[gw];
- dula=1;
- _nop_();_nop_();
- dula=0;
- delay1(2); //显示个 十 百 千
-
- } */
- void pwmmaic()//产生PWM
- {
- if(pwm_lefr<=duojiguizhong)
- moto_pwm=1;
- else
- moto_pwm=0;
- if(pwm_lefr>=200)
- pwm_lefr=0;
- }
- void duoji_kz()//舵机控制
- {
- yan=0;
- duojiguizhong=9;
-
- delay(800);
- yan=0;
- duojiguizhong=16;
-
- delay(800);
- yan=0;
- duojiguizhong=24;
-
- delay(800);
- yan=0;
- duojiguizhong=16;
-
- delay(800);
- yan=0;
- delay(1500);
- yan=1;
- }
- void lankong() //蓝色LED
- {
- landeng=0;
- delay1(80);
- landeng=1;
- delay1(80);
- }
- void hongkong() //红色LED
- {
- hondeng=0;
- delay1(80);
- hondeng=1;
- delay1(80);
- }
- void qianjin()
- {
- in1=1;in2=0;in3=0;in4=1;
- w=40;
- yan=0;landeng=0;hondeng=0;yinyue=0;
- }
- void houtui()
- {
- in1=0;in2=1;in3=1;in4=0;
- w=40;
- yan=1;hondeng=0;yinyue=0;
- }
- void zuozhuan()
- {
- in3=1;in4=0;
- w=40;
- lankong();yinyue=1;
- }
- void youzhuan()
- {
- in1=0;in2=1;
- w=40;
- hongkong();yinyue=1;
- }
- void tingzhi()
- {
- in1=0;in2=0;in3=0 ;in4=0;
- hongkong();lankong();yinyue=1;
- }
- void zhuanquan()
- {
- in1=1;in2=0;in3=1;in4=0;
- w=40;
- lankong();yinyue=1;
- }
- void bizhang() //避障
- {
- if(bz_l==1&&bz_z==1)
- qianjin();
- if(bz_l==0&&bz_z==1)
- zuozhuan();
- if(bz_l==1&&bz_z==0)
- youzhuan();
- if(bz_l==0&&bz_z==0)
- {
- tingzhi();
- delay(100);
- zuozhuan();
- delay(400);
- qianjin();
- }
- /*
- if(bz_l==1&&bz_z==1&&bz_r==1)
- qianjin();
- if(bz_l==0&&bz_z==1&&bz_r==1)
- zuozhuan();
- if(bz_l==0&&bz_z==0&&bz_r==1)
- zuozhuan();
- if(bz_l==1&&bz_z==1&&bz_r==0)
- youzhuan();
- if(bz_l==1&&bz_z==0&&bz_r==0)
- youzhuan();
- if(bz_l==1&&bz_z==0&&bz_r==1)
- {
- tingzhi();
- delay(100);
- houtui();
- delay(500);
- zuozhuan();
- delay(200);
- qianjin();
- }
- if(bz_l==0&&bz_z==0&&bz_r==0)
- {
- tingzhi();
- delay(100);
- houtui();
- delay(500);
- zuozhuan();
- delay(200);
- qianjin();
- } */
- }
- void init() //串口初始化
- { //关中断
- SCON =0x50; //串口模式1,允许接收
- TMOD =0x20;//定时器1为模式2 8-bit自动装载方式用于产生波特率
- PCON=0x00;//波特率不倍增
- TH1=0xfd;//波特率9600 11.0592M
- TL1=0xfd;
- TH0=0xfd;
- TH0=0xae;
- EA=1;
- ES=1;
- ET0=1; // T0中断允许
- TR0=1; // 启动T0定时器
- TR1=1; // 启动T1定时器
- duojiguizhong=16;
- duojiguizhong1=16;
- }
- void ctrl() //接收处理函数
- {
- switch(tmp)
- {
- case 1:qianjin(); break;
- case 2: houtui();break;
- case 3:zuozhuan(); break;
- case 4:youzhuan();break;
- case 0:tingzhi(); break;
- case 7:duoji_kz();break; //舵机 C
- case 8: bizhang(); break; //避障 D
-
- /*case 1:duoji_kz();qianjin(); break;
- case 2:duoji_kz(); houtui();break;
- case 3:zuozhuan(); break;
- case 4:youzhuan();break;
- case 0:tingzhi(); break; */
- }
- }
- void tiaosu () //调速
- {
- switch(sudu)
- {
- case 5: zhuanquan(); break; //A
- case 6: w=75; break; //B
- }
- }
- void main()
- {
- init();
- while(1)
- {
- // xianshi( num ); //显示函数
- ctrl();
- tiaosu();
- }
- }
- void time0() interrupt 1
- {
- TH0=0xfd;
- TL0=0xae;
- tt++;
- pwm_lefr++;
- // pwm_lefr1++;
- pwmmaic();
- if(tt<w)
- {
- ina=1;
- inb=1;
- }
- else
- {
- ina=0;
- inb=0;
- }
- if(tt==100)
- {tt=0;
-
- num++;
- if(num==9999)num=0;
- }
- }
- void ckzd() interrupt 4
- {
- ES=0;
- RI=0;
- tmp=SBUF;
- sudu=SBUF;
- ES=1;
- }
复制代码 |