这是工厂提供的代码我想把这程序改成51单片机程序DF_Command这个变量没有弄明白而且是怎么定义出来的要怎么才能把压力和温度的数据通过窜口发送出去呢
I2C.c
/*PB0 = SDA*/
/*PB1 = SCL*/
#include “i2c.h”
void WriteSCL(unsigned char state)
{
if (state)
DDRB &= 0xfd; /* input ... pullup will pull high or Slave will drive low */
else
DDRB |= 0x02; /* output ... port latch will drive low */
}
void WriteSDA(unsigned char state)
{
if (state)
DDRB &= 0xfe; /* input ... pullup will pull high or Slave will drive low */
else
DDRB |= 0x01; /* output ... port latch will drive low */
}
unsigned char SetSCLHigh(void)
{
WriteSCL(1); /* release SCL*/
/* set up timer counter 0 for timeout */
t0_timed_out = FALSE; /* will be set after approximately 34 us */
TCNT0 = 0; /* clear counter */
TCCR0 = 1; /* ck/1 .. enable counting */
/* wait till SCL goes to a 1 */
while (! (PINB & 0x02) && !t0_timed_out)
;
TCCR0 = 0; /* stop the counter clock */
return(t0_timed_out);
}
void BitDelay(void)
{
char delay;
delay = 0x03;
do
{
_NOP();
} while (--delay);
}
/* Routine SendStopBit generates an TWI stop bit assumes SCL is low stop bit is a 0 to 1 transition on SDA while SCL is high
____________
/
SCL ___/
______
/
SDA _________/
*/
void SendStopBit(void)
{
WriteSDA(0);
BitDelay();
SetSCLHigh();
BitDelay();
WriteSDA(1);
BitDelay();
}
/* Routine SendStartBit generates an start bit start bit is a 1 to 0 transition on SDA while SCL is high
____________
/
SCL ___/
__________
\
SDA \_____
*/
void SendStartBit(void)
{
WriteSDA(1);
BitDelay();
SetSCLHigh();
BitDelay();
WriteSDA(0);
BitDelay();
WriteSCL(0);
BitDelay();
}
unsigned char SendByte(unsigned char byte)
{
unsigned char i;
unsigned char error;
for (i = 0; i < 8; i++)
{
WriteSDA(byte & 0x80); /* if > 0 SDA will be a 1 */
byte = byte << 1; /* send each bit */
BitDelay();
SetSCLHigh();
BitDelay();
WriteSCL(0);
BitDelay();
}
/* now for an ack */
/* Master generates clock pulse for ACK */
WriteSDA(1); /* release SDA ... listen for ACK */
BitDelay();
SetSCLHigh(); /* ACK should be stable ... data not allowed to change when SCL is high */
/* SDA at 0 ?*/
error = (PINB & 0x01); /* ack didn't happen if bit 0 = 1 */
WriteSCL(0);
BitDelay();
return(error);
}
unsigned char GetOneByte(unsigned char lastbyte)
{
/* lastbyte ==1 for last byte */
unsingned char i;
unsigned char data;
DDRB &=0xfe; /* release SDA ... listen for slave output */
data=0;
for (i=0; i<8;i++)
{
SetSCLHigh(); /* Slave output should be stable ... data not allowed to change when SCL is high */
BitDelay();
data=data<<1;
if (PINB & 0x01)
data=data | 1;
WriteSCL(0);
BitDelay();
}
/*send ACK*/
WriteSDA (lastbyte); /* no ack on last byte ... lastbyte = 1 for the last byte */
BitDelay();
SetSCLHigh();
BitDelay();
WriteSCL(0);
BitDelay();
WriteSDA(1);
BitDelay();
return (data);
}
I2C.h
#include “iom164p.h”
#define DF2 2
#define DF3 3
#define DF4 4
#define write 0
#define read 1
ReadWithPollingI2C.c
/*
ReadWithPollingI2C.c reads the digital output simply at any time and be assured the data is no older than the selected response time specification by checking the status of the 2 MSBs of the bridge high byte data
*/
#include “i2c.h”
extern unsigned char GetOneByte(unsigned char lastbyte);
extern unsigned char SendByte(unsigned char byte);
extern void SendStartBit(void);
extern void SendStopBit(void);
extern void BitDelay(void);
extern unsigned char SetSCLHigh(void);
extern void WriteSDA(unsigned char state);
extern void WriteSCL(unsigned char state);
unsigned char HM19DO_Address;
unsigned char bufptr[4];
void Init (void)
{
__disable_interrupt();
/* P0 = SDA - bidirectional */
/* P1 = SCL - output */
/* P7, P6, P5, P4, P3, P2, P1, P0 */
/* O O O O O O O O */
/* 1 1 1 1 1 1 1 1 */
DDRB = 0xff;
PORTB = 0xfc;
/*setup HM19DO device address*/
HM19DO_Address=0x28;
/*
The factory setting for I2C slave address is 0x28, 0x36 or 0x46 depending on the interface type selected from the ordering information.
For this sample code, 0x28 is used for Slave address of HM19DO.
*/
}
unsigned char ReadHM19DO(unsigned char DF_Command)
{
unsigned char i;
unsigned char error;
SendStartBit();
if (SendByte((HM19DO_Address<<1) + read)) /*send salve address byte*/
{
return (1); /*check error*/
}
for (i=0; i< (DF_Command-1); i++)
{
bufptr[DF_Command-1] = GetOneByte (0); /* 1 byte of read sequence */
}
bufptr[DF_Command-1] = GetOneByte (1); /* 1 signals last byte of read sequence */
SendStopBit ();
return (0);
}
void main (void)
{
float Pressure, Temperature;
unsigned int Dpressure, Dtemperature;
float P1=1638.3; /* P1= 10% * 16383 – A type*/
float P2=13106.4; /* P2= 80% * 16383 – A type*/
float Pmax=15.0;
float Pmin=0.0;
Init();
do
{
ReadHM19DO (DF4); /*Read_DF4 command – data fetch 4 bytes */
If ((bufptr [DF_Command-1] & 0xc0) ==0x00) /*test status of the 2 MSBs of the bridge high byte of data*/
{
Dpressure= ((unsigned int) (bufptr [DF_Command-1] & 0x3f) <<8) + (bufptr [DF_Command-2]);
Dtemperature= (((unsigned int) bufptr [DF_Command-3]) <<3) + bufptr [DF_Command-4];
Pressure= (((float) Dpressure)-P1) * (Pmax-Pmin) / P2+Pmin;
Temperature= ((float) Dtemperature) * 200 / 2047 -50;
}
} while (1);
} /* main */
|