Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/19/00 05:04
Read: times


 
#3840 - RE: C code for i2c single master with 89s53
See if this will help !!
From: Ben
Add your own At89s53 include file
/* ---------------------------------------------------------------------------
This module is used to write to the AT24C02A Serial EEPROM.
The function WriteByte is used to write a byte to EEPROM
The function ReadByte is used to read a byte from the EEPROM
The device address on pin A0,A1,A2 must be zero. To Change the address the
constant DEV0_WRITE and DEV0_READ must be changed.
The SDA line is P1.2 and SCL is P2.7 on the DS5002
* ========================================================================== */
////#include<ds5001.h>

#define FAIL_ 1
#define PASS_ 0
#define DEV0_WRITE 0xA6
#define DEV0_READ 0xA7

bit StartBit (void);
void StopBit (void);
void Ack (void);
void Nak (void);
bit Shout (void);
unsigned char Shin (void);
bit WriteByte (unsigned char address_, unsigned char data_);
unsigned char ReadByte (unsigned char address_);

sbit SDA = 0x92;
sbit SCL = 0xA7;


/* =============================================================================
Send START, defined as high-to-low SDA with SCL high.
Return with SCL, SDA low.
Returns CY set if bus is not available.

========================================================================== */
bit StartBit (void) {
#pragma asm

setb SDA
setb SCL

; Verify bus available.

jnb SDA, x40 ; jump if not high
jnb SCL, x40 ; jump if not high

nop ; enforce setup delay and cycle delay
clr SDA
nop ; enforce hold delay
nop
nop
nop
nop
clr SCL

clr c ; clear error flag
jmp x41
x40:
setb c ; set error flag
x41:

#pragma endasm

return (CY);
}

/* =============================================================================
Send STOP, defined as low-to-high SDA with SCL high.
SCL expected low on entry. Return with SCL, SDA high.
========================================================================== */
void StopBit (void) {
#pragma asm

clr SDA
nop ; enforce SCL low and data setup
nop
setb SCL
nop ; enforce setup delay
nop
nop
nop
nop
setb SDA

#pragma endasm

return;
}

/* =============================================================================
Clock out an acknowledge bit (low).
SCL expected low on entry. Return with SCL, SDA low.
========================================================================== */
void Ack (void) {
#pragma asm

clr SDA ; ACK bit
nop ; enforce SCL low and data setup
nop
setb SCL ; raise clock
nop ; enforce SCL high
nop
nop
nop
clr SCL ; drop clock

#pragma endasm

return;
}

/* =============================================================================
Clock out a negative acknowledge bit (high).
SCL expected low on entry. Return with SCL low, SDA high.
========================================================================== */
void Nak (void) {
#pragma asm

setb SDA ; NAK bit
nop ; enforce SCL low and data setup
nop
setb SCL ; raise clock
nop ; enforce SCL high
nop
nop
nop
clr SCL ; drop clock

#pragma endasm
return;
}


/* =============================================================================
Shift out a byte to the AT24Cxx, most significant bit first.
SCL, SDA expected low on entry. Return with SCL low.
Called with data to send in A.
Returns CY set to indicate failure by slave to acknowledge.
Destroys A.
========================================================================== */
bit Shout (void) {
#pragma asm

push b
mov b, #8 ; bit counter
x42:
rlc A ; move bit into CY
mov SDA, c ; output bit
nop ; enforce SCL low and data setup
setb SCL ; raise clock
nop ; enforce SCL high
nop
nop
nop
clr SCL ; drop clock
djnz b, x42 ; next bit

setb SDA ; release SDA for ACK
nop ; enforce SCL low and tAA
nop
setb SCL ; raise ACK clock
nop ; enforce SCL high
nop
nop
nop
mov c, SDA ; get ACK bit
clr SCL ; drop ACK clock

pop b

#pragma endasm
return(CY);
}

/* =============================================================================
Shift in a byte from the AT24Cxx, most significant bit first.
SCL expected low on entry. Return with SCL low.
Returns received data byte in A.
========================================================================== */
unsigned char Shin (void) {
#pragma asm

setb SDA ; make SDA an input
clr A ; clear acc dfd
push b
mov b, #8 ; bit count
clr SCL ; make clock low
x43:
nop ; enforce SCL low and data setup
nop
nop
setb SCL ; raise clock
nop ; enforce SCL high
nop
mov c, SDA ; input bit
rlc A ; move bit into byte
clr SCL ; drop clock
djnz b, x43 ; next bit
pop b

#pragma endasm

return(ACC);
}

/* =============================================================================
WriteByte :- The address is passed in address_
The data_ is passes in data_
Once the write is complete verify the Write.
* ========================================================================== */
bit WriteByte(unsigned char address_, unsigned char data_) {
EA=0;

StartBit();

ACC=DEV0_WRITE;
Shout();

ACC=address_;
Shout();

ACC=data_;
Shout();

StopBit();

EA=1; //Enable interrupts

return(PASS_);
}
/* =============================================================================
ReadByte :- The address_ is passed in addressed.
The data is copied from the ACCUMULATOR into the address of the
variable passed to the function
* ========================================================================== */
unsigned char ReadByte (unsigned char address_) {
unsigned char data_;

EA=0;

StartBit();

/* Dummy Write */
ACC=DEV0_WRITE;

Shout();

ACC=address_;

Shout();

StartBit();

ACC=DEV0_READ;

Shout();

Shin();

data_=ACC;

Nak();

StopBit();

EA=1;

return(data_);
}


List of 3 messages in thread
TopicAuthorDate
C code for i2c single master with 89s53            01/01/70 00:00      
RE: C code for i2c single master with 89s53            01/01/70 00:00      
RE: C code for i2c single master with 89s53            01/01/70 00:00      

Back to Subject List