<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: 16 Bit HardwareSPI</title>
		<link>http://forums.leaflabs.com/topic.php?id=3997</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:08:09 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=3997" rel="self" type="application/rss+xml" />

		<item>
			<title>ron on "16 Bit HardwareSPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=3997#post-16283</link>
			<pubDate>Tue, 25 Sep 2012 01:57:21 +0000</pubDate>
			<dc:creator>ron</dc:creator>
			<guid isPermaLink="false">16283@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the help Andy, i tried this in a previous sketch ... but it doesn't seem to make any difference. SS got pulled LOW at the beginning of spiReadData() and set to HIGH again at the end. Maybe i get the diagram for mode and timing wrong ... is it possible to post an image?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>palsor on "16 Bit HardwareSPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=3997#post-15931</link>
			<pubDate>Sun, 23 Sep 2012 20:44:14 +0000</pubDate>
			<dc:creator>palsor</dc:creator>
			<guid isPermaLink="false">15931@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Are you pulling SS low before you read/write? I think you need to do that manually. That is, I don't think HardwareSPI pulls the SS pin low for you, so you need to do a &#60;/p&#62;
&#60;p&#62;digitalWrite(&#38;lt;insert_ss_pin_number&#38;gt;, LOW) &#60;/p&#62;
&#60;p&#62;to get the slave device to listen to you, assuming the Maple is the master. At least, when I use SPI, I control it manually :). &#60;/p&#62;
&#60;p&#62;- Andy
&#60;/p&#62;</description>
		</item>
		<item>
			<title>higwoshy on "16 Bit HardwareSPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=3997#post-15510</link>
			<pubDate>Fri, 21 Sep 2012 12:10:03 +0000</pubDate>
			<dc:creator>higwoshy</dc:creator>
			<guid isPermaLink="false">15510@http://forums.leaflabs.com/</guid>
			<description>&#60;br /&#62;</description>
		</item>
		<item>
			<title>ron on "16 Bit HardwareSPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=3997#post-15418</link>
			<pubDate>Fri, 21 Sep 2012 03:46:59 +0000</pubDate>
			<dc:creator>ron</dc:creator>
			<guid isPermaLink="false">15418@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi, thanks for the hint. I'm using a maple r5 on a OS X 10.7.4 System and i try to read an AS5048A magnetic rotary encoder (&#60;a href=&#34;http://www.ams.com)&#34; rel=&#34;nofollow&#34;&#62;http://www.ams.com)&#60;/a&#62;. I guess i found a way for the 16 Bit communication but still i get no valid data. The following code is based on the AS5048B-AB-v1-1.0_Operation-Manual_Rev.1.0.pdf. &#60;/p&#62;
&#60;pre&#62;&#60;code&#62;HardwareSPI SPI(1);

#define SPI_CMD_READ 0x4000 /*!&#38;lt; flag indicating read attempt when using SPI interface */
#define SPI_REG_AGC 0x3ffd  /*!&#38;lt; agc register when using SPI */
#define SPI_REG_MAG 0x3ffe  /*!&#38;lt; magnitude register when using SPI */
#define SPI_REG_DATA 0x3fff /*!&#38;lt; data register when using SPI */
#define SPI_REG_CLRERR 0x1  /*!&#38;lt; clear error register when using SPI */

uint16 dat=0, datIn=0;
unsigned int angle, agcreg, magreg;
byte agc;
boolean alarmHi, alarmLo;

void setup() {
  SPI.begin(SPI_4_5MHZ, MSBFIRST, 1); // Freq &#38;lt; 8MHz, MSBFIRST, CLK idle LOW, read falling edge -&#38;gt; MODE 1
}

void loop() {
  spiReadData();
  delay(50);
}

/*!
 *****************************************************************************
 * Reads out chip data via SPI interface
 *
 * This function is used to read out cordic value from chips supporting SPI
 * interface.
 *****************************************************************************
 */

