TPA81.c
Code: Select all
/*
This Code was created by Tiago Caldeira (docilio)
Any information or questions, visit:
www.nxt4you.com
*/
#define TPA_CMD 0x00
#define TPA_AMB 0x01
#define TPA_P01 0x02
#define TPA_P02 0x03
#define TPA_P03 0x04
#define TPA_P04 0x05
#define TPA_P05 0x06
#define TPA_P06 0x07
#define TPA_P07 0x08
#define TPA_P08 0x09
/* Variaveis Globais */
ubyte Read[] = {0x02, 0xB0, 0x00};
ubyte Write[] = {0x03, 0xB0, 0x00, 0x00};
ubyte outbuf[10];
/* Prototipos */
int i2c_read(tSensors port, byte addr , byte regs);
int i2c_read(tSensors port, byte addr , byte regs, int n );
void i2c_write(tSensors port, byte addr , byte regs, byte cmd);
int ubyteToInt(ubyte _byte);
int TPA_Ambiente(tSensors port, byte addr);
/* Funcoes */
int ubyteToInt(ubyte _byte) {
int _ret = 0;
_ret = (_byte & 0x80) ? (_byte & 0x7F) + 0x80 : _byte;
return _ret;
}
void i2c_write(tSensors port, byte addr , byte regs, byte cmd)
{
Write[1] = addr;
Write[2] = regs;
Write[3] = cmd;
while (nI2CStatus[port] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
sendI2CMsg(port, Write[0], 0);
}
int i2c_read_multi(tSensors port, ubyte addr , ubyte regs, int n )
{
int out=0;
const int count = n;
Read[1] = addr;Read[2] = regs;
while (nI2CStatus[port] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
sendI2CMsg(port, Read[0], count);
while (nI2CStatus[port] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
if(nI2CBytesReady[port] == count)
{
readI2CReply(port, outbuf[0], count);
if( n== 1) out=ubyteToInt(outbuf[0]);
else
{
int i;
for(i=0;i<n;i++)
{
TEMPERATURA[i] = outbuf[i];
//out += outbuf[i];
}
}
}
else {
memset(outbuf, 0, 0); //RobotC
}
return out;
}
int i2c_SRF_read(tSensors port, byte addr , byte regs)
{
int out=0;
const int count = 2;
Read[1] = addr;Read[2] = regs;
while (nI2CStatus[port] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
sendI2CMsg(port, Read[0], count);
while (nI2CStatus[port] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
if(nI2CBytesReady[port] == count)
{
readI2CReply(port, outbuf[0], count);
out = ubyteToInt(outbuf[0])*256;
out+= ubyteToInt(outbuf[1]);
}
else {
memset(outbuf, 0, 0); //RobotC
}
return out;
}
int i2c_read(tSensors port, byte addr , byte regs) {return i2c_read_multi(port,addr,regs,1);}
int i2c_read(tSensors port, byte addr , byte regs, int n ){return i2c_read_multi(port,addr,regs,n);}
int TPA_Sofware(tSensors port, byte addr)
{
return i2c_read(port,addr,TPA_CMD);
}
int TPA_Ambiente(tSensors port, byte addr)
{
return i2c_read(port,addr,TPA_AMB);
}
int TPA_Px(tSensors port, byte addr,int x)
{
return i2c_read(port,addr,TPA_P01+x-1);
}
void TPA_ALL(tSensors port, byte addr)
{
i2c_read_multi(port,addr,TPA_AMB,9);
}
int TPA_Max(tSensors port, byte addr)
{
i2c_read_multi(port,addr,TPA_AMB,9);
int i=0;int max=0;
for( i = 1 ; i < 9 ; i++ )
{
if (max < TEMPERATURA[i] )
max = TEMPERATURA[i];
}
return max;
}
int TPA_DELTA(tSensors port, ubyte addr)
{
i2c_read_multi(port,addr,0x00,10);
int i=0;int max=0;
for( i = 2 ; i < 10 ; i++ )
{
if (max < TEMPERATURA[i] )
max = TEMPERATURA[i];
}
return max - TEMPERATURA[1];
}
void TPA_Servo(tSensors port, ubyte addr, ubyte pos)
{
i2c_write(port, addr ,0x01,pos);
}
Test_TPA81.c
Code: Select all
#pragma config(Sensor, S2, MD25, sensorI2CCustom)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
This Code was created by Tiago Caldeira (docilio)
Any information or questions, visit:
www.nxt4you.com
*/
#pragma fileExtension("rtm")
#pragma platform("NXT")
#define TPA81_Port 0xD0
int TEMPERATURA[10];
#include "TPA81.c"
void ChangeADDR(tSensors port, int addr, int newadd)
{
i2c_write(port,addr,SRF_Comands,CHANGE_ADD_1);wait1Msec(10);
i2c_write(port,addr,SRF_Comands,CHANGE_ADD_2);wait1Msec(10);
i2c_write(port,addr,SRF_Comands,CHANGE_ADD_3);wait1Msec(10);
i2c_write(port,addr,SRF_Comands,newadd);wait1Msec(10);
}
void init()
{
}
task main()
{
init();
TPA_Servo(MD25,TPA81_Port,15);
wait1Msec(1000);
while(1)
{
int xD = TPA_DELTA(MD25,TPA81_Port);
nxtDisplayTextLine(0,"SFTW = %d",TEMPERATURA[0]);
nxtDisplayTextLine(1,"Ambiente = %d",TEMPERATURA[1]);
nxtDisplayTextLine(2,"P1 = %d , P2 = %d",TEMPERATURA[2],TEMPERATURA[3]);
nxtDisplayTextLine(3,"P3 = %d , P4 = %d",TEMPERATURA[4],TEMPERATURA[5]);
nxtDisplayTextLine(4,"P5 = %d , P6 = %d",TEMPERATURA[6],TEMPERATURA[7]);
nxtDisplayTextLine(5,"P7 = %d , P8 = %d",TEMPERATURA[8],TEMPERATURA[9]);
nxtDisplayTextLine(7,"DELTA = %d",xD);
if( xD > 10)
{
PlayImmediateTone(20*xD,10);
wait1Msec(100);
}
}
}
=> DO NOT USE PULL-UP RESISTORS!
=> THIS CODE IS FOR ROBOT-C