完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
各位好!我用ADXL345出现问题,一筹莫展,查不出问题所在。
mcu用stc15w102,IIC读写。 主函数中如果没有“devid = ADXL_ReadOneByte(0x00);”语句,串口发送“COMM_SendOneByte(0xA5);”发送到电脑上串口助手的数据正常;如果加上“devid = ADXL_ReadOneByte(0x00);”语句,串口发送“COMM_SendOneByte(0xA5);”发送到电脑上串口助手的数据就不正常。说明是ADXL_ReadOneByte(xxx);程序有问题。但反反复复检查就是查不出来问题所在。请大家帮助,不胜感谢!下面是相关程序: // Use mcu STC15W102 void Dly5us(void) //@11.0592MHz { uchr i; nop_(); //i = 11; i = 8; while (--i); } void ADXL_StrtTrans(void) { SDA_TXD = 1; SCL = 1; Dly5us(); SDA_TXD = 0; Dly5us(); SCL = 0; //Dly5us(); // tem } void ADXL_StopTrans(void) { SDA_TXD = 0; SCL = 1; Dly5us(); SDA_TXD = 1; Dly5us(); //SCL = 0; // tem } void ADXL_WritAck(bit ack) { SDA_TXD = ack; SCL = 1; Dly5us(); SCL = 0; Dly5us(); //SDA_TXD = !SDA_TXD; // tem } bit ADXL_ReadAck(void) { //SDA_TXD = 1; // tem SCL = 1; Dly5us(); CY = SDA_TXD; SCL = 0; Dly5us(); return CY; } void ADXL_WritEightBit(uchr dat) { uchr i; for (i=0; i<8; i++) { dat <<= 1; SDA_TXD = CY; //Dly5us(); // tem SCL = 1; Dly5us(); SCL = 0; Dly5us(); // } ADXL_ReadAck(); // } uchr ADXL_ReadEightBit(void) { uchr i; uchr dat = 0; SDA_TXD = 1; for (i=0; i<8; i++) { dat <<= 1; // SCL = 1; Dly5us(); dat |= SDA_TXD; //dat <<= 1; // tem SCL = 0; Dly5us(); } return dat; } void ADXL_WriteOneByte(uchr AdxlRegAddr,uchr EightBitData) { ADXL_StrtTrans(); // ADXL345 start transmission ADXL_WritEightBit(ADXL_SLV_ADDR); // write the IIC slave address of device into ADXL345 //ADXL_ReadAck(); // tem ADXL_WritEightBit(AdxlRegAddr); // write the register address into ADXL345 //ADXL_ReadAck(); // tem ADXL_WritEightBit(EightBitData); // write the data into register of ADXL345 //ADXL_ReadAck(); // tem ADXL_StopTrans(); // ADXL345 stop transmission } uchr ADXL_ReadOneByte(uchr AdxlRegAddr) { uchr AdxlReadDatBuf; ADXL_StrtTrans(); // ADXL345 start transmission ADXL_WritEightBit(ADXL_SLV_ADDR); // write the IIC slave address of device into ADXL345 //ADXL_ReadAck(); // tem ADXL_WritEightBit(AdxlRegAddr); // write the register address into ADXL345 //ADXL_ReadAck(); // tem ADXL_StrtTrans(); // ADXL345 start transmission ADXL_WritEightBit(ADXL_SLV_ADDR+1); // write the IIC slave address of device into ADXL345 //ADXL_ReadAck(); // tem AdxlReadDatBuf = ADXL_ReadEightBit(); // register data in ADXL345 is readed out ADXL_WritAck(1); // Send ACK to ADXL345 ADXL_StopTrans(); // ADXL345 stop transmission return AdxlReadDatBuf; } void ADXL_Init(void) { ADXL_WriteOneByte(ADXL_DATA_FORMAT, 0x0B); //Set DATA_FORMAT:mesurement scope,+ or - 16 gram,13 bit mode. ADXL_WriteOneByte(ADXL_BW_RATE, 0x08); //Set BW_RATE:Rate = 25Hz. ADXL_WriteOneByte(ADXL_POWER_CTL, 0x08); //Set POWER_CTL:mesurement mode is started. ADXL_WriteOneByte(ADXL_INT_ENABLE, 0x80); //Set INT_ENABLE:DATA_READY interrupt is inhibit. ADXL_WriteOneByte(ADXL_OFFSETX, 0x00); //Set ADXL_OFFSETX. ADXL_WriteOneByte(ADXL_OFFSETY, 0x00); //Set ADXL_OFFSETY. ADXL_WriteOneByte(ADXL_OFFSETZ, 0x05); //Set ADXL_OFFSETZ. } void main(void) { ADXL_Init(); while(1) { devid = ADXL_ReadOneByte(0x00); COMM_SendOneByte(0xA5); } } |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2554 浏览 1 评论
8127 浏览 0 评论
1942 浏览 0 评论
VL53L5CX #高达 8x8 多区域测距和 63°对角线宽视场 飞行时间多区域测距传感器
17324 浏览 0 评论
【罗姆传感器评估板试用体验连载】罗姆传感器整合进同一个示例程序
21965 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-30 08:45 , Processed in 0.696061 second(s), Total 76, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号