EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Keeping track of date and time

Started by David Sousa Mendes October 6, 2009
> ...
MOSI - connected to DataIn of my RTC
MISO - connected to DataOut of my RTC
SCLK - connected to SCLK
VCC - connected to Vccc1
Gnd - connected to GROUND

... "Error verifying external EEPROM "

You also need a /CS line to a BX-24 pin, which should also have a pullup
resistor to Vcc. During processor Reset, all pins float so the RTC /CS
state will be uncertain withoutthe pullup. Since the processor uses SPI
to access the program code in EEPROM, your RTC will interfere unless its
/CS state is proper.

You're close.
Tom
Tom,
On 1305 datasheet, I can't see any CS pin...
Do you think it could be the CE (chip enable) pin?
Thanx

David M.

> ... the CE (chip enable) pin?

CE (chip enable), CS (chip select), SS (slave select) are all usually
synonymous.

Yes, although it is active-high, not active-low, which is the convention
in my experience - and what the BX-24 expects. You'll need, then, to
invert \CS from the processor.

You can use any inverting logic device like a 74n04, or a transistor
configured as an inverter (typically two resistors and an NPN but, since
the CE pin has an internal pulldown, just a single base resistor on a
PNP, emitter to Vcc, collector to CE, should also work), between the /CS
pin you select on the BX-24 and the CE input of the RTC. You should
still put a pullup resistor between your selected /CS pin and Vcc to
avoid the Reset concern.
Tom
Tom, problems that have been solved so far:
1- Bx does not stuck anymore while downloading (since I put the pullup
resistor in CS pin)
2 - BX's CS is active low. I'm now communicating with max146, which is
also active low, so no need to invert logic using a transistor or a 74N04.

I'm trying to communicate with max146 instead of DS1305 because I'm more
familiar with it...
Now the problem is that I only receive bytes containing zeros...

My code is:

dim sent_bytes(1 to 3)as byte
dim received_bytes(1 to 3) as byte
sent_bytes(1)=bx1000_1111
sent_bytes(2)=bx0000_0000
sent_bytes(3)=bx0000_0000
do
Call SPICmd(1,3,sent_bytes,3,received_bytes)
debug.print "1:"; CStr(received_bytes(1))
debug.print "2:"; CStr(received_bytes(2))
debug.print "3:"; CStr(received_bytes(3))
call delay (1.0)
loop

Any ideas?
How should I configure the initial state of CS pin? Tristate, Pullup,
Output_low, Output_high?
Thanx again for your help

David M.

No OpenSPI()? The specified /CS pin will be automatically configured by it.

Tom

> dim sent_bytes(1 to 3)as byte
> dim received_bytes(1 to 3) as byte
> sent_bytes(1)=bx1000_1111
> sent_bytes(2)=bx0000_0000
> sent_bytes(3)=bx0000_0000
> do
> Call SPICmd(1,3,sent_bytes,3,received_bytes)
> debug.print "1:"; CStr(received_bytes(1))
> debug.print "2:"; CStr(received_bytes(2))
> debug.print "3:"; CStr(received_bytes(3))
> call delay (1.0)
> loop
>

Sorry, Tom, didn't give you all the code to make it simpler...
here goes:

Option Explicit
public Channel As Byte

Public Sub Main()

call abre_spi()
call mede_ADC()
End Sub
'__________________________________________________________________________________
Public sub mede_ADC()
dim sent_bytes(1 to 3)as byte
dim received_bytes(1 to 3) as byte
sent_bytes(1)=bx1000_1111 ' para channel 0
sent_bytes(2)=bx0000_0000
sent_bytes(3)=bx0000_0000

do
Call SPICmd(1,3,sent_bytes,3,received_bytes)
debug.print "1:"; CStr(received_bytes(1))
debug.print "2:"; CStr(received_bytes(2))
debug.print "3:"; CStr(received_bytes(3))
call delay (1.0)
loop

End sub

'_____________________________________________________________________________________
public sub abre_spi()
Dim ChipSelectPin As Byte
Dim LSBFirst As Boolean
Dim ClockPolarity As Boolean
Dim ClockPhase As Boolean
Channel = 1
ChipSelectPin = 13
LSBFirst = False
ClockPolarity = False
ClockPhase = False
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
Const ClockRateSelect As Byte= SPIRateDiv128
' 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.

Dim SetupByte As Byte

' Safety clamp.
If (Channel < 1) Then
Channel = 1
ElseIf (Channel > 4) Then
Channel = 4
End If

SetupByte = ClockRateSelect

If (LSBFirst) Then
SetupByte = SetupByte Or DORD
End If

If (ClockPolarity) Then
SetupByte = SetupByte Or CPOL
End If

If (ClockPhase) Then
SetupByte = SetupByte Or CPHA
End If

Call OpenSPI(Channel, SetupByte, ChipSelectPin)
End sub

Looking quickly, I don't see an obvious problem. I posted a simple
two-transistor SPI loopback drawing (actually, a tristate non-inverting
buffer, enabled by /CS, is simpler if you happen to have one) some time
ago. The loopback should simply echo your sent data. If it works, the
problem is probably with the device.

If this link doesn't work, look for it in
http://tech.groups.yahoo.com/group/basicx/files/Datasheets-Appnotes-Examples-Drawings/Comm/SPI/SPI_Loopback.JPG
.

Tom

Hello
Eventough it still needs some tunning, I'm being able to receive
valuable data from the max146...
One question: how should the CS pin be configured? Tristate, Pullup,
Output_Low, Output_High?

Reg
David M.

And yet another question:
In my code:

Call SPICmd(1,1,sent_bytes(1),1,received_bytes(1))
Call SPICmd(1,1,sent_bytes(2),1,received_bytes(2))
Call SPICmd(1,1,sent_bytes(3),1,received_bytes(3))
debug.print CStr(received_bytes(1)) ; ";" ; CStr(received_bytes(2)); ";"
; CStr(received_bytes(3))

The result of the ADC conversion is being sent on the first 2 received
bytes whereas it should be sent on the last 2 received bytes...
Any ideas, on why this is happening?

Reg

David M.

> ... how should the CS pin be configured?

OpenSPI will configure the /CS pin high. It's state before that should
be either untouched (since a processor Reset sets all pins to
InputTristate) or, if you like, you may set the pin to any state except
Low, which will likely cause the SPI device to interfere with code
execution.

Assuming you have an SPI device connected, with its /CS on pin 12, you
can test this easily:

Sub Main()
call sleep(1.0)

' call PutPin(12, bxOutputLow) ' will probably crash the machine
here, no flashing LED

call OpenSPI(1, 0, 12) ' pin 12 set high by Open

do ' flash LED
call PutPin(25, bxOutputLow)
call sleep(0.1)
call PutPin(25, bxOutputHigh)
call sleep(0.1)
loop

End Sub
Tom


Memfault Beyond the Launch