void spiReadData(){
  /* Send READ AGC command. Received data is thrown away: this data comes from the precedent
   command (unknown)*/
  dat = SPI_CMD_READ &#124; SPI_REG_AGC;
  dat &#124;= spiCalcEvenParity(dat) &#38;lt;&#38;lt; 15;
  SPI.write((uint8*)&#38;amp;dat, sizeof(uint16));
  SPI.read((uint8*)&#38;amp;datIn, sizeof(uint16));

  /* Send READ MAG command. Received data is the AGC value: this data comes from the
   precedent command (unknown)*/
  dat = SPI_CMD_READ &#124; SPI_REG_MAG;
  dat &#124;= spiCalcEvenParity(dat) &#38;lt;&#38;lt; 15;
  SPI.write((uint8*)&#38;amp;dat, sizeof(uint16));
  SPI.read((uint8*)&#38;amp;datIn, sizeof(uint16));
  magreg = datIn;

  /* Send READ ANGLE command. Received data is the MAG value, from the precedent command */
  dat = SPI_CMD_READ &#124; SPI_REG_DATA;
  dat &#124;= spiCalcEvenParity(dat) &#38;lt;&#38;lt; 15;
  SPI.write((uint8*)&#38;amp;dat, sizeof(uint16));
  SPI.read((uint8*)&#38;amp;datIn, sizeof(uint16));
  agcreg = datIn;

  /* Send NOP command. Received data is the ANGLE value, from the precedent command */
  dat = 0x0000; // NOP command.
  SPI.write((uint8*)&#38;amp;dat, sizeof(uint16));
  SPI.read((uint8*)&#38;amp;datIn, sizeof(uint16));
  angle = datIn &#38;gt;&#38;gt; 2;

  if ((datIn &#38;amp; 0x4000) &#124;&#124; (agcreg &#38;amp; 0x4000) &#124;&#124; (magreg &#38;amp; 0x4000)) {
    /* error flag set - need to reset it */
    dat = SPI_CMD_READ &#124; SPI_REG_CLRERR;
    dat &#124;= spiCalcEvenParity(dat)&#38;lt;&#38;lt;15;
    SPI.write((uint8*)&#38;amp;dat, sizeof(uint16));
    SPI.read((uint8*)&#38;amp;datIn, sizeof(uint16));
    SerialUSB.println(&#38;quot;err&#38;quot;);
  } else {
    SerialUSB.println(magreg);
  }
}

/*!
 *****************************************************************************
 *  Calculate even parity of a 16 bit unsigned integer
 *
 *  This function is used by the SPI interface to calculate the even parity
 *  of the data which will be sent via SPI to the encoder.
 *
 *  \param[in] datIn : 16 bit unsigned integer whose parity shall be calculated
 *
 *  \return : Even parity
 *
 *****************************************************************************
 */
uint8 spiCalcEvenParity(uint16 datIn){
  uint8 cnt = 0;
  uint8 i; 

  for (i = 0; i &#38;lt; 16; i++) {
    if (datIn &#38;amp; 0x1){
      cnt++;
    }
    datIn &#38;gt;&#38;gt;= 1;
  }
  return cnt &#38;amp; 0x1;
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>StephenFromNYC on "16 Bit HardwareSPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=3997#post-14927</link>
			<pubDate>Wed, 19 Sep 2012 06:30:03 +0000</pubDate>
			<dc:creator>StephenFromNYC</dc:creator>
			<guid isPermaLink="false">14927@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hello Ron,&#60;/p&#62;
&#60;p&#62;Welcome to the LeafLabs forums.&#60;/p&#62;
&#60;p&#62;Please refer to the &#34;Guidelines for Posting&#34;.  It is a &#34;sticky&#34; topic at the beginning of the forum discussions.&#60;/p&#62;
&#60;p&#62;For example, your title is short and descriptive (great!).  However, it is also helpful to describe the board you are using, your OS, etc.&#60;/p&#62;
&#60;p&#62;Just trying to help get an answer which helps both you and other people with similar questions.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>ron on "16 Bit HardwareSPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=3997#post-14866</link>
			<pubDate>Wed, 19 Sep 2012 02:39:46 +0000</pubDate>
			<dc:creator>ron</dc:creator>
			<guid isPermaLink="false">14866@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi,&#60;br /&#62;
i try to setup a SPI communication, but it has to run in 16 Bit (which should be possible).&#60;br /&#62;
Unfortunately i don't get how to switch the resolution. Can anyone help me please?
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
