|
小車(chē)的單片機(jī)源程序如下:
- #include<reg52.h>
- ////////////電機(jī)轉(zhuǎn)動(dòng)
- sbit P30=P2^0;
- sbit P31=P2^1;
- sbit P32=P2^2;
- sbit P33=P2^3;
- /////////pwm調(diào)試使能端
- sbit ENA=P0^0;
- sbit ENB=P0^1;
- ////////////四路循跡
- sbit P10=P1^7;
- sbit P11=P1^6;
- sbit P12=P1^5;
- sbit P13=P1^4;
- ////////////////
- #define Right_moto_pwm P0^0 //接驅(qū)動(dòng)模塊ENA使能端輸入PWM信號(hào)調(diào)節(jié)速度
- void delay(unsigned int t); //函數(shù)聲明
- #define Left_moto_pwm P0^1 //接驅(qū)動(dòng)模塊ENB使能端輸入PWM信號(hào)調(diào)節(jié)速度
- void Init_Timer0(void);//定時(shí)器初始化
-
- ///////////////定義電機(jī)轉(zhuǎn)動(dòng)方向
- #define Left_moto_back {P30=1,P31=0;} //左電機(jī)后退
- #define Left_moto_go {P30=0,P31=1;} //左電機(jī)前進(jìn)
- #define Left_moto_stop {P30=1,P31=1;} //左電機(jī)停轉(zhuǎn)
- #define Right_moto_back {P32=1,P33=0;} //右電機(jī)后退
- #define Right_moto_go {P32=0,P33=1;} //右電機(jī)前轉(zhuǎn)
- #define Right_moto_stop {P32=1,P33=1;} //右電機(jī)停轉(zhuǎn)
- //////////////////////////////
- #define uchar unsigned char
- #define uint unsigned int
- /////////////////////////////
- uchar pwm_val_left =0;
- uchar push_val_left =0; //左電機(jī)占空比N/10
- uchar pwm_val_right =0;
- uchar push_val_right=0; //右電機(jī)占空比N/10
- bit Right_moto_stp=1;
- bit Left_moto_stp =1;
- uint num,i,d,j=0;
- /****************************************************************
- ********/
- void run(void) //前進(jìn)函數(shù)
- {
- push_val_left =17; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度
- push_val_right =17; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度
- Left_moto_go ; //左電機(jī)前進(jìn)
- Right_moto_go ; //右電機(jī)前進(jìn)
- }
- void run1(void)//前進(jìn)函數(shù)1
- {
- push_val_left =4.8; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度
- push_val_right =4.8; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度
- Left_moto_go ; //左電機(jī)前進(jìn)
- Right_moto_go ; //右電機(jī)前進(jìn)
- }
- void run2(void)//前進(jìn)函數(shù)1
- {
- push_val_left =21; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度
- push_val_right =21; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度
- Left_moto_go ; //左電機(jī)前進(jìn)
- Right_moto_go ; //右電機(jī)前進(jìn)
- }
- /****************************************************************
- ********/
- void left(void) //左轉(zhuǎn)函數(shù)
- {
- push_val_left =19;
- push_val_right =19;
- Right_moto_go; //右電機(jī)繼續(xù)
- Left_moto_stop; //左電機(jī)停走
- }
- void left1(void) //左轉(zhuǎn)函數(shù) 1
- {
- push_val_left =20;
- push_val_right =20;
- Right_moto_go; //右電機(jī)繼續(xù)
- Left_moto_stop; //左電機(jī)停走
- }
- /*************************************** *********************************/
- void right(void) //右轉(zhuǎn)函數(shù)
- {
- push_val_left =19;
-
- push_val_right =19;
- Right_moto_stop; //右電機(jī)停走
- Left_moto_go; //左電機(jī)繼續(xù)
- }
- void right1(void) //右轉(zhuǎn)函數(shù)1
- {
- push_val_left =20;
- push_val_right =20;
- Right_moto_stop; //右電機(jī)停走
- Left_moto_go; //左電機(jī)繼續(xù)
- }
- ///////////////////////////////////////////////////////////////停止
- void stop(void)
- {
- Right_moto_stop; //右電機(jī)停走
- Left_moto_stop; //左電機(jī)停走
- //run();
- //
- //Delayms(100);
- }
- ///////////////////////////////////////////
- void zzhijiao()
- {
- push_val_left =19;
-
- push_val_right =19;
-
- Left_moto_go ; //左電機(jī)前進(jìn)
-
- Right_moto_back ;
- }
- ///////////////////////////////////////
- void yzhijiao()
- {
- push_val_left =19;
- push_val_right =19;
-
- Left_moto_back ; //左電機(jī)前進(jìn)
-
- Right_moto_go ;}
- /*************************PWM調(diào) 制 電 機(jī) 轉(zhuǎn) 速
- ********************************/
- void pwm_out_left_moto(void) //左電機(jī)調(diào)速,調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比
- {
- if(Left_moto_stp)
- {
- {if(pwm_val_left<=push_val_left)
- { ENB=1;}
- else
- {ENB=0;}
- }
- {if(pwm_val_left>=20)
- {pwm_val_left=0;}
- }
- }
- else
- {ENB=0;}
- }
- void pwm_out_right_moto(void) //右電機(jī)調(diào)速,調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比
- {
- if(Right_moto_stp)
- {
- if(pwm_val_right<=push_val_right)
- {ENA=1;}
- else
- {ENA=0;}
- if(pwm_val_right>=20)
- {pwm_val_right=0;}
- }
- else
- {ENA=0;}
-
- }
- /***************************************************/
- void xunji()
- {
- uchar a=0,b=0;
-
- if( P10==1&&P11==1&&P12==1&&P13==1)
- {
- i++;
- }
- if( P10==0&&P11==0&&P12==0&&P13==0)
- {
- run();
- }
- if( P10==0&&P11==0&&P12==1&&P13==0||P10==0&&P11==0&&P12==0&&P13==1)
- {
- right();
- }
- if( P10==0&&P11==1&&P12==0&&P13==0||P10==1&&P11==0&&P12==0&&P13==0)
- {
- left();
- }
- if( P10==1&&P11==1&&P12==1&&P13==1&&i>=500) //600
- {
- j++;
- }
- while(j==2)
- {
- if( P10==0&&P11==0&&P12==0&&P13==0)
- {
- run1();
- }
-
- if( P10==0&&P11==0&&P12==1&&P13==0||P10==0&&P11==0&&P12==0&&P13==1)
- {
- right();
- }
- if( P10==1&&P11==1&&P12==0&&P13==0||P10==1&&P11==1&&P12==1&&P13==0)
- {
-
- yzhijiao();
- a++;
- }
- if( P10==0&&P11==0&&P12==1&&P13==1||P10==0&&P11==1&&P12==1&&P13==1)
- {
- zzhijiao();
- d++;
-
- }
- if( P10==0&&P11==1&&P12==0&&P13==0||P10==1&&P11==0&&P12==0&&P13==0)
- { left();}
-
-
- if(a+d>=2400)
- {
- if( P10==0&&P11==0&&P12==0&&P13==0)
- {
- run2();
- }
- if( P10==0&&P11==0&&P12==1&&P13==0||P10==0&&P11==0&&P12==0&&P13==1)
- {
- right1();
- }
- if( P10==0&&P11==1&&P12==0&&P13==0||P10==1&&P11==0&&P12==0&&P13==0)
- {
- left1();
- }
- if( P10==1&&P11==1&&P12==1&&P13==1) //600
- {
- stop();
-
- }
- }
- }
-
-
- }
-
- /***********TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)**********/
- void Init_Timer0()interrupt 1 using 2
- {
-
- TH0=0XFC; //2Ms定時(shí)
- TL0=0X30;
-
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
循跡小車(chē).rar
(24.16 KB, 下載次數(shù): 489)
2017-10-27 22:27 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|