SPI and I2C


Results 1 to 8 of 8

Thread: SPI and I2C

Threaded View

  1. #5
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default

    Am I right in stating that with the said PIC having hardware SPI that it would be better to use that (following the example given) as networking is more demanding than using I2C to read the DS1307 chip.
    That would be my recommendation.

    Below is some code I previously used with a different basic compiler (note that the SSPCON register is sometimes defined as SSPCON1 register on newer chips).
    Code:
    'Include file for HW SPI (master only)
    'Procedures:
    '> PROC spi_prep(mode As Byte, clock As Byte)
    '>> mode-> 0 for Mode 0,0 ; 1 for Mode 0,1 ; 2 for Mode 1,0 ; 3 for Mode 1,1
    '>> clock -> 0 for CLK/4 ; 1 for CLK/16 ; 2 for CLK/64 ; 3 for TMR2/4
    '> PROC spi_send(spi_data As Byte)
    '> FUNCTION spi_receive(spi_data As Byte) As Byte
    '>> can be used to send and receive data
    
    Proc spi_prep(mode As Byte, clock As Byte)
    	Select Case mode
    		Case 0  'SPI_MODE 0,0
    			SSPCON.SSPEN = 1
    			SSPCON.CKP = 0
    			SSPSTAT.CKE = 1
    			SSPSTAT.SMP = 0
    		Case 1  'SPI_MODE 0,1
    			SSPCON.SSPEN = 1
    			SSPCON.CKP = 0
    			SSPSTAT.CKE = 0
    			SSPSTAT.SMP = 0
    		Case 2  'SPI_MODE 1,0
    			SSPCON.SSPEN = 1
    			SSPCON.CKP = 1
    			SSPSTAT.CKE = 1
    			SSPSTAT.SMP = 0
    		Case 3  'SPI_MODE 1,1
    			SSPCON.SSPEN = 1
    			SSPCON.CKP = 1
    			SSPSTAT.CKE = 0
    			SSPSTAT.SMP = 0
    		Case Else  'set for SPI_MODE 0,0
    			SSPCON.SSPEN = 1
    			SSPCON.CKP = 0
    			SSPSTAT.CKE = 1
    			SSPSTAT.SMP = 0
    	EndSelect
    
    	Select Case clock
    		Case 0  'SPI_CLOCK = OSC/4
    			SSPCON.SSPM3 = 0
    			SSPCON.SSPM2 = 0
    			SSPCON.SSPM1 = 0
    			SSPCON.SSPM0 = 0
    		Case 1  'SPI_CLOCK = OSC/16
    			SSPCON.SSPM3 = 0
    			SSPCON.SSPM2 = 0
    			SSPCON.SSPM1 = 0
    			SSPCON.SSPM0 = 1
    		Case 2  'SPI_CLOCK = OSC/64
    			SSPCON.SSPM3 = 0
    			SSPCON.SSPM2 = 0
    			SSPCON.SSPM1 = 1
    			SSPCON.SSPM0 = 0
    		Case 3  'SPI_CLOCK = TMR2/2
    			SSPCON.SSPM3 = 0
    			SSPCON.SSPM2 = 0
    			SSPCON.SSPM1 = 1
    			SSPCON.SSPM0 = 1
    		Case Else  'SPI_CLOCK = OSC/4
    			SSPCON.SSPM3 = 0
    			SSPCON.SSPM2 = 0
    			SSPCON.SSPM1 = 0
    			SSPCON.SSPM0 = 0
    	EndSelect
    End Proc
    
    Proc spi_send(spi_data As Byte)
    	SSPBUF = spi_data
    	While SSPSTAT.BF = 0
    	Wend  'Wait until buffer is clear
    End Proc
    Function spi_receive(spi_data As Byte) As Byte
    	SSPBUF = spi_data
    	While SSPSTAT.BF = 0
    	Wend  'Wait until buffer is clear
    	spi_receive = SSPBUF
    End Function
    Last edited by languer; - 27th October 2010 at 21:39. Reason: sample code

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts