Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/ioctl.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <stdint.h>
  11.  
  12. #include "wiringX.h"
  13.  
  14. /* I2C Config */
  15. #define I2C_ADDR 0x48
  16. #define TMP_WR 0x90 //Assume ADR0 is tied to VCC
  17. #define TMP_RD 0x91
  18. #define TEMP_REG 0x00
  19.  
  20. /* SPI Config */
  21. #define SPI_CHAN 0
  22. #define SPI_SPEED 250000
  23.  
  24. int fd;
  25. int fd_spi;
  26.  
  27. void clearDisplaySPI() {
  28. unsigned char spi_data[1] = {0x76};
  29. fprintf(stderr, "data = %X\n", spi_data[0]);
  30. wiringXSPIDataRW(SPI_CHAN, spi_data, 1); // Clear display command
  31. }
  32. void setCursorSPI() {
  33. unsigned char spi_data[2] = {0x79, 0x00};
  34. fprintf(stderr, "data = %X%X\n", spi_data[0], spi_data[1]);
  35. wiringXSPIDataRW(SPI_CHAN, spi_data, 2); // set cursor command
  36. }
  37. void setDecimalsSPI(unsigned char decimals) {
  38. unsigned char spi_data[2] = {0x77, decimals};
  39. fprintf(stderr, "data = %X%X\n", spi_data[0], spi_data[1]);
  40. wiringXSPIDataRW(SPI_CHAN, spi_data, 2);
  41. }
  42.  
  43. int main(void) {
  44. wiringXSetup();
  45. if ((fd = wiringXI2CSetup(I2C_ADDR)) < 0 ) {
  46. fprintf(stderr, "I2C Setup failed: %i\n", fd);
  47. return -1;
  48. } else {
  49. fprintf(stderr, "I2C Setup OK: %i\n", fd);
  50. }
  51. char reg[2] = {0,0};
  52. int data = 0;
  53. unsigned char spi_data[4] = "0000";
  54. char temp_str[5];
  55.  
  56. pinMode(1, OUTPUT);
  57. if ((fd_spi = wiringXSPISetup(SPI_CHAN, SPI_SPEED)) < 0) {
  58. fprintf(stderr, "SPI Setup failed: %i\n", fd_spi);
  59. } else {
  60. fprintf(stderr, "SPI Setup OK: %i\n", fd_spi);
  61. }
  62. clearDisplaySPI();
  63. while(1) {
  64. digitalWrite(1, HIGH);
  65. data = wiringXI2CReadReg16(fd, TEMP_REG);
  66. //fprintf(stderr, "data = 0x%0*x\n", 4, data);
  67. reg[0] = (data>>8)&0xff;
  68. reg[1] = data&0xff;
  69. //fprintf(stderr, "msb = %i\n", reg[1]);
  70. //fprintf(stderr, "lsb = %i\n", reg[0]);
  71. int16_t res = ((int8_t)reg[1] << 4) | ((uint8_t)reg[0] >> 4);
  72. float temp = (float) ((float)res * 0.0625);
  73. fprintf(stderr, "temp = %2.2f\n", temp);
  74. sprintf(temp_str, "%2.2f", temp);
  75. spi_data[0] = (temp_str[0] - '0') & 0xff;
  76. spi_data[1] = (temp_str[1] - '0') & 0xff;
  77. spi_data[2] = (temp_str[3] - '0') & 0xff;
  78. spi_data[3] = (temp_str[4] - '0') & 0xff;
  79. fprintf(stderr, "data = %X%X%X%X\n", spi_data[0], spi_data[1], spi_data[2], spi_data[3]);
  80. setCursorSPI();
  81. wiringXSPIDataRW(SPI_CHAN, spi_data, 4);
  82. setDecimalsSPI(2&0xff);
  83. sleep(2);
  84. digitalWrite(1, LOW);
  85. sleep(2);
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement