程序运行中,有些函数不运行,直接跳过去,是怎么回事?下面是个最简单的程序了,只读X轴数据,在SPI_GetX();其中SPI_Write2Bytes(0x1000); 这句就不执行 直接跳过去了。。。
#include"sca3060.h" #include<math.h> #include<intrins.h> #include <stdio.h> #include <aduc845.h> //#include"global.h"
sbit CSB=P3^7; sbit CLK=P3^6; sbit MISO=P3^5; sbit MOSI=P3^4; sbit INT=P2^4; sbit XRST=P2^5; //************************************************************ void SPI_Delay(uchar n){ while(n--); } //*********写两个字节,即16个位********************************* void SPI_Write2Bytes(uchar wdata) { uchar i; for(i=0;i<16;i++) { CLK=LOW; MOSI=wdata&(0x8000>>i); CLK=HIGH; } CLK=LOW; } //**********读n个位************************************ uint SPI_ReadBits( uchar n ) { uint retemp,i; MISO=1; for( i = 0 ; i < n ; ++i ) { retemp<<=1; if(MISO==1) {retemp|=0x0001; } else retemp&=0xfffe; CLK=HIGH; CLK=LOW; } return retemp; } ///* *********SCA3060初始化************************************** void SCA3060_Init(void) { CSB=LOW; SPI_Write2Bytes(0x7a00); CSB=HIGH; CSB=LOW; SPI_Write2Bytes(0x7a50); CSB=HIGH; CSB=LOW; SPI_Write2Bytes(0x7aa0); //unlock 寄存器解锁 CSB=HIGH; CSB=LOW; SPI_Write2Bytes(0x620b); //选择OUT_CTRL寄存器 CSB=HIGH; CSB=LOW; SPI_Write2Bytes(0x8a1d); //储存X Y Z轴数据,采样率1/2 CSB=HIGH; CSB=LOW; SPI_Write2Bytes(0x8681); // 缓冲区满3/4时产生中断,INT引脚高电平有效 CSB=HIGH; CSB=LOW; SPI_Write2Bytes(0x52c0); //使能输出缓冲区,数据长度8bits // */ CSB=HIGH; //CSB=LOW; } //************读取X轴数据************************************************** uint SPI_GetX( void ){ uint resultMSB,resultLSB,result; CSB=LOW; SPI_Write2Bytes(0x1000); CSB=HIGH; CSB=LOW; resultLSB = SPI_ReadBits(16); //X_MSB
SPI_Write2Bytes(0x1400); CSB=HIGH; CSB=LOW; resultMSB = SPI_ReadBits(16); //X_LSB //SPI_Delay(10);
resultLSB=resultLSB&0x00ff; resultMSB=(resultMSB&0x00ff)<<8; //X通道数据 result=resultMSB|resultLSB; //Bia2Dec(result); return result; }
|