Hello Group.....<br><br>Does anyone have a GOOD example (in C) on how to do a write on the USI SPI?&nbsp; I&#39;m using a Tiny44 in SLAVE mode and I can get it to read the data coming in, but I can&#39;t seem to get it to write correctly.&nbsp; I&#39;m trying to have it send a response after it receives a command. Here is what I have:
<br clear="all"><br><br>void SpiWriteByte(unsigned char byte)<br>{<br>&nbsp; USISR = (1 &lt;&lt; USIOIF);<br>&nbsp; USIDR = byte;<br>}<br>//---------------------------------------------------------------------<br>unsigned char Read_USI_SPI_Byte(void)
<br>{<br>&nbsp; USISR = (1 &lt;&lt; USIOIF);<br>&nbsp; while(1)<br>&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(USISR &amp; (1 &lt;&lt; USIOIF)) return USIDR;<br>&nbsp;&nbsp;&nbsp; &nbsp; if(!(PINA &amp; ENABLE)) break;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; <br>&nbsp;&nbsp; return USIDR;<br>}<br>//------------------------------------------------------------------
<br>main()<br>{<br>&nbsp; USICR = 0x18;&nbsp; // init usi for 3-wire mode, external clock<br>&nbsp; while(1)<br>&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; if(PINA &amp; ENABLE)&nbsp; // enable pin - PortA.0<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SpiWriteByte(0x01);&nbsp;&nbsp; // 1st &quot;ACK&quot; after enable
<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; channel = Read_USI_SPI_Byte();&nbsp; // read pwm channel&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SpiWriteByte(0x02);&nbsp; // &quot;ACK&quot; after command<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = Read_USI_SPI_Byte();&nbsp; // read hi-byte&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;  
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SpiWriteByte(0x04);&nbsp; // &quot;ACK&quot; after command<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>}<br>//------------------------------------------------------------------<br>
<br>I would expect the values 1, 2, 4 to be sent, but never do.&nbsp; I get data but it is not correct.<br>Any clues as to what may be wrong????<br><br>-- <br>Paul Mateer, AA9GG<br>Elan Engineering Corp.<br><a href="http://www.elanengr.com">
www.elanengr.com</a>