|
| 1 | + /****************************************************************************** |
| 2 | + * @file sht31.c |
| 3 | + * @author MCD Application Team |
| 4 | + * @version V1.1.2 |
| 5 | + * @date 30-Novermber-2018 |
| 6 | + * @brief manages the sensors on the application |
| 7 | + ****************************************************************************** |
| 8 | + * @attention |
| 9 | + * |
| 10 | + * <h2><center>© Copyright (c) 2018 STMicroelectronics International N.V. |
| 11 | + * All rights reserved.</center></h2> |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without |
| 14 | + * modification, are permitted, provided that the following conditions are met: |
| 15 | + * |
| 16 | + * 1. Redistribution of source code must retain the above copyright notice, |
| 17 | + * this list of conditions and the following disclaimer. |
| 18 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 | + * this list of conditions and the following disclaimer in the documentation |
| 20 | + * and/or other materials provided with the distribution. |
| 21 | + * 3. Neither the name of STMicroelectronics nor the names of other |
| 22 | + * contributors to this software may be used to endorse or promote products |
| 23 | + * derived from this software without specific written permission. |
| 24 | + * 4. This software, including modifications and/or derivative works of this |
| 25 | + * software, must execute solely and exclusively on microcontroller or |
| 26 | + * microprocessor devices manufactured by or for STMicroelectronics. |
| 27 | + * 5. Redistribution and use of this software other than as permitted under |
| 28 | + * this license is void and will automatically terminate your rights under |
| 29 | + * this license. |
| 30 | + * |
| 31 | + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" |
| 32 | + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT |
| 33 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 34 | + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY |
| 35 | + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT |
| 36 | + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 37 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 38 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 39 | + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 40 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 41 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 42 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | + * |
| 44 | + ****************************************************************************** |
| 45 | + */ |
| 46 | + |
| 47 | + /* Includes ------------------------------------------------------------------*/ |
| 48 | + |
| 49 | +#include "bh1750.h" |
| 50 | +#include "delay.h" |
| 51 | + |
| 52 | +bool iic_noack=0; |
| 53 | +extern bool debug_flags; |
| 54 | +/* Private typedef -----------------------------------------------------------*/ |
| 55 | +/* Private define ------------------------------------------------------------*/ |
| 56 | + |
| 57 | +/* Private macro -------------------------------------------------------------*/ |
| 58 | +/* Private variables ---------------------------------------------------------*/ |
| 59 | +/* Private function prototypes -----------------------------------------------*/ |
| 60 | +/* Exported functions ---------------------------------------------------------*/ |
| 61 | +float bh1750_read(void) |
| 62 | +{ |
| 63 | + uint8_t rxdata[2]; |
| 64 | + uint16_t AD_code; |
| 65 | + float luminance; |
| 66 | + |
| 67 | + DelayMs(10);//Required |
| 68 | + IIC_Write_Byte(0x23,0x01); //power on |
| 69 | + IIC_Write_Byte(0x23,0x20); //read data |
| 70 | + DelayMs(200); |
| 71 | + IIC_Read_Len(0x23,2,rxdata); |
| 72 | + IIC_Write_Byte(0x23,0x07); //clear |
| 73 | + |
| 74 | + if(iic_noack==1) |
| 75 | + { |
| 76 | + luminance=65535; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + AD_code=(rxdata[0]<<8)+rxdata[1]; |
| 81 | + luminance=AD_code/1.2; |
| 82 | + } |
| 83 | + |
| 84 | + iic_noack=0; |
| 85 | + if(debug_flags==1) |
| 86 | + { |
| 87 | + PPRINTF("\r\n"); |
| 88 | + PPRINTF("Luminance:%d lux\r\n",(int)luminance); |
| 89 | + } |
| 90 | + return luminance; |
| 91 | +} |
| 92 | + |
| 93 | +void I2C_IoInit(void) |
| 94 | +{ |
| 95 | + static GPIO_InitTypeDef GPIO_InitStruct; |
| 96 | + |
| 97 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 98 | + |
| 99 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; |
| 100 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 101 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| 102 | + GPIO_InitStruct.Pin =IIC_SCL_PIN | IIC_SDA_PIN; |
| 103 | + |
| 104 | + HAL_GPIO_Init(GPIO_PORT_IIC, &GPIO_InitStruct); |
| 105 | +} |
| 106 | + |
| 107 | +void I2C_DoInit(void) |
| 108 | +{ |
| 109 | + static GPIO_InitTypeDef GPIO_InitStruct; |
| 110 | + |
| 111 | + GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 112 | + GPIO_InitStruct.Pin =IIC_SCL_PIN | IIC_SDA_PIN; |
| 113 | + |
| 114 | + HAL_GPIO_Init(GPIO_PORT_IIC, &GPIO_InitStruct); |
| 115 | +} |
| 116 | + |
| 117 | +void SDA_IN(void) |
| 118 | +{ |
| 119 | + static GPIO_InitTypeDef GPIO_InitStruct; |
| 120 | + |
| 121 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 122 | + |
| 123 | + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 124 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 125 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| 126 | + GPIO_InitStruct.Pin = IIC_SDA_PIN; |
| 127 | + |
| 128 | + HAL_GPIO_Init(GPIO_PORT_IIC, &GPIO_InitStruct); |
| 129 | +} |
| 130 | + |
| 131 | +void SDA_OUT(void) |
| 132 | +{ |
| 133 | + static GPIO_InitTypeDef GPIO_InitStruct; |
| 134 | + |
| 135 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 136 | + |
| 137 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 138 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 139 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| 140 | + GPIO_InitStruct.Pin = IIC_SDA_PIN; |
| 141 | + |
| 142 | + HAL_GPIO_Init(GPIO_PORT_IIC, &GPIO_InitStruct); |
| 143 | +} |
| 144 | + |
| 145 | +void IIC_Delay(void) |
| 146 | +{ |
| 147 | + volatile int i = 14; |
| 148 | + while (i) { |
| 149 | + i--; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +void IIC_Start(void) |
| 154 | +{ |
| 155 | + SDA_OUT(); |
| 156 | + IIC_SDA_1; |
| 157 | + IIC_SCL_1; |
| 158 | + IIC_Delay(); |
| 159 | + IIC_SDA_0; |
| 160 | + IIC_Delay(); |
| 161 | + IIC_SCL_0; |
| 162 | + IIC_Delay(); |
| 163 | +} |
| 164 | +void IIC_Stop(void) |
| 165 | +{ |
| 166 | + SDA_OUT(); |
| 167 | + IIC_SCL_0; |
| 168 | + IIC_SDA_0; |
| 169 | + IIC_Delay(); |
| 170 | + IIC_SCL_1; |
| 171 | + IIC_SDA_1; |
| 172 | + IIC_Delay(); |
| 173 | +} |
| 174 | +uint8_t IIC_WaitAck(void)//0:ACK 1:no ACK |
| 175 | +{ |
| 176 | + uint8_t ucErrTime=0; |
| 177 | + SDA_IN(); |
| 178 | + IIC_SDA_1; |
| 179 | + IIC_Delay(); |
| 180 | + IIC_SCL_1; |
| 181 | + IIC_Delay(); |
| 182 | + while (IIC_SDA_READ()) |
| 183 | + { |
| 184 | + ucErrTime++; |
| 185 | + if(ucErrTime>250) |
| 186 | + { |
| 187 | + IIC_Stop(); |
| 188 | + return 1; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + IIC_SCL_0; |
| 193 | + IIC_Delay(); |
| 194 | + return 0; |
| 195 | +} |
| 196 | +void IIC_SendByte(uint8_t Byte) |
| 197 | +{ |
| 198 | + uint8_t i; |
| 199 | + SDA_OUT(); |
| 200 | + IIC_SCL_0; |
| 201 | + for (i = 0; i < 8; i++) |
| 202 | + { |
| 203 | + if (Byte & 0x80) |
| 204 | + { |
| 205 | + IIC_SDA_1; |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + IIC_SDA_0; |
| 210 | + } |
| 211 | + IIC_Delay(); |
| 212 | + IIC_SCL_1; |
| 213 | + IIC_Delay(); |
| 214 | + IIC_SCL_0; |
| 215 | + IIC_SDA_1; |
| 216 | + Byte <<= 1; |
| 217 | + IIC_Delay(); |
| 218 | + } |
| 219 | +} |
| 220 | +uint8_t IIC_ReadByte(unsigned char ack) |
| 221 | +{ |
| 222 | + uint8_t i; |
| 223 | + uint8_t value; |
| 224 | + SDA_IN(); |
| 225 | + value = 0; |
| 226 | + for (i = 0; i < 8; i++) |
| 227 | + { |
| 228 | + value <<= 1; |
| 229 | + IIC_SCL_0; |
| 230 | + IIC_Delay(); |
| 231 | + IIC_SCL_1; |
| 232 | + |
| 233 | + if (IIC_SDA_READ()) |
| 234 | + { |
| 235 | + value++; |
| 236 | + } |
| 237 | + IIC_Delay(); |
| 238 | + } |
| 239 | + |
| 240 | + if (!ack) |
| 241 | + IIC_NAck(); |
| 242 | + else |
| 243 | + IIC_Ack(); |
| 244 | + return value; |
| 245 | +} |
| 246 | + |
| 247 | +void IIC_Ack(void) |
| 248 | +{ |
| 249 | + IIC_SCL_0; |
| 250 | + SDA_OUT(); |
| 251 | + IIC_SDA_0; |
| 252 | + IIC_Delay(); |
| 253 | + IIC_SCL_1; |
| 254 | + IIC_Delay(); |
| 255 | + IIC_SCL_0; |
| 256 | +} |
| 257 | + |
| 258 | +void IIC_NAck(void) |
| 259 | +{ |
| 260 | + IIC_SCL_0; |
| 261 | + SDA_OUT(); |
| 262 | + IIC_SDA_1; |
| 263 | + IIC_Delay(); |
| 264 | + IIC_SCL_1; |
| 265 | + IIC_Delay(); |
| 266 | + IIC_SCL_0; |
| 267 | +} |
| 268 | + |
| 269 | +uint8_t IIC_Write_Len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf) |
| 270 | +{ |
| 271 | + uint8_t i; |
| 272 | + IIC_Start(); |
| 273 | + IIC_SendByte((addr<<1)|0); |
| 274 | + if(IIC_WaitAck()) |
| 275 | + { |
| 276 | + IIC_Stop(); |
| 277 | + return 1; |
| 278 | + } |
| 279 | + IIC_SendByte(reg); |
| 280 | + IIC_WaitAck(); |
| 281 | + for(i=0;i<len;i++) |
| 282 | + { |
| 283 | + IIC_SendByte(buf[i]); |
| 284 | + if(IIC_WaitAck()) |
| 285 | + { |
| 286 | + IIC_Stop(); |
| 287 | + return 1; |
| 288 | + } |
| 289 | + } |
| 290 | + IIC_Stop(); |
| 291 | + return 0; |
| 292 | +} |
| 293 | + |
| 294 | +uint8_t IIC_Read_Len(uint8_t addr,uint8_t len,uint8_t *buf) |
| 295 | +{ |
| 296 | + IIC_Start(); |
| 297 | + IIC_SendByte((addr<<1)|0); |
| 298 | + if(IIC_WaitAck()) |
| 299 | + { |
| 300 | + IIC_Stop(); |
| 301 | + return 1; |
| 302 | + } |
| 303 | + IIC_Start(); |
| 304 | + IIC_SendByte((addr<<1)|1); |
| 305 | + IIC_WaitAck(); |
| 306 | + while(len) |
| 307 | + { |
| 308 | + if(len==1)*buf=IIC_ReadByte(0); |
| 309 | + else *buf=IIC_ReadByte(1); |
| 310 | + len--; |
| 311 | + buf++; |
| 312 | + } |
| 313 | + IIC_Stop(); |
| 314 | + return 0; |
| 315 | +} |
| 316 | + |
| 317 | +uint8_t IIC_Write_Byte(uint8_t addr,uint8_t data) |
| 318 | +{ |
| 319 | + IIC_Start(); |
| 320 | + IIC_SendByte((addr<<1)|0); |
| 321 | + if(IIC_WaitAck()) |
| 322 | + { |
| 323 | + IIC_Stop(); |
| 324 | + iic_noack=1; |
| 325 | + return 1; |
| 326 | + } |
| 327 | + IIC_SendByte(data); |
| 328 | + if(IIC_WaitAck()) |
| 329 | + { |
| 330 | + IIC_Stop(); |
| 331 | + iic_noack=1; |
| 332 | + return 1; |
| 333 | + } |
| 334 | + IIC_Stop(); |
| 335 | + return 0; |
| 336 | +} |
| 337 | + |
| 338 | +uint8_t IIC_Read_Byte(uint8_t addr,uint8_t reg) |
| 339 | +{ |
| 340 | + uint8_t res; |
| 341 | + IIC_Start(); |
| 342 | + IIC_SendByte((addr<<1)|0); |
| 343 | + IIC_WaitAck(); |
| 344 | + IIC_SendByte(reg); |
| 345 | + IIC_WaitAck(); |
| 346 | + IIC_Start(); |
| 347 | + IIC_SendByte((addr<<1)|1); |
| 348 | + IIC_WaitAck(); |
| 349 | + res=IIC_ReadByte(0); |
| 350 | + IIC_Stop(); |
| 351 | + return res; |
| 352 | +} |
| 353 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments