News:

;) This forum is the property of Proton software developers

Main Menu

SPI Master Mode Example Not Working

Started by GDeSantis, May 16, 2021, 06:20 PM

Previous topic - Next topic

GDeSantis

Normally when writing Positron BASIC programs, if there are errors, they are mostly straight forward to find and correct. However, this time around, this simple SPI program does not work and finding the error has been a challenge.16F1503_SPI Example.txt 

top204

#1
I don't have a PIC16F1503 device, but reading the datasheet, it follows standard MSSP protocols. The procedures below should work:

'--------------------------------------------------------
' Initialise the SPI interface on a PIC16F1503 device
' Input     : None
' Output    : None
' Notes     : Initialised for SPI mode-0
'
Proc SPI_Init()
    SSP1STAT = %01000000        ' Transmission occurs on transition from active to idle clock state
    SSP1CON1 = %00000001        ' Clock = FOSC/16
    SSP1ADD = %00000001
    SSP1CON1bits_SSPEN = 0      ' Enable the SPI interface
   
    Output PORTC.0              ' Make the SCK pin an output
    Input PORTC.1               ' Make the SDI pin an input
    Output PORTC.2              ' Make the SDO pin an output
EndProc

'--------------------------------------------------------
' Disable the SPI interface on a PIC16F1503 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc SPI_Close()
    SSP1CON1bits_SSPEN = 0
EndProc

'--------------------------------------------------------
' Write and Read the SPI interface on a PIC16F1503 device
' Input     : pData is the 8-bit value to send
' Output    : Returns the 8-bit value read from SPI
' Notes     : None
'
Proc SPI_WriteRead8(pData As Byte), Byte
    SSP1BUF = pData
    Repeat: Until PIR1bits_SSP1IF = 1
    PIR1bits_SSP1IF = 0
    Result = SSP1BUF
EndProc
      
'--------------------------------------------------------
'
Main:
    SPI_Init()
    SPI_WriteRead8(123)

trastikata

What exactly is not working?

The only thing I notice is the delay of 1 ms between LED = 1 and LED = 0. You wouldn't see the 1ms on-time.

GDeSantis

Les
Thank you.  Your suggestion is excellent and all is under control.

top204

#4
I forgot to place an As Byte in the SPI_WriteRead8 procedure's parameter, so it would have defaulted to a Word parameter instead of a Byte parameter, and that would have wasted RAM when loading it. I have adjusted the code above.


Procedures make a world of difference in the compilers, but they are not mandatory, so people can use them or not. :-)