Go Down

Topic: Need help in Coding a SSI Binary Absolute rotary encoder (Read 1 time) previous topic - next topic

jrejaud

Hi

I am new to using a Ardunio programming.

I have written the following code after reviewing several codes online but still not getting any readings
Code: [Select]
/* Vikash Balasubramanian
SSI (Serial Synchron Interface) 
Hardware specifications:
* 13-bit rotary absolute multiturn encoder
* Interface: SSI (with RS485 line definitions)
* encoded as: Binary

* SSI:
  - SSI-Clock TicTac must be equal or less 10µs.
  - 14 Clock TicTac are needed for the whole Position consiting of a 13 Bit word, the first TicTac is needed for the status bit, which enconde the status of the device, (0) for not clear.
  - After the the 14 TicTacs a puse of max. 80µs is needed for a new Position to be available.

* This Program:
  - emulates a SSI with the Arduino Ports 12 (clock) and 11 (data).
  - transform the binary code into absolute angle values
  - send the Position to COmputer via USB
 
Pins Chosen for Arduino UNO
*/

#define SSI_CLOCK 12
#define SSI_DATA 11
#define ENCODER_LENGTH 13
#define ENCODER_SPEED 4

byte encoder[ENCODER_LENGTH + 1];
byte encoderRev[ENCODER_LENGTH];
unsigned long value;
float angle;
int bitRes;
byte dummy;
/*
functionprototypes
*/
void getPosition();               // saves the status bit and the 13 position bits in the encoder, read from the register as PINB. The only time-critical process.
void condensePosition();          // condenses the recieved Byte (PINB) in encoder  to the only intersting bit (PB3) in PINB.
void transformPosition();         // transforms the 13 bit binaryArray into absolute angle
void sendPosition();              // sends the absolute angle - enclosed in '255'



void setup (){
  pinMode(SSI_CLOCK, OUTPUT);       // 
  pinMode(SSI_DATA, INPUT);         // 
  PORTB = B1111111;             
  delay(500);
  Serial.begin(115200);
}

void loop()
{
 
  if (PINB & (1<<PORTB3)) // this means that if PB3 is low it will read 0 and if PB3 is high it will read a value different from 0, in this case the value will be 0×08, or in binary 0b00001000.
  {
    getPosition();
  }
  else
  {
    Serial.println ("PortB3 not high");
  }
  if(Serial.available())
  {
    Serial.println("Serial Available");
    while(Serial.available()>0)
    {
      dummy = Serial.read();
    }
    condensePosition();
    transformPosition();
    sendPosition();
  }
  delay(2);
}

void getPosition()
{
  for(bitRes = 0;bitRes <= ENCODER_LENGTH; bitRes++)          // Untested yet
  {
     encoder[bitRes] = 0;
     encoderRev[bitRes]=0;
  }

  delayMicroseconds(40);
 
  for(bitRes = 0;bitRes <= ENCODER_LENGTH; bitRes++)
  {
    PORTB = (0<<PORTB4);
    delayMicroseconds(ENCODER_SPEED);
    PORTB = (1<<PORTB4);
    delayMicroseconds(ENCODER_SPEED);
    encoder[bitRes]= PINB;  //delayMicroseconds(3) fits a 10us tictac 
  }
 
  delayMicroseconds(100);
}

void condensePosition()
{
    for(bitRes = 0; bitRes < ENCODER_LENGTH; bitRes++)
    {
      if(encoder[bitRes] & (1<<PORTB3))
      {
        encoder[bitRes] = 1;
      }
     
      else
      {
        encoder[bitRes] = 0;
      }
    }
}

void transformPosition()
{
 
   value =0;
   
   for(bitRes = 0; bitRes < ENCODER_LENGTH; bitRes++)
   {
     encoderRev[ENCODER_LENGTH-1-bitRes] = encoder[bitRes];             // convert to unsigned long int
   }
   for (bitRes = 0; bitRes <ENCODER_LENGTH; bitRes ++)
   {
      value = value + pow(2,bitRes)*encoderRev[bitRes];             // convert to unsigned long int
   }
   Serial.print ("Value = ");
   Serial.println (value);
}

void sendPosition()
{
  angle = (value/(pow(2,ENCODER_LENGTH))) * 360 ;
  Serial.print("Angle =");
  Serial.println(angle);
}



Can some one tell me where am I going wrong.

Thanks

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy