News:

Let's find out together what makes a PIC Tick!

Main Menu

Why do PIC24's hate me

Started by RGV250, Mar 16, 2021, 01:18 PM

Previous topic - Next topic

RGV250

Hi,
I am trying my best to like PIC24's but it is not easy. I am trying to convert my FT800 code over, first I found SPI.INC does not exist but SPI_33.INC did but I could not fathom out how to set it up or if it was suitable. I then tried to modify the routines from FileSys24.INC which is used for the SD card and that also gave me grief.
After a lot of grief without beer I seem to be getting somewhere but have no idea why the example from Filesys does not work.

In this example the first routine appears to work according to the logic analyser but the second one which is from the FileSys does not. The only difference is the way the data is sent to the procedure using WREG0.
'****************************************************************
'*  Name    : FT800_SPI TEST                                    *
'*  Author  : Bobby Garrett                                     *
'*  Date    : 16/03/2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'****************************************************************

Device = 24HJ128GP502
Declare Xtal = 79.23

' Configure for internal 7.37MHz oscillator with PLL
' OSC pins are general purpose I/O
    Config FBS = BWRP_WRPROTECT_OFF
    Config FSS = SWRP_WRPROTECT_OFF
    Config FGS = GWRP_OFF
    Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
    Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSECME
    Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
    Config FPOR = FPWRT_PWR128, ALTI2C_OFF
    Config FICD = ICS_PGD1, JTAGEN_OFF   
   
PLL_Setup(43, 2, 2, $0300)                  'Configure the Oscillator to operate the device at 79.23MHz
 
' USART2 declares
    Declare Hserial2_Baud = 9600            'UART2 baud rate
    Declare HRSOut2_Pin = PORTB.5           'Select which pin is to be used for TX with USART2
    PPS_Output(cOut_Pin_RP5, cOut_Fn_U2TX)  'Make Pin RB5 U1TX       


        Dim DATA1 As Byte
        Dim DATA2 As Byte
        Dim DATA3 As Byte       
        Dim DATA4 As Byte       
        DATA1 = %10101010            'TEST DATA
        DATA2 = %11001100            'TEST DATA
        DATA3 = %11110000            'TEST DATA
        DATA4 = %00001111            'TEST DATA
        Dim SPI_DELAY As Byte
        SPI_DELAY = 4
       
        Symbol SD_CLK = PORTB.13
        Symbol SD_SDO = PORTB.14     
        Symbol SD_SDI = PORTB.15
       
' SPI BitOrder(MsbFirst)             ' Send data most significant bit first
' SPI DataMode(SPI_MODE0)            ' Clock idle zero, clock data into FT800 on rising edge       

'Set port directions 
        TRISB.13 = 0     '   CLK
        TRISB.14 = 0     '   MISO
        TRISB.15 = 1     '   MOSI

'***************************************************************************
Do 
        FT_WriteByte8(DATA3)
        FT_WriteByteSD(DATA3)       
        DelayMS 1                 
Loop

'***************************************************************************
'Routine modified from FileSys24.INC
Proc FT_WriteByte8(SEND_DATA As Byte)
    WREG1 = 8                                   ' \ 8-bit SPI loop
    Repeat                                      ' /
        SD_SDO = SEND_DATA.7                    ' Put the current outgoing bit on SPI DOUT
        SEND_DATA = SEND_DATA << 1              ' Shift the next bit into MSB
        Set SD_CLK                              ' Set CLK high
            DelayUS SPI_DELAY
        Clear SD_CLK                            ' Pull CLK pin low
        DelayUS SPI_DELAY
        Dec WREG1                               ' \
    Until SRbits_Z = 1                          ' / Do all 8-bits
EndProc

'Routine copied from FileSys24.INC
'Added extra delay as did not look right on logic analyser
Proc FT_WriteByteSD(pByte As WREG0.Byte0)
    WREG1 = 8                                   ' \ 8-bit SPI loop
    Repeat                                      ' /
        SD_SDO = WREG0.7                        ' Put the current outgoing bit on SPI DOUT
        WREG0 = WREG0 << 1                      ' Shift the next bit into MSB
        Set SD_CLK                              ' Set CLK high
            DelayUS SPI_DELAY
        Clear SD_CLK                            ' Pull CLK pin low
            DelayUS SPI_DELAY       
        Dec WREG1                               ' \
    Until SRbits_Z = 1                          ' / Do all 8-bits
EndProc           



This is what I see in the logic analyser, why does it not work for me but obviously works for Les.
spi.jpg

Bob


tumbleweed

It looks to me like the call to 'DelayUS SPI_DELAY' is using WREG (WREG0), and so does FT_WriteByteSD(pByte As WREG0.Byte0).

I don't see how that's supposed to work...

RGV250

Thanks tumbleweed, I put a fixed value in and that resolved it. Now I have to think of a different way of altering the rate as I recall the FT800 SPI is slow until it is configured then it can go faster.

Bob

tumbleweed

You could either change FT_WriteByteSD to use another register, say WREG4 in place of WREG0 (delay calls use W1, W2, and W3) , or just push/pop WREG0 around the delayus calls...

Proc FT_WriteByteSD_(pByte As WREG0.Byte0)
    WREG1 = 8                                   ' \ 8-bit SPI loop
    Repeat                                      ' /
        SD_SDO = WREG0.7                        ' Put the current outgoing bit on SPI DOUT
        WREG0 = WREG0 << 1                      ' Shift the next bit into MSB
        Push.w WREG0
        Set SD_CLK                              ' Set CLK high
            DelayUS SPI_DELAY
        Clear SD_CLK                            ' Pull CLK pin low
            DelayUS SPI_DELAY      
        Pop.w WREG0
        Dec WREG1                               ' \
    Until SRbits_Z = 1                          ' / Do all 8-bits
EndProc          

top204

The WREG registers are volatile, so they should only ever be used when the microcontroller's architecture is known.

I created the procedures within the FAT library for specific reasons using the WREG SFRs, so they would run a little faster. However, for general purpose procedures, just use standard parameter types and variables.