7 years, 1 month ago.

I have to read the AMt20 encoder spi with mbed but mi code doen't work

  1. include "mbed.h"

SPI spi(PTD2, PTD3, PTD1); mosi, miso, sclk

DigitalOut cs(PTD0);

Serial pc(USBTX, USBRX); tx, rx

int main(void) {

PwmOut rled(LED1); PwmOut gled(LED2);

PwmOut bled(LED3);

pc.baud(115200);Set baudrate here.

Chip must be deselected

Setup the spi for 8 bit data, high steady state clock,

second edge capture, with a 1MHz clock rate

spi.format(8,3);

spi.frequency(500000);

Select the device by seting chip select low

cs = 0;

Send 0x8f, the command to read the WHOAMI register

spi.write(0x8f);

Send a dummy byte to receive the contents of the WHOAMI register

uint8_t whoami = spi.write(0x00);

Deselect the device cs = 1;

while(1)

{ cs = 0;

whoami = spi.write(0x10); read position rd_pos

cs = 1;

if (whoami==0xA5)

{ cs=0;

spi.write(0x00);

cs=1;

}

else

{ if(whoami==0x10)

{

cs=0;

uint8_t who1=spi.write(0x00);

pc.printf(whoami);

cs=1;

wait_us(20);

cs=0;

uint8_t who2=spi.write(0x00); cs=1; uint8_t d = who1 << 8|who2;

float m=map(d,0, 4096,0,360);

(long x, long in_min, long in_max, long out_min, long out_max)

int m = ((d*360)/4096);

int n= 360-m;

rled = 1.0f - m;

pc.printf("grados: %d, n: %d\n", m, d); } }

wait_ms(5); }

}

Hi, did you find a solution. Beacause I can't get it to work

posted by Robin Galipeau 19 Jun 2018
Be the first to answer this question.