- 打卡等级:初来乍到
- 打卡总天数:6
- 最近打卡:2024-09-29 08:20:57
注册会员
- 积分
- 114
|
发表于 2024-6-27 19:08:01
|
显示全部楼层
8G2K64S4代码如下,运行以后,主机打狗棒上P20和P21从来没亮过:
sbit SDA = P1^4;
sbit SCL = P1^5;
bit isda;
bit isma;
u8 addr;
u8 xdata buffer[256];
void i2c_init(){
I2CCFG = 0x81; //I2C speed = Fosc / 2 / (2 * 1 + 4) = Fosc /12 !?
I2CSLADR = 0x5a; // 0x5a: [0101 101]0: 0 for strict-address mode
I2CSLST = 0x00;
I2CSLCR = 0x78;
isda = 1;
isma = 1;
I2CTXD = buffer[addr];
}
void I2C_Isr() interrupt 24 {
//_push_(P_SW2);
//P_SW2 |= 0x80;
if(I2CSLST & 0x40){
I2CSLST &= ~0x40;
isda = 1;
}
else if(I2CSLST & 0x20){ //meanwhile, SLACKO will be sent to master ?
I2CSLST &= ~0x20;
if(isda){
isda = 0;
}
else if(isma){
isma = 0;
addr = I2CRXD;
I2CTXD = buffer[addr]; //since write-mode, will NOT be sent at once, but will be sent next read-cmd?
}
else{
buffer[addr++] = I2CRXD;
}
}
else if(I2CSLST & 0x10){
I2CSLST &= ~0x10;
if(I2CSLST & 0x02){
I2CTXD = 0xff;
}
else{
I2CTXD = buffer[++addr]; //since read-mode, will be sent next read-cmd ?
}
}
else if(I2CSLST & 0x08){
I2CSLST &= ~0x08;
isda = 1;
isma = 1;
}
//_pop_(P_SW2);
}
void main(){
P_SW2 = 0x80;
i2c_init();
EA = 1;
while(1){
}
}
|
|