/*模拟i2c 读取数据*/
void USR_I2C0_init()
{
SCL_1_Write(1);
SDA_1_Write(1);
}
//void USR_I2C1_init()
//{
// /*34 gpio14 i2c1 scl pin05*/
// PinTypeGPIO(PIN_05, PIN_MODE_0, false);//GPIO 00
// PinConfigSet(PIN_05,PIN_STRENGTH_2MA,PIN_TYPE_STD_PU);
// MAP_GPIODirModeSet(GPIOA1_BASE, 0x40, GPIO_DIR_MODE_OUT);
// MAP_GPIOPinWrite(GPIOA1_BASE,0x40,0x40);//i2c1 scl
// /*35 gpio15 i2c1 sda pin06*/
// PinTypeGPIO(PIN_06, PIN_MODE_0, false);//GPIO 00
// PinConfigSet(PIN_06,PIN_STRENGTH_2MA,PIN_TYPE_OD_PD);
// MAP_GPIODirModeSet(GPIOA1_BASE, 0x80, GPIO_DIR_MODE_OUT);
// MAP_GPIOPinWrite(GPIOA1_BASE,0x80,0x80);//i2c1 scl
//}
#define i2c0_scl_0 (SCL_1_Write(0))//i2c0 scl
#define i2c0_scl_1 (SCL_1_Write(1))//i2c0 scl
#define i2c0_sda_0 (SDA_1_Write(0))//i2c0 scl
#define i2c0_sda_1 (SDA_1_Write(1))//i2c0 scl
#define i2c0_sda_read (SDA_1_Read())//i2c0 read
#define i2c0_scl_read (SCL_1_Read())//i2c0 read
#define i2c0_sda_out (SCL_1_SetDriveMode( SCL_1_DM_STRONG ))//out
#define i2c0_sda_in (SCL_1_SetDriveMode(SCL_1_DM_RES_UPDWN))//out
#define i2c0_scl_in (SDA_1_SetDriveMode(SDA_1_DM_RES_UPDWN))//out
#define i2c0_scl_out (SDA_1_SetDriveMode(SDA_1_DM_STRONG ))//out
//#define i2c1_scl_0 (MAP_GPIOPinWrite(GPIOA1_BASE,0x40,0x00))//i2c0 scl
//#define i2c1_scl_1 (MAP_GPIOPinWrite(GPIOA1_BASE,0x40,0x40))//i2c0 scl
//#define i2c1_sda_0 (MAP_GPIOPinWrite(GPIOA1_BASE,0x80,0x00))//i2c0 scl
//#define i2c1_sda_1 (MAP_GPIOPinWrite(GPIOA1_BASE,0x80,0x80))//i2c0 scl
//
//#define i2c1_sda_read (GPIOPinRead(GPIOA1_BASE,0x80))//i2c0 read
//#define i2c1_scl_read (GPIOPinRead(GPIOA1_BASE,0x40))//i2c0 read
//
//#define i2c1_sda_out (MAP_GPIODirModeSet(GPIOA1_BASE, 0x80, GPIO_DIR_MODE_OUT))//out
//#define i2c1_sda_in (MAP_GPIODirModeSet(GPIOA1_BASE, 0x80, GPIO_DIR_MODE_IN))//out
//#define i2c1_scl_in (MAP_GPIODirModeSet(GPIOA1_BASE, 0x40, GPIO_DIR_MODE_IN))//out
//#define i2c1_scl_out (MAP_GPIODirModeSet(GPIOA1_BASE, 0x40, GPIO_DIR_MODE_OUT))//out
#define i2c_delay CyDelayUs(4)
////////////////////////////////////////////////////////////////////usr_i2c0
void USR_I2C0_Start()
{
i2c0_sda_1;
i2c0_scl_1;
i2c_delay;
// nop(); //起始条件建立时间大于4.7us
//nop();
i2c0_sda_0; //起始条件锁定时间大于4us
i2c_delay;
// nop();
// nop();
i2c0_scl_0; //钳住总线准备发数据
i2c_delay;
}
void USR_I2C0_Stop(void)
{
i2c0_sda_0;
i2c0_scl_0; //发送总线时钟信号
i2c0_scl_1; //发送总线时钟信号
i2c_delay;
// nop();
// nop(); //结束总线时间大于4us
i2c0_sda_1; //结束总线
i2c_delay;
// nop();
// nop();
}
unsigned char USR_I2C0_write_byte(unsigned char data_send)
{
//scl 默认高电平
unsigned char i,error=0;
for(i=0;i<8;i++)
{
if( data_send & 0x80 )
{
i2c0_sda_1;
}
else
{
i2c0_sda_0;
}
i2c_delay;
i2c0_scl_1;
i2c_delay;
//yanshi
i2c0_scl_0;
//yanshi
i2c_delay;
data_send<<=1;
}
i2c0_sda_1;
i2c0_scl_1;
i2c0_sda_in;
i2c_delay;
if(i2c0_sda_read)
{
error = 1;
}
i2c0_scl_0;
i2c_delay;
i2c0_sda_out;
return error;
}
unsigned char USR_I2C0_read_byte(unsigned char ack)
{
//scl 默认高电平
unsigned char i,data_return=0;
i2c0_sda_1;
i2c0_sda_in;
i2c_delay;
for(i=0;i<8;i++)
{
data_return<<=1;
i2c0_scl_1;
i2c_delay;
if(i2c0_sda_read)
{
//UART_PRINT("\r\ni2c byte %02x;\r\n",i2c0_sda_read);
data_return |= 0x01;
}
i2c0_scl_0;
//yanshi
i2c_delay;
}
i2c0_sda_out;
if(ack)
{
i2c0_sda_0;
}
else
{
i2c0_sda_1;
}
i2c0_scl_1;
i2c_delay;
i2c0_scl_0;
i2c0_sda_1;
i2c_delay;
return data_return;
}
unsigned char SHT_check_crc(unsigned char * data_addr,unsigned char data_num,unsigned char check_crc)
{
unsigned char crc=0,i,j;
for(i=0;i<data_num;i++)
{
crc^=*(data_addr+i);
for(j=0;j<8;j++)
{
if(crc&0x80)
{
crc=(crc<<1)^0x131;
}
else
{
crc=crc<<1;
}
}
}
if(crc!=check_crc)
{
return 1;
}
else
{
return 0;
}
}
void USR_I2C_read_T_R()
{
unsigned char error_data=0;
unsigned char data[20];
unsigned int i;
char buff[20];
data[0]=0x00;
data[1]=0x00;
data[2]=0x00;
USR_I2C0_init();
data[0]=0x80;
data[1]=0xf3;
USR_I2C0_Start();
error_data=USR_I2C0_write_byte(data[0]);
//UART_PRINT("\r\n T W A %d;\r\n",error_data);
//USR_Test_Ack();
error_data=USR_I2C0_write_byte(data[1]);
//USR_Test_Ack();
//UART_PRINT("\r\n T W B %d;\r\n",error_data);
data[0]=0x81;
//i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;
i=0;
do
{
USR_I2C0_Start();
i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;
i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;
//USR_I2C0_write_byte(data[0]);
i++;
}while((USR_I2C0_write_byte(data[0])==1)&&(i<2000));
if(i>=2000)
{
//UART_PRINT("\r\n i2c read t timeout");
}
//for(i=0;i<3;i++)
//{
data[0]=USR_I2C0_read_byte(1);
data[1]=USR_I2C0_read_byte(1);
data[2]=USR_I2C0_read_byte(0);
//USR_Test_Ack();
//}
USR_I2C0_Stop();
//if(data[2]==(data[0]+data[1]))
if(!(SHT_check_crc(data,2,data[2])))
{
t_temp=data[0];
t_temp<<=8;
t_temp+=data[1];
//T=-46.85+175.72*St/65536
//T*100=17572*St/65536-4685
t_temp=t_temp*17572/65536-4685;
t_temp=t_temp/10;//多保留一位小数;
}
else
{
t_temp=0;
}
sprintf(buff,"\r\ni2c test T %d;\r\n",t_temp);
UART_UartPutString(buff);
//UART_PRINT("\r\n i2c read t %02x;%02x;%02x",data[0],data[1],data[2]);
//////////////////////////////////////////////////////////////////////////////////////////
data[0]=0x00;
data[1]=0x00;
data[2]=0x00;
USR_I2C0_init();
data[0]=0x80;
data[1]=0xf5;
USR_I2C0_Start();
error_data=USR_I2C0_write_byte(data[0]);
//UART_PRINT("\r\n R W A %d;\r\n",error_data);
//USR_Test_Ack();
error_data=USR_I2C0_write_byte(data[1]);
//USR_Test_Ack();
//UART_PRINT("\r\n R W B %d;\r\n",error_data);
data[0]=0x81;
//i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;
i=0;
do
{
USR_I2C0_Start();
i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;
i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;i2c_delay;
//USR_I2C0_write_byte(data[0]);
i++;
}while((USR_I2C0_write_byte(data[0])==1)&&(i<2000));
if(i>=2000)
{
//UART_PRINT("\r\n i2c read R timeout");
}
//for(i=0;i<3;i++)
//{
data[0]=USR_I2C0_read_byte(1);
data[1]=USR_I2C0_read_byte(1);
data[2]=USR_I2C0_read_byte(0);
//USR_Test_Ack();
//}
USR_I2C0_Stop();
//if(data[2]==(data[0]+data[1]))
if(!(SHT_check_crc(data,2,data[2])))
{
r_temp=data[0];
r_temp<<=8;
r_temp+=data[1];
//T=-46.85+175.72*St/65536
//T*100=17572*St/65536-4685
r_temp=r_temp*1250/65536-60;
//r_temp=r_temp/10;//多保留一位小数;
}
else
{
r_temp=0;
}
//UART_PRINT("\r\n i2c read rh %02x;%02x;%02x",data[0],data[1],data[2]);
//UART_PRINT("\r\ni2c test RH %d;\r\n",r_temp);
} |