Hi
Also erst ma Danke für das Programm.
Aber ich bin so langsam am verzweifeln bin ich zu doof für so was hier?!
Habe das Prog genommen und einmal denn empfangenem Buffer in eine Variable schreiben lassen. Dann nach dieser Variablen gefragt und wenn dies gleich is soll eine LED eingeschaltet werden.
Hier der Code
µC 1
Code:
$crystal = 16000000
$include "MyTWI.bas"
$baud = 9600
$hwstack = 64
$swstack = 64
$framesize = 64
Const Tmr_c_prescale = 64
Const Tmr_c_preload = 131
Const Tmr_c_divis = 250
Declare Sub Twi_show_state(byref State As Byte)
Config Porta = Input
Config Portb = Input
Config Portd = Output
Waitms 30
'-------------------------------------------------
' lokale I2C Adresse (als Slave)
'-------------------------------------------------
Const Mn1_adr = &H40
'-------------------------------------------------
' fremde I2C Adresse (als Master)
'-------------------------------------------------
Const Mn2_adr = &H50
Dim Slv_byte As Byte 'sende Byte (irgendwas)
Dim Twi_mst_buff(24) As Byte 'Buffer für Masterfunktionen
Dim Twi_slv_buff(24) As Byte 'Buffer für Slavefunktionen
'-------------------------------------------------
Config Timer0 = Timer , Prescale = Tmr_c_prescale 'Timer 1mS
On Timer0 Interrupt_ticker ' Timer for Timer Queue
Dim Timeout As Byte
Dim Timediv As Word
'-------------------------------------------------
Dim Temp As Byte
Twi_slv_addr = Mn1_adr 'lokale I2C-Adresse
Twi_sr_data = Varptr(twi_slv_buff(1)) 'datenbuffer empfang
Twi_st_data = Varptr(twi_slv_buff(1)) 'datenbuffer senden
Twar = Twi_slv_addr + 1 'I2C Adress Mega32 + GCA
Config Twi = 400000 'I2C Speed
On Twi Twi_mst_slv_isr , Nosave 'ISR f. TWI
Gosub Twi_sense 'Aktivieren Slave-Funktion
Enable Timer0 'Timer
Enable Interrupts 'Generell
'---------------------------------------------
' initialiesierung
'---------------------------------------------
Dim Speicher(24) As Byte
Led1 Alias Portd.2
'------------------Konfigurierung-------------
Portd = &HFF
Led1 = 1
Slv_byte = &H02
Do
If Twi_slv_flag <> 0 Then
'-----------------------------------------------------------------
' Ergebnis als SLave auswerten
' Vorsicht, der Bus ist solange blockiert
'-----------------------------------------------------------------
Select Case Twi_slv_flag
Case &H60 : 'Print Spc(30) ; "SLAVE recv:" ; 'es ist was empfangen worden
'Print Hex(twi_slv_addr) ; " ";
For Temp = 1 To Twi_sr_cntr
'Print Hex(twi_slv_buff(temp)); 'print der Daten
Next
Case &HA8 : 'Print Spc(30) ; "SLAVE tran:" ; 'es ist was abgeholt worden
'Print Hex(twi_slv_addr) ; " ";
For Temp = 1 To Twi_st_cntr
' Print Hex(twi_slv_buff(temp)); 'print der Daten
Next
Case &H70 : 'Print Spc(30) ; "SLAVE GCA :" ; 'ein General Call ist gekommen
'Print Hex(twi_slv_addr) ; " ";
For Temp = 1 To Twi_sr_cntr
' Print Hex(twi_slv_buff(temp)); 'print der Daten
Next
Case Else:
'Print Chr(7) ; 'Irgendein Fehler
Call Twi_show_state(twi_slv_flag) 'Print status-text
End Select
'Print
Twi_slv_flag = 0 'löschen marker
Twi_slv_stat = 0 'löschen marker
Gosub Twi_sense 'alles wieder enablen
'und Bus freigeben
End If
'-----------------------------------------------------------------
' Jede Sekunde was senden & empfangen
'-----------------------------------------------------------------
If Timeout = 1 Then
Gosub Master_transmit
If Twi_mst_flag = 0 Then 'Hats geklappt ?
Timeout = 0 'ja
' else (sonst probieren wir's gleich nochmal)
End If
End If
'-----------------------------------------------------------------
If Speicher(1) = &H03 Then Led1 = 0
Loop
End
'----------------------------------------
' 1 mS Ticker
'----------------------------------------
Interrupt_ticker: ' Timer for Timer Queue
Timer0 = Tmr_c_preload
Incr Timediv
If Timediv >= Tmr_c_divis Then
Timeout = 1 'eine Sekunde ist rum
Timediv = 0
End If
Return
'----------------------------------------
$include "Twi_show_state.bas"
'----------------------------------------
Run_wait_master:
Gosub Mst_start 'aufruf
Select Case Twi_mst_flag 'ergebnis ?
Case Twi_m_sent: 'gesendet
'Print "<<<<<<MASTER sent:" ;
'Print Hex(twi_mst_addr) ; " ";
For Temp = 1 To Twi_mt_cntr
'Print Hex(twi_mst_buff(temp));
Next
'Print
Twi_mst_flag = 0
Case Twi_m_recv: 'geholt
'Print ">>>>>>MASTER read:" ;
'Print Hex(twi_mst_addr) ; " ";
For Temp = 1 To Twi_mr_cntr
'Print Hex(twi_mst_buff(temp));
Speicher(temp) = Twi_mst_buff(temp)
Next
'Print
Twi_mst_flag = 0
Case Else: 'irgendein Problem
'Print Chr(7) ; Hex(twi_mst_addr) ; " ";
Call Twi_show_state(twi_mst_flag)
End Select
Return
'----------------------------------------
' Beispiel 8 byte senden,
' dann mit Rep.Start von gleicher adresse 3 Byte empfangen
'----------------------------------------
Master_transmit:
' sende buffer (mit testdaten) füllen
For Temp = 1 To 24
Twi_mst_buff(temp) = Slv_byte
Next
Twi_mst_addr = Mn2_adr 'I2C adresse
Twi_mt_cntr = 3 ' erstmal 8 Byte senden
Twi_mt_data = Varptr(twi_mst_buff(1)) ' adresse Daten
Twi_mr_cntr = 3 ' dann 3 Byte empfangen
Twi_mr_data = Varptr(twi_mst_buff(1)) ' empfangsbuffer
Gosub Run_wait_master ' auf gehts
Return
µC 2
Code:
'------------------------interface-------------------
$regfile = "m32def.dat"
$crystal = 16000000
$include "MyTWI.bas"
$baud = 9600
$hwstack = 64
$swstack = 64
$framesize = 64
Const Tmr_c_prescale = 64
Const Tmr_c_preload = 131
Const Tmr_c_divis = 250
Declare Sub Twi_show_state(byref State As Byte)
Config Porta = Output
Config Portb = Output
Config Portc = Input
'-------------------------------------------------
' lokale I2C Adresse (als Slave)
'-------------------------------------------------
Const Mn1_adr = &H50
'-------------------------------------------------
' fremde I2C Adresse (als Master)
'-------------------------------------------------
Const Mn2_adr = &H40
Dim Slv_byte As Byte 'sende Byte (irgendwas)
Dim Twi_mst_buff(24) As Byte 'Buffer für Masterfunktionen
Dim Twi_slv_buff(24) As Byte 'Buffer für Slavefunktionen
'-------------------------------------------------
Config Timer0 = Timer , Prescale = Tmr_c_prescale 'Timer 1mS
On Timer0 Interrupt_ticker ' Timer for Timer Queue
Dim Timeout As Byte
Dim Timediv As Word
'-------------------------------------------------
Dim Temp As Byte
Twi_slv_addr = Mn1_adr 'lokale I2C-Adresse
Twi_sr_data = Varptr(twi_slv_buff(1)) 'datenbuffer empfang
Twi_st_data = Varptr(twi_slv_buff(1)) 'datenbuffer senden
Twar = Twi_slv_addr + 1 'I2C Adress Mega32 + GCA
Config Twi = 400000 'I2C Speed
On Twi Twi_mst_slv_isr , Nosave 'ISR f. TWI
Gosub Twi_sense 'Aktivieren Slave-Funktion
Enable Timer0 'Timer
Enable Interrupts 'Generell
'---------------------------------------------
' initialiesierung
'---------------------------------------------
Dim Speicher(24) As Byte
Led1 Alias Portb.0
'------------------Konfigurierung-------------
Porta = &HFF
Portb = &HFF
Slv_byte = &H03
Led1 = 1
Do
If Twi_slv_flag <> 0 Then
'-----------------------------------------------------------------
' Ergebnis als SLave auswerten
' Vorsicht, der Bus ist solange blockiert
'-----------------------------------------------------------------
Select Case Twi_slv_flag
Case &H60 : 'Print Spc(30) ; "SLAVE recv:" ; 'es ist was empfangen worden
'Print Hex(twi_slv_addr) ; " ";
For Temp = 1 To Twi_sr_cntr
'Print Hex(twi_slv_buff(temp)); 'print der Daten
Next
Case &HA8 : 'Print Spc(30) ; "SLAVE tran:" ; 'es ist was abgeholt worden
'Print Hex(twi_slv_addr) ; " ";
For Temp = 1 To Twi_st_cntr
'Print Hex(twi_slv_buff(temp)); 'print der Daten
Next
Case &H70 : 'Print Spc(30) ; "SLAVE GCA :" ; 'ein General Call ist gekommen
'Print Hex(twi_slv_addr) ; " ";
For Temp = 1 To Twi_sr_cntr
'Print Hex(twi_slv_buff(temp)); 'print der Daten
Next
Case Else:
'Print Chr(7) ; 'Irgendein Fehler
Call Twi_show_state(twi_slv_flag) 'Print status-text
End Select
'Print
Twi_slv_flag = 0 'löschen marker
Twi_slv_stat = 0 'löschen marker
Gosub Twi_sense 'alles wieder enablen
'und Bus freigeben
End If
'-----------------------------------------------------------------
' Jede Sekunde was senden & empfangen
'-----------------------------------------------------------------
If Timeout = 1 Then
Gosub Master_transmit
If Twi_mst_flag = 0 Then 'Hats geklappt ?
Timeout = 0 'ja
' else (sonst probieren wir's gleich nochmal)
End If
End If
'-----------------------------------------------------------------
If Speicher(1) = &H03 Then Led1 = 0
Loop
End
'----------------------------------------
' 1 mS Ticker
'----------------------------------------
Interrupt_ticker: ' Timer for Timer Queue
Timer0 = Tmr_c_preload
Incr Timediv
If Timediv >= Tmr_c_divis Then
Timeout = 1 'eine Sekunde ist rum
Timediv = 0
End If
Return
'----------------------------------------
$include "Twi_show_state.bas"
'----------------------------------------
Run_wait_master:
Gosub Mst_start 'aufruf
Select Case Twi_mst_flag 'ergebnis ?
Case Twi_m_sent: 'gesendet
'Print "<<<<<<MASTER sent:" ;
'Print Hex(twi_mst_addr) ; " ";
For Temp = 1 To Twi_mt_cntr
'Print Hex(twi_mst_buff(temp));
Next
'Print
Twi_mst_flag = 0
Case Twi_m_recv: 'geholt
'Print ">>>>>>MASTER read:" ;
'Print Hex(twi_mst_addr) ; " ";
For Temp = 1 To Twi_mr_cntr
'Print Hex(twi_mst_buff(temp));
Speicher(temp) = Twi_mst_buff(temp)
Next
'Print
Twi_mst_flag = 0
Case Else: 'irgendein Problem
'Print Chr(7) ; Hex(twi_mst_addr) ; " ";
Call Twi_show_state(twi_mst_flag)
End Select
Return
'----------------------------------------
' Beispiel 8 byte senden,
' dann mit Rep.Start von gleicher adresse 3 Byte empfangen
'----------------------------------------
Master_transmit:
' sende buffer (mit testdaten) füllen
For Temp = 1 To 24
Twi_mst_buff(temp) = Slv_byte
Next
Twi_mst_addr = Mn2_adr 'I2C adresse
Twi_mt_cntr = 3 ' erstmal 8 Byte senden
Twi_mt_data = Varptr(twi_mst_buff(1)) ' adresse Daten
Twi_mr_cntr = 3 ' dann 3 Byte empfangen
Twi_mr_data = Varptr(twi_mst_buff(1)) ' empfangsbuffer
Gosub Run_wait_master ' auf gehts
Return
Mein Problem ist wenn ich beim µC1
Mn1_adr = &H40
Mn2_adr = &H50
und bei µC2
Mn1_adr = &H50
Mn2_adr = &H40
nehme dann geht nichts....
wenn ich aber beim µC1
Mn1_adr = &H50
Mn2_adr = &H40
und bei µC2
Mn1_adr = &H40
Mn2_adr = &H50
nehme dann leuchtet nur bei µC2 die LED?!
Wieso...........
Danke für jede Unterstützung.
mfg dämmi
Lesezeichen