MCU是stm32f103系列的,控制sht10传感器工作,可是读不出数据,我用串口打印了出错信息,每个函数的返回值都不对,说明传感器没有响应,但是我对着数据手册的时序图看了很多遍,都不知道问题出在哪,请教各位大神指点迷津,小女子在此感激不尽!- void init_sht(void) //初始化I2C的引脚
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_12 | GPIO_Pin_10);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_PinLockConfig(GPIOC, GPIO_Pin_12 | GPIO_Pin_10); //锁定时钟输出的属性
- s_softreset(); //通讯复位
- }
- char s_softreset(void)
- {
- unsigned char error=0;
- s_connectionreset(); //reset communication
- error+=s_write_byte(RESET); //send RESET-command to sensor
- printf("softreset is ready.\r\n");
- printf("%d\n",error);
- return error; //error=1 in case of no response from the sensor
- }
- void s_transstart(void)
- {
- _nop_();
- GPIO_SetBits(DATA_WR);
- _nop_();
- GPIO_ResetBits(SCK); //SCK=0; //Initial state
- _nop_();
- GPIO_SetBits(SCK); //SCK=1;
- _nop_();
- GPIO_ResetBits(DATA_WR); //DATA_TRIS=0; // DATA_WR=0; DATA_TRIS=0;
- _nop_();
- GPIO_ResetBits(SCK);//SCK=0;
- _nop_();_nop_();_nop_();
- GPIO_SetBits(SCK);//SCK=1;
- _nop_();
- GPIO_SetBits(DATA_WR);
- _nop_();
- GPIO_ResetBits(SCK);//SCK=0;
- _nop_();
- printf("start is ready.\r\n");
- }
- void s_connectionreset(void)
- {
- unsigned char i;
- _nop_();
- GPIO_SetBits(DATA_WR); //DATA_WR = 1; //set data pin high
- _nop_(); //SCK_TRIS = 0; //set CLK pin an output low
- GPIO_ResetBits(SCK);// SCK = 0;
-
- for(i=0;i<9;i++) //9 SCK cycles for connection reset sequence
- { GPIO_SetBits(SCK);//SCK=1;
- _nop_();
- GPIO_ResetBits(SCK);//SCK=0;
- _nop_();
- }
- _nop_();
- s_transstart(); //transmission start
- printf("reset is ready.\r\n");
- }
- char s_read_byte(unsigned char ack)
- {
- unsigned char i,val=0;
- _nop_();
- GPIO_SetBits(DATA_WR);
- _nop_();
- for (i=0x80;i>0;i/=2) //shift bit for masking
- { GPIO_SetBits(SCK);//SCK=1; //clk for SENSI-BUS
- _nop_();
- if (DATA_RD) val=(val | i); //read bit
- _nop_();
- GPIO_ResetBits(SCK);//SCK=0;
- _nop_();
- }
- if(ack==ACK)
- GPIO_ResetBits(DATA_WR);//DATA_WR = 0;
- else
- GPIO_SetBits(DATA_WR);
- _nop_();
- GPIO_SetBits(SCK);//SCK=1; //clk #9 for ack
- _nop_();_nop_();_nop_(); //pulse-width approx. 5 us
- GPIO_ResetBits(SCK);//SCK=0;
- _nop_();
- GPIO_SetBits(DATA_WR);
- _nop_();
- printf("read is ready.\r\n");
- printf("%d\n",val);
- return val;
- }
- char s_write_byte(unsigned char value)
- {
- unsigned char i,error=0;
- _nop_();
- for (i=0x80;i>0;i/=2) //shift bit for masking
- { if (i & value) GPIO_SetBits(DATA_WR);// DATA_WR=1; //masking value with i , write to SENSI-BUS
- else GPIO_ResetBits(DATA_WR); //DATA_WR=0;
- _nop_();
- GPIO_SetBits(SCK);//SCK=1; //clk for SENSI-BUS
- _nop_();_nop_();_nop_(); //pulse-width approx. 5 us
- GPIO_ResetBits(SCK);//SCK=0;
-
- }
- GPIO_SetBits(DATA_WR);
- _nop_();
- GPIO_SetBits(SCK);//SCK=1; //clk #9 for ack
- _nop_();
- error=DATA_RD; //check ack (DATA will be pulled down by SHT11)
- _nop_();
- GPIO_ResetBits(SCK);//SCK=0;
- _nop_();
- printf("write is ready.\r\n");
- printf("%d\n",error);
- return error; //error=1 in case of no acknowledge
- }
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
- {
- unsigned error=0;
- unsigned int i;
- s_transstart(); //transmission start
- switch(mode){ //send command to sensor
- case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
- case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
- default : break;
- }
- GPIO_SetBits(DATA_WR);
- _nop_();
- for (i=0;i<65535;i++) if(DATA_RD==0) break; //wait until sensor has finished the measurement
- _nop_();
- if(DATA_RD) error+=1; //or timeout (~2 sec.) is reached
- *(p_value+1) = s_read_byte(ACK); //read the first byte (MSB)
- *(p_value) = s_read_byte(ACK); //read the second byte (LSB)
- *p_checksum = s_read_byte(noACK); //read checksum
- printf("measure is ready.\r\n");
- printf("%d\n",error);
- return error;
- }
|