|
使用STC89C52單片機,傳感器為XGZP6869D,傳感器可以直接進行IIC采集信息,現在我使用了串口把數據發送至串口助手,但是串口助手不顯示任何數據,傳感器數據采集部分使用了傳感器數據手冊中的示例程序。麻煩有大佬幫我看看我的問題出現在哪里了?是不是我直接接了傳感器芯片,沒有接上拉電阻的原因?還是我的程序有問題?或者其他的什么問題?
接線:1腳SDA-->P1.7 7腳SCL-->P1.6 3腳VDD-->5V 5腳GND-->GND (傳感器-->51單片機)
代碼:
#include <reg52.h>
#include <math.h>
#include<stdio.h>
#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
}
}
|
|