<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.mcselec.com/bavr/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.mcselec.com/bavr/index.php?action=history&amp;feed=atom&amp;title=SPISLAVE</id>
		<title>SPISLAVE - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.mcselec.com/bavr/index.php?action=history&amp;feed=atom&amp;title=SPISLAVE"/>
		<link rel="alternate" type="text/html" href="http://wiki.mcselec.com/bavr/index.php?title=SPISLAVE&amp;action=history"/>
		<updated>2026-04-28T21:16:29Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.18.6</generator>

	<entry>
		<id>http://wiki.mcselec.com/bavr/index.php?title=SPISLAVE&amp;diff=1249&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;SPISLAVE.LIB (LBX) is a library that can be used to create a SPI slave chip when the chip does not have a hardware SPI interface.  Although most AVR chips have an ISP interfac...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.mcselec.com/bavr/index.php?title=SPISLAVE&amp;diff=1249&amp;oldid=prev"/>
				<updated>2013-02-13T11:27:19Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;SPISLAVE.LIB (LBX) is a library that can be used to create a SPI slave chip when the chip does not have a hardware SPI interface.  Although most AVR chips have an ISP interfac...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;SPISLAVE.LIB (LBX) is a library that can be used to create a SPI slave chip when the chip does not have a hardware SPI interface.&lt;br /&gt;
&lt;br /&gt;
Although most AVR chips have an ISP interface to program the chip, the 2313 for example does not have a SPI interface.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-family: Arial;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you want to control various micro’s with the SPI protocol you can use the SPISLAVE library.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-family: Arial;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The SPI-softslave.bas sample from the samples directory shows how you can use the SPISLAVE library.&lt;br /&gt;
&lt;br /&gt;
Also look at the spi-slave.bas sample that is intended to be used with hardware SPI.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-family: Arial;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sendspi.bas sample from the samples directory shows how you can use the SPI hardware interface for the master controller chip.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bascomavr&amp;quot;&amp;gt;&lt;br /&gt;
'-----------------------------------------------------------------------------------------&lt;br /&gt;
'name : spi-softslave.bas&lt;br /&gt;
'copyright : (c) 1995-2005, MCS Electronics&lt;br /&gt;
'purpose : shows how to implement a SPI SLAVE with software&lt;br /&gt;
'micro : AT90S2313&lt;br /&gt;
'suited for demo : yes&lt;br /&gt;
'commercial addon needed : no&lt;br /&gt;
'-----------------------------------------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
$regfile = &amp;quot;2313def.dat&amp;quot; ' specify the used micro&lt;br /&gt;
$crystal = 4000000 ' used crystal frequency&lt;br /&gt;
$baud = 19200 ' use baud rate&lt;br /&gt;
$hwstack = 32 ' default use 32 for the hardware stack&lt;br /&gt;
$swstack = 10 ' default use 10 for the SW stack&lt;br /&gt;
$framesize = 40 ' default use 40 for the frame space&lt;br /&gt;
 &lt;br /&gt;
'Some atmel chips like the 2313 do not have a SPI port.&lt;br /&gt;
'The BASCOM SPI routines are all master mode routines&lt;br /&gt;
'This example show how to create a slave using the 2313&lt;br /&gt;
'ISP slave code&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
'define the constants used by the SPI slave&lt;br /&gt;
Const _softslavespi_port = Portd ' we used portD&lt;br /&gt;
Const _softslavespi_pin = Pind 'we use the PIND register for reading&lt;br /&gt;
Const _softslavespi_ddr = Ddrd ' data direction of port D&lt;br /&gt;
 &lt;br /&gt;
Const _softslavespi_clock = 5 'pD.5 is used for the CLOCK&lt;br /&gt;
Const _softslavespi_miso = 3 'pD.3 is MISO&lt;br /&gt;
Const _softslavespi_mosi = 4 'pd.4 is MOSI&lt;br /&gt;
Const _softslavespi_ss = 2 ' pd.2 is SS&lt;br /&gt;
'while you may choose all pins you must use the INT0 pin for the SS&lt;br /&gt;
'for the 2313 this is pin 2&lt;br /&gt;
 &lt;br /&gt;
'PD.3(7), MISO must be output&lt;br /&gt;
'PD.4(8), MOSI&lt;br /&gt;
'Pd.5(9) , Clock&lt;br /&gt;
'PD.2(6), SS /INT0&lt;br /&gt;
 &lt;br /&gt;
'define the spi slave lib&lt;br /&gt;
$lib &amp;quot;spislave.lbx&amp;quot;&lt;br /&gt;
'sepcify wich routine to use&lt;br /&gt;
$external _spisoftslave&lt;br /&gt;
 &lt;br /&gt;
