查看: 1955|回复: 0

想做个简单的计步器,但是从adxl362读不出数据,求大神帮....

[复制链接]

该用户从未签到

发表于 2016-6-26 13:39:03 | 显示全部楼层 |阅读模式
分享到:
求助各位大神,我是一名大一学生,上个月学习单片机,后来想做个简单的计步器。选的是90c516rd芯片,和adxl362加速度传感器,但是现在发现一直从362芯片中读不出数据,按照下面的程序一直输出的都是255,麻烦帮忙看看我到底错在哪了。。卡了两个星期。。。

接线如下
vin  3.3v电源
GND 接地
SCL P1^0
SDA P1^1
SDO P1^2
CS    P1^3

程序如下:
#include <reg52.h>
#include <intrins.h>
#define unchar unsigned char
#define unint unsigned int

unchar tt[]="0123456789";
sbit lcden = P2^7;
sbit lcdrs = P2^6;
sbit lcde  = P2^5;
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
sbit SCL  = P1^0;
sbit SDA  = P1^2;
sbit SDO  = P1^1;
sbit CS  = P1^3;

void delay(unint z){
        unint x;
        unint y ;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}

void write_com(unchar com){
        lcde=0;
        lcdrs = 0;
        P0 = com;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
}

void write_data(unchar da){
        lcdrs = 1;
        lcde=0;
        P0 = da;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
}

void init (){
//        dula =0;
//        wela =0;
        lcden=0;
        write_com(0x38);
        write_com(0x0e);
        write_com(0x06);
        write_com(0x01);
        write_com(0x80);
}


//////////////////////////////////////////////////
void SPISendByte(unsigned char ch)
{
unsigned char idata n=8; // 向SDA上发送一位数据字节,共八位
SCL = 1 ; //时钟置高
CS = 0 ; //选择从机
while(n--)
{
delayNOP();
SCL = 0 ; //时钟置低
if((ch&0x80) == 0x80) // 若要发送的数据最高位为1则发送位1
{
SDA = 1; // 传送位1
}
else
{
SDA = 0; // 否则传送位0
}
delayNOP();
ch = ch<<1; // 数据左移一位
SCL = 1 ; //时钟置高
}
}
//--------------------------------------------------------------------------------------------------
// 函数名称: SPIreceiveByte
// 返回接收的数据
// 函数功能: 接收一字节子程序
//--------------------------------------------------------------------------------------------------
unsigned char SPIreceiveByte()
{
unsigned char idata n=8; // 从MISO线上读取一上数据字节,共八位
unsigned char tdata;
SCL = 1; //时钟为高
CS = 0; //选择从机
while(n--)
{
        delayNOP();
        CS = 0; //时钟为低
        delayNOP();
        tdata = tdata<<1; // 左移一位,或_crol_(temp,1)
        if(SDO == 1)
        tdata = tdata|0x01; // 若接收到的位为1,则数据的最后一位置1
        else
        tdata = tdata&0xfe; // 否则数据的最后一位置0
        SCL=1;
}
return(tdata);
}
//--------------------------------------------------------------------------------------------------
// 函数名称: SPIsend_receiveByte
// 入口参数: ch
// 返回接收的数据
// 函数功能:串行输入/输出子程序
//--------------------------------------------------------------------------------------------------
unsigned char SPIsend_receiveByte(unsigned char ch)
{
unsigned char idata n=8; // 从MISO线上读取一上数据字节,共八位
unsigned char tdata;
SCL = 1; //时钟为高
CS = 0; //选择从机
while(n--)
{
delayNOP();
SCL = 0; //时钟为低
delayNOP();
{
tdata = tdata<<1; // 左移一位,或_crol_(temp,1)
if(SDO == 1)
tdata = tdata|0x01; // 若接收到的位为1,则数据的最后一位置1
else
tdata = tdata&0xfe; // 否则数据的最后一位置0
}
{
if((ch&0x80) == 0x80) // 若要发送的数据最高位为1则发送位1
{
SDA = 1; // 传送位1
}
else
{
SDA = 0; // 否则传送位0
}
ch = ch<<1; // 数据左移一位
}
SCL=1;
}
return(tdata);
}



////////////////////////////


unint cc ;
unint num ;

void wri(unint xx){
       
        if(xx/10000!=0){
                write_data(tt[xx/10000]);
        }
       
        if((xx%10000)/1000!=0){
                write_data(tt[(xx%10000)/1000]);
        }
       
        if((xx%1000)/100!=0){
                write_data(tt[(xx%1000)/100]);
        }
       
        if((xx%100)/10!=0){
                write_data(tt[(xx%100)/10]);
        }
       
        if((xx%10)!=0){
                write_data(tt[xx%10]);
        }
       
}
void mminit(){

        SPISendByte(0x03);
        SPISendByte(0x01);
       
        SPISendByte(0x08);
        SPISendByte(0x00);
       
        SPISendByte(0x09);
        SPISendByte(0x00);
       
        SPISendByte(0x0A);
        SPISendByte(0x00);
       
}

void main(){
       

        init();
        mminit();
        while(1){
                write_com(0x01);
                write_com(0x80);//x轴
                SPISendByte(0x0B);
                SPISendByte(0x0E);
                wri(SPIsend_receiveByte(0x0E));
                num++;
       
                delay(3000);
               
        }

       
}


显示

显示

接线图

接线图

adxl芯片

adxl芯片
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条



手机版|小黑屋|与非网

GMT+8, 2024-3-28 19:34 , Processed in 0.121664 second(s), 16 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.