內容包含智能小車相關應用程序,
電路原理圖如下:
單片機源程序如下:
- #include<reg52.h>
- #include<math.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar Buffer =0; //從串口接收的數據
- uint URTAReceivedCount=0,n=1;
- uchar data Tempdatatable[5],CommandDatatable[5];//數據包
- uchar serVal[2];
- uint pwm[]={1382,1382,1382,1382,1382,1382,1382,1382}; //初始90度,(實際是1382.4,取整得1382)
- uchar pwm_flag=0;
- uchar Robots_Run_Status;
- uint code ms0_5Con=461; //0.5ms計數
- uint code ms2_5Con=2304; //2.5ms計數
- bit key_stime_ok;
- sbit StatusLight=P2^0; //狀態燈
- sbit MainLight=P2^1; //主大燈
- sbit Input_Detect_LEFT=P2^3; //右側探頭
- sbit Input_Detect_RIGHT=P2^4; //左側探頭
- sbit Input_Detect0=P2^2; //右側探頭
- sbit Input_Detect1=P2^5; //左側探測
- sbit servo0=P2^6; //舵機控制
- sbit servo1=P2^7;
- /********************************************************************
- * 名稱 : Delay_1ms()
- * 功能 : 延時子程序,延時時間為 1ms * x
- * 輸入 : x (延時一毫秒的個數)
- * 輸出 : 無
- ***********************************************************************/
- void Delay_1ms(uint i)//1ms延時
- {
- uchar x,j;
- for(j=0;j<i;j++)
- for(x=0;x<=148;x++);
- }
- void TurnOnStatusLight()
- {
- StatusLight=0;
- }
- /********************************************************************
- * 名稱 : Send_Data()
- * 功能 : 向上位機傳送字符
- * 輸入 : 無
- * 輸出 : 無
- ***********************************************************************/
- void Send_Data(uchar type,uchar cmd,uchar dat)
- {
- uchar data Buffer[5];//構建數據包
- uchar *p;
- uint Send_Count=0;
- p = Buffer;
- Buffer[0]=0XFF;
- Buffer[1]=type;
- Buffer[2]=cmd;
- Buffer[3]=dat;
- Buffer[4]=0XFF;
- while(1)
- {
- if(*p==0XFF)
- {
- Send_Count++; //0XFF標志統計位
- }
- SBUF = *p; //發送
- while(!TI) //如果發送完畢,硬件會置位TI,等待發送完畢
- {
- _nop_();
- }
- p++;
- TI = 0;
- if(Send_Count == 2) //當統計到兩次出現0XFF,則認為一個數據包發送完畢,跳出循環
- {
- TI = 0;
- break;
- }
- }
- }
復制代碼
聲波測距,超聲波避障,紅外循跡,紅外避障等,希望大家互相學習交流
全部資料51hei下載地址:
智能小車相關資料.rar
(5.2 MB, 下載次數: 109)
2019-4-22 18:22 上傳
點擊文件名下載附件
|