📄 i2c_bb_pg_def.c
字号:
//
#include "ADUC834.h"
#include<intrins.h> // 声明了void _nop_(void);
#include "i2c_bb_pg_dec.h"
sfr S1CONX = 0xB6 ; // correct declaration of S1CON
int h,j;
#define DEV_ADR_WR 0xa0 // slave address + write_bit
#define DEV_ADR_RD 0xa1 // slave address + read_bit
//............................................................................
// GLOBAL VARIABLES
//............................................................................
unsigned char bdata eep_buf ; // eeprom's data buffer
sbit shift7 = eep_buf^7 ; // bit7 of data buffer
sbit shift0 = eep_buf^0 ; // bit0 of data buffer
//...........................................................................
// INITIALIZATION FUNCTIONS
//...........................................................................
void ini_osc(void)
{ // TRIM |= 0xc0 ;
PLLCON = 1; // core clock = 6.291456MHz
CFG834 = 0x01; // enable internal XRAM
}
void ini_i2c(void) // I2C bit-bang init
{ SDA = 1 ; SCL = 1 ; // SDA = SCL = 1 , pre-start condition
}
void ini_com1(void) // UART1's init
{ T3CON = 0x84;
T3FD = 0x12;
SCON = 0x52;
//SBUF=0X41 ;
}
void ini_tim(void) // Timers' init
{ TMOD = 0x22 ; // T0,1 = MODE2 (8b auto-reload)
// GATE0,1=0(delay timer_0 will start at TR0)
}
//.............................................................................
// I2C ACCESS FUNCTIONS DEFINITIONS
//*****************************************************************************
void i2c_start(void)
{ SDA = 1 ; SCL = 1 ; // initial state of the I2C bus
dly_usec(7) ; SDA = 0 ; // SCL=1 , SDA=0 after 7 usecs
dly_usec(5) ; SCL = 0 ; // //钳住I2C总线,准备发送或接收数据
}
//.............................................................................
void i2c_stop(void)
{ SDA = 0 ; // SDA=0,SCL=0, initial state of I2C
dly_usec(2) ; SCL = 1 ; // SDA=0,SCL=1, after 2 usecs
dly_usec(6) ; SDA = 1 ; // SDA=1,SCL=1, after other 5 usecs
}
//.............................................................................
void nack_mcu(void)
{ SDA = 1 ; dly_usec(3) ; // SDA=1,SCL=0 for 3 usecs
SCL = 1 ; dly_usec(6) ; // SDA=1,SCL=1 for 6 usecs ; +pulse SCL
SCL = 0 ; dly_usec(2) ; // SDA=1,SCL=0,SCL stabilization 2us
}
//............................................................................
void ack_mcu(void)
{ SDA = 0 ; dly_usec(3) ; // SDA=0,SCL=0 for 3 usec
SCL = 1 ; dly_usec(6) ; // +pulse SCL for 6 usec
SCL = 0 ; dly_usec(2) ; // 2 usec stabilization for SCL
SDA = 1 ; dly_usec(2) ; // prepare SDA for reception
}
//............................................................................
void i2c_wr(void) // write an 8b streaming
{ unsigned char bit_count = 0 ; // bit counter for the 8b streaming
while(bit_count<8)
{ dly_usec(3) ; SDA=shift7 ; // SDA = bit_n of eeprom's data buffer
dly_usec(2) ; SCL=1 ; // pulse high SCL
dly_usec(5) ; SCL=0 ; // for 5 microseconds
eep_buf=eep_buf<<1 ; // shift left 1bit the eep's data buf
bit_count++ ; dly_usec(2) ; } // increment bit counter(repeat for 8b)
SDA=1 ; // rise-up SDA and program it as input //置时钟线为高,通知被控器开始接收数据位
dly_usec(3) ; // to read the ACK from the memory
SCL=1 ; dly_usec(5) ; // pulse high SCL for 5 useconds
SCL=0 ; // program again SDA as open-drain out //置时钟线为低,准备接收数据位
}
//............................................................................
unsigned char i2c_rd(void) // read an 8b streaming
{ unsigned char bit_count = 0 ; // bit counter the 8b streaming
SDA=1 ; // prepare SDA as input (=1)
while(bit_count<8)
{ eep_buf=eep_buf<<1 ; // shift left 1b eeprom data buffer
dly_usec(4) ; SCL=1 ; // rise-up SCL //置时钟线为高使数据线上数据有效
shift0=SDA ; dly_usec(4) ; // read bit_n from eeprom
SCL=0 ; dly_usec(2) ; // pulse SCL
bit_count++ ; dly_usec(2) ; } // increment bit counter(repeat for 8b)
return eep_buf ; // SDA open drain(return data buf)
}
//...........................................................................
void i2c_rndwr(unsigned int eep_adr, unsigned char eep_data)
{ i2c_start() ; // START command
eep_buf = DEV_ADR_WR ;
i2c_wr() ; // write first slave adr + write_bit
eep_buf = eep_adr>>8 ;
i2c_wr() ; // write the high_byte of the adr
eep_buf = eep_adr&0xff ;
i2c_wr() ; // write the low_byte of the adr
eep_buf = eep_data ;
i2c_wr() ; // write the data_character
i2c_stop() ; // finally , STOP command
dly5ms() ; // write cycle time after each byte
}
//............................................................................
void i2c_rndrd(unsigned int eep_adr,unsigned char *dst)
{ i2c_start() ; // START command
eep_buf = DEV_ADR_WR ;
i2c_wr() ; // write slave address + write_bit
eep_buf = eep_adr>>8 ;
i2c_wr() ; // write the high_byte of the adr
eep_buf = eep_adr&0xff ;
i2c_wr() ; // write the low_byte of the adr
i2c_start() ; // REPEATED START condition
eep_buf = DEV_ADR_RD ;
i2c_wr() ; // change the direction of the trsf
*dst = i2c_rd() ; // store the result in "dst"(<-eep_buf)
nack_mcu() ; // send a NACK from MCU to the memory
i2c_stop() ; // finally , STOP command
}
//............................................................................
void i2c_pgwr(unsigned char *source,unsigned int eep_adr,unsigned char lofsstr)
{
unsigned char k = 0 ; // auxiliary counter
i2c_start() ; // START command
eep_buf = DEV_ADR_WR ;
i2c_wr() ; // write SLAVE ADDRESS + wr_bit
eep_buf = eep_adr>>8 ;
// eep_buf = (eep_adr>>8)&0xff ;
i2c_wr() ; // write high address
eep_buf = eep_adr&0xff ;
i2c_wr() ; // write low address
while(k<lofsstr)
{ eep_buf = *source ; // load buffer from char string
// eep_buf = source[k] ;
// eep_buf = *(source+k) ;
i2c_wr() ; // stream data in the memory
source++ ; // increment pointer
k++ ; // increment counter
} // do until end
i2c_stop() ; // STOP command
dly5ms() ; // final write cycle time
}
//............................................................................
void i2c_pgrd(unsigned char *dest,unsigned int eep_adr,unsigned char lofdstr)
{ unsigned char k = 0 ; // auxiliary counter
i2c_start() ; // START command
eep_buf = DEV_ADR_WR ;
i2c_wr() ; // write slave address + write_bit
eep_buf = eep_adr>>8 ;
i2c_wr() ; // write the high_byte of the adr
eep_buf = eep_adr&0xff ;
i2c_wr() ; // write the low_byte of the adr
i2c_start() ; // REPEATED START condition
eep_buf = DEV_ADR_RD ;
i2c_wr() ; // change the direction of the trsf
if(lofdstr==1) ; // if single byte string
// {;} // do nothing , the byte will be read
// & the NACK will be sent at the end
else
{ while(k<lofdstr-1) // if length of the string > 1
// do for the first (lofdstr-1) bytes
{ *dest = i2c_rd() ; // read byte
ack_mcu() ; // send ACK, requesting more bytes
dest++ ; k++ ; } } // increment pointer & counter
*dest = i2c_rd() ; // read the last byte
nack_mcu() ; // send the final NACK (for the last byte)
i2c_stop() ; } // STOP transfer
//****************************************************************************
// AUXILIARY FUNCTIONS DEFINITIONS
//****************************************************************************
void dly_usec(unsigned char num_usec) // delay in micro-seconds
{
while(--num_usec);
}
//............................................................................
void dly5ms(void) // 5 miliseconds delay
{ unsigned char j = 30 ; // 100 * 50usec = 5 msec
while(--j) { dly_usec(50) ; }
}
//............................................................................
void dly50ms(void) // 250 miliseconds delay
{ unsigned char k = 10 ; // 50 * 5 msec = 250 msec
while(--k) { dly5ms() ; } // usefull for messages' display
}
void dly250ms(void) // 250 miliseconds delay
{ unsigned char k = 50 ; // 50 * 5 msec = 250 msec
while(--k) { dly5ms() ; } // usefull for messages' display
}
//.............................................................................
void dly1s(void) // 1 second delay
{ dly250ms() ; dly250ms() ; // 4 * 250 ms = 1 second
dly250ms() ; dly250ms() ; } // usefull for messages' display
//.............................................................................
void tx_com1(unsigned char com1_buf) // sends on UART1 line com1_buf
{ // TI1 = set in S1CON init
// while(!(S1CON&0x02)) ; // wait for TI_1 = 1
// S1CON &= 0xfd ; // clear TI_1
SBUF = com1_buf ; // write data in serial data buffer
// SBUF=0X13;
//SBUF=0X10;
}
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -