test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Revision:
0:d383e2dee0f7
Child:
1:3c6771928b35
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AS5045/AS5045.cpp	Mon Jul 06 17:18:59 2020 +0530
@@ -0,0 +1,60 @@
+#include "AS5045.h"
+
+/** Constructor
+ *
+ *	@param CS Pin number for the digital output
+ *
+ *	@note
+ *		PinName CS is the digital output pin number
+ */
+AS5045::AS5045(PinName CS) :
+  _spi(NC,D12, D13),	// MBED SPI init
+  _cs(CS)				// Digital output pin init
+{
+	// Set SPI bitwidth
+	_spi.format(9,2);
+
+	// Set SPI frequency
+	_spi.frequency(SPI_FREQ);
+
+	// Set the digital output high
+	_cs = 1;
+}
+
+/** Read tick amount from encoder (position)
+ *
+ *	@note
+ *		Tick amount is recieved through SPI
+ */
+int AS5045::getPosition()
+{
+	int upper,		// Upper part of the tick amount integer
+		lower;		// Lower part of the tick amount integer
+
+	// Set the chip select pin low
+	_cs = 0;
+
+	// Read data from the encoder
+ 	upper = _spi.write(0x00);
+ 	lower = _spi.write(0x00);
+
+	// Set the chip select pin high
+ 	_cs = 1;
+
+ 	// Return full 9-bits tick amount
+	return ((upper << 3)+(lower >> 6));
+}
+
+/** Convert position of the encoder to degrees
+ *
+ *	@note
+ *		Tick amount is recieved internally
+ */
+float AS5045::getRotation()
+{
+	// Get data from the encoder
+ 	float value = (float)GetInt();
+
+ 	// Return degrees of rotation of the encoder
+ 	return value * RESOLUTION;
+}