ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8253

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

© MCS Electronics, 1995-2007

$baud = 19200

'you can view the difference by compiling and simulating this sample with the 'line below remarked and active

Config Single = Scientific,Digits= 7

Dim S As Single

S = 1

Do

S= S / 10

Print S

Loop

CONFIG SPI

Action

Configures the SPI related statements.

Syntax for software SPI

CONFIG SPI = SOFT, DIN = PIN, DOUT = PIN , SS = PIN|NONE, CLOCK = PIN

Syntax for hardware SPI

CONFIG SPI = 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

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 signalto 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 clockrate 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 ni

master mode.

page -382-


© MCS Electronics, 1995-2007

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.

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 mode the SPIINIT statement will set the SPI pins to : sbi DDRB,7 ; SCK output

cbi DDRB,6 ; MISO input sbi DDRB,5 ; MOSI output

In softmode 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, wil 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.

All SPI routines are SPI-master routines. Example 2 below demonstrates how to create a soft SPI slave. 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

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

CONFIG SERVOS

page -383-


© MCS Electronics, 1995-2007

Action

Configures how much servo’s will be controlled.

Syntax

CONFIG SERVOS = X , Servo1 = Portb.0 , Servo2 = Portb.1 , Reload = rl

Remarks

Servo’s need a variable pulse in order to operate. The CONFIG SERVOS directive will set up a byte array with the servo pulse width values and will initialize an ISR that uses TIMER0.

XThe number of servo’s you want to control. Each used servo will use one byte of SRAM.

PORT

The port pin the servo is attached too.

RL

The reload value for the ISR in uS.

When you use for example :

Config Servos = 2 , Servo1 = Portb.0 , Servo2 = Portb.1 , Reload = 10 The internal ISR will execute every 10 uS.

An arrays named SERVO() will be created and it can hold 2 bytes : servo(1) and servo(2).

By setting the value of the servo() array you control how long the positive pulse will last. After it has reached this value it will be reset to 0.

The reload value should be set to 10. After 20 mS, a new pulse wil be generated.

You can use other reload values but it will also mean that the repeat value will change.

The PORT pins specified must be set to work as an output pin by the user. CONFIG PINB.0 = OUTPUT

Will set a pin to output mode.

Resources used

TIMER0 is used to create the ISR.

ASM

NONE

Example

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

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

: servos.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstrates the SERVO option

'micro

: 90S2313

'suited for demo

: yes

'commercial addon needed

: no

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

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

$regfile = "2313def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

page -384-


© MCS Electronics, 1995-2007

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

'Servo's need a pulse in order to operate

'with the config statement CONFIG SERVOS we can specify how many servo's we 'will use and which port pins are used

'A maximum of 14 servos might be used

'The SERVO statements use one byte for an interrupt counter and the TIMER0 'This means that you can not use TIMER0 anymore

'The reload value specifies the interval of the timer in uS

'Config Servos = 2 , Servo1 = Portb.0 , Servo2 = Portb.1 , Reload = 10

Config Servos = 1 , Servo1 = Portb.0 , Reload = 10 'as an option you can use TIMER1

'Config Servos = 2 , Servo1 = Portb.0 , Servo2 = Portb.1 , Reload = 10 , Timer = Timer1

'we use 2 servos with 10 uS resolution(steps)

'we must configure the port pins used to act as output

Config Portb = Output

'finally we must turn on the global interrupt

Enable Interrupts

'the servo() array is created automatic. You can used it to set the

'time the servo must be on

'10 times 10 = 100

Servo(1) = 10

uS on

'20 times 10 =

'Servo(2) = 20

200 uS on

Do

Loop

Dim I As Byte

Do

For I = 0 To 100

Servo(1) = I

Waitms 1000

Next

For I = 100 To 0 Step -1

' Servo(1) = I

Waitms 1000

Next

Loop

End

CONFIG TCPIP

Action

Configures the TCP/IP W3100A chip.

Syntax

page -385-