CONFIG SPI/de

From MCS Wiki AVR
< CONFIG SPI
Revision as of 20:13, 15 February 2013 by Admin (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

= (**COPIED FROM ENGLISH PAGE**) === Action

Configures the SPI related statements.

 

 

Syntax for software SPI

CONFIG SPI|SPISOFT = SOFT, DIN = PIN, DOUT = PIN , SS = PIN|NONE, CLOCK = PIN , SPIIN=value , MODE=mode

 

 

Syntax for hardware SPI

CONFIG SPI|SPIHARD = HARD, INTERRUPT=ON|OFF, DATA_ORDER = LSB|MSB , MASTER = YES|NO , POLARITY = HIGH|LOW , PHASE = 0|1, CLOCKRATE = 4|16|64|128 , NOSS=1|0 , SPIIN=value

 

 

 

Remarks

SPI

SOFT

for software emulation of SPI, this allows you to choose the PINS to use. Only works in master mode.

 

HARD for the internal SPI hardware, that will use fixed pins of the microprocessor.

DIN

Data input or MISO. Pin is the pin number to use such as PINB.0

DOUT

Data output or MOSI. Pin is the pin number to use such as PORTB.1

SS

Slave Select. Pin is the pin number to use such as PORTB.2

 

Use NONE when you do not want the SS signal to be generated. See remarks

CLOCK

Clock. Pin is the pin number to use such as PORTB.3

DATA ORDER

Selects if MSB or LSB is transferred first.

MASTER

Selects if the SPI is run in master or slave mode.

POLARITY

Select HIGH to make the CLOCK line high while the SPI is idle. LOW will make clock LOW while idle.

PHASE

Refer to a data sheet to learn about the different settings in combination with polarity.

CLOCKRATE

The clock rate selects the division of the of the oscillator frequency that serves as the SPI clock. So with 4 you will have a clock rate of 4.000000 / 4 = 1 MHz , when a 4 MHZ XTAL is used.

NOSS

1 or 0. Use 1 when you do not want the SS signal to be generated in master mode.

INTERRUPT

Specify ON or OFF. ON will enable the SPI interrupts to occur. While OFF disables SPI interrupts. ENABLE SPI and DISABLE SPI will accomplish the same.

SPIIN

When reading from the SPI slave, it should not matter what kind of data you send. But some chips require a value of 255 while others require a value of 0. By default, when the SPIIN option is not provided, a value of 0 will be sent to the SPI slave. With this SPIIN option you can override this value.

MODE

A constant in the range from 0-3 which defines the SPI MODE.

Without MODE, the default mode 1 will be used.

Also, when using MODE, new SPI code will be used.

When using MODE, you can also specify SPEED and SETUP.

MODE is for Software SPI only !

 

Mode

Leading Edge

Trailing Edge

0

Rising, Sample

Falling, Setup

1

Rising, Setup

Falling, Sample

2

Falling, Sample

Rising, Setup

3

Falling, Setup

Rising, Sample

SPEED

Is a numeric constant for an optional delay. This delay is in us. When you specify 1, it will result in 2 us delay : 1 use before and 1 us after the clock. By default there is no delay. Slower chip would require a delay.

SPEED is for Software SPI only and when MODE is used !

SETUP

Setup is the delay in uS before sampling the MISO pin. A numeric constant must be used.

SETUP is for Software SPI only and when MODE is used !

 

The default setting for hardware SPI when set from the Compiler, Options, SPI menu is MSB first, POLARITY = HIGH, MASTER = YES, PHASE = 0, CLOCKRATE = 4

 

When you use CONFIG SPI = HARD alone without the other parameters, the SPI will only be enabled. It will work in slave mode then with CPOL =0 and CPH=0.

 

In hardware spi mode the SPIINIT statement will set the SPI pins to :

SCK = Ouput

MISO = Input

MOSI = Output

 

In software spi mode the SPIINIT statement will set the SPI pins for example to :

sbi PORTB,5 ;set latch bit hi (inactive)SS

sbi DDRB,5 ;make it an output SS

cbi PORTB,4 ;set clk line lo

sbi DDRB,4 ;make it an output

cbi PORTB,6 ;set data-out lo MOSI

sbi DDRB,6 ;make it an output MOSI

cbi DDRB,7 ;MISO input

Ret

 

 

When you want to address multiple slaves with the software SPI you need multiple pins to select/activate the slave chip. Specify NONE for SS in that case. This also means that before every SPI command you need to set the logic level to 0 to address the chip and after the SPI command you need to set it back to a logic high level.

 

The hardware SPI also has this option. The NOSS parameter with a value of 1, will not set the SS line to logic 0 when the SPI operation begins. You need to set SS or any other pin of your choice to a logic 0 yourself. After the SPI command(s) are used you need to set it back to a logic 1 to deselect the slave chip.

 

Notice.jpg
In order to use the SPI in master mode, you need to set the SS pin to output. In input mode, this pin can be used to set the SPI bus into slave mode. You only need to set the pin to output when you use the NOSS=1 option.

 

All SPI routines are SPI-master routines. In the samples directory you will also find a SPI hardware master and SPI hardware slave sample.

 

 

See also

SPIIN , SPIOUT , SPIINIT , SPI , SPIMOVE

 

 

Example for Software SPI


Config Spi = Soft , Din = Pinb.0 , Dout = Portb.1 , Ss = Portb.2 , Clock = Portb.3
Dim var As Byte
SPIINIT 'Init SPI state and pins.
SPIOUT var , 1 'send 1 byte

Example for Hardware SPI


Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 1
Spiinit

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language