#define OVERFLOW 65535
#define PIN_ADXL_XOUT PIN_B6
#define PIN_ADXL_YOUT PIN_B5
#define PIN_ADXL_ZOUT PIN_B3
#define PIN_ADXL_UOUT PIN_B2
void init_sensor_board()
{
restart_wdt();
setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
}
void init_ds1621(byte id)
{
id<<1;
i2c_start();
i2c_write(0x90+(2*id));
i2c_write(0xac);
i2c_write(9);
i2c_stop();
}
byte read_temp_ds1621(byte id)
{
byte datah,datal;
id<<1;
i2c_start();
i2c_write(0x90+(2*id));
i2c_write(0xee);
i2c_stop();
delay_ms(1000);
restart_wdt();
i2c_start();
i2c_write(0x90+(2*id));
i2c_write(0xaa);
i2c_start();
i2c_write(0x91+(2*id));
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();
return(datah);
}
void led_on1()
{
output_low(PIN_B2);
}
void led_off1()
{
output_high(PIN_B2);
}
void led_on2()
{
output_low(PIN_B3);
}
void led_off2()
{
output_high(PIN_B3);
}
short int PIR_raw()
{
short int p;
p=input(PIN_B4);
return p;
}
short int PIR()
{
short int p;
int i;
for (i=0;i<100;i++) {
p=input(PIN_B4);
if (p==0) { return 0; };
delay_ms(1);
}
return 1;
}
short int touch()
{
short int p;
p=input(PIN_B1);
if (p==0) return 1;
else return 0;
}
long light1()
{
long tmp;
set_adc_channel(0);
delay_us(10);
tmp = Read_ADC();
return tmp;
}
long light2()
{
long tmp;
set_adc_channel(2);
delay_us(10);
tmp = Read_ADC();
return tmp;
}
unsigned long get_accelerationX()
{
unsigned long x_t1,x_t3,scaledacc,counter;
counter =0;
restart_wdt();
while( INPUT(PIN_ADXL_XOUT) &&(counter<OVERFLOW) ) {++counter;};
counter =0;
restart_wdt();
while( !INPUT(PIN_ADXL_XOUT)&&(counter<OVERFLOW) ) {++counter;};
x_t1=0;
restart_wdt();
while( INPUT(PIN_ADXL_XOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};
x_t3=0;
restart_wdt();
while( !INPUT(PIN_ADXL_XOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};
if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
{
return 0;
}
x_t3=x_t3+x_t1;
counter=65535;
scaledacc=0;
restart_wdt();
while (counter>x_t3)
{
scaledacc=scaledacc+x_t1;
counter=counter-x_t3;
}
return(scaledacc);
}
unsigned long get_accelerationY()
{
unsigned long x_t1,x_t3,scaledacc,counter;
counter =0;
restart_wdt();
while( INPUT(PIN_ADXL_YOUT) &&(counter<OVERFLOW) ) {++counter;};
counter =0;
restart_wdt();
while( !INPUT(PIN_ADXL_YOUT)&&(counter<OVERFLOW) ) {++counter;};
x_t1=0;
restart_wdt();
while( INPUT(PIN_ADXL_YOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};
x_t3=0;
restart_wdt();
while( !INPUT(PIN_ADXL_YOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};
if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
{
return 0;
}
x_t3=x_t3+x_t1;
restart_wdt();
counter=65535;
scaledacc=0;
while (counter>x_t3)
{
scaledacc=scaledacc+x_t1;
counter=counter-x_t3;
}
return(scaledacc);
}
unsigned long get_accelerationZ()
{
unsigned long x_t1,x_t3,scaledacc,counter;
counter =0;
restart_wdt();
while( INPUT(PIN_ADXL_ZOUT) &&(counter<OVERFLOW) ) {++counter;};
counter =0;
restart_wdt();
while( !INPUT(PIN_ADXL_ZOUT)&&(counter<OVERFLOW) ) {++counter;};
x_t1=0;
restart_wdt();
while( INPUT(PIN_ADXL_ZOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};
x_t3=0;
restart_wdt();
while( !INPUT(PIN_ADXL_ZOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};
if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
{
return 0;
}
x_t3=x_t3+x_t1;
restart_wdt();
counter=65535;
scaledacc=0;
while (counter>x_t3)
{
scaledacc=scaledacc+x_t1;
counter=counter-x_t3;
}
return(scaledacc);
}
unsigned long get_accelerationU()
{
unsigned long x_t1,x_t3,scaledacc,counter;
counter =0;
restart_wdt();
while( INPUT(PIN_ADXL_UOUT) &&(counter<OVERFLOW) ) {++counter;};
counter =0;
restart_wdt();
while( !INPUT(PIN_ADXL_UOUT)&&(counter<OVERFLOW) ) {++counter;};
x_t1=0;
restart_wdt();
while( INPUT(PIN_ADXL_UOUT) &&(x_t1 <OVERFLOW) ) {++x_t1;};
x_t3=0;
restart_wdt();
while( !INPUT(PIN_ADXL_UOUT)&&(x_t3 <OVERFLOW) ) {++x_t3;};
if ( (x_t1==OVERFLOW) || (x_t1==OVERFLOW) )
{
return 0;
}
x_t3=x_t3+x_t1;
restart_wdt();
counter=65535;
scaledacc=0;
while (counter>x_t3)
{
scaledacc=scaledacc+x_t1;
counter=counter-x_t3;
}
return(scaledacc);
}
byte temp()
{
byte tmp;
init_ds1621(6);
delay_ms(10);
tmp = read_temp_ds1621(6);
return tmp;
}
unsigned int get_accX()
{
unsigned long a;
unsigned int as;
a=get_accelerationX();
as = make8(a,1);
return as;
}
unsigned int get_accY()
{
unsigned long a;
unsigned int as;
a=get_accelerationY();
as = make8(a,1);
return as;
}
unsigned int get_accZ()
{
unsigned long a;
unsigned int as;
a=get_accelerationZ();
as = make8(a,1);
return as;
}
unsigned int get_accU()
{
unsigned long a;
unsigned int as;
a=get_accelerationU();
as = make8(a,1);
return as;
}