|
CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit是一块强大的开发板
PSoC6双核“处理器”,6轴力学传感器,数字麦克风,FLASH,FRAME(预留接口),E-INK电子墨水屏应有尽有
尤其E-INK电子墨水屏大尺寸、功耗低,能用它开发很多低功耗的东西,比如环境监测等等
原先手里有一个SENSIRION的开发板,家里一直用它来测温湿度
体积小,电池供电,放哪都很方便
前段时间不小收把屏幕给摔坏了,现在只能看到温度,挺不方便
一直想再做一个温湿度传感器,问题都出在屏幕上了
手里有很多带屏幕的开发板,可惜都是TFT屏,比较耗电,必需接电源才能工作,用起来比较麻烦
用数码管显示数据也存在供电的问题,都不是很理想
现在用CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit用来做温湿度传感器感觉还是非常不错的,除了体积大点
264*176的分辨率可以用来显示很多内容
比如时间、日期、温湿度、气压和历史曲线等等
今天第一步先把温湿度传感器加进去
传感器使用silicon labs的si7020,接口和通信协议和sensirion 的sht20通用
为了省事参考了Cypress太阳能开发板Solar-Powered IoT Device Kit的EH_Motherboard工程
毕竟以前用过它测温湿度
https://bbs.eeworld.com.cn/thread-485570-1-1.html
本以为分分钟可以搞定的事实际还是花了很多时间
好像每次做东西都会有很多困难需要解决
还好这次都解决了
开发板预留了很多接口,可惜电源接口比较少,尤其将E-LINK插在开发板上的时候基本是没有电源可以用的
另外I2C接口基本不是被E-LINK占用就是被E-LINK遮挡住,没法使用,除非飞线解决
唯一没被遮挡的是P8.0和P8.1,开始的时候使用P8.0和P8.1,调了一会儿没调通
后来发现这两个引脚被其它功能占用了,实际没并有连接
最后不得不先把E-LINK拆下来,准备先把温湿度传感器跑通再说
Arduion接口包含了I2C引脚,接口对应SCL,SDO,VREF,GND
我的传感器评估板也是这么设计的,一搬的开发板直接插上就可以工作
因为通常VREF都会和模拟电连接,所以VREF可以用作电源输出
可惜CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit的VREF只连接到PSoC6的VREF引脚,并没有和开发板上的电源连接
最后不得不再从别处飞一根电源线过来
传感器使用了Solar-Powered IoT Device Kit的EH_Motherboard工程中的代码
编译时出现很多错误,原因是EH_Motherboard的I2C模块和PSoC6有很大区别
把所有和I2C模块有关的函数都修改后总算能把温湿度数据读出
下一步再看看怎么能让温湿度传感器和E-INK并存,这样就不用在电脑上用DEBUG的方式看环境温湿度了
附上修改后的si7020代码
- /******************************************************************************
- littleshrimp [email]1440507229@qq.com[/email]
- ******************************************************************************/
- #include <project.h>
- #ifndef __SI7020_H_
- #define __SI7020_H_
- /******************************************************************************
- * Macros and Constants
- ******************************************************************************/
- #define SI7020_SLAVE_ADDR (0x40) /* Sensor's I2C slave address */
- #define SI7020_WRITE_USER_REG (0xE6) /* I2C command for Writing RH/T User Register */
- #define SI7020_READ_USER_REG (0xE7) /* I2C command for Reading RH/T User Register */
- #define SI7020_MEASURE_RH (0xE5) /* I2C command for Measuring Relative Humidity */
- #define SI7020_READ_TEMP (0xE0) /* I2C command for Reading Temperature Value from Previous RH Measurement */
- /******************************************************************************
- * External Function Prototypes
- ******************************************************************************/
- uint32 Si7020_Init(void);
- uint32 Si7020_WriteRead(uint8 * data, uint32 sCnt, uint32 rCnt);
- float Si7020_GetTemperature(void);
- float Si7020_GetHhumidity(void);
- #endif /* __SI7020_H_ */
复制代码- /******************************************************************************
- littleshrimp [email]1440507229@qq.com[/email]
- ******************************************************************************/
- #include "Si7020.h"
- #define I2C_TIMEOUT (100UL)
- const uint16_t POLYNOMIAL = 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001
- #define ERROR 1
- #define SUCCESS 0
- uint32 Si7020_Init(void)
- {
- cy_en_scb_i2c_status_t status;
- /* Write User Register */
-
- status = Cy_SCB_I2C_MasterSendStart(mI2C_HW, SI7020_SLAVE_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR;
- status = Cy_SCB_I2C_MasterWriteByte(mI2C_HW, SI7020_WRITE_USER_REG, I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR ;
- status = Cy_SCB_I2C_MasterWriteByte(mI2C_HW, 0x3A | 0x01, I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR;
- /* Send Stop condition on the bus */
- if (Cy_SCB_I2C_MasterSendStop(mI2C_HW, I2C_TIMEOUT, &mI2C_context) != CY_SCB_I2C_SUCCESS)
- {
- return ERROR;
- }
- return SUCCESS;
- }
- uint32 Si7020_WriteRead(uint8 * data, uint32 sCnt, uint32 rCnt)
- {
- cy_en_scb_i2c_status_t status;
- uint32 i;
- status = Cy_SCB_I2C_MasterSendStart(mI2C_HW, SI7020_SLAVE_ADDR, CY_SCB_I2C_WRITE_XFER, I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR;
- for(i=0; i<sCnt; i++)
- {
- status = Cy_SCB_I2C_MasterWriteByte(mI2C_HW, data[i], I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR ;
- }
- // Conversion Time(us) of Si7020 sensor
- CyDelayUs(40);
-
- status = Cy_SCB_I2C_MasterSendReStart(mI2C_HW, SI7020_SLAVE_ADDR, CY_SCB_I2C_READ_XFER, I2C_TIMEOUT, &mI2C_context); // read direction
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR ;
- for(i=0; i<(rCnt-1); i++)
- {
- status = Cy_SCB_I2C_MasterReadByte(mI2C_HW, CY_SCB_I2C_ACK, &data[i], I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR ;
- }
- status = Cy_SCB_I2C_MasterReadByte(mI2C_HW, CY_SCB_I2C_NAK, &data[rCnt-1], I2C_TIMEOUT, &mI2C_context);
- /* If error occurs in I2C communication, reset I2C status and return. */
- if(status != CY_SCB_I2C_SUCCESS) return ERROR ;
- /* Send Stop condition on the bus */
- if (Cy_SCB_I2C_MasterSendStop(mI2C_HW, I2C_TIMEOUT, &mI2C_context) != CY_SCB_I2C_SUCCESS)
- {
- return ERROR;
- }
- return SUCCESS;
- }
- uint8_t Si7020_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
- {
- uint8_t crc = 0;
- uint8_t byteCtr;
- //calculates 8-Bit checksum with given polynomial
- for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
- {
- crc ^= (data[byteCtr]);
-
- for (uint8_t bit = 8; bit > 0; --bit)
- {
- if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
- else crc = (crc << 1);
- }
- }
- if (crc != checksum) return 0;
- else return 1;
- }
- float Si7020_GetTemperature(void)
- {
- uint8_t crc;
- uint8_t buf[3];
- uint16_t temp_code;
- float temperature;
- /* Read Temperature data from I2C Sensor */
- buf[0] = SI7020_READ_TEMP;
- Si7020_WriteRead(buf, 1, 3);
- temp_code = buf[0] << 8;
- temp_code |= buf[1];
- temperature = ((175.72 * temp_code) / 65536.00) - 46.85;
- return temperature;
- }
- float Si7020_GetHhumidity(void)
- {
- uint8_t crc;
- uint8_t buf[3];
- uint16_t temp_code;
- float humidity;
- buf[0] = SI7020_MEASURE_RH;
- Si7020_WriteRead(buf, 1, 3);
- temp_code = buf[0] << 8;
- temp_code |= buf[1];
- temp_code &= ~0x0003; // clear bits [1..0] (status bits)
- humidity = -6.0 + 125.0/65536 * (float)temp_code; // RH= -6 + 125 * SRH/2^16
- return humidity;
- }
复制代码
|
|