y para enviar el dato al conversor he usado está instrucción...
spi_write( dato );
Donde dato es la variable que recojo del conversor analógico digital del pic, es decir:
dato=(ADC*5)/255; // Convierto a analógico
:shock: es por software o por hardware??No, indica que debe ser por software, en ese caso debe cambiarlo por FORCE_SW
si es por soft lo que esta marcado con rojo esta de más...
#use spi(MASTER,CLK=PIN_B4, DO=PIN_B2, DI=PIN_B1, BITS=8, MODE=3, FORCE_HW, stream=BUSSPI)
y si es por hard
creo que no es necesario ese
#use spi
y utilizar el
setup_spi
SPI mode allows 8 bits of data to be synchronously
transmitted and received simultaneously. To
accomplish communication, typically three pins are
used:
• Serial Data Out (SDO) RB2/SDO/RX/DT
• Serial Data In (SDI) RB1/SDI/SDA
• Serial Clock (SCK) RB4/SCK/SCL
Additionally, a fourth pin may be used when in a Slave
mode of operation:
• Slave Select (SS) RB5/SS/TX/CK
When initializing the SPI, several options need to be
specified. This is done by programming the appropriate
control bits in the SSPCON register (SSPCON<5:0>)
and the SSPSTAT register (SSPSTAT<7:6>). These
control bits allow the following to be specified:
• Master mode (SCK is the clock output)
• Slave mode (SCK is the clock input)
• Clock Polarity (Idle state of SCK)
• Clock Edge (output data on rising/falling
edge of SCK)
• Clock Rate (Master mode only)
• Slave Select mode (Slave mode only)#use spi(FORCE_HW, BITS=16, stream=SPI_STREAM)
// uses hardware SPI and gives this stream the name SPI_STREAM
Aca podemos utilizar la funcion spi_xfer.#USE SPI
--------------------------------------------------------------------------------
Syntax:
#use spi (options)
Elements:
Options are separated by commas and may be:
MASTER
Set the device as the master. (default)
SLAVE
Set the device as the slave.
BAUD=n
Target bits per second, default is as fast as possible.
CLOCK_HIGH=n
High time of clock in us (not needed if BAUD= is used). (default=0)
CLOCK_LOW=n
Low time of clock in us (not needed if BAUD= is used). (default=0)
DI=pin
Optional pin for incoming data.
DO=pin
Optional pin for outgoing data.
CLK=pin
Clock pin.
MODE=n
The mode to put the SPI bus.
ENABLE=pin
Optional pin to be active during data transfer.
LOAD=pin
Optional pin to be pulsed active after data is transferred.
DIAGNOSTIC=pin
Optional pin to the set high when data is sampled.
SAMPLE_RISE
Sample on rising edge.
SAMPLE_FALL
Sample on falling edge (default).
BITS=n
Max number of bits in a transfer. (default=32)
SAMPLE_COUNT=n
Number of samples to take (uses majority vote). (default=1
LOAD_ACTIVE=n
Active state for LOAD pin (0, 1).
ENABLE_ACTIVE=n
Active state for ENABLE pin (0, 1). (default=0)
IDLE=n
Inactive state for CLK pin (0, 1). (default=0)
ENABLE_DELAY=n
Time in us to delay after ENABLE is activated. (default=0)
DATA_HOLD=n
Time between data change and clock change
LSB_FIRST
LSB is sent first.
MSB_FIRST
MSB is sent first. (default)
STREAM=id
Specify a stream name for this protocol.
SPI1
Use the hardware pins for SPI Port 1
SPI2
Use the hardware pins for SPI Port 2
FORCE_HW
Use the pic hardware SPI.
Purpose:
The SPI library contains functions to implement an SPI bus. After setting all of the proper parameters in #use spi, the spi_xfer() function can be used to both transfer and receive data on the SPI bus.
The SPI1 and SPI2 options will use the SPI hardware onboard the PIC. The most common pins present on hardware SPI are: DI, DO, and CLK. These pins don’t need to be assigned values through the options; the compiler will automatically assign hardware-specific values to these pins. Consult your PIC’s data sheet as to where the pins for hardware SPI are. If hardware SPI is not used, then software SPI will be used. Software SPI is much slower than hardware SPI, but software SPI can use any pins to transfer and receive data other than just the pins tied to the PIC’s hardware SPI pins.
The MODE option is more or less a quick way to specify how the stream is going to sample data. MODE=0 sets IDLE=0 and SAMPLE_RISE. MODE=1 sets IDLE=0 and SAMPLE_FALL. MODE=2 sets IDLE=1 and SAMPLE_FALL. MODE=3 sets IDLE=1 and SAMPLE_RISE. There are only these 4 MODEs.
SPI cannot use the same pins for DI and DO. If needed, specify two streams: one to send data and another to receive data.
The pins must be specified with DI, DO, CLK or SPIx, all other options are defaulted as indicated above.
Examples:
#use spi(DI=PIN_B1, DO=PIN_B0, CLK=PIN_B2, ENABLE=PIN_B4, BITS=16)
// uses software SPI
#use spi(FORCE_HW, BITS=16, stream=SPI_STREAM)
// uses hardware SPI and gives this stream the name SPI_STREAM
/quote]
Esta es la definicion de Use_SPI
Saquen sus conclusiones... :mrgreen:
Estás preguntando??? No entiendo!!!
Atte: Enigma... La llanerita de Guayana ;-)
Preguntas para alguien que halla utilizado el SPI por SW:
¿Que velocidades de transmisión se pueden alcanzar ?
Pero esos bytes los enviabas por el programa, es decir, en el C le dabas instrucciones de mandar esos bytes antes de enviar los datos que querías???
Pero esos bytes los enviabas por el programa, es decir, en el C le dabas instrucciones de mandar esos bytes antes de enviar los datos que querías???
Me refiero a habilitarla por medio del pin respectivo del dispositivo SS. La rutina de envío que yo uso habilita primero la linea SS, luego uso la función spi_write(); para enviar el dato y al final, deshabilito la linea SS.
Según el datasheet, se debe configurar el IDLE en alto o bajo.
Código: C
spi_xfer(DA_spi,0x0554);/* |4 bits dummy|10bits DA|2 bits x */