'we use the int0 interrupt to detect that our slave is addressed&lt;br /&gt;
On Int0 Isr_sspi Nosave&lt;br /&gt;
'we enable the int0 interrupt&lt;br /&gt;
Enable Int0&lt;br /&gt;
'we configure the INT0 interrupt to trigger when a falling edge is detected&lt;br /&gt;
Config Int0 = Falling&lt;br /&gt;
'finally we enabled interrupts&lt;br /&gt;
Enable Interrupts&lt;br /&gt;
 &lt;br /&gt;
'&lt;br /&gt;
Dim _ssspdr As Byte ' this is out SPI SLAVE SPDR register&lt;br /&gt;
Dim _ssspif As Bit ' SPI interrupt revceive bit&lt;br /&gt;
Dim Bsend As Byte , I As Byte , B As Byte ' some other demo variables&lt;br /&gt;
 &lt;br /&gt;
_ssspdr = 0 ' we send a 0 the first time the master sends data&lt;br /&gt;
Do&lt;br /&gt;
If _ssspif = 1 Then&lt;br /&gt;
Print &amp;quot;received: &amp;quot; ; _ssspdr&lt;br /&gt;
Reset _ssspif&lt;br /&gt;
 _ssspdr = _ssspdr + 1 ' we send this the next time&lt;br /&gt;
End If&lt;br /&gt;
Loop&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;font-family: 'Courier New';&amp;quot;&amp;gt;When the chip has a SPI interface, you can also use the following example:&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bascomavr&amp;quot;&amp;gt;&lt;br /&gt;
'-----------------------------------------------------------------------------------------&lt;br /&gt;
'name : spi-slave.bas&lt;br /&gt;
'copyright : (c) 1995-2005, MCS Electronics&lt;br /&gt;
'purpose : shows how to create a SPI SLAVE&lt;br /&gt;
'micro : AT90S8515&lt;br /&gt;
'suited for demo : yes&lt;br /&gt;
'commercial addon needed : no&lt;br /&gt;
'-----------------------------------------------------------------------------------------&lt;br /&gt;
 &lt;br /&gt;
$regfile = &amp;quot;8515def.dat&amp;quot; ' specify the used micro&lt;br /&gt;
$crystal = 3680000 ' used crystal frequency&lt;br /&gt;
$baud = 19200 ' use baud rate&lt;br /&gt;
$hwstack = 32 ' default use 32 for the hardware stack&lt;br /&gt;
$swstack = 10 ' default use 10 for the SW stack&lt;br /&gt;
$framesize = 40 ' default use 40 for the frame space&lt;br /&gt;
 &lt;br /&gt;
' use together with sendspi.bas&lt;br /&gt;
'------------------------------------------------------------------&lt;br /&gt;
' Tested on the STK500. The STK200 will NOT work.&lt;br /&gt;
' Use the STK500 or another circuit&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
Dim B As Byte , Rbit As Bit , Bsend As Byte&lt;br /&gt;
 &lt;br /&gt;
'First configure the MISO pin&lt;br /&gt;
Config Pinb.6 = Output ' MISO&lt;br /&gt;
 &lt;br /&gt;
'Then configure the SPI hardware SPCR register&lt;br /&gt;
Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = No , Polarity = Low , Phase = 0 , Clockrate = 128&lt;br /&gt;
 &lt;br /&gt;
'Then init the SPI pins directly after the CONFIG SPI statement.&lt;br /&gt;
Spiinit&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
'specify the SPI interrupt&lt;br /&gt;
On Spi Spi_isr Nosave&lt;br /&gt;
 &lt;br /&gt;
'enable global interrupts&lt;br /&gt;
Enable Interrupts&lt;br /&gt;
 &lt;br /&gt;
'show that we started&lt;br /&gt;
Print &amp;quot;start&amp;quot;&lt;br /&gt;
Spdr = 0 ' start with sending 0 the first time&lt;br /&gt;
Do&lt;br /&gt;
If Rbit = 1 Then&lt;br /&gt;
 Print &amp;quot;received : &amp;quot; ; B&lt;br /&gt;
 Reset Rbit&lt;br /&gt;
 Bsend = Bsend + 1 : Spdr = Bsend 'increase SPDR&lt;br /&gt;
End If&lt;br /&gt;
' your code goes here&lt;br /&gt;
Loop&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
'Interrupt routine&lt;br /&gt;
'since we used NOSAVE, we must save and restore the registers ourself&lt;br /&gt;
'when this ISR is called it will send the content from SPDR to the master&lt;br /&gt;
'the first time this is 0&lt;br /&gt;
Spi_isr:&lt;br /&gt;
push r24 ; save used register&lt;br /&gt;
in r24,sreg ; save sreg&lt;br /&gt;
push r24&lt;br /&gt;
B = Spdr&lt;br /&gt;
Set Rbit ' we received something&lt;br /&gt;
pop r24&lt;br /&gt;
!out sreg,r24 ; restore sreg&lt;br /&gt;
pop r24 ; and the used register&lt;br /&gt;
Return &lt;br /&gt;
&amp;lt;/source&amp;gt; {{Languages}}&lt;br /&gt;
&lt;br /&gt;
[[Category:SPI]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>