Forum: Mikrocontroller und Digitale Elektronik DS1302 Bascom liest falsche werte!?


von Basti (Gast)


Lesenswert?

Hi ich habe einen DS1302 and einem Mega32
1
'Pin Configs
2
Rtc_clk Alias Portb.0
3
Config Pinb.0 = Output
4
5
Rtc_read Alias Pinb.3
6
Rtc_write Alias Portb.3
7
Config Pinb.3 = Output
8
9
Rtc_select Alias Portb.4
10
Config Pinb.4 = Output

erster Programmabschnitt, vorallem der trickle charger
1
'write enable
2
Config Rtc_read = Output
3
Set Rtc_select
4
Waitms 1
5
Rtc_cmd = &H8E
6
Rtc_data = &B00000000
7
Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 8 , 200
8
Shiftout Rtc_write , Rtc_clk , Rtc_data , 3 , 8 , 200
9
Waitms 1
10
Reset Rtc_select
11
12
'activate the charger
13
Config Rtc_read = Output
14
Set Rtc_select
15
Waitms 1
16
Rtc_cmd = &H90                                              '90H
17
Rtc_data = &B10100101                                       '1 diode, 2k
18
Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 8 , 200
19
Shiftout Rtc_write , Rtc_clk , Rtc_data , 3 , 8 , 200
20
Waitms 1
21
Reset Rtc_select
22
23
'set the time to whatever
24
Config Rtc_read = Output
25
Set Rtc_select
26
Waitms 1
27
Rtc_cmd = &H80                                              'set seconds and CH
28
Rtc_data = &B000000                                         'start with 0, clock is running
29
Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 8 , 500
30
Shiftout Rtc_write , Rtc_clk , Rtc_data , 3 , 8 , 500
31
Waitms 1
32
Reset Rtc_select

das setzen der Werte funktioniert ohne probleme (ich hab geprüft, dass 
die Spannung an Pin 1 ansteigt)

jetzt lese ich die Sekunden folgendermaßen aus
1
Rtc_cmd = &H81                                           ' read seconds
2
   Config Rtc_read = Output
3
   Set Rtc_select
4
   Waitms 1
5
   Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 8 , 500
6
   Config Rtc_read = Input
7
   'Rtc_write = 1
8
   Shiftin Rtc_read , Rtc_clk , Seconds , 2 , 8 , 500
9
   Config Rtc_read = Output
10
   'Rtc_write = 0
11
   Waitms 1
12
   Reset Rtc_select

ob jetzt lsb oder msb beim reinlesen der werte gesetzt ist ist erstmal 
egal. das macht das ganze spiegelverkehrt...

allerdings lese ich im programm 2 mal pro sekunde.
Es ändert sich jede sekunde der wert völlig richtig, und fängt auch bei 
00 an

allerdings danach passiert sekundenweise folgendes:


00000000
10000000
00000001
10000001
00000010
10000010
00000011
10000011
00000100

wie zu erkennen ist ändert er zwar völlig richtig sekundenweise bit 7 
(bzw  bit0, je nachdem wie rum man einliest) allerdings erwarte ich 
nicht dass sich dann "am anderen Ende" der Binärzahl was ändert, oder??

Laut Datasheet bit 3-0 für die seconds, 4-6 10 seconds und 7 für ch
ch sollte sich auf keinen fall von alleine ändern, oder?

von Basti (Gast)


Lesenswert?

Fehler gefunden:

Note that the first data bit to be transmitted occurs on the first 
falling edge after the last bit of the command byte is written.
Additional SCLK cycles retransmit the data bytes


Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 8 , 500
-jetzt wird aber das erste bit für shiftin schon ausgegeben (un 
natürlich verpasst)
Shiftin Rtc_read , Rtc_clk , Seconds , 2 , 8 , 500
-, und das achte bit ist das wiederholte 0te


LÖSUNG:
Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 8 , 500
Shiftin Rtc_read , Rtc_clk , Seconds , 2 , 7 , 500
Shiftin Rtc_read , Rtc_clk , Seconds , 2 , 8 , 500

schmutzig aber funktioniert ;-)

von AN (Gast)


Lesenswert?

Hallo zusammen,
wollte heute das Programm übernehmen, es klappt nicht. Verstehe auch 
nicht warum das klappen sollte.  Da gibt es auch den Beitrag 
(https://www.roboternetz.de/community/threads/64232-DS1302-RealTimeClock-1-Bit-fehlt), 
der funktionier auch nicht bei mir.

Meine Lösung ist die: Es werden nur 7 Bits übertragen und das 8. wird 
manuell übertragen und am Ende die High-Flanke belassen (BASCOM macht 
automatisch diese auf Low).


Shiftout Rtc_write , Rtc_clk , Rtc_cmd , 3 , 7 , 500   'die ersten 7 
Bits

'nun manuell das 8. Bit übertragen
Waitus 500                           'gleiches Timing übernehmen
If Rtc_cmd.7 = 1 Then Set Rtc_out Else Reset Rtc_out
Set Rtc_clk                          'high-Flanke setzen und lassen!
Waitus 500

'ab hier greift das ursprüngliche Programm...
Config Rtc_in = Input
Shiftin Rtc_read , Rtc_clk , Seconds , 2 , 8 , 500
Config Rtc_in = Output

von Jorge (Gast)


Lesenswert?

Function Ds1302_read(byval Addr As Byte)as Byte
    Result = 0
    Set Ce
    nop
    Shiftout Io , Sck , Addr , 3 , 8 , 10
' 
======================================================================== 
===
'    Shiftin Io_pin , Sck , Result , 2 , 8 , 10  '  Doesn't work.
'   The last level of sck is low and the bit0 is already exposed.
'   Read bit0 before the others:
    Config Pinc.2 = Input
    Result.0 = Io_pin
    Set Sck
    For N = 1 To 7
        Reset Sck
        Waitus 10
        Result.n = Io_pin
        Set Sck
        Waitus 10
    Next N
' 
======================================================================== 
===
    Reset Ce
    Ds1302_read = Result
End Function

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.