Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BASCOM AVR, help reference (2007).PDF
Скачиваний:
305
Добавлен:
12.08.2013
Размер:
17.02 Mб
Скачать

© MCS Electronics, 1995-2007

SPI

SPISLAVE

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 interface to program the chip, the 2313 for example does not have a SPI interface.

When you want to control various micro’s with the SPI protocol you can use the SPISLAVE library.

The spi-softslave.bas sample from the samples directory shows how you can use the SPISLAVE library.

Also look at the spi-slave.bas sample that is intended to be used with hardware SPI.

The sendspi.bas sample from the samples directory shows how you can use the SPI hardware interface for the master controller chip.

'-----------------------------------------------------------------------------

 

------------

: spi-softslave.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: shows how to implement a SPI SLAVE with software

'micro

: AT90S2313

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "2313def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

 

'Some atmel chips like the 2313 do not have a SPI port. 'The BASCOM SPI routines are all master mode routines 'This example show how to create a slave using the 2313 'ISP slave code

'define the constants used by the SPI slave

' we used portD

Const _softslavespi_port = Portd

Const _softslavespi_pin = Pind

'we use the PIND

register for reading

' data direction

Const _softslavespi_ddr = Ddrd

of port D

 

Const _softslavespi_clock = 5

'pD.5 is used for

the CLOCK

'pD.3 is MISO

Const _softslavespi_miso = 3

page -754-

 

 

 

© MCS Electronics, 1995-2007

Const _softslavespi_mosi =

4

'pd.4 is MOSI

Const _softslavespi_ss = 2

 

' pd.2 is SS

'while you may choose all pins you must use the INT0 pin for the SS 'for the 2313 this is pin 2

'PD.3(7), MISO must be output 'PD.4(8), MOSI

'Pd.5(9) , Clock 'PD.2(6), SS /INT0

'define the spi slave lib $lib "spislave.lbx" 'sepcify wich routine to use $external _spisoftslave

'we use the int0 interrupt to detect that our slave is addressed

On Int0 Isr_sspi Nosave

'we enable the int0 interrupt

Enable Int0

'we configure the INT0 interrupt to trigger when a falling edge is detected

Config Int0 = Falling

'finally we enabled interrupts

Enable Interrupts

'

' this is out SPI

Dim _ssspdr As Byte

SLAVE SPDR register

' SPI interrupt

Dim _ssspif As Bit

revceive bit

' some other demo

Dim Bsend As Byte , I As Byte , B As Byte

variables

 

_ssspdr = 0

' we send a 0 the

first time the master sends data

 

Do

 

If _ssspif = 1 Then

 

Print "received: " ; _ssspdr

 

Reset _ssspif

' we send this the

_ssspdr = _ssspdr + 1

next time

 

End If

 

Loop

 

When the chip has a SPI interface, you can also use the following example:

'-----------------------------------------------------------------------------

 

------------

: spi-slave.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: shows how to create a SPI SLAVE

'micro

: AT90S8515

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "8515def.dat"

' specify the used

micro

' used crystal

$crystal = 3680000

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

page -755-

© MCS Electronics, 1995-2007

 

for the SW stack

' default use 40

$framesize = 40

for the frame space

 

' use together with sendspi.bas

 

'------------------------------------------------------------------

 

' Tested on the STK500. The STK200 will NOT work.

 

' Use the STK500 or another circuit

 

Dim B As Byte , Rbit As Bit , Bsend As Byte

 

'First configure the MISO pin

' MISO

Config Pinb.6 = Output

'Then configure the SPI hardware SPCR register

Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = No , Polarity = Low , Phase = 0 , Clockrate = 128

'Then init the SPI pins directly after the CONFIG SPI statement.

Spiinit

'specify the SPI interrupt On Spi Spi_isr Nosave

'enable global interrupts

Enable Interrupts

'show that we started

 

Print "start"

' start with

Spdr = 0

sending 0 the first time

 

Do

 

If Rbit = 1 Then

 

Print "received : " ; B

 

Reset Rbit

'increase SPDR

Bsend = Bsend + 1 : Spdr = Bsend

End If

 

' your code goes here

 

Loop

 

'Interrupt routine

'since we used NOSAVE, we must save and restore the registers ourself 'when this ISR is called it will send the content from SPDR to the master 'the first time this is 0

Spi_isr:

push r24

; save used register

in r24,sreg ; save sreg

push r24

 

B = Spdr

' we received

Set Rbit

something

 

pop r24

 

!out sreg,r24 ; restore sreg

pop r24

; and the used register

Return

' this will

generate a reti

page -756-