Go Down

Topic: Arduino Due SPI for 32 Bit Quadrature Encoder  (Read 157 times) previous topic - next topic

kbhargavram1990

Hello All,
I am stuck in programming for Arduino Due with 32 bit quadrature encoder. I attached the program so far.
I need to read the data at a very high speed. I tried using digitalWritefast library but it doesnt seem to be working for Arduino Due.

Any Suggestions would be very helpful.


Thanks

#include<SPI.h>
signed long encoder1count = 0;
long count_value;  

void initEncoders()
{
 delayMicroseconds(100);
 digitalWrite(slaveSelectEnc1,HIGH);
 SPI.begin(4);
 SPI.setClockDivider(4,21);
 digitalWrite(slaveSelectEnc1,LOW);        
 SPI.transfer(0x88);     // MDR0                  
 SPI.transfer(0x03);     // Quad x 4  
 digitalWrite(slaveSelectEnc1,HIGH);
 delayMicroseconds(100);
 digitalWrite(slaveSelectEnc1,LOW);        
 SPI.transfer(0x90); // MR 1                      
 SPI.transfer(0x01); // Four Byte 0, Three 1                  
 digitalWrite(slaveSelectEnc1,HIGH);    
}


void clearEncoderCount()
{
 digitalWrite(slaveSelectEnc1,LOW);      // Begin SPI conversation  
 SPI.transfer(0x98);    
 SPI.transfer(0x00);
 SPI.transfer(0x00);          
 SPI.transfer(0x00);          
 SPI.transfer(0x00);  // lowest order byte
 digitalWrite(slaveSelectEnc1,HIGH);
 delayMicroseconds(100);
 digitalWrite(slaveSelectEnc1,LOW);      // Begin SPI conversation  
 SPI.transfer(0xE0);    
 digitalWrite(slaveSelectEnc1,HIGH);     // Terminate SPI conversation  
}

void setup()
{
Serial.begin(9600);
}

void loop()
{
     delay(100);
     initEncoders();  
     clearEncoderCount();
     while(1==1)
     {
     digitalWrite(slaveSelectEnc1,LOW);
     SPI.transfer(0x60);  
     count_value = SPI.transfer(0x00);
     count_value = (count_value << 8) + SPI.transfer(0x00);
     count_value = (count_value << 8) + SPI.transfer(0x00);
     count_value = (count_value << 8) + SPI.transfer(0x00);
     digitalWrite(slaveSelectEnc1,HIGH);
     Serial.println(count_value);
     }
}


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