Interfacing the AS5045 using SPI

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*     This program is designed to interface with the AS5045 IC encoder
00002     It calls a function to read the encoder value every second 
00003     The program then prints the vaule to a PC screen via hyperterminal*/
00004 
00005 #include "mbed.h"
00006 
00007 Serial pc(USBTX, USBRX);
00008 SPI spi(p5, p6, p7); // mosi, miso, sclk
00009 DigitalOut cs(p25);
00010 
00011 long EncoderValue; 
00012 int Encoder;
00013 
00014 Ticker ReadEncoder;
00015 
00016 void Read(){
00017 
00018     spi.format(9,2);            // Setup the spi for 9 bit data, high steady state clock,
00019     spi.frequency(500000);  
00020     cs = 0;                     // Select the device by seting chip select low  
00021 
00022 // Send a dummy byte to receive the contents encoder
00023     int upper = spi.write(0x00);
00024     int lower = spi.write(0x00);
00025     lower = lower >> 6;   
00026     upper = (upper << 6)+lower;
00027     cs = 1;
00028     
00029     pc.printf("%d  \r", upper);   
00030 }
00031 
00032 
00033 int main() {
00034     pc.printf("Hello World!\n");
00035     ReadEncoder.attach_us(&Read, 100000); // call flip every 100 milli-seconds
00036     
00037     while(1) {
00038     }
00039 }
00040