Interfacing the AS5045 using SPI

Dependencies:   mbed

Committer:
ms523
Date:
Fri Oct 22 17:36:47 2010 +0000
Revision:
0:ce4a539ca1d4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ms523 0:ce4a539ca1d4 1 /* This program is designed to interface with the AS5045 IC encoder
ms523 0:ce4a539ca1d4 2 It calls a function to read the encoder value every second
ms523 0:ce4a539ca1d4 3 The program then prints the vaule to a PC screen via hyperterminal*/
ms523 0:ce4a539ca1d4 4
ms523 0:ce4a539ca1d4 5 #include "mbed.h"
ms523 0:ce4a539ca1d4 6
ms523 0:ce4a539ca1d4 7 Serial pc(USBTX, USBRX);
ms523 0:ce4a539ca1d4 8 SPI spi(p5, p6, p7); // mosi, miso, sclk
ms523 0:ce4a539ca1d4 9 DigitalOut cs(p25);
ms523 0:ce4a539ca1d4 10
ms523 0:ce4a539ca1d4 11 long EncoderValue;
ms523 0:ce4a539ca1d4 12 int Encoder;
ms523 0:ce4a539ca1d4 13
ms523 0:ce4a539ca1d4 14 Ticker ReadEncoder;
ms523 0:ce4a539ca1d4 15
ms523 0:ce4a539ca1d4 16 void Read(){
ms523 0:ce4a539ca1d4 17
ms523 0:ce4a539ca1d4 18 spi.format(9,2); // Setup the spi for 9 bit data, high steady state clock,
ms523 0:ce4a539ca1d4 19 spi.frequency(500000);
ms523 0:ce4a539ca1d4 20 cs = 0; // Select the device by seting chip select low
ms523 0:ce4a539ca1d4 21
ms523 0:ce4a539ca1d4 22 // Send a dummy byte to receive the contents encoder
ms523 0:ce4a539ca1d4 23 int upper = spi.write(0x00);
ms523 0:ce4a539ca1d4 24 int lower = spi.write(0x00);
ms523 0:ce4a539ca1d4 25 lower = lower >> 6;
ms523 0:ce4a539ca1d4 26 upper = (upper << 6)+lower;
ms523 0:ce4a539ca1d4 27 cs = 1;
ms523 0:ce4a539ca1d4 28
ms523 0:ce4a539ca1d4 29 pc.printf("%d \r", upper);
ms523 0:ce4a539ca1d4 30 }
ms523 0:ce4a539ca1d4 31
ms523 0:ce4a539ca1d4 32
ms523 0:ce4a539ca1d4 33 int main() {
ms523 0:ce4a539ca1d4 34 pc.printf("Hello World!\n");
ms523 0:ce4a539ca1d4 35 ReadEncoder.attach_us(&Read, 100000); // call flip every 100 milli-seconds
ms523 0:ce4a539ca1d4 36
ms523 0:ce4a539ca1d4 37 while(1) {
ms523 0:ce4a539ca1d4 38 }
ms523 0:ce4a539ca1d4 39 }
ms523 0:ce4a539ca1d4 40