![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-52.png)
AMS_AS5047P
Introduction Position Sensor
A position sensor is a sensor that facilitates measurement of mechanical position. A position sensor may indicate absolute position (location) or relative position (displacement), in terms of linear travel, rotational angle, or three-dimensional space.
Comparison Chart
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-53-1024x477.png)
Currently, there are six main types of sensors commonly used for position sensing, including Inductive, Hall Effect, and Magnetic Encoder. Among them, Inductive sensors have a higher threshold and require reference to the PCB’s coil winding. As shown in the figure below, different coil winding methods can result in different waveforms, and general manufacturers do not provide PCB winding support for design and verification. Therefore, Hall Effect and Magnetic Encoder are more widely used due to cost considerations, with Hall Effect being the most common.
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-54-1024x484.png)
AMS Magnetic Encoder Circuit diagram of working principle
Here we introduce Magnetic Encoder with AMS as the main axis. The comparison table below shows that AMS has a shorter response time than Allegro, although it loses in resolution. However, response time is more important in motor control. Another advantage of AMS products is their better ability to resist surrounding magnetic fields, as they use vertical magnetic field changes in contrast to horizontal magnetic fields on one side.
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-55-1024x570.png)
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-56-1024x527.png)
SPI Setting on STM32 and Coding Sample
Here we choose to explain SPI, which can be seen in the datasheet that the first 2 bits of each 16-bit data are defined separately, and the main data is transmitted by the following 14 bits. However, the full data is still transmitted in 16-bit through SPI transmission.
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-57.png)
Here, we can see that in the Datasheet, there are separate Read and Write transmissions. We can see that after sending the Read message, there is a time difference until the DATA is received for the next data point.
Here is an example of a read operation.
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-58.png)
![](http://en.stamssolution.com/wp-content/uploads/2022/03/image-59.png)
SPI Read the Data
typedef union {
uint16_t raw;
struct __attribute__ ((packed)) {
uint16_t data:14;
uint16_t ef:1;
uint16_t pard:1;
} values;
} ReadDataFrame;
float getANGLE()
{
ReadDataFrame readDataFrame = readRegister(CMD_ANGLE);
Angle angle;
angle.raw = readDataFrame.values.data;
return angle.values.cordicang/16384.*360.;
}
SPI on Position sensor
ReadDataFrame readRegister(uint16_t registerAddress)
{
CommandFrame command;
command.values.rw = READ;
command.values.commandFrame = registerAddress;
command.values.parc = isEven(command.raw);
CommandFrame nopCommand;
nopCommand.values.rw = READ;
nopCommand.values.commandFrame = CMD_NOP;
nopCommand.values.parc = isEven(nopCommand.raw);
ReadDataFrame receivedFrame;
receivedFrame.raw = SPI_read(command.raw, nopCommand.raw);
return receivedFrame;
}
void writeRegister(uint16_t registerAddress, uint16_t registerValue)
{
CommandFrame command;
command.values.rw = WRITE;
command.values.commandFrame = registerAddress;
command.values.parc = isEven(command.raw);
WriteDataFrame contentFrame;
contentFrame.values.data = registerValue;
contentFrame.values.low = 0;
contentFrame.values.pard = isEven(contentFrame.raw);
SPI_write(command.raw, contentFrame.raw);
}