完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
使用STC89C52单片机,传感器为XGZP6869D,传感器可以直接进行IIC采集信息,现在我使用了串口把数据发送至串口助手,但是串口助手不显示任何数据,传感器数据采集部分使用了传感器数据手册中的示例程序。麻烦有大佬帮我看看我的问题出现在哪里了?是不是我直接接了传感器芯片,没有接上拉电阻的原因?还是我的程序有问题?或者其他的什么问题?
接线:1脚SDA-->P1.7 7脚SCL-->P1.6 3脚VDD-->5V 5脚GND-->GND (传感器-->51单片机) 代码: #include #include #include #define DELAY_tiME 600 #define TRUE 1 #define FALSE 0 #define uchar unsigned char #define uint unsigned int //----define IIC SCL, SDA port---- sbit SCL = P1 ^ 7; sbit SDA = P1 ^ 6; //----delay time_us---- void DELAY(uint t) { while (t != 0) t--; } //----IIC START CONDITION---- void I2C_Start(void) { SDA = 1; //SDA output high DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); //SCL output high SDA = 0; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME); } //----IIC STOP CONDITION---- void I2C_Stop(void) { SDA = 0; //SDA OUTPUT LOW DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SDA = 1; DELAY(DELAY_TIME); SCL = 0; //SCL OUTPUT LOW DELAY(DELAY_TIME); } //----IIC SEND DATA "0"---- void SEND_0(void) { SDA = 0; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME); } //----IIC SEND DATA "1"---- void SEND_1(void) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME); } //----Check SLAVE's Acknowledge---- bit Check_Acknowledge(void) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME / 2); F0 = SDA; DELAY(DELAY_TIME / 2); SCL = 0; DELAY(DELAY_TIME); if (F0 == 1) return FALSE; return TRUE; } //----Write One Byte of Data---- void WriteI2CByte(uchar b) reentrant { char i; for (i = 0; i < 8; i++) if ((b << i) & 0x80) SEND_1(); else SEND_0(); } //----Read One Byte of Data---- uchar ReadI2CByte(void) reentrant { char b = 0, i; for (i = 0; i < 8; i++) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); F0 = SDA; DELAY(DELAY_TIME); SCL = 0; if (F0 == 1) { b = b << 1; b = b | 0x01; } else b = b << 1; } return b; } //----write One Byte of Data,Data from MASTER to the SLAVER---- void Write_One_Byte(uchar addr, uchar thedata) { //Write "thedata" to the SLAVER's address of "addr" bit acktemp = 1; I2C_Start(); //IIC START WriteI2CByte(0x6D << 1 + 0); //IIC WRITE operation, SLAVER address bit: 0x6D acktemp = Check_Acknowledge(); //check the SLAVER WriteI2CByte(addr); //address acktemp = Check_Acknowledge(); WriteI2CByte(thedata); //thedata acktemp = Check_Acknowledge(); I2C_Stop(); //IIC STOP } //----Read One Byte of Data,Data from SLAVER to the MASTER---- uchar Read_One_Byte(uchar addr) { bit acktemp = 1; uchar mydata; I2C_Start(); WriteI2CByte(0x6D << 1 + 0); //IIC WRITE operation, SLAVER address bit: 0x6D acktemp = Check_Acknowledge(); WriteI2CByte(addr); acktemp = Check_Acknowledge(); I2C_Start(); WriteI2CByte(0x6D << 1 + 1); //IIC READ operation, SLAVER address bit: 0x6D acktemp = Check_Acknowledge(); mydata = ReadI2CByte(); I2C_Stop(); return mydata; } //----Delay_ms---- void Delay_xms(uint x) { uint i, j; for (i = 0; i < x; i++) for (j = 0; j < 112; j++); } //----UART Initialization---- void UART_Init() { TMOD = 0x20; // Timer1 mode 2, 8-bit auto-reload TH1 = 0xFD; // 9600 baud rate SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled TR1 = 1; // Start Timer1 } //----UART Send Byte---- void UART_SendByte(uchar dat) { SBUF = dat; // Load data into SBUF while (!TI); // Wait for transmission to complete TI = 0; // Clear TI flag } //----UART Send String---- void UART_SendString(uchar *str) { while (*str) { UART_SendByte(*str++); } } //----The main function---- void main(void) { uchar pressure_H, pressure_M, pressure_L, temperature_H, temperature_L; long int pressure_adc, temperature_adc; float pressure, temperature; uchar pressure_str[20], temperature_str[20]; UART_Init(); // Initialize UART Delay_xms(1000); // delay 1000ms while (1) { Write_One_Byte(0x30, 0x0A); while ((Read_One_Byte(0x30) & 0x08) > 0); //如果注释掉这行,串口助手会显示收到数据,但是都是0000 Delay_xms(20); // delay 20ms pressure_H = Read_One_Byte(0x06); pressure_M = Read_One_Byte(0x07); pressure_L = Read_One_Byte(0x08); pressure_adc = pressure_H * 65536 + pressure_M * 256 + pressure_L; if (pressure_adc > 8388608) pressure = (pressure_adc - 16777216) / 64; // unit: Pa else pressure = pressure_adc / 64; // unit: Pa temperature_H = Read_One_Byte(0x09); temperature_L = Read_One_Byte(0x0A); temperature_adc = temperature_H * 256 + temperature_L; if (temperature_adc > 32768) temperature = (temperature_adc - 65536) / 256; // unit: Celsius else temperature = temperature_adc / 256; // unit: Celsius sprintf(pressure_str, "Pressure: %.2fPa\r\n", pressure); sprintf(temperature_str, "Temperature: %.2fC\r\n", temperature); UART_SendString(pressure_str); UART_SendString(temperature_str); Delay_xms(100); // delay 100ms } }
|
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
464 浏览 0 评论
478 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
439 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
994 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
1056 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11780 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 06:16 , Processed in 0.499055 second(s), Total 44, Slave 34 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号