Last active
October 1, 2016 17:22
-
-
Save crazystick/4bd75a978fba91cfbea9 to your computer and use it in GitHub Desktop.
wiringX Hummingboard TMP102 I2C and sparkfun 7-segment display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/ioctl.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <stdint.h> | |
#include "wiringX.h" | |
/* I2C Config */ | |
#define I2C_ADDR 0x48 | |
#define TMP_WR 0x90 //Assume ADR0 is tied to VCC | |
#define TMP_RD 0x91 | |
#define TEMP_REG 0x00 | |
/* SPI Config */ | |
#define SPI_CHAN 0 | |
#define SPI_SPEED 250000 | |
int fd; | |
int fd_spi; | |
void clearDisplaySPI() { | |
unsigned char spi_data[1] = {0x76}; | |
fprintf(stderr, "data = %X\n", spi_data[0]); | |
wiringXSPIDataRW(SPI_CHAN, spi_data, 1); // Clear display command | |
} | |
void setCursorSPI() { | |
unsigned char spi_data[2] = {0x79, 0x00}; | |
fprintf(stderr, "data = %X%X\n", spi_data[0], spi_data[1]); | |
wiringXSPIDataRW(SPI_CHAN, spi_data, 2); // set cursor command | |
} | |
void setDecimalsSPI(unsigned char decimals) { | |
unsigned char spi_data[2] = {0x77, decimals}; | |
fprintf(stderr, "data = %X%X\n", spi_data[0], spi_data[1]); | |
wiringXSPIDataRW(SPI_CHAN, spi_data, 2); | |
} | |
int main(void) { | |
wiringXSetup(); | |
if ((fd = wiringXI2CSetup(I2C_ADDR)) < 0 ) { | |
fprintf(stderr, "I2C Setup failed: %i\n", fd); | |
return -1; | |
} else { | |
fprintf(stderr, "I2C Setup OK: %i\n", fd); | |
} | |
char reg[2] = {0,0}; | |
int data = 0; | |
unsigned char spi_data[4] = "0000"; | |
char temp_str[5]; | |
pinMode(1, OUTPUT); | |
if ((fd_spi = wiringXSPISetup(SPI_CHAN, SPI_SPEED)) < 0) { | |
fprintf(stderr, "SPI Setup failed: %i\n", fd_spi); | |
} else { | |
fprintf(stderr, "SPI Setup OK: %i\n", fd_spi); | |
} | |
clearDisplaySPI(); | |
while(1) { | |
digitalWrite(1, HIGH); | |
data = wiringXI2CReadReg16(fd, TEMP_REG); | |
//fprintf(stderr, "data = 0x%0*x\n", 4, data); | |
reg[0] = (data>>8)&0xff; | |
reg[1] = data&0xff; | |
//fprintf(stderr, "msb = %i\n", reg[1]); | |
//fprintf(stderr, "lsb = %i\n", reg[0]); | |
int16_t res = ((int8_t)reg[1] << 4) | ((uint8_t)reg[0] >> 4); | |
float temp = (float) ((float)res * 0.0625); | |
fprintf(stderr, "temp = %2.2f\n", temp); | |
sprintf(temp_str, "%2.2f", temp); | |
spi_data[0] = (temp_str[0] - '0') & 0xff; | |
spi_data[1] = (temp_str[1] - '0') & 0xff; | |
spi_data[2] = (temp_str[3] - '0') & 0xff; | |
spi_data[3] = (temp_str[4] - '0') & 0xff; | |
fprintf(stderr, "data = %X%X%X%X\n", spi_data[0], spi_data[1], spi_data[2], spi_data[3]); | |
setCursorSPI(); | |
wiringXSPIDataRW(SPI_CHAN, spi_data, 4); | |
setDecimalsSPI(2&0xff); | |
sleep(2); | |
digitalWrite(1, LOW); | |
sleep(2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment