stm8l 模拟 双i2c 测试好用,带诊断NACK
时间:10-02
整理:3721RD
点击:
程序是给 tsl2561光传感器的。
#define SDAA_PORT GPIOD
#define SDAA_PIN GPIO_Pin_1
#define SCLA_PORT GPIOD
#define SCLA_PIN GPIO_Pin_2
#define SDAB_PORT GPIOD
#define SDAB_PIN GPIO_Pin_3
#define SCLB_PORT GPIOD
#define SCLB_PIN GPIO_Pin_4
#define SDAA(H_L) if(H_L==1)GPIO_SetBits(SDAA_PORT, SDAA_PIN);
else GPIO_ResetBits(SDAA_PORT, SDAA_PIN)
#define SCLA(H_L) if(H_L==1)GPIO_SetBits(SCLA_PORT, SCLA_PIN);
else GPIO_ResetBits(SCLA_PORT, SCLA_PIN)
#define SDAB(H_L) if(H_L==1)GPIO_SetBits(SDAB_PORT, SDAB_PIN);
else GPIO_ResetBits(SDAB_PORT, SDAB_PIN)
#define SCLB(H_L) if(H_L==1)GPIO_SetBits(SCLB_PORT, SCLB_PIN);
else GPIO_ResetBits(SCLB_PORT, SCLB_PIN)
#define SDAA_OUT() GPIO_Init(SDAA_PORT, SDAA_PIN,GPIO_Mode_Out_OD_HiZ_Slow)
#define SDAA_IN() GPIO_Init(SDAA_PORT, SDAA_PIN,GPIO_Mode_In_FL_No_IT)
#define CHECK_SDAA() GPIO_ReadInputDataBit(SDAA_PORT, SDAA_PIN)
#define SDAB_OUT() GPIO_Init(SDAB_PORT, SDAB_PIN,GPIO_Mode_Out_OD_HiZ_Slow)
#define SDAB_IN() GPIO_Init(SDAB_PORT, SDAB_PIN,GPIO_Mode_In_FL_No_IT)
#define CHECK_SDAB() GPIO_ReadInputDataBit(SDAB_PORT, SDAB_PIN)
void DELAYUS(__IO uint16_t nCount)
{
while (--nCount);
}
void IIC_DELAY(void)
{
DELAYUS(40);
}
void IIC_START(uint8_t AB)
{
if(AB == 0)
{
SDAA_OUT();
SDAA(1);
SCLA(1);
IIC_DELAY();
SDAA(0);
IIC_DELAY();
SCLA(0);
}
else
{
SDAB_OUT();
SDAB(1);
SCLB(1);
IIC_DELAY();
SDAB(0);
IIC_DELAY();
SCLB(0);
}
}
void IIC_STOP(uint8_t AB)
{
if(AB == 0)
{
SDAA_OUT();
SDAA(0);
SCLA(1);
IIC_DELAY();
SDAA(1);
}
else
{
SDAB_OUT();
SDAB(0);
SCLB(1);
IIC_DELAY();
SDAB(1);
}
}
void IIC_SEND_ACK(uint8_t AB,uint8_t ACK)
{
if(AB == 0)
{
SDAA_OUT();
SDAA(ACK);
SCLA(1);
IIC_DELAY();
SCLA(0);
}
else
{
SDAB_OUT();
SDAB(ACK);
SCLB(1);
IIC_DELAY();
SCLB(0);
}
}
BitStatus IIC_RECV_ACK(uint8_t AB)
{
BitStatus BITS;
if(AB == 0)
{
SDAA_IN();
SCLA(1);
IIC_DELAY();
BITS = CHECK_SDAA();
SCLA(0);
IIC_DELAY();
return BITS;
}
else
{
SDAB_IN();
SCLB(1);
IIC_DELAY();
BITS = CHECK_SDAB();
SCLB(0);
IIC_DELAY();
return BITS;
}
}
ErrorStatus IIC_SEND_BYTE(uint8_t AB,uint8_t DAT)
{
BitStatus k;
uint8_t i;
if(AB == 0)
{
SDAA_OUT();
IIC_DELAY();
for(i=0;i<8;i++)
{
if(DAT&0x80)
{
SDAA(1);
}
else
{
SDAA(0);
}
DAT <<= 1;
IIC_DELAY();
SCLA(1);
IIC_DELAY();
SCLA(0);
IIC_DELAY();
}
}
else
{
SDAB_OUT();
IIC_DELAY();
for(i=0;i<8;i++)
{
if(DAT&0x80)
{
SDAB(1);
}
else
{
SDAB(0);
}
DAT <<= 1;
SCLB(1);
IIC_DELAY();
SCLB(0);
IIC_DELAY();
}
}
k = IIC_RECV_ACK(AB);
if(k) return ERROR;
else return SUCCESS;
}
uint8_t IIC_RECV_BYTE(uint8_t AB)
{
BitStatus k;
uint8_t i,temp;
temp = 0;
if(AB == 0)
{
SDAA_IN();
for(i=0;i<8;i++)
{
SCLA(1);
IIC_DELAY();
k = CHECK_SDAA();
temp <<=1;
if(k) temp |= 1;
SCLA(0);
IIC_DELAY();
}
return temp;
}
else
{
SDAB_IN();
for(i=0;i<8;i++)
{
SCLB(1);
IIC_DELAY();
k = CHECK_SDAB();
temp <<=1;
if(k) temp |= 1;
SCLB(0);
IIC_DELAY();
}
return temp;
}
}
void main(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
GPIO_Init(SCLA_PORT, SCLA_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
GPIO_Init(SCLB_PORT, SCLB_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
GPIO_Init(SDAA_PORT, SDAA_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
GPIO_Init(SDAB_PORT, SDAB_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
DELAYUS(0X4FFF);
while (1)
{
//IIC_START(0);
//IIC_SEND_BYTE(0,0X72);
//IIC_SEND_BYTE(0,0X80);
//IIC_SEND_BYTE(0,0X03);
//IIC_STOP(0);
//DELAYUS(0X0FFF);
//IIC_START(0);
//IIC_SEND_BYTE(0,0X72);
//IIC_SEND_BYTE(0,REG_IR_L);
//IIC_DELAY();
//IIC_DELAY();
//IIC_DELAY();
//IIC_START(0);
//OKorERR = IIC_SEND_BYTE(0,0X73);
//LIG_L = IIC_RECV_BYTE(0);
//IIC_SEND_ACK(0,1);
//IIC_STOP(0);
//DELAYUS(0X0FFF);
//IIC_START(0);
//IIC_SEND_BYTE(0,0X72);
//IIC_SEND_BYTE(0,REG_IR_H);
//IIC_DELAY();
//IIC_DELAY();
//IIC_DELAY();
//IIC_START(0);
//IIC_SEND_BYTE(0,0X73);
//LIG_H = IIC_RECV_BYTE(0);
//IIC_SEND_ACK(0,1);
//IIC_STOP(0);
IIC_START(1);
IIC_SEND_BYTE(1,0X72);
IIC_SEND_BYTE(1,0X80);
IIC_SEND_BYTE(1,0X03);
IIC_STOP(1);
DELAYUS(0X0FFF);
IIC_START(1);
IIC_SEND_BYTE(1,0X72);
IIC_SEND_BYTE(1,REG_IR_L);
IIC_DELAY();
IIC_DELAY();
IIC_DELAY();
IIC_START(1);
OKorERR = IIC_SEND_BYTE(1,0X73);
LIG_L = IIC_RECV_BYTE(1);
IIC_SEND_ACK(1,1);
IIC_STOP(1);
DELAYUS(0X0FFF);
IIC_START(1);
IIC_SEND_BYTE(1,0X72);
IIC_SEND_BYTE(1,REG_IR_H);
IIC_DELAY();
IIC_DELAY();
IIC_DELAY();
IIC_START(1);
IIC_SEND_BYTE(1,0X73);
LIG_H = IIC_RECV_BYTE(1);
IIC_SEND_ACK(1,1);
IIC_STOP(1);
DELAYUS(0X4FFF);}
#define SDAA_PORT GPIOD
#define SDAA_PIN GPIO_Pin_1
#define SCLA_PORT GPIOD
#define SCLA_PIN GPIO_Pin_2
#define SDAB_PORT GPIOD
#define SDAB_PIN GPIO_Pin_3
#define SCLB_PORT GPIOD
#define SCLB_PIN GPIO_Pin_4
#define SDAA(H_L) if(H_L==1)GPIO_SetBits(SDAA_PORT, SDAA_PIN);
else GPIO_ResetBits(SDAA_PORT, SDAA_PIN)
#define SCLA(H_L) if(H_L==1)GPIO_SetBits(SCLA_PORT, SCLA_PIN);
else GPIO_ResetBits(SCLA_PORT, SCLA_PIN)
#define SDAB(H_L) if(H_L==1)GPIO_SetBits(SDAB_PORT, SDAB_PIN);
else GPIO_ResetBits(SDAB_PORT, SDAB_PIN)
#define SCLB(H_L) if(H_L==1)GPIO_SetBits(SCLB_PORT, SCLB_PIN);
else GPIO_ResetBits(SCLB_PORT, SCLB_PIN)
#define SDAA_OUT() GPIO_Init(SDAA_PORT, SDAA_PIN,GPIO_Mode_Out_OD_HiZ_Slow)
#define SDAA_IN() GPIO_Init(SDAA_PORT, SDAA_PIN,GPIO_Mode_In_FL_No_IT)
#define CHECK_SDAA() GPIO_ReadInputDataBit(SDAA_PORT, SDAA_PIN)
#define SDAB_OUT() GPIO_Init(SDAB_PORT, SDAB_PIN,GPIO_Mode_Out_OD_HiZ_Slow)
#define SDAB_IN() GPIO_Init(SDAB_PORT, SDAB_PIN,GPIO_Mode_In_FL_No_IT)
#define CHECK_SDAB() GPIO_ReadInputDataBit(SDAB_PORT, SDAB_PIN)
void DELAYUS(__IO uint16_t nCount)
{
while (--nCount);
}
void IIC_DELAY(void)
{
DELAYUS(40);
}
void IIC_START(uint8_t AB)
{
if(AB == 0)
{
SDAA_OUT();
SDAA(1);
SCLA(1);
IIC_DELAY();
SDAA(0);
IIC_DELAY();
SCLA(0);
}
else
{
SDAB_OUT();
SDAB(1);
SCLB(1);
IIC_DELAY();
SDAB(0);
IIC_DELAY();
SCLB(0);
}
}
void IIC_STOP(uint8_t AB)
{
if(AB == 0)
{
SDAA_OUT();
SDAA(0);
SCLA(1);
IIC_DELAY();
SDAA(1);
}
else
{
SDAB_OUT();
SDAB(0);
SCLB(1);
IIC_DELAY();
SDAB(1);
}
}
void IIC_SEND_ACK(uint8_t AB,uint8_t ACK)
{
if(AB == 0)
{
SDAA_OUT();
SDAA(ACK);
SCLA(1);
IIC_DELAY();
SCLA(0);
}
else
{
SDAB_OUT();
SDAB(ACK);
SCLB(1);
IIC_DELAY();
SCLB(0);
}
}
BitStatus IIC_RECV_ACK(uint8_t AB)
{
BitStatus BITS;
if(AB == 0)
{
SDAA_IN();
SCLA(1);
IIC_DELAY();
BITS = CHECK_SDAA();
SCLA(0);
IIC_DELAY();
return BITS;
}
else
{
SDAB_IN();
SCLB(1);
IIC_DELAY();
BITS = CHECK_SDAB();
SCLB(0);
IIC_DELAY();
return BITS;
}
}
ErrorStatus IIC_SEND_BYTE(uint8_t AB,uint8_t DAT)
{
BitStatus k;
uint8_t i;
if(AB == 0)
{
SDAA_OUT();
IIC_DELAY();
for(i=0;i<8;i++)
{
if(DAT&0x80)
{
SDAA(1);
}
else
{
SDAA(0);
}
DAT <<= 1;
IIC_DELAY();
SCLA(1);
IIC_DELAY();
SCLA(0);
IIC_DELAY();
}
}
else
{
SDAB_OUT();
IIC_DELAY();
for(i=0;i<8;i++)
{
if(DAT&0x80)
{
SDAB(1);
}
else
{
SDAB(0);
}
DAT <<= 1;
SCLB(1);
IIC_DELAY();
SCLB(0);
IIC_DELAY();
}
}
k = IIC_RECV_ACK(AB);
if(k) return ERROR;
else return SUCCESS;
}
uint8_t IIC_RECV_BYTE(uint8_t AB)
{
BitStatus k;
uint8_t i,temp;
temp = 0;
if(AB == 0)
{
SDAA_IN();
for(i=0;i<8;i++)
{
SCLA(1);
IIC_DELAY();
k = CHECK_SDAA();
temp <<=1;
if(k) temp |= 1;
SCLA(0);
IIC_DELAY();
}
return temp;
}
else
{
SDAB_IN();
for(i=0;i<8;i++)
{
SCLB(1);
IIC_DELAY();
k = CHECK_SDAB();
temp <<=1;
if(k) temp |= 1;
SCLB(0);
IIC_DELAY();
}
return temp;
}
}
void main(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
GPIO_Init(SCLA_PORT, SCLA_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
GPIO_Init(SCLB_PORT, SCLB_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
GPIO_Init(SDAA_PORT, SDAA_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
GPIO_Init(SDAB_PORT, SDAB_PIN, GPIO_Mode_Out_OD_HiZ_Slow);
DELAYUS(0X4FFF);
while (1)
{
//IIC_START(0);
//IIC_SEND_BYTE(0,0X72);
//IIC_SEND_BYTE(0,0X80);
//IIC_SEND_BYTE(0,0X03);
//IIC_STOP(0);
//DELAYUS(0X0FFF);
//IIC_START(0);
//IIC_SEND_BYTE(0,0X72);
//IIC_SEND_BYTE(0,REG_IR_L);
//IIC_DELAY();
//IIC_DELAY();
//IIC_DELAY();
//IIC_START(0);
//OKorERR = IIC_SEND_BYTE(0,0X73);
//LIG_L = IIC_RECV_BYTE(0);
//IIC_SEND_ACK(0,1);
//IIC_STOP(0);
//DELAYUS(0X0FFF);
//IIC_START(0);
//IIC_SEND_BYTE(0,0X72);
//IIC_SEND_BYTE(0,REG_IR_H);
//IIC_DELAY();
//IIC_DELAY();
//IIC_DELAY();
//IIC_START(0);
//IIC_SEND_BYTE(0,0X73);
//LIG_H = IIC_RECV_BYTE(0);
//IIC_SEND_ACK(0,1);
//IIC_STOP(0);
IIC_START(1);
IIC_SEND_BYTE(1,0X72);
IIC_SEND_BYTE(1,0X80);
IIC_SEND_BYTE(1,0X03);
IIC_STOP(1);
DELAYUS(0X0FFF);
IIC_START(1);
IIC_SEND_BYTE(1,0X72);
IIC_SEND_BYTE(1,REG_IR_L);
IIC_DELAY();
IIC_DELAY();
IIC_DELAY();
IIC_START(1);
OKorERR = IIC_SEND_BYTE(1,0X73);
LIG_L = IIC_RECV_BYTE(1);
IIC_SEND_ACK(1,1);
IIC_STOP(1);
DELAYUS(0X0FFF);
IIC_START(1);
IIC_SEND_BYTE(1,0X72);
IIC_SEND_BYTE(1,REG_IR_H);
IIC_DELAY();
IIC_DELAY();
IIC_DELAY();
IIC_START(1);
IIC_SEND_BYTE(1,0X73);
LIG_H = IIC_RECV_BYTE(1);
IIC_SEND_ACK(1,1);
IIC_STOP(1);
DELAYUS(0X4FFF);}