Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Plain Text by Silver ( 13 years ago )
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int func(unsigned char byteFlag, int whichBit)
{
if (whichBit > 0 && whichBit <= 8)
return (byteFlag & (1<<(whichBit-1)));
else
return 0;
}
main()
{
float min = 0;
float max = 0;
printf("Podaj minimalny zakres: ");
scanf("%f", &min;);
printf("Podaj maksymalny zakres: ");
scanf("%f", &max;);
wiringPiSetup();
int fd; // File descrition
char *fileName = "/dev/i2c-1"; // Name of the port we will be using
int address = 0x48; // Address of TPA81 shifted right 1 bit
unsigned char buf[10];
pinMode(0,OUTPUT);
digitalWrite(0,LOW);
if ((fd = open(fileName, O_RDWR)) < 0) { // Open port for reading and writing
printf("Failed to open i2c port\n");
exit(1);
}
if (ioctl(fd, I2C_SLAVE, address) < 0) { // Set the port options and set the address of the device we wish to speak to
printf("Unable to get bus access to talk to slave\n");
exit(1);
}
buf[0] = 0; // This is the register we wish to read from
if ((write(fd, buf, 1)) != 1) {
//printf("%c\n",buf); // Send register to read from
printf("Error writing to i2c slave\n");
exit(1);
}
float temp = 0;
for(;;)
{
if (read(fd, buf, 10) != 10) { // Read back data into buf[]
printf("Unable to read from slave\n");
exit(1);
}
else {
int znak = func(buf[0],8);
unsigned char x;
//printf("plus czy minus %d\n",znak);
if(znak == 128){
x = buf[0];
x = x- 0x80;
x = 127 - x;
}
else{
x=buf[0];
}
temp =(float) buf[1]/64;
temp = temp /4;
temp = temp + x;
if (znak ==0){
printf("Temperatura to plus: %f\n",temp);
}
else {
printf("Temperatura to minus: %f\n",temp);
}
}
if(temp > max || temp < min){
digitalWrite(0,HIGH);
delay(500);
digitalWrite(0,LOW);
delay(500);
}
}
}
Revise this Paste