【完整版】基于51的避障循迹重力感应遥控的智能小车设计40C语言41_毕业论文 下载本文

}

SPI_RW(reg); Select register to read from.. reg_val = SPI_RW(0); ..then read registervalue CSN = 1; CSN

return(reg_val); return register value

uint SPI_RW_Reg(uchar reg, uchar value) { }

uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars) {

uint status,uchar_ctr;

CSN = 0; status = SPI_RW(reg);

for(uchar_ctr=0;uchar_ctr

CSN = 1;

return(status); return nRF24L01 status uchar

13 uint status;

CSN = 0; CSN low, init SPI transaction status = SPI_RW(reg); select register

SPI_RW(value); ..and write value to it.. CSN = 1; CSN

return(status); return nRF24L01 status uchar

Set CSN low, init SPI tranaction

Select register to write to and read status uchar

pBuf[uchar_ctr] = SPI_RW(0);

}

uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) { uint status,uchar_ctr;

CSN = 0; SPI使能 status = SPI_RW(reg);

for(uchar_ctr=0; uchar_ctr

SPI_RW(*pBuf++);

CSN = 1; 关闭SPI return(status);

}

unsigned char nRF24L01_RxPacket(unsigned char * rx_buf) {

unsigned char revale=0; CE = 1;

inerDelay_us(130);

sta=SPI_Read(STATUS); 读取状态寄存其来判断数据接收状况 if(RX_DR)

判断是否接收到数据

{

CE = 0;

SPI使能

SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);

read

payload from RX_FIFO buffer revale =1;

读取数据完成标志

}

SPI_RW_Reg(WRITE_REG+STATUS,sta);

RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 return revale;

}

14 receive

4.2 遥控端程序

文件一< main.c >

#include

uchar X_SIGN=0;小车转弯标志 1:小车左转弯 0:小车右转弯 uchar Y_SIGN=0; uchar SIGN=0;

uchar FLAG_ANGLE=0; 1 :小车前进或后退(可差速转弯) 0 :原地打转 uchar speed1,speed2; uchar flag_key=0; uchar bai,shi,ge; unsigned int bb;

uchar BACK_LED_FLAG=0; int X_ANGLE; int Y_ANGLE; uchar a=0;

BACK_LED_FLAG=1; BACK_LED=1; init_system();系统初始化 while(1) {

***********获取MPU0605坐标数据,处理后发送至小车端************

if(aa%2==0) {

X_ANGLE=GetData(ACCEL_XOUT_H); Y_ANGLE=GetData(ACCEL_YOUT_H);

15

************将X轴倾斜数据转化成十进制****************

X_ANGLE=64; if(X_ANGLE<0) { }

else X_SIGN=0; bai =X_ANGLE100;

X_ANGLE=X_ANGLE0; 取余运算 shi =X_ANGLE10;

X_ANGLE=X_ANGLE; 取余运算 ge =X_ANGLE;

X_ANGLE=bai*100+shi*10+ge;

X_SIGN=1;

X_ANGLE=-X_ANGLE;

***********将Y轴倾斜数据转化成十进制**********

Y_ANGLE=64; if(Y_ANGLE<0) { }

else Y_SIGN=0; bai =Y_ANGLE100;

Y_ANGLE=Y_ANGLE0; 取余运算 shi =Y_ANGLE10;

Y_ANGLE=Y_ANGLE; 取余运算 ge =Y_ANGLE;

Y_ANGLE=bai*100+shi*10+ge;

Y_SIGN=1;

Y_ANGLE=-Y_ANGLE;

********************************

16