SPI routines 

These are software SPI routines, not using the SPI module of the PIC. Unlike the hardware routines, which can transmit and receive simultaneously, there are separate receive and transmit routines. 

SPIIn(addr(port), data_bit_mask, addr(clock),clock_bit_mask, num_bits)
   
     Receives up to 16 bits from a serial device, using a data line and a clock line. 

Parameters:

Port                    - the port to use for the data input
data_bit_mask
  - the bit position as a mask (0x01,0x024,etc) in the port of the data input
clock
                  - the port to use for the clock output
clock_bit_mask
- the bit position as a mask (0x01,0x024,etc) in the port of the clock output
num_bits
            - the number of bits (1 to 16) of data to be sent. 

Returns: 
   
        
a uInteger containing the data received

 

Notes:

·       The clock pulse is at least .2 usec long with a 40 mHz PIC clock. The clock code   just toggles the clock pin.

·         Use #DEFINE SPI_CLOCKUS to make the clock pulse "SPI_CLOCKUS" in length

·         Use #DEFINE SPI_READUS to delay the data read for”SPI_READUS” us after clock trailing edge

·         The shift loop is 14 instructions long, thus the clockrate is 700 kHz at 40mHz, 350 at 20mHz, etc

·         The user MUST set the clock pin HIGH or LOW initially, depending on the requirements of the device.

 

SPIOut(addr(port),data_bit_mask,addr(clock),clock_bit_mask,data,num_bits)
   
Sends up to 16 bits to a serial device, using a data line and a clock line. 

Parameters:

Port                  - the port to use for the data input
data_bit_mask
  - the bit position as a mask (0x01,0x024,etc) in the port of the data input
clock
                - the port to use for the clock output
clock_bit_mask
- the bit position as a mask (0x01,0x024,etc) in the port of the clock output
data
                 - a uInteger containing the data to be sent, right justified
num_bits
          - the number of bits (1 to 16) of data to be sent. 

Notes:

·         The clock pulse is at least .2 usec long with a 40 mHz PIC clock. The clock code   just toggles the clock pin.

·         Use #DEFINE SPI_CLOCKUS to make the clock pulse "SPI_CLOCKUS" in length

·         Use #DEFINE SPI_WRITEUS to delay the data read for”SPI_WRITEUS” us after clock trailing edge

·         The shift loop is 16 instructions long, thus the clockrate is 625 kHz at 40mHz, 312 at 20mHz, etc

·         The user MUST set the clock pin HIGH or LOW initially, depending on the requirements of the device.         

·         The data is set up before the clock is toggled, so the target device can be set to clock in the data on either the leading or trailing edge.