Kampi
08.06.2012, 19:53
Hallo Forum,
ich bastel derweil etwas an meinem CAN-Bus rum und stecke nun an der Software um Daten mit einem MCP2515 zu empfangen und an meinen PC zu senden.
Dabei habe ich mich, so gut es ging, an dieses Tutorial gehalten:
http://www.kreatives-chaos.com/artikel/ansteuerung-eines-mcp2515#receive
Jetzt spuckt der Mikrocontroller der Empfangen soll aber nur 255 aus und nicht den Wert den ich (hoffe zu) sende.
Vielleicht findet ja einer von euch den Fehler.
Hier ist das Programm welches zum Empfangen da ist (ich mache das erstmal nur mittels Polling der Register. Interrupt kommt dann sobald dies funktioniert):
'Mikrocontroller
$regfile = "m32def.dat"
$crystal = 16000000
'Stacks
$hwstack = 200
$swstack = 200
$framesize = 400
'UART konfigurieren
$baud = 19200
'SPI konfigurieren
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16
Spiinit
'Unterprogramme
Declare Sub Read_register(byval Adress As Byte )
Declare Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Declare Sub Bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Declare Sub Register
Declare Sub Mcp2515_init
Declare Sub Send_can(byval Daten As Byte)
Declare Sub Read_rx_status
'Befehle
Const Spi_read = &H03
Const Spi_write = &H02
Const Spi_reset = &HC0
Const Spi_rts0 = &H81
Const Spi_bitmodify = &H05
Const Spi_rx_status = &HB0
'Registeradressen
Const Cnf1 = &H2A
Const Cnf2 = &H29
Const Cnf3 = &H28
Const Canctrl = &H0F
Const Caninte = &H2B
Const Txb0ctrl = &H30 'Transmit Buffer 0 Control Register
Const Txb0sidh = &H31 'Transmit Buffer 0 Std Identifier High
Const Txb0sidl = &H32 'Transmit Buffer 0 Std Identifier Low
Const Txb0eid8 = &H33 'Transmit Buffer 0 Ext Identifier High
Const Txb0eid0 = &H34 'Transmit Buffer 0 Ext Identifier Low
Const Txb0dlc = &H35 'Transmit Buffer 0 Data Length Code
Const Txb0d0 = &H36 'Transmit Buffer 0 Data Byte 0
Const Txb0d1 = &H37 'Transmit Buffer 0 Data Byte 1
Const Txb0d2 = &H38 'Transmit Buffer 0 Data Byte 2
Const Txb0d3 = &H39 'Transmit Buffer 0 Data Byte 3
Const Txb0d4 = &H3A 'Transmit Buffer 0 Data Byte 4
Const Txb0d5 = &H3B 'Transmit Buffer 0 Data Byte 5
Const Txb0d6 = &H3C 'Transmit Buffer 0 Data Byte 6
Const Txb0d7 = &H3D 'Transmit Buffer 0 Data Byte 7
Const Rxm0sidh = &H20 'Receive Buffer 0 Std Identifier High
Const Rxm0sidl = &H21 'Receive Buffer 0 Std Identifier Low
Const Rxm0eid8 = &H22 'Receive Buffer 0 Ext Identifier High
Const Rxm0eid0 = &H23 'Receive Buffer 0 Ext Identifier low
Const Rxm1sidh = &BH24 'Receive Buffer 1 Std Identifier High
Const Rxm1sidl = &H25 'Receive Buffer 1 Std Identifier Low
Const Rxm1eid8 = &H26 'Receive Buffer 1 Ext Identifier High
Const Rxm1eid0 = &H27 'Receive Buffer 1 Ext Identifier low
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
'I/O Einstellungen
Config Portb.4 = Output
'Namen vergeben
Cs Alias Portb.4
'Variablen
Dim Canin As Byte
'Interrupts aktivieren
Enable Interrupts
'Chipselect auf High setzen
Set Cs
Wait 1
'MCP2515 initialisieren
Call Mcp2515_init
'Hauptprogramm
Do
'Call Send_can(1) 'Daten senden
Call Read_rx_status
'Call Register
'Register auslesen
Waitms 100
Loop
End
'Unterprogramme
'-------------------------------------------------------------------------------------------------------------------
Sub Read_register(byval Adress As Byte )
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_read 'Inhalt von "SPI_Read" im Datenregister speichern 'Read Instruction (0000 0011) senden
Spiout Spdr , 1
Spiout Adress , 1
Spiin Canin , 1 'Registerinhalt einlesen
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_write 'Inhalt von "SPI-Write" ins SPI_Data_Register schieben
Do 'Schleife bis SPI Interrupt Flag gesetzt wurde, also bis
Loop Until Spsr.spif = 1 'die Übertragung abgeschlossen ist
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = Daten
Do
Loop Until Spsr.spif = 1
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Mcp2515_init
Reset Cs
'MCP2515 reseten
Spdr = Spi_reset
Do
Loop Until Spsr.spif = 1
Waitms 10
Set Cs '10ms warten, da sonst das Auslesen von CNF1
'nicht richtig erfolgen kann
'Control Register beschreiben
Write_register Canctrl , &H8F 'Device in Configuration Mode versetzen, OSC-Pin
'aktivieren, Fclk = F / 8 einstellen
'Interrupts einstellen
Write_register Caninte , &H05 'Transmit und Recieve Buffer 0 Interrupt aktivieren
'TX-Konfiguration
Bitmodify Txb0ctrl , &H03 , &H03
'Bittiming einstellen
Write_register Cnf1 , &H03 'Baudrate einstellen
Write_register Cnf2 , &HA0 'Phasensegmenteinstellungen
Write_register Cnf3 , &H02
'Rx Buffer einstellen
Write_register Rxb0ctrl , &H60
Write_register Rxb1ctrl , &H60
Write_register Rxm0sidh , 0 'Empfängeradressen auf 0, Filter aus
Write_register Rxm0sidl , 0
Write_register Rxm0eid8 , 0
Write_register Rxm0eid0 , 0
Write_register Rxm1sidh , 0
Write_register Rxm1sidl , 0
Write_register Rxm1eid8 , 0
Write_register Rxm1eid0 , 0
'CAN-Controller in den "Normal Mode" versetzen
Bitmodify Canctrl , &HE0 , &H0
'Priorität einstellen
Write_register Txb0ctrl , &H03 'Höchste Message Priority für Transmit Buffer 0
'Message Identifier einstellen
Write_register Txb0sidh , &H00
Write_register Txb0sidl , &H03
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Bitmodify(reg_add , Reg_mask , Reg_val)
Reset Cs
Spdr = Spi_bitmodify
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Reg_add
Do
Loop Until Spsr.spif = 1
Spdr = Reg_mask
Do
Loop Until Spsr.spif = 1
Spdr = Reg_val
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Send_can(byval Daten As Byte)
Write_register Txb0sidh , 0
Write_register Txb0sidl , 1
Write_register Txb0dlc , 1
Write_register Txb0d0 , Daten
Reset Cs
Spdr = Spi_rts0 'Übertragung auslösen
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Read_rx_status
Reset Cs
Spdr = Spi_rx_status
Do
Loop Until Spsr.spif = 1
Canin = Spdr
Set Cs
Print "Wert: " ; Canin
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Register
'Terminalausgabe
Print "Registereinstellungen:"
Read_register Cnf1
Print "Registerinhalt CNF1: " ; Canin
Read_register Cnf2
Print "Registerinhalt CNF2: " ; Canin
Read_register Cnf3
Print "Registerinhalt CNF3: " ; Canin
Read_register Canctrl
Print "Registerinhalt CAN Control: " ; Canin
Read_register Txb0ctrl
Print "Registerinhalt TXB0 Control: " ; Canin
Read_register Txb0sidl
Print "Registerinhalt TXB0 Identifier Low: " ; Canin
Read_register Txb0sidh
Print "Registerinhalt TXB0 Identifier High: " ; Canin
Read_register Txb0dlc
Print "Registerinhalt TXB0 Data Lenght: " ; Canin
Read_register Caninte
Print "Registerinhalt CAN Interrupt Enable: " ; Canin
Print "----------------------------------------------------------"
End Sub
'-------------------------------------------------------------------------------------------------------------------
Und hier nochmal der vollständigkeit halber der Code zum senden:
'Mikrocontroller
$regfile = "m32def.dat"
$crystal = 16000000
'Stacks
$hwstack = 200
$swstack = 200
$framesize = 400
'UART konfigurieren
$baud = 19200
'SPI konfigurieren
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16
Spiinit
'Unterprogramme
Declare Sub Read_register(byval Adress As Byte )
Declare Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Declare Sub Bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Declare Sub Register
Declare Sub Mcp2515_init
Declare Sub Send_can(byval Daten As Byte)
'Befehle
Const Spi_read = &H03
Const Spi_write = &H02
Const Spi_reset = &HC0
Const Spi_rts0 = &H81
Const Spi_bitmodify = &H05
'Registeradressen
Const Cnf1 = &H2A
Const Cnf2 = &H29
Const Cnf3 = &H28
Const Canctrl = &H0F
Const Caninte = &H2B
Const Txb0ctrl = &H30 'Transmit Buffer 0 Control Register
Const Txb0sidh = &H31 'Transmit Buffer 0 Std Identifier High
Const Txb0sidl = &H32 'Transmit Buffer 0 Std Identifier Low
Const Txb0eid8 = &H33 'Transmit Buffer 0 Ext Identifier High
Const Txb0eid0 = &H34 'Transmit Buffer 0 Ext Identifier Low
Const Txb0dlc = &H35 'Transmit Buffer 0 Data Length Code
Const Txb0d0 = &H36 'Transmit Buffer 0 Data Byte 0
Const Txb0d1 = &H37 'Transmit Buffer 0 Data Byte 1
Const Txb0d2 = &H38 'Transmit Buffer 0 Data Byte 2
Const Txb0d3 = &H39 'Transmit Buffer 0 Data Byte 3
Const Txb0d4 = &H3A 'Transmit Buffer 0 Data Byte 4
Const Txb0d5 = &H3B 'Transmit Buffer 0 Data Byte 5
Const Txb0d6 = &H3C 'Transmit Buffer 0 Data Byte 6
Const Txb0d7 = &H3D 'Transmit Buffer 0 Data Byte 7
Const Rxm0sidh = &H20 'Receive Buffer 0 Std Identifier High
Const Rxm0sidl = &H21 'Receive Buffer 0 Std Identifier Low
Const Rxm0eid8 = &H22 'Receive Buffer 0 Ext Identifier High
Const Rxm0eid0 = &H23 'Receive Buffer 0 Ext Identifier low
Const Rxm1sidh = &BH24 'Receive Buffer 1 Std Identifier High
Const Rxm1sidl = &H25 'Receive Buffer 1 Std Identifier Low
Const Rxm1eid8 = &H26 'Receive Buffer 1 Ext Identifier High
Const Rxm1eid0 = &H27 'Receive Buffer 1 Ext Identifier low
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
'I/O Einstellungen
Config Portb.4 = Output
'Namen vergeben
Cs Alias Portb.4
'Variablen
Dim Canin As Byte
'Interrupts aktivieren
Enable Interrupts
'Chipselect auf High setzen
Set Cs
Wait 1
'MCP2515 initialisieren
Call Mcp2515_init
'Hauptprogramm
Do
Call Send_can(1) 'Daten senden
Call Register
'Register auslesen
Waitms 100
Loop
End
'Unterprogramme
'-------------------------------------------------------------------------------------------------------------------
Sub Read_register(byval Adress As Byte )
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_read 'Inhalt von "SPI_Read" im Datenregister speichern 'Read Instruction (0000 0011) senden
Spiout Spdr , 1
Spiout Adress , 1
Spiin Canin , 1 'Registerinhalt einlesen
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_write 'Inhalt von "SPI-Write" ins SPI_Data_Register schieben
Do 'Schleife bis SPI Interrupt Flag gesetzt wurde, also bis
Loop Until Spsr.spif = 1 'die Übertragung abgeschlossen ist
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = Daten
Do
Loop Until Spsr.spif = 1
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Mcp2515_init
Reset Cs
'MCP2515 reseten
Spdr = Spi_reset
Do
Loop Until Spsr.spif = 1
Waitms 10
Set Cs '10ms warten, da sonst das Auslesen von CNF1
'nicht richtig erfolgen kann
'Control Register beschreiben
Write_register Canctrl , &H8F 'Device in Configuration Mode versetzen, OSC-Pin
'aktivieren, Fclk = F / 8 einstellen
'Interrupts einstellen
Write_register Caninte , &H05 'Transmit und Recieve Buffer 0 Interrupt aktivieren
'TX-Konfiguration
Bitmodify Txb0ctrl , &H03 , &H03
'Bittiming einstellen
Write_register Cnf1 , &H03 'Baudrate einstellen
Write_register Cnf2 , &HA0 'Phasensegmenteinstellungen
Write_register Cnf3 , &H02
'Rx Buffer einstellen
Write_register Rxb0ctrl , &H60
Write_register Rxb1ctrl , &H60
Write_register Rxm0sidh , 0 'Empfängeradressen auf 0, Filter aus
Write_register Rxm0sidl , 0
Write_register Rxm0eid8 , 0
Write_register Rxm0eid0 , 0
Write_register Rxm1sidh , 0
Write_register Rxm1sidl , 0
Write_register Rxm1eid8 , 0
Write_register Rxm1eid0 , 0
'CAN-Controller in den "Normal Mode" versetzen
Bitmodify Canctrl , &HE0 , &H0
'Priorität einstellen
Write_register Txb0ctrl , &H03 'Höchste Message Priority für Transmit Buffer 0
'Message Identifier einstellen
Write_register Txb0sidh , &H00
Write_register Txb0sidl , &H03
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Bitmodify(reg_add , Reg_mask , Reg_val)
Reset Cs
Spdr = Spi_bitmodify
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Reg_add
Do
Loop Until Spsr.spif = 1
Spdr = Reg_mask
Do
Loop Until Spsr.spif = 1
Spdr = Reg_val
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Send_can(byval Daten As Byte)
Write_register Txb0sidh , 0
Write_register Txb0sidl , 1
Write_register Txb0dlc , 1
Write_register Txb0d0 , Daten
Reset Cs
Spdr = Spi_rts0 'Übertragung auslösen
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Register
'Terminalausgabe
Print "Registereinstellungen:"
Read_register Cnf1
Print "Registerinhalt CNF1: " ; Canin
Read_register Cnf2
Print "Registerinhalt CNF2: " ; Canin
Read_register Cnf3
Print "Registerinhalt CNF3: " ; Canin
Read_register Canctrl
Print "Registerinhalt CAN Control: " ; Canin
Read_register Txb0ctrl
Print "Registerinhalt TXB0 Control: " ; Canin
Read_register Txb0sidl
Print "Registerinhalt TXB0 Identifier Low: " ; Canin
Read_register Txb0sidh
Print "Registerinhalt TXB0 Identifier High: " ; Canin
Read_register Txb0dlc
Print "Registerinhalt TXB0 Data Lenght: " ; Canin
Read_register Caninte
Print "Registerinhalt CAN Interrupt Enable: " ; Canin
Print "----------------------------------------------------------"
End Sub
'-------------------------------------------------------------------------------------------------------------------
Wie man sieht, sende ich erstmal nur den Wert "1". Die Übertragung habe ich auch schon mit meinem Oszilloskop kontrolliert. Das Bild sieht so aus:
http://img826.imageshack.us/img826/3728/cansenden1.png (http://imageshack.us/photo/my-images/826/cansenden1.png/)
Danke schonmal für die Hilfe :)
ich bastel derweil etwas an meinem CAN-Bus rum und stecke nun an der Software um Daten mit einem MCP2515 zu empfangen und an meinen PC zu senden.
Dabei habe ich mich, so gut es ging, an dieses Tutorial gehalten:
http://www.kreatives-chaos.com/artikel/ansteuerung-eines-mcp2515#receive
Jetzt spuckt der Mikrocontroller der Empfangen soll aber nur 255 aus und nicht den Wert den ich (hoffe zu) sende.
Vielleicht findet ja einer von euch den Fehler.
Hier ist das Programm welches zum Empfangen da ist (ich mache das erstmal nur mittels Polling der Register. Interrupt kommt dann sobald dies funktioniert):
'Mikrocontroller
$regfile = "m32def.dat"
$crystal = 16000000
'Stacks
$hwstack = 200
$swstack = 200
$framesize = 400
'UART konfigurieren
$baud = 19200
'SPI konfigurieren
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16
Spiinit
'Unterprogramme
Declare Sub Read_register(byval Adress As Byte )
Declare Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Declare Sub Bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Declare Sub Register
Declare Sub Mcp2515_init
Declare Sub Send_can(byval Daten As Byte)
Declare Sub Read_rx_status
'Befehle
Const Spi_read = &H03
Const Spi_write = &H02
Const Spi_reset = &HC0
Const Spi_rts0 = &H81
Const Spi_bitmodify = &H05
Const Spi_rx_status = &HB0
'Registeradressen
Const Cnf1 = &H2A
Const Cnf2 = &H29
Const Cnf3 = &H28
Const Canctrl = &H0F
Const Caninte = &H2B
Const Txb0ctrl = &H30 'Transmit Buffer 0 Control Register
Const Txb0sidh = &H31 'Transmit Buffer 0 Std Identifier High
Const Txb0sidl = &H32 'Transmit Buffer 0 Std Identifier Low
Const Txb0eid8 = &H33 'Transmit Buffer 0 Ext Identifier High
Const Txb0eid0 = &H34 'Transmit Buffer 0 Ext Identifier Low
Const Txb0dlc = &H35 'Transmit Buffer 0 Data Length Code
Const Txb0d0 = &H36 'Transmit Buffer 0 Data Byte 0
Const Txb0d1 = &H37 'Transmit Buffer 0 Data Byte 1
Const Txb0d2 = &H38 'Transmit Buffer 0 Data Byte 2
Const Txb0d3 = &H39 'Transmit Buffer 0 Data Byte 3
Const Txb0d4 = &H3A 'Transmit Buffer 0 Data Byte 4
Const Txb0d5 = &H3B 'Transmit Buffer 0 Data Byte 5
Const Txb0d6 = &H3C 'Transmit Buffer 0 Data Byte 6
Const Txb0d7 = &H3D 'Transmit Buffer 0 Data Byte 7
Const Rxm0sidh = &H20 'Receive Buffer 0 Std Identifier High
Const Rxm0sidl = &H21 'Receive Buffer 0 Std Identifier Low
Const Rxm0eid8 = &H22 'Receive Buffer 0 Ext Identifier High
Const Rxm0eid0 = &H23 'Receive Buffer 0 Ext Identifier low
Const Rxm1sidh = &BH24 'Receive Buffer 1 Std Identifier High
Const Rxm1sidl = &H25 'Receive Buffer 1 Std Identifier Low
Const Rxm1eid8 = &H26 'Receive Buffer 1 Ext Identifier High
Const Rxm1eid0 = &H27 'Receive Buffer 1 Ext Identifier low
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
'I/O Einstellungen
Config Portb.4 = Output
'Namen vergeben
Cs Alias Portb.4
'Variablen
Dim Canin As Byte
'Interrupts aktivieren
Enable Interrupts
'Chipselect auf High setzen
Set Cs
Wait 1
'MCP2515 initialisieren
Call Mcp2515_init
'Hauptprogramm
Do
'Call Send_can(1) 'Daten senden
Call Read_rx_status
'Call Register
'Register auslesen
Waitms 100
Loop
End
'Unterprogramme
'-------------------------------------------------------------------------------------------------------------------
Sub Read_register(byval Adress As Byte )
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_read 'Inhalt von "SPI_Read" im Datenregister speichern 'Read Instruction (0000 0011) senden
Spiout Spdr , 1
Spiout Adress , 1
Spiin Canin , 1 'Registerinhalt einlesen
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_write 'Inhalt von "SPI-Write" ins SPI_Data_Register schieben
Do 'Schleife bis SPI Interrupt Flag gesetzt wurde, also bis
Loop Until Spsr.spif = 1 'die Übertragung abgeschlossen ist
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = Daten
Do
Loop Until Spsr.spif = 1
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Mcp2515_init
Reset Cs
'MCP2515 reseten
Spdr = Spi_reset
Do
Loop Until Spsr.spif = 1
Waitms 10
Set Cs '10ms warten, da sonst das Auslesen von CNF1
'nicht richtig erfolgen kann
'Control Register beschreiben
Write_register Canctrl , &H8F 'Device in Configuration Mode versetzen, OSC-Pin
'aktivieren, Fclk = F / 8 einstellen
'Interrupts einstellen
Write_register Caninte , &H05 'Transmit und Recieve Buffer 0 Interrupt aktivieren
'TX-Konfiguration
Bitmodify Txb0ctrl , &H03 , &H03
'Bittiming einstellen
Write_register Cnf1 , &H03 'Baudrate einstellen
Write_register Cnf2 , &HA0 'Phasensegmenteinstellungen
Write_register Cnf3 , &H02
'Rx Buffer einstellen
Write_register Rxb0ctrl , &H60
Write_register Rxb1ctrl , &H60
Write_register Rxm0sidh , 0 'Empfängeradressen auf 0, Filter aus
Write_register Rxm0sidl , 0
Write_register Rxm0eid8 , 0
Write_register Rxm0eid0 , 0
Write_register Rxm1sidh , 0
Write_register Rxm1sidl , 0
Write_register Rxm1eid8 , 0
Write_register Rxm1eid0 , 0
'CAN-Controller in den "Normal Mode" versetzen
Bitmodify Canctrl , &HE0 , &H0
'Priorität einstellen
Write_register Txb0ctrl , &H03 'Höchste Message Priority für Transmit Buffer 0
'Message Identifier einstellen
Write_register Txb0sidh , &H00
Write_register Txb0sidl , &H03
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Bitmodify(reg_add , Reg_mask , Reg_val)
Reset Cs
Spdr = Spi_bitmodify
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Reg_add
Do
Loop Until Spsr.spif = 1
Spdr = Reg_mask
Do
Loop Until Spsr.spif = 1
Spdr = Reg_val
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Send_can(byval Daten As Byte)
Write_register Txb0sidh , 0
Write_register Txb0sidl , 1
Write_register Txb0dlc , 1
Write_register Txb0d0 , Daten
Reset Cs
Spdr = Spi_rts0 'Übertragung auslösen
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Read_rx_status
Reset Cs
Spdr = Spi_rx_status
Do
Loop Until Spsr.spif = 1
Canin = Spdr
Set Cs
Print "Wert: " ; Canin
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Register
'Terminalausgabe
Print "Registereinstellungen:"
Read_register Cnf1
Print "Registerinhalt CNF1: " ; Canin
Read_register Cnf2
Print "Registerinhalt CNF2: " ; Canin
Read_register Cnf3
Print "Registerinhalt CNF3: " ; Canin
Read_register Canctrl
Print "Registerinhalt CAN Control: " ; Canin
Read_register Txb0ctrl
Print "Registerinhalt TXB0 Control: " ; Canin
Read_register Txb0sidl
Print "Registerinhalt TXB0 Identifier Low: " ; Canin
Read_register Txb0sidh
Print "Registerinhalt TXB0 Identifier High: " ; Canin
Read_register Txb0dlc
Print "Registerinhalt TXB0 Data Lenght: " ; Canin
Read_register Caninte
Print "Registerinhalt CAN Interrupt Enable: " ; Canin
Print "----------------------------------------------------------"
End Sub
'-------------------------------------------------------------------------------------------------------------------
Und hier nochmal der vollständigkeit halber der Code zum senden:
'Mikrocontroller
$regfile = "m32def.dat"
$crystal = 16000000
'Stacks
$hwstack = 200
$swstack = 200
$framesize = 400
'UART konfigurieren
$baud = 19200
'SPI konfigurieren
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16
Spiinit
'Unterprogramme
Declare Sub Read_register(byval Adress As Byte )
Declare Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Declare Sub Bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Declare Sub Register
Declare Sub Mcp2515_init
Declare Sub Send_can(byval Daten As Byte)
'Befehle
Const Spi_read = &H03
Const Spi_write = &H02
Const Spi_reset = &HC0
Const Spi_rts0 = &H81
Const Spi_bitmodify = &H05
'Registeradressen
Const Cnf1 = &H2A
Const Cnf2 = &H29
Const Cnf3 = &H28
Const Canctrl = &H0F
Const Caninte = &H2B
Const Txb0ctrl = &H30 'Transmit Buffer 0 Control Register
Const Txb0sidh = &H31 'Transmit Buffer 0 Std Identifier High
Const Txb0sidl = &H32 'Transmit Buffer 0 Std Identifier Low
Const Txb0eid8 = &H33 'Transmit Buffer 0 Ext Identifier High
Const Txb0eid0 = &H34 'Transmit Buffer 0 Ext Identifier Low
Const Txb0dlc = &H35 'Transmit Buffer 0 Data Length Code
Const Txb0d0 = &H36 'Transmit Buffer 0 Data Byte 0
Const Txb0d1 = &H37 'Transmit Buffer 0 Data Byte 1
Const Txb0d2 = &H38 'Transmit Buffer 0 Data Byte 2
Const Txb0d3 = &H39 'Transmit Buffer 0 Data Byte 3
Const Txb0d4 = &H3A 'Transmit Buffer 0 Data Byte 4
Const Txb0d5 = &H3B 'Transmit Buffer 0 Data Byte 5
Const Txb0d6 = &H3C 'Transmit Buffer 0 Data Byte 6
Const Txb0d7 = &H3D 'Transmit Buffer 0 Data Byte 7
Const Rxm0sidh = &H20 'Receive Buffer 0 Std Identifier High
Const Rxm0sidl = &H21 'Receive Buffer 0 Std Identifier Low
Const Rxm0eid8 = &H22 'Receive Buffer 0 Ext Identifier High
Const Rxm0eid0 = &H23 'Receive Buffer 0 Ext Identifier low
Const Rxm1sidh = &BH24 'Receive Buffer 1 Std Identifier High
Const Rxm1sidl = &H25 'Receive Buffer 1 Std Identifier Low
Const Rxm1eid8 = &H26 'Receive Buffer 1 Ext Identifier High
Const Rxm1eid0 = &H27 'Receive Buffer 1 Ext Identifier low
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
'I/O Einstellungen
Config Portb.4 = Output
'Namen vergeben
Cs Alias Portb.4
'Variablen
Dim Canin As Byte
'Interrupts aktivieren
Enable Interrupts
'Chipselect auf High setzen
Set Cs
Wait 1
'MCP2515 initialisieren
Call Mcp2515_init
'Hauptprogramm
Do
Call Send_can(1) 'Daten senden
Call Register
'Register auslesen
Waitms 100
Loop
End
'Unterprogramme
'-------------------------------------------------------------------------------------------------------------------
Sub Read_register(byval Adress As Byte )
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_read 'Inhalt von "SPI_Read" im Datenregister speichern 'Read Instruction (0000 0011) senden
Spiout Spdr , 1
Spiout Adress , 1
Spiin Canin , 1 'Registerinhalt einlesen
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_write 'Inhalt von "SPI-Write" ins SPI_Data_Register schieben
Do 'Schleife bis SPI Interrupt Flag gesetzt wurde, also bis
Loop Until Spsr.spif = 1 'die Übertragung abgeschlossen ist
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = Daten
Do
Loop Until Spsr.spif = 1
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Mcp2515_init
Reset Cs
'MCP2515 reseten
Spdr = Spi_reset
Do
Loop Until Spsr.spif = 1
Waitms 10
Set Cs '10ms warten, da sonst das Auslesen von CNF1
'nicht richtig erfolgen kann
'Control Register beschreiben
Write_register Canctrl , &H8F 'Device in Configuration Mode versetzen, OSC-Pin
'aktivieren, Fclk = F / 8 einstellen
'Interrupts einstellen
Write_register Caninte , &H05 'Transmit und Recieve Buffer 0 Interrupt aktivieren
'TX-Konfiguration
Bitmodify Txb0ctrl , &H03 , &H03
'Bittiming einstellen
Write_register Cnf1 , &H03 'Baudrate einstellen
Write_register Cnf2 , &HA0 'Phasensegmenteinstellungen
Write_register Cnf3 , &H02
'Rx Buffer einstellen
Write_register Rxb0ctrl , &H60
Write_register Rxb1ctrl , &H60
Write_register Rxm0sidh , 0 'Empfängeradressen auf 0, Filter aus
Write_register Rxm0sidl , 0
Write_register Rxm0eid8 , 0
Write_register Rxm0eid0 , 0
Write_register Rxm1sidh , 0
Write_register Rxm1sidl , 0
Write_register Rxm1eid8 , 0
Write_register Rxm1eid0 , 0
'CAN-Controller in den "Normal Mode" versetzen
Bitmodify Canctrl , &HE0 , &H0
'Priorität einstellen
Write_register Txb0ctrl , &H03 'Höchste Message Priority für Transmit Buffer 0
'Message Identifier einstellen
Write_register Txb0sidh , &H00
Write_register Txb0sidl , &H03
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Bitmodify(reg_add , Reg_mask , Reg_val)
Reset Cs
Spdr = Spi_bitmodify
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Reg_add
Do
Loop Until Spsr.spif = 1
Spdr = Reg_mask
Do
Loop Until Spsr.spif = 1
Spdr = Reg_val
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Send_can(byval Daten As Byte)
Write_register Txb0sidh , 0
Write_register Txb0sidl , 1
Write_register Txb0dlc , 1
Write_register Txb0d0 , Daten
Reset Cs
Spdr = Spi_rts0 'Übertragung auslösen
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Register
'Terminalausgabe
Print "Registereinstellungen:"
Read_register Cnf1
Print "Registerinhalt CNF1: " ; Canin
Read_register Cnf2
Print "Registerinhalt CNF2: " ; Canin
Read_register Cnf3
Print "Registerinhalt CNF3: " ; Canin
Read_register Canctrl
Print "Registerinhalt CAN Control: " ; Canin
Read_register Txb0ctrl
Print "Registerinhalt TXB0 Control: " ; Canin
Read_register Txb0sidl
Print "Registerinhalt TXB0 Identifier Low: " ; Canin
Read_register Txb0sidh
Print "Registerinhalt TXB0 Identifier High: " ; Canin
Read_register Txb0dlc
Print "Registerinhalt TXB0 Data Lenght: " ; Canin
Read_register Caninte
Print "Registerinhalt CAN Interrupt Enable: " ; Canin
Print "----------------------------------------------------------"
End Sub
'-------------------------------------------------------------------------------------------------------------------
Wie man sieht, sende ich erstmal nur den Wert "1". Die Übertragung habe ich auch schon mit meinem Oszilloskop kontrolliert. Das Bild sieht so aus:
http://img826.imageshack.us/img826/3728/cansenden1.png (http://imageshack.us/photo/my-images/826/cansenden1.png/)
Danke schonmal für die Hilfe :)