欢迎来到天天文库
浏览记录
ID:52909416
大小:27.00 KB
页数:3页
时间:2020-03-31
《角度传感器的程序.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*****************************************************************************文件名:倾角.c描述:通过SPI检测得到X和Y角度的数字量的值,比较数字量的值,大概在-40到+40内就可以基本平衡。版本:1.02011-7-4*****************************************************************************/#include#include"angle.h"#defineSETB(BIT
2、)P1OUT
3、=BIT#defineCLRB(BIT)P1OUT&=~BITunsignedintSPI_ReadBits(unsignedcharn);voidSPI_WriteByte(unsignedcharwdata);voidSPI_Start(void);voidSPI_Stop(void);voidSPI_Delay(unsignedint);signedintSPI_GetX(void);signedintSPI_GetY(void);voidSPI_Init(){P1DIR
4、=(CSB+MOSI+SCK);P1DIR&=~MISO;}voidSP
5、I_Stop(void){CLRB(SCK);SETB(CSB);//SPI_Delay(100);}voidSPI_Delay(unsignedintn){while(n--);}voidSPI_Start(void){CLRB(SCK);CLRB(CSB);//SPI_Delay(100);}//主机写一个BYTE到器件//最终状态:CLK = LOWvoidSPI_WriteByte(unsignedcharwdata){unsignedchari;for(i=0;i<8;++i){CLRB(SCK);SPI_Delay(10);if((wdata&0x80
6、)!=0)SETB(MOSI);elseCLRB(MOSI);wdata=wdata<<1;SPI_Delay(10);SETB(SCK);SPI_Delay(10);}}//主机读入N个比特//n限制为16及其以下//结束状态 CLK = 0unsignedintSPI_ReadBits(unsignedcharn){unsignedintrtemp=0,i;for(i=0;i7、=0x8、0001;}else{rtemp&=0xFFFE;}}returnrtemp;}signedintSPI_GetX(void){unsignedintresult;SPI_Start();SPI_WriteByte(RDAX);//SPI_Delay(10000);//>150USresult=SPI_ReadBits(11);SPI_Stop();return(result);//-1024}signedintSPI_GetY(void){unsignedintresult;SPI_Start();SPI_WriteByte(RDAY);//SPI_Delay(9、10000);//>150USresult=SPI_ReadBits(11);SPI_Stop();return(result);//-1024}
7、=0x
8、0001;}else{rtemp&=0xFFFE;}}returnrtemp;}signedintSPI_GetX(void){unsignedintresult;SPI_Start();SPI_WriteByte(RDAX);//SPI_Delay(10000);//>150USresult=SPI_ReadBits(11);SPI_Stop();return(result);//-1024}signedintSPI_GetY(void){unsignedintresult;SPI_Start();SPI_WriteByte(RDAY);//SPI_Delay(
9、10000);//>150USresult=SPI_ReadBits(11);SPI_Stop();return(result);//-1024}
此文档下载收益归作者所有