Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 9 months ago.
Hi, I have to measure an encoder AMT20 with kl25z board, can you give me advice about this code.
- 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); 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(1000000); 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; uint8_t 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); 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\r\n, %d\r\n", m, n); } } wait_ms(5); }
}