Counting Rev's with PWM on a AMT203s-V absolute encoder.

So we are using two encoders for two different motors. The first encoder measures angle, the second counts revolutions and times them. A wonderful website provides a sample code for the former that works well using the ICSP pins on our Arduino Mega 2560. I have been trying to use that code as a guide to make a code that counts revolutions using the PWM pins, but with very little success.

Here's the provided code, which works wonderfully:

#include <SPI.h>
 
#define CS 3 //Chip or Slave select 
 
uint16_t ABSposition = 0;
uint16_t ABSposition_last = 0;
uint8_t temp[2];    //This one.
 
float deg = 0.00;
 
void setup()
{
  pinMode(CS,OUTPUT);//Slave Select
  digitalWrite(CS,HIGH);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV32);
  Serial.begin(115200);
  Serial.println("starting");
  Serial.flush();
  delay(2000);
  SPI.end();
 
}
uint8_t SPI_T (uint8_t msg)    //Repetive SPI transmit sequence
{
   uint8_t msg_temp = 0;  //vairable to hold recieved data
   digitalWrite(CS,LOW);     //select spi device
   msg_temp = SPI.transfer(msg);    //send and recieve
   digitalWrite(CS,HIGH);    //deselect spi device
   return(msg_temp);      //return recieved byte
}
 
void loop()
{ 
   uint8_t recieved = 0xA5;    //just a temp vairable
   ABSposition = 0;    //reset position vairable
   
   SPI.begin();    //start transmition
   digitalWrite(CS,LOW);
   
   SPI_T(0x10);   //issue read command
   
   recieved = SPI_T(0x00);    //issue NOP to check if encoder is ready to send
   
   while (recieved != 0x10)    //loop while encoder is not ready to send
   {
     recieved = SPI_T(0x00);    //cleck again if encoder is still working 
     delay(2);    //wait a bit
   }
   
   temp[0] = SPI_T(0x00);    //Recieve MSB
   temp[1] = SPI_T(0x00);    // recieve LSB
   
   digitalWrite(CS,HIGH);  //just to make sure   
   SPI.end();    //end transmition
   
   temp[0] &=~ 0xF0;    //mask out the first 4 bits
    
   ABSposition = temp[0] << 8;    //shift MSB to correct ABSposition in ABSposition message
   ABSposition += temp[1];    // add LSB to ABSposition message to complete message
    
   if (ABSposition != ABSposition_last)    //if nothing has changed dont wast time sending position
   {
     ABSposition_last = ABSposition;    //set last position to current position
     deg = ABSposition;
     deg = deg * 0.08789;    // aprox 360/4096
     Serial.println(deg);     //send position in degrees
   }   
 
   delay(10);    //wait a bit till next check
 
}

In the following code, I'm not even trying to count anything yet, I'm only trying to get meaningful data. I'm getting something, but it isn't useful, I think I'm just telling the slave to repeat what the master is sending.

#include <SPI.h>



int CSB = 2;
int PIN_SCK = 3;
int PIN_MOSI = 4;
int PIN_sck = 5;
int PIN_MISO = 7;

float val;
float temp;

void setup() {
Serial.begin(115200);

SPI.begin();
pinMode(CSB,OUTPUT); //ss
pinMode(PIN_MOSI,OUTPUT);
pinMode(PIN_MISO,INPUT);
pinMode(PIN_SCK,OUTPUT);


Serial.println("Starting");
digitalWrite(PIN_SCK,128);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(CSB, HIGH);
  SPI.beginTransaction(SPISettings(100000,MSBFIRST,SPI_MODE0)); // SPI Settings 1MHZ - Falling Edge - IDLE When Low
  digitalWrite(CSB,LOW);
  delay(10);
  Serial.println(digitalRead(PIN_MISO));
  

}

Any help you can give will be much, much appreciated.

If they are the same encodes, you communicate with them both using SPI. You have their CS lines connected to different pins on your Arduino.

To talk to the first encoder, you enable the CS1 line and communicate using your SPI_T() function. When done, disable that CS1 line.

To talk to the second encoder, you enable the CS2 line and communicate using your SPI_T() function. When done, disable that CS2 line.

Thanks, I'll see if I can get that to work.