40X2 and S_W2

Aries

New Member
From time to time I write small routines used in many different projects. The routines often use spare system variables S_W0, etc, to avoid corrupting any variables in use within the calling program.

The manual explicitly states that S_W2 is adcsetup2 only for the 28X2. However, I have found that it applies to the 40X2 as well. It manifested itself in a curious way, when hspi didn't work sometimes. I narrowed it down, finally, to a question of whether an odd or even number of bytes was being retrieved. Here is the code:
Code:
#no_data
#no_table

' symbols for interfacing with Microchip LC23/LC25 SPI-based chips
symbol Lc23Read                                        = 0x03
symbol Lc23Write                                    = 0x02

symbol EnableDisplayMemory                         = A.2        ' 25LC640


#macro ReadFromChip(ChipLocation,Bytes)
S_W1 = ChipLocation
S_W2 = Bytes
gosub ReadChipData
#endmacro

#terminal 76800
setfreq em64

pause 40000
    hspisetup off
    hspisetup spimode11e,spimedium
    
    ptr = 0
    ReadFromChip(0,10)
    ReadFromChip(0,11)
    ReadFromChip(0,12)
    end

ReadChipData:
        S_W0 = w0
        w0 = S_W1
        sertxd(13,10,"Read ",#S_W2," bytes from location ",#w0,":")
        low EnableDefinitionMemory                                                    ' select chip
        hspiout (Lc23Read,b1,b0)
        for w0 = 1 to S_W2
            hspiin (@ptr)
            sertxd(",",#@ptrinc)
        next w0
        input EnableDefinitionMemory                                                ' terminate operation
        w0 = S_W0
        return
This is the result:
Code:
Read 10 bytes from location 0:,85,78,75,78,79,87,78,32,83,79
Read 11 bytes from location 0:,0,0,0,0,0,0,0,0,0,0,0
Read 12 bytes from location 0:,85,78,75,78,79,87,78,32,83,79,85,82
The odd number of bytes (it doesn't matter how many) always returns zeroes.

The reason turned out to be that S_W2 is being interpreted as adcsetup2, and bit0 is ADC16 which on the 40X2 is C.4, which is the hspi SDI pin. So, apparently, when ADC16 is set to be ADC rather than logic, it is always zero, and so hspiin retrieves zeroes every time.

I think the manual needs correcting.
 
Top