Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Wiki Markup
{code}
 #include <SPI.h>
#include <stdio.h>

const byte READ = 0b11111100;     
const byte WRITE = 0b00000010;

// CSB pinni
const int AccChipSelectPin = 48;

// ACC muuttujat
unsigned short acc_msb_ydata;
unsigned short acc_lsb_ydata;
short acc_ydata;

unsigned short acc_msb_xdata;
unsigned short acc_lsb_xdata;
short acc_xdata;

unsigned short acc_msb_zdata;
unsigned short acc_lsb_zdata;
short acc_zdata;

void setup() {
  Serial.begin(115200);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV64);
  pinMode(AccChipSelectPin, OUTPUT);
}

void loop()
{     
//Acc akseleiden luku    
//Acc x-data
acc_msb_xdata=AccReadRegister(0x05, 1);
acc_lsb_xdata=AccReadRegister(0x04, 1);
acc_xdata=(acc_msb_xdata<<8) | acc_lsb_xdata;
acc_xdata=realValues(acc_xdata);

//Acc y-data
acc_msb_ydata=AccReadRegister(0x07, 1);
acc_lsb_ydata=AccReadRegister(0x06, 1);
acc_ydata=(acc_msb_ydata<<8) | acc_lsb_ydata;
acc_ydata=realValues(acc_ydata);

//Acc z-data
acc_msb_zdata=AccReadRegister(0x09, 1);
acc_lsb_zdata=AccReadRegister(0x08, 1);
acc_zdata=(acc_msb_zdata<<8) | acc_lsb_zdata;
acc_zdata=realValues(acc_zdata);

// tulostetaan arvot sarjaväylään
Serial.print("ACC: ");
Serial.print(acc_xdata, DEC);
Serial.print(" ");
Serial.print(acc_ydata, DEC);
Serial.print(" ");
Serial.println(acc_zdata, DEC);

}
//acc read
unsigned int AccReadRegister(byte thisRegister, int bytesToRead ) {
  byte inByte = 0;           
  unsigned int result = 0;   

  thisRegister = thisRegister << 2;
  byte dataToSend = thisRegister & READ;
  digitalWrite(AccChipSelectPin, LOW);
  SPI.transfer(dataToSend);
  result = SPI.transfer(0x00);
  bytesToRead--;
  if (bytesToRead > 0)
  {    
    result = result << 8;
    inByte = SPI.transfer(0x00);  
    result = result | inByte;  
    bytesToRead--;
  }
  digitalWrite(AccChipSelectPin, HIGH);
  delay(10);
  return(result);
}

//acc write
void AccWriteRegister(byte thisRegister, byte thisValue)
{  
  thisRegister = thisRegister << 2;
  byte dataToSend = thisRegister | WRITE;
  digitalWrite(AccChipSelectPin, LOW);
  SPI.transfer(dataToSend);
  SPI.transfer(thisValue);  
  digitalWrite(AccChipSelectPin, HIGH);
  delay(20);
}


unsigned int shift(unsigned short thisData, int thisBit)
{
unsigned short data;
data=thisData>>(thisBit-1);
data=data&0x01;
return data;
}
unsigned short realValues(unsigned short values)
{
  short taulukko[14];
  int i;
  int bitti=3;
  unsigned short valmis;
  for(i=0;i<14;i++)
  {
    
    taulukko[i]= shift(values,bitti);
    bitti++;
  }
valmis=(10/9)*(-taulukko[13]*pow(2,13)+taulukko[12]*pow(2,12)+taulukko[11]*pow(2,11)+taulukko[10]*pow(2,10)+taulukko[9]*pow(2,9)+taulukko[8]*pow(2,8)+taulukko[7]*pow(2,7)+taulukko[6]*pow(2,6)+taulukko[5]*pow(2,5)+taulukko[4]*pow(2,4)+taulukko[3]*pow(2,3)+taulukko[2]*pow(2,2)+taulukko[1]*2+taulukko[0]);

return valmis;
}
{code}