BX24 SPI CE

Started by geekfarmer December 21, 2012
I am new to SPI and am trying to connect a BX24 to a DS1305 RTC and am having trouble getting the CE to function active high. If I understand SPI on the BX24 correctly to configure the CE to be active high bit 4 of the configuration word should be 0. I have tryed 0 and 1 but the CE is always active low. Below is my code. What am I missing? Thanks.

ublic Sub Main()
'Option Explicit
'Function RTC_Config(ByVal CE_Pin as byte) as Boolean
'Const SPIRateDiv4 As Byte = bx0000_0000 ' Clock speed = f/4
'Const SPIRateDiv16 As Byte = bx0000_0001 ' Clock speed = f/16
'Const SPIRateDiv64 As Byte = bx0000_0010 ' Clock speed = f/64
'Const SPIRateDiv128 As Byte = bx0000_0011 ' Clock speed = f/128
'Dim ClockRateSelect As Byte

' See Atmel documentation for these bits.
'Const DORD As Byte = bx0010_0000 ' Data order, LSB first.
'Const CPOL As Byte = bx0000_1000 ' Clock polarity, SCK high when idle.
'Const CPHA As Byte = bx0000_0100 ' Clock phase.

Const RTC_Init as byte = bx00000000 ' Turn off WP
dim write_data (1 to 2) as byte
dim read_data (1 to 2) as byte
dim SPISetupByte as byte

' See Atmel documentation for these bits.
'Const DORD As Byte = bx0010_0000 ' Data order, LSB first.
'Const CPOL As Byte = bx0000_1000 ' Clock polarity, SCK high when idle.
'Const CPHA As Byte = bx0000_0100 ' Clock phase.
Const SPI_LSB as Byte = bx0000_0000 'lsb transmitted first
Const SPI_CPOL as Byte = bx0000_0000 'sck is mostly high
Const SPI_CPHA as Byte = bx0000_0100 'clock phase, see atmel docs for timing
Const SPI_SCK4 as Byte = &H00 'clock speed f/4
Const SPI_SCK16 as Byte = &H01 'clock speed f/16
Const SPI_SCK64 as Byte = &H02 'clock speed f/64
Const SPI_SCK128 as Byte = &H03 'clock speed f/128
SPISetupByte=SPI_CPOL or SPI_CPHA or SPI_SCK128

Debug.print CStr(SPISetupByte)

'Call PutPin(12,bxOutputLow)
'On the BX, set the SPI Channel to 2, CE = 12
Call OpenSPI(2, SPISetupByte, 12)

'Now unset the WP bit
write_data(1) = &H8f
write_data(2) = RTC_Init
Call SPICmd(2, 2, write_data, 0, read_data(1))

'Now read the control reg.
write_data(1) = &H0f
write_data(2) = &H00
Call SPICmd(2,1,write_data(1), 1, read_data(1))
debug.print "Cntl Reg Aft init = " & CStr(read_data(1))

'turn on the osc
write_data(1) = &H8f
write_data(2) = &H00
Call SPICmd(2,2,write_data, 0, read_data)

'Now read the control reg.
write_data(1) = &H0f
write_data(2) = &H00
Call SPICmd(2,1,write_data(1), 1, read_data(1))
debug.print "Cntl Reg Aft Osc on = " & CStr(read_data(1))

'set the time
write_data(1) = &H80
write_data(2) = &H00
Call SPICmd(2,1,write_data, 2, read_data)
'Call Delay(5.0)
'now read the seconds for time
write_data(1) = &H00
Call SPICmd(2,1,write_data(1), 2, read_data)
debug.print "the data " & CStr(read_data(1)) & " " & CStr(read_data(2))
end sub

> ... to configure the CE to be active high bit 4 of the configuration
word should be 0...

I believe the hardware SPI /SS pin must always be active-low; there is
no published internal option to make it active-high that I'm aware of.

I think you have two options. You might add an inverter in the DS1305
CE line, or you might use a software SPI implementation (discretely
controlling the DS1305 CE pin and using ShiftIn and ShiftOut for data
while CE is high).

You'll find a lengthy discussion of the DS1305 on BX-24s in the forum
archive; search for "Keeping track of date and time". In particular,
http://tech.groups.yahoo.com/group/basicx/message/23210 talks about
DS1305 CE.

Tom