Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Help with magnetic encoder and spi


  • Please log in to reply
2 replies to this topic

#1 alexandrosroidis

alexandrosroidis

    New Member

  • Members
  • Pip
  • 6 posts

Posted 28 April 2013 - 08:15 AM

Hi to all,

I need to connect this board from ams to netduino

http://www.ams.com/e...8A-EK-AB-STM1.0

It has the as5048a magnetic encoder ic which is spi compatible.

the ic datasheet is here

http://www.ams.com/e...Sensors/AS5048A

 

so i connect everything as i should on the netduino

my code is

 


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using System.Globalization;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
using System.Text;

namespace spi_encoders
{
  public class Program
  {

  public static void Main()
  {
SPI.Configuration as5048a = new Microsoft.SPOT.Hardware.SPI.Configuration
  (SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D5, false, 100, 100, false, true, 10000,
  SecretLabs.NETMF.Hardware.Netduino.SPI_Devices.SPI1);

  ushort[] command_buffer = { 0x3FFF};//0x3fff is the read command
 
  ushort[] angle_buffer = new ushort[1];//the ic returns 16bit reply to the read command
 
  ushort result;
  double angle;
  SPI spibus = new SPI(as5048a);
  while (true)
  {
 
  spibus.WriteRead(command_buffer, angle_buffer);
 
 
Debug.Print("Data from chip trial:" +" "+ angle_buffer[0].ToString());

 

  }
  }

 
  }
}
 

without the magnet around i getting nonsense result from the chip

 

like

Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576
Data from chip trial: 24576

if i put the magnet near and start rotating it i get:

Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8335
Data from chip trial: 8336
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41104
Data from chip trial: 41106
Data from chip trial: 8338
Data from chip trial: 8339
Data from chip trial: 41107
Data from chip trial: 57493
Data from chip trial: 24725
Data from chip trial: 8342
Data from chip trial: 41112
Data from chip trial: 8345
Data from chip trial: 41115
Data from chip trial: 8348
Data from chip trial: 41118
Data from chip trial: 8351
Data from chip trial: 41121
Data from chip trial: 8356
Data from chip trial: 8357
Data from chip trial: 57512
Data from chip trial: 57512
Data from chip trial: 57514
Data from chip trial: 24747
Data from chip trial: 57515
Data from chip trial: 57515
Data from chip trial: 57515
Data from chip trial: 57515
Data from chip trial: 57515
Data from chip trial: 57514
Data from chip trial: 24746
Data from chip trial: 24744
Data from chip trial: 41127

 

I am very frustrated because i bought the netduino for this purpose and it seems unable to do what i want

this is the second mag encoder i bought the first was from avago and it was ssi

 

Can anyone help me out

Is something wrong with my code

 

Alexandros

 



#2 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 28 April 2013 - 02:12 PM

is it the correct SPI mode?  the data sheet seems to suggest mode 1 (q.v. S 4.2.1), but I think (!) you set mode 0, as per the final two booleans  in your SPI.Configuration.  I think they should be false, false for mode 1.

 

After that I would look at the command itself, and the exact meaning of WriteRead.  SPI as you know is a shift register, and there is always input while there is output, though you might discard it.  I would imagine that 0x3fff has an associated input that is discarded, and the subsequent transfer (maybe using output 0x0000 NOP as per S 4.2.7?) has the input that is the angle (returned from the 0x3fff).

 

Please just take that as a suggestion -- I have never worked with this particular device, and also have little hands-on experience with the NP2 SPI implementation (though I done it with other controllers).

 

Also, if you have an oscilloscope, you might want to hook that up and crank down your clock rate to see what's really going on.  (This was recently invaluable for me in debugging an i2c problem; I would have spent forever guessing).



#3 alexandrosroidis

alexandrosroidis

    New Member

  • Members
  • Pip
  • 6 posts

Posted 30 April 2013 - 06:32 AM

Solved

Thanks for the help ziggurat






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.