mma7361角速度传感器实验
时间:12-02 09:48 阅读:1481次
*温馨提示:点击图片可以放大观看高清大图
简介:这里给大家分享了一个mma7361角速度传感器实验的源代码。
#include <STC12C5A60S2.h>
#include <math.h>
#define lcd P0
sbit sl=P1^0;
sbit og=P1^1;
sbit st=P1^2;
sbit gs=P1^3;
sbit adcs=P2^0;
sbit adclk=P2^1;
sbit addo=P2^2;
sbit addi=P2^2;
sbit lcdrs=P1^5;
sbit lcdrw=P1^6;
sbit lcden=P1^7;
void delay(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<1000;j++);
}
void lcdwr_com(unsigned char com)
//дָÁÊý
{
lcdrs=0;
delay(1);
lcd=com;
lcden=1;
lcden=0;
}
void lcdwr_data(unsigned char date)
//дÊý¾Ý
{
lcdrs=1;
delay(1);
lcd=date;
lcden=1;
lcden=0;
}
void lcdinit()
//³õʼ»¯º¯Êý
{
lcdrw=0;
lcden=0;
lcdwr_com(0x38);
lcdwr_com(0x0c);
lcdwr_com(0x06);
lcdwr_com(0x01);
lcdwr_com(0x80);
}
int rd_ad(unsigned char *date,int SGL,int ODD) //¶ÁÈ¡adÊý¾Ý
{
unsigned char data0=0,data1=0;
int i,j;
adcs=1;
adcs=0;
addi=1;
adclk=0;
adclk=1;
adclk=0; //µÚÒ»¸öÂö³å
addi=SGL; //ģʽѡÔñ
adclk=1;
adclk=0; //µÚ¶þ¸öÂö³å
addi=ODD;
adclk=1;
adclk=0; //µÚÈý¸öÂö³å
addo=1; //ÊÍ•ÅÊý¾Ý¶Ë¿Ú
for(i=0;i<8;i++){
data0<<=1;
adclk=1;
adclk=0;
j++;j++;j++;j++;j++;
j++;j++;j++;j++;j++; //ÑÓʱ5us
if(addo) data0|=0x01;
}
if(addo==1) data1|=0x80;
for(i=0;i<7;i++){
data1>>=1;
adclk=1;
adclk=0;
j++;j++;j++;j++;j++;
j++;j++;j++;j++;j++;
if(addo==1)data1|=0x80;
}
adcs=1;
addo=1;
adclk=1; //ÊÍ•Å×ÜÏß
if(data0==data1){
*date=data1; //ת»»³É¹¦±£´æÊý¾Ý
return 1;
}
else return 0;
}
void main()
{
unsigned char date;
double disp;
unsigned char BUF[6];
int pm; //•ûºÅλ
int i;
unsigned int angle; //½Ç¶È
sl=1;
og=1;
gs=1;
st=1;
lcdinit();
while(1){
if(rd_ad(&date,1,0)){
disp=date;
disp*=195;
if(disp>=16500){
pm=1; //Ϊ¸º
angle=5730*asin((disp-16500)/8000);//»¡¶Èת»»Îª½Ç¶È
if(angle%10>=5) angle=angle/10+1;
else angle/=10; //ËÄÉáÎåÈëËãһλСÊý
}
else {
pm=0; //ΪÕý
angle=5730*asin((16500-disp)/8000);//»¡¶Èת»»Îª½Ç¶È
if(angle%10>=5) angle=angle/10+1;
else angle/=10; //ËÄÉáÎåÈëËãһλСÊý
}
BUF[0]=angle/100%10+'0';
BUF[1]=angle/10%10+'0';
BUF[2]='.';
BUF[3]=angle%10+'0';
i=0;
while(BUF[i]=='0'&&BUF[i+1]!='.'){
BUF[i]='#';
i++;
}
lcdwr_com(0x80);
if(pm==1){
lcdwr_data('-');
delay(1);
}
for(i=0;i<4;i++)
if(BUF[i]!='#'){
lcdwr_data(BUF[i]);
delay(1);
}
lcdwr_data(0xdf);//ÏÔʾ¶È•ûºÅ
delay(1);
lcdwr_data(' ');//²¹Æ볤¶È²î
delay(1);
lcdwr_data(' ');
}
delay(300);
}
}