Hallo
ich möchte gerne via I²C zwei Bytes vom Master (AtMega8) zum Slave
(ebenfalls Mega8) übertragen, sowie ein Byte vom Slave zurück zum
Master.
Die Kommunikation zwischen den beiden scheint auch zu funktionieren. Der
Master findet den Slave, und das Senden eines einzelnen Bytes in je eine
Richtung geht ebenfalls. Nur beim Versuch 2Bytes Master->Slave, 1Byte
Slave <- Master geht es schief. Beim Slave kommen meist für das zweite
Byte nur noch komische Werte an. Beispiel Byte1=50, Byte2=88, dann kommt
beim Slave 50 und 67 an, oder 50 und 43.
Die Daten sollen nach Möglichkeit recht häufig übertragen werden, also
am besten alle 10 msec, und ein Wait währe auch sehr unpassend.
Ich Programmiere mit BASCOM, die Codes für Master und Slave habe ich
angehängt.
Würde mich sehr feuen, wenn mir jemand weiter helfen könnte.
Vielen Dank und schöne Grüße
Mike
Code im MASTER:
1 | $regfile = "m8def.dat" 'ATmega8
|
2 | $crystal = 3686411 '3690000 Angepasst für Zeitmessung 'für STK500
|
3 | $baud = 19200
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | ' #############################
|
13 | ' ### Variablen Deklaration ###
|
14 | ' #############################
|
15 |
|
16 |
|
17 |
|
18 | Dim Byte1 As Byte
|
19 | Dim Byte2 As Byte
|
20 | Dim Return_byte As Byte
|
21 |
|
22 | Dim I2c_address As Byte
|
23 |
|
24 |
|
25 |
|
26 | ' #########################
|
27 | ' ### I²C Konfiguration ###
|
28 | ' #########################
|
29 | Config Scl = Portc.5 'Konfiguration der I²C Verbindung
|
30 | Config Sda = Portc.4
|
31 |
|
32 |
|
33 | I2cinit
|
34 | Config Twi = 100000
|
35 |
|
36 |
|
37 | ' #########################
|
38 | ' ### LCD Konfiguration ###
|
39 | ' #########################
|
40 | Config Lcd = 16 * 2
|
41 | Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | ' ########################################
|
47 | ' ### Scan I²C Bus nach Busteilnehmern ###
|
48 | ' ########################################
|
49 |
|
50 | For I2c_address = 2 To 254 Step 2
|
51 |
|
52 | I2cstart
|
53 | I2cwbyte I2c_address
|
54 |
|
55 | If Err = 0 Then
|
56 | Exit For
|
57 | End If
|
58 | Next
|
59 | I2cstop
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Byte1 = 50
|
66 | Byte2 = 88
|
67 |
|
68 |
|
69 | I2csend I2c_address , Byte1
|
70 |
|
71 | I2csend I2c_address , Byte2
|
72 |
|
73 | I2creceive I2c_address , Return_byte
|
74 |
|
75 |
|
76 | Cls
|
77 | Upperline
|
78 | Lcd "send data"
|
79 | Lowerline
|
80 | Lcd "adr:"
|
81 | Lcd I2c_address
|
82 | Lcd "Err:"
|
83 | Lcd Return_byte
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | Do
|
89 | nop
|
90 | Loop
|
Code im SLAVE:
1 | $regfile = "m8def.dat" 'ATmega8
|
2 | $crystal = 4000000 '3690000 Angepasst für Zeitmessung 'für STK500
|
3 | $baud = 19200
|
4 |
|
5 |
|
6 | Dim Abfragebyte As Byte
|
7 |
|
8 | Dim Byte1 As Byte
|
9 | Dim Byte2 As Byte
|
10 | Dim Return_byte As Byte
|
11 | Dim Index As Byte
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | Config Twislave = &H70 , Btr = 1 , Bitrate = 100000
|
17 | Config Scl = Portc.5 'Konfiguration der I²C Verbindung
|
18 | Config Sda = Portc.4
|
19 |
|
20 |
|
21 |
|
22 | ' #########################
|
23 | ' ### LCD Konfiguration ###
|
24 | ' #########################
|
25 | Config Lcd = 16 * 2
|
26 | Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2
|
27 |
|
28 |
|
29 |
|
30 | Enable Interrupts
|
31 |
|
32 |
|
33 | Return_byte = 145
|
34 | Index = 1
|
35 |
|
36 |
|
37 |
|
38 | Do
|
39 |
|
40 |
|
41 | Cls
|
42 | Upperline
|
43 | Lcd Abfragebyte
|
44 | Lcd ":"
|
45 | Lcd Index
|
46 | Lowerline
|
47 | Lcd "By1:"
|
48 | Lcd Byte1
|
49 | Lcd "By2:"
|
50 | Lcd Byte2
|
51 |
|
52 | Loop
|
53 |
|
54 | End
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | 'A master can send or receive bytes.
|
76 |
|
77 | 'A master protocol can also send some bytes, then receive some bytes
|
78 |
|
79 | 'The master and slave must match.
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | 'the following labels are called from the library
|
85 |
|
86 | Twi_stop_rstart_received:
|
87 |
|
88 |
|
89 | Return
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | Twi_addressed_goread:
|
98 |
|
99 |
|
100 | Return
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | Twi_addressed_gowrite:
|
109 |
|
110 |
|
111 | Return
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | 'this label is called when the master sends data and the slave has received the byte
|
120 |
|
121 | 'the variable TWI holds the received value
|
122 |
|
123 | Twi_gotdata:
|
124 |
|
125 | If Index = 1 Then
|
126 | Byte1 = Twi
|
127 | Incr Index
|
128 | Else
|
129 | Byte2 = Twi
|
130 | Index = 1
|
131 | End If
|
132 |
|
133 |
|
134 | Return
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | 'this label is called when the master receives data and needs a byte
|
140 |
|
141 | 'the variable twi_btr is a byte variable that holds the index of the needed byte
|
142 |
|
143 | 'so when sending multiple bytes from an array, twi_btr can be used for the index
|
144 |
|
145 | Twi_master_needs_byte:
|
146 |
|
147 |
|
148 | Abfragebyte = Twi_btr
|
149 |
|
150 | Twi = Return_byte
|
151 |
|
152 | ' twi must be filled with a value
|
153 |
|
154 | Return
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | 'when the mast has all bytes received this label will be called
|
163 |
|
164 | Twi_master_need_nomore_byte:
|
165 |
|
166 |
|
167 | Return
|