B
balaece07
Guest
Hej Alle,89s52[/b] Microcontroller Using I2C..I tried So many Times....But Didnt get the output..
Bala Here.I jeg forsøger at kommunikere TO
89s52 [/ b] Microcontroller Brug I2C .. Jeg har prøvet så mange gange .... Men Didnt få produktionen ..
Her er jeg herunder min kode .. PLS finde fejlen og Hjælp mig ..
Tahnks in Advance
kode:
# include <AT898252.h>
# include <intrins.h>
unsigned char readI2c (bit ACK_Bit);
void writei2c (unsigned char W_Byte);
void writemc (unsigned char adr, unsigned char data1);
void readmc (unsigned char addr);
sbit SDA = P0 ^ 1; / / forbinde til SDA pin (Data)
sbit SCL = P0 ^ 0; / / forbinde til SCL pin (ur)
forsinkelse ();
void main ()
(
writemc (0x24, 0x55);
readmc (0x24);
)
//-------------------------------
/ / Start I2C
//-------------------------------
void Start (void)
(
SDA = 1;
SCL = 1;
_nop_ (); _nop_ ();
SDA = 0;
_nop_ (); _nop_ ();
SCL = 0;
_nop_ (); _nop_ ();
)
//-------------------------------
/ / Stop I2C
//-------------------------------
void stop (void)
(
SDA = 0;
_nop_ (); _nop_ ();
SCL = 1;
_nop_ (); _nop_ ();
SDA = 1;
)
//-------------------------------
/ / Skriv I2C
//-------------------------------
void writei2c (unsigned char data)
(
unsigned char i;
for (i = 0; i <8; i )
(
SDA = (Data & 0x80)?1:0;
SCL = 1;
SCL = 0;
Data <<= 1;
forsinkelse ();
)
SCL = 1;
_nop_ (); _nop_ ();
SCL = 0;
)
//-------------------------------
/ / Read I2C
//-------------------------------
unsigned char readI2c (bit ACK_Bit)
(
unsigned char Data = 0;
unsigned char i;
SDA = 1;
for (i = 0; i <8; i )
(
SCL = 1;
Data <<= 1;
Data = (Data | SDA);
SCL = 0;
_nop_ ();
forsinkelse ()
if (ACK_Bit == 1)
SDA = 0; / / Send ACK
anden
SDA = 1; / / Send NO ACK
_nop_ (); _nop_ ();
SCL = 1;
_nop_ (); _nop_ ();
SCL = 0;
return data;
)
void writemc (unsigned char adr, unsigned char data1)
(
Start ();
writei2c (0xC0);
writei2c (addr);
writei2c (data1);
Stop ();
)
void readmc (unsigned char addr1)
(
unsigned char rdata;
Start ();
writei2c (0xC0);
writei2c (addr1);
Start ();
writei2c (0xC1);
rdata = readI2c ();
Stop ();
P2 = rdata;
)
forsinkelse () / * One Second Delay * /
(
T2MOD = 0x03;
TL2 = 0xFC;
TH2 = 0xFE;
TR2 = 1;
while (TF2! = 1);
TR2 = 0;
TF2 = 0;
)
Bala Here.I jeg forsøger at kommunikere TO
89s52 [/ b] Microcontroller Brug I2C .. Jeg har prøvet så mange gange .... Men Didnt få produktionen ..
Her er jeg herunder min kode .. PLS finde fejlen og Hjælp mig ..
Tahnks in Advance
kode:
# include <AT898252.h>
# include <intrins.h>
unsigned char readI2c (bit ACK_Bit);
void writei2c (unsigned char W_Byte);
void writemc (unsigned char adr, unsigned char data1);
void readmc (unsigned char addr);
sbit SDA = P0 ^ 1; / / forbinde til SDA pin (Data)
sbit SCL = P0 ^ 0; / / forbinde til SCL pin (ur)
forsinkelse ();
void main ()
(
writemc (0x24, 0x55);
readmc (0x24);
)
//-------------------------------
/ / Start I2C
//-------------------------------
void Start (void)
(
SDA = 1;
SCL = 1;
_nop_ (); _nop_ ();
SDA = 0;
_nop_ (); _nop_ ();
SCL = 0;
_nop_ (); _nop_ ();
)
//-------------------------------
/ / Stop I2C
//-------------------------------
void stop (void)
(
SDA = 0;
_nop_ (); _nop_ ();
SCL = 1;
_nop_ (); _nop_ ();
SDA = 1;
)
//-------------------------------
/ / Skriv I2C
//-------------------------------
void writei2c (unsigned char data)
(
unsigned char i;
for (i = 0; i <8; i )
(
SDA = (Data & 0x80)?1:0;
SCL = 1;
SCL = 0;
Data <<= 1;
forsinkelse ();
)
SCL = 1;
_nop_ (); _nop_ ();
SCL = 0;
)
//-------------------------------
/ / Read I2C
//-------------------------------
unsigned char readI2c (bit ACK_Bit)
(
unsigned char Data = 0;
unsigned char i;
SDA = 1;
for (i = 0; i <8; i )
(
SCL = 1;
Data <<= 1;
Data = (Data | SDA);
SCL = 0;
_nop_ ();
forsinkelse ()
if (ACK_Bit == 1)
SDA = 0; / / Send ACK
anden
SDA = 1; / / Send NO ACK
_nop_ (); _nop_ ();
SCL = 1;
_nop_ (); _nop_ ();
SCL = 0;
return data;
)
void writemc (unsigned char adr, unsigned char data1)
(
Start ();
writei2c (0xC0);
writei2c (addr);
writei2c (data1);
Stop ();
)
void readmc (unsigned char addr1)
(
unsigned char rdata;
Start ();
writei2c (0xC0);
writei2c (addr1);
Start ();
writei2c (0xC1);
rdata = readI2c ();
Stop ();
P2 = rdata;
)
forsinkelse () / * One Second Delay * /
(
T2MOD = 0x03;
TL2 = 0xFC;
TH2 = 0xFE;
TR2 = 1;
while (TF2! = 1);
TR2 = 0;
TF2 = 0;
)