Advertisement
telqt04

Untitled

Mar 6th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. /**
  2. @copyright (C) 2017 Melexis N.V.
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7.  
  8. http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15.  
  16. */
  17. #include <Arduino.h>
  18.  
  19. #include <Wire.h>
  20.  
  21. #include "MLX90640_I2C_Driver.h"
  22.  
  23. void MLX90640_I2CInit()
  24. {
  25.  
  26. }
  27.  
  28. //Read a number of words from startAddress. Store into Data array.
  29. //Returns 0 if successful, -1 if error
  30. int MLX90640_I2CRead(uint8_t _deviceAddress, unsigned int startAddress, unsigned int nWordsRead, uint16_t *data)
  31. {
  32.  
  33. //Caller passes number of 'unsigned ints to read', increase this to 'bytes to read'
  34. uint16_t bytesRemaining = nWordsRead * 2;
  35.  
  36. //It doesn't look like sequential read works. Do we need to re-issue the address command each time?
  37.  
  38. uint16_t dataSpot = 0; //Start at beginning of array
  39.  
  40. //Setup a series of chunked I2C_BUFFER_LENGTH byte reads
  41. while (bytesRemaining > 0)
  42. {
  43. Wire.beginTransmission(_deviceAddress);
  44. Wire.write(startAddress >> 8); //MSB
  45. Wire.write(startAddress & 0xFF); //LSB
  46. if (Wire.endTransmission(false) != 0) //Do not release bus
  47. {
  48. Serial.println("No ack read");
  49. return (0); //Sensor did not ACK
  50. }
  51.  
  52. uint16_t numberOfBytesToRead = bytesRemaining;
  53. if (numberOfBytesToRead > I2C_BUFFER_LENGTH) numberOfBytesToRead = I2C_BUFFER_LENGTH;
  54.  
  55. Wire.requestFrom((uint8_t)_deviceAddress, numberOfBytesToRead);
  56. if (Wire.available())
  57. {
  58. for (uint16_t x = 0 ; x < numberOfBytesToRead / 2; x++)
  59. {
  60. //Store data into array
  61. data[dataSpot] = Wire.read() << 8; //MSB
  62. data[dataSpot] |= Wire.read(); //LSB
  63.  
  64. dataSpot++;
  65. }
  66. }
  67.  
  68. bytesRemaining -= numberOfBytesToRead;
  69.  
  70. startAddress += numberOfBytesToRead / 2;
  71. }
  72.  
  73. return (0); //Success
  74. }
  75.  
  76. //Set I2C Freq, in kHz
  77. //MLX90640_I2CFreqSet(1000) sets frequency to 1MHz
  78. void MLX90640_I2CFreqSet(int freq)
  79. {
  80. //i2c.frequency(1000 * freq);
  81. Wire.setClock((long)1000 * freq);
  82. }
  83.  
  84. //Write two bytes to a two byte address
  85. int MLX90640_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data)
  86. {
  87. Wire.beginTransmission((uint8_t)_deviceAddress);
  88. Wire.write(writeAddress >> 8); //MSB
  89. Wire.write(writeAddress & 0xFF); //LSB
  90. Wire.write(data >> 8); //MSB
  91. Wire.write(data & 0xFF); //LSB
  92. if (Wire.endTransmission() != 0)
  93. {
  94. //Sensor did not ACK
  95. Serial.println("Error: Sensor did not ack");
  96. return (-1);
  97. }
  98.  
  99. uint16_t dataCheck;
  100. MLX90640_I2CRead(_deviceAddress, writeAddress, 1, &dataCheck);
  101. if (dataCheck != data)
  102. {
  103. //Serial.println("The write request didn't stick");
  104. return -2;
  105. }
  106.  
  107. return (0); //Success
  108. }
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement