Code:
$regfile = "m328pdef.dat"
$crystal = 16000000
$hwstack = 256
$swstack = 256
$framesize = 256
$baud = 38400
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16 'Test mir Atmega wegen HardwareSPI
'init the spi pins
Spiinit
Config Pind.3 = Input
Mcpint Alias Pind.3
Css Alias Portb.2
Config Css = Output
Set Css
Wait 1
Declare Sub Mcp2515_write_register(byval Adress As Byte , Byval Daten As Byte)
Declare Sub Mcp2515_read_register(byval Adress As Byte )
Declare Sub Mcp2515_bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Dim Mcphilf As Byte
Dim Id As Integer
Dim Idh As Byte
Dim Idl As Byte
Dim Dfl As Byte
Dim Idi As Byte
Dim Canin As Byte
Dim Db0(8) As Byte
Dim Canstat As Byte
Dim T As Integer
Dim L As Byte
Dim Hilf1 As Word
Dim Hilf2 As Word
Const Spi_reset = &HC0
Const Spi_write = &H02
Const Spi_read = &H03
Const Spi_rts0 = &H81
Const Spi_rts1 = &H82
Const Spi_rts2 = &H84
Const Spi_read_status = &HA0
Const Spi_bit_modify = &H05
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
Const Rxm0sidh = &H20
Const Rxm0sidl = &H21
Const Rxm0eid8 = &H22
Const Rxm0eid0 = &H23
Const Rxm1sidh = &H24
Const Rxm1sidl = &H25
Const Rxm1eid8 = &H26
Const Rxm1eid0 = &H27
Const Bfpctrl = &H0C
Const Txrtsctrl = &H0D
Const Canctrl = &H0F
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 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 Txb1ctrl = &H40 ' Transmit Buffer 0 Control Register
Const Txb1sidh = &H41 ' Transmit Buffer 0 Std Identifier High
Const Txb1sidl = &H42 ' Transmit Buffer 0 Std Identifier Low
Const Txb1dlc = &H45 ' Transmit Buffer 0 Data Length Code
Const Txb1d0 = &H46 ' Transmit Buffer 0 Data Byte 0
Const Txb1d1 = &H47 ' Transmit Buffer 0 Data Byte 1
Const Txb1d2 = &H48 ' Transmit Buffer 0 Data Byte 2
Const Txb1d3 = &H49 ' Transmit Buffer 0 Data Byte 3
Const Txb1d4 = &H4A ' Transmit Buffer 0 Data Byte 4
Const Txb1d5 = &H4B ' Transmit Buffer 0 Data Byte 5
Const Txb1d6 = &H4C ' Transmit Buffer 0 Data Byte 6
Const Txb1d7 = &H4D ' Transmit Buffer 0 Data Byte 7
Const Txb2ctrl = &H50 ' Transmit Buffer 0 Control Register
Const Txb2sidh = &H51 ' Transmit Buffer 0 Std Identifier High
Const Txb2sidl = &H52 ' Transmit Buffer 0 Std Identifier Low
Const Txb2dlc = &H55 ' Transmit Buffer 0 Data Length Code
Const Txb2d0 = &H56 ' Transmit Buffer 0 Data Byte 0
Const Txb2d1 = &H57 ' Transmit Buffer 0 Data Byte 1
Const Txb2d2 = &H58 ' Transmit Buffer 0 Data Byte 2
Const Txb2d3 = &H59 ' Transmit Buffer 0 Data Byte 3
Const Txb2d4 = &H5A ' Transmit Buffer 0 Data Byte 4
Const Txb2d5 = &H5B ' Transmit Buffer 0 Data Byte 5
Const Txb2d6 = &H5C ' Transmit Buffer 0 Data Byte 6
Const Txb2d7 = &H5D ' Transmit Buffer 0 Data Byte 7
Const Rxb0sidh = &H61
Const Rxb0sidl = &H62
Const Rxb0dlc = &H65
Const Rxb1sidh = &H71
Const Rxb1sidl = &H72
Const Rxb1dlc = &H75
Const Cnf1 = &H2A
Const Cnf2 = &H29
Const Cnf3 = &H28
Const Caninte = &H2B
Const Canintf = &H2C
Gosub Mcp2515_init
Mcp2515_read_register Caninte
Print "CANINTE: " ; Canin
Mcp2515_read_register Canintf
Print "CANINTF: " ; Canin
Idi = 10
Wait 1
Print "let's GO"
Do
T = T + 1
If T > 40 Then T = 0
Print "T=" ; T ; " MCP Pinb.3:" ; Mcpint
'Abfrage ob Interrupt aktiv ist
If Mcpint = 0 Then Gosub Mcpempfang
If T = 25 And Mcpint = 1 Then
Print"Send ACC"
Id = 661 'Identifer der zu sendenden Daten
Dfl = 8
Db0(1) = 03
Db0(2) = 00
Db0(3) = 00
Db0(4) = 00
Db0(5) = 00
Db0(6) = 00
Db0(7) = 00
Db0(8) = 00
Gosub Senden
End If
'Mcp2515_read_register Canintf
Mcp2515_read_register Canintf 'Spi_rx_status
Canstat = Canin
Print "CANINTF: " ; Canstat
Mcp2515_bitmodify Canintf , &B11110000 , &B00000000
Waitms 500
Loop
'------------- Versenden von Nachrichten ----------------------
Senden:
'kombinierte Abfrage, ob ein Register frei zum senden ist ( Datenblatt Seite64)
'es werden abgefragt (&H30 40 50) TXBnCTRL.TXREQ (n steht für Buffer 0,1,2)
'Alternativ Buffer0 (&H30) TXB0CTRL abfragen und bit 3=Status muss "0" sein (Seite 18)
Mcp2515_read_register Txb0ctrl '&HA0
Print "SPI_read_Status0: " ; Canin
If Canin.3 = 0 Then Goto Send_buffer0
Mcp2515_read_register Txb1ctrl '&HA0
Print "SPI_read_Status1: " ; Canin
If Canin.3 = 0 Then Goto Send_buffer1
Mcp2515_read_register Txb2ctrl '&HA0
Print "SPI_read_Status2: " ; Canin
If Canin.3 = 0 Then Goto Send_buffer2
Return
'--------- senden mit Buffer 0
Send_buffer0:
Hilf1 = Id / 8
Idh = Hilf1
Hilf1 = Idh * 8
Hilf1 = Id - Hilf1
Hilf1 = Hilf1 * 32
Idl = Hilf1
'Priorität einstellen bit 1-0 11=>höchste / 10=>hoch
' 01=>niedrig / 00=> niedrigste
Mcp2515_bitmodify Txb0ctrl , &B00000011 , &B00000011
'Standard ID einstellen
Mcp2515_write_register Txb0sidh , Idh
Mcp2515_write_register Txb0sidl , Idl
'Nachrichtenlänge einstellen (DFL)
Mcp2515_write_register Txb0dlc , Dfl
'Daten
Mcp2515_write_register Txb0d0 , Db0(1)
Mcp2515_write_register Txb0d1 , Db0(2)
Mcp2515_write_register Txb0d2 , Db0(3)
Mcp2515_write_register Txb0d3 , Db0(4)
Mcp2515_write_register Txb0d4 , Db0(5)
Mcp2515_write_register Txb0d5 , Db0(6)
Mcp2515_write_register Txb0d6 , Db0(7)
Mcp2515_write_register Txb0d7 , Db0(8)
'nachricht versenden
Reset Css
Waitus 10
Mcphilf = Spi_rts0
Spdr = Mcphilf
Do
Loop Until Spsr.spif = 1
Waitus 10
Set Css
Print "gesendet Buffer-0"
Waitms 50
Return
'--------- senden mit Buffer 1
Send_buffer1:
Hilf1 = Id / 8
Idh = Hilf1
Hilf1 = Idh * 8
Hilf1 = Id - Hilf1
Hilf1 = Hilf1 * 32
Idl = Hilf1
Mcp2515_bitmodify Txb1ctrl , &B00000011 , &B00000011
'Standard ID einstellen
Mcp2515_write_register Txb1sidh , Idh
Mcp2515_write_register Txb1sidl , Idl
'Nachrichtenlänge einstellen (DFL)
Mcp2515_write_register Txb1dlc , Dfl
'Daten
Mcp2515_write_register Txb1d0 , Db0(1)
Mcp2515_write_register Txb1d1 , Db0(2)
Mcp2515_write_register Txb1d2 , Db0(3)
Mcp2515_write_register Txb1d3 , Db0(4)
Mcp2515_write_register Txb1d4 , Db0(5)
Mcp2515_write_register Txb1d5 , Db0(6)
Mcp2515_write_register Txb1d6 , Db0(7)
Mcp2515_write_register Txb1d7 , Db0(8)
'nachricht versenden
Reset Css
Waitus 10
Mcphilf = Spi_rts1
Spdr = Mcphilf
Do
Loop Until Spsr.spif = 1
Waitus 10
Set Css
Print "gesendet Buffer-1"
Waitms 50
Return
'--------- senden mit Buffer 2
Send_buffer2:
Hilf1 = Id / 8
Idh = Hilf1
Hilf1 = Idh * 8
Hilf1 = Id - Hilf1
Hilf1 = Hilf1 * 32
Idl = Hilf1
Mcp2515_bitmodify Txb2ctrl , &B00000011 , &B00000011
'Standard ID einstellen
Mcp2515_write_register Txb2sidh , Idh
Mcp2515_write_register Txb2sidl , Idl
'Nachrichtenlänge einstellen (DFL)
Mcp2515_write_register Txb2dlc , Dfl
'Daten
Mcp2515_write_register Txb2d0 , Db0(1)
Mcp2515_write_register Txb2d1 , Db0(2)
Mcp2515_write_register Txb2d2 , Db0(3)
Mcp2515_write_register Txb2d3 , Db0(4)
Mcp2515_write_register Txb2d4 , Db0(5)
Mcp2515_write_register Txb2d5 , Db0(6)
Mcp2515_write_register Txb2d6 , Db0(7)
Mcp2515_write_register Txb2d7 , Db0(8)
'nachricht versenden
Reset Css
Waitus 10
Mcphilf = Spi_rts2
Spdr = Mcphilf
Do
Loop Until Spsr.spif = 1
Waitus 10
Set Css
Print "gesendet Buffer-2"
Waitms 50
Return
'--------------- Empfangen von Nachrichten -------------------------
Mcpempfang:
Print "Empfangen"
Mcp2515_read_register Canintf 'Spi_rx_status
Canstat = Canin
Print "CANINTF: " ; Canstat
If Canstat.0 = 1 Then Gosub Mcp_read_buffer0
If Canstat.1 = 1 Then Gosub Mcp_read_buffer1
Return
'-------- lesen vom Buffer 0
Mcp_read_buffer0:
Mcp2515_read_register Rxb0sidh
Idh = Canin
Mcp2515_read_register Rxb0sidl
Idl = Canin
Mcp2515_read_register Rxb0dlc
Dfl = Canin
Id = Idh * 8 : Hilf1 = Idl / 32 : Id = Id + Hilf1
Print "ID-0=" ; Id ; " DFL-0=" ; Dfl
'Daten
For L = 1 To 8
If L = 1 Then Mcp2515_read_register &H66
If L = 2 Then Mcp2515_read_register &H67
If L = 3 Then Mcp2515_read_register &H68
If L = 4 Then Mcp2515_read_register &H69
If L = 5 Then Mcp2515_read_register &H6A
If L = 6 Then Mcp2515_read_register &H6B
If L = 7 Then Mcp2515_read_register &H6C
If L = 8 Then Mcp2515_read_register &H6D
Db0(l) = Canin
Next L
Mcp2515_bitmodify Canintf , &B00000001 , &B00000000
Print "Daten Buffer-0:" ; Db0(1) ; "-" ; Db0(2) ; "-" ; Db0(3) ; "-" ; Db0(4) ; "-" ; Db0(5) ; "-" ; Db0(6) ; "-" ; Db0(7) ; "-" ; Db0(8)
Return
'-------- lesen vom Buffer 0
Mcp_read_buffer1:
Mcp2515_read_register Rxb1sidh
Idh = Canin
Mcp2515_read_register Rxb1sidl
Idl = Canin
Mcp2515_read_register Rxb1dlc
Dfl = Canin
Id = Idh * 8 : Hilf1 = Idl / 32 : Id = Id + Hilf1
Print "ID-1=" ; Id ; " DFL-1=" ; Dfl
'Daten
For L = 1 To 8
If L = 1 Then Mcp2515_read_register &H76
If L = 2 Then Mcp2515_read_register &H77
If L = 3 Then Mcp2515_read_register &H78
If L = 4 Then Mcp2515_read_register &H79
If L = 5 Then Mcp2515_read_register &H7A
If L = 6 Then Mcp2515_read_register &H7B
If L = 7 Then Mcp2515_read_register &H7C
If L = 8 Then Mcp2515_read_register &H7D
Db0(l) = Canin
Next L
Mcp2515_bitmodify Canintf , &B00000010 , &B00000000
Print "Daten Buffer-0:" ; Db0(1) ; "-" ; Db0(2) ; "-" ; Db0(3) ; "-" ; Db0(4) ; "-" ; Db0(5) ; "-" ; Db0(6) ; "-" ; Db0(7) ; "-" ; Db0(8)
Return
'------------- MCP2515 Initialisieren ------------------------------
Mcp2515_init:
'MCP2515 per Software Reset zurücksetzen
Mcphilf = Spi_reset
Reset Css
Waitus 10
Spdr = Mcphilf
Do
Loop Until Spsr.spif = 1
Waitus 10
Set Css
'einstellen des Bit Timings
'CNF1 &H07==>125 kbit/s &H13==>50 kbit/s
Mcp2515_write_register Cnf1 , &H13
Mcp2515_write_register Cnf2 , &H90
Mcp2515_write_register Cnf3 , &H02
'Interrupt einstellen
Mcp2515_write_register Caninte , &B00000011
'Einstellen der Filter
'Buffer 0: Empfangen aller Nachrichten
Mcp2515_write_register Rxb0ctrl , &B01100000
'Buffer 1: Empfang aller Nachrichten
Mcp2515_write_register Rxb1ctrl , &B01100000
'alle Bits der Empfangsmaske löschen
'damit werden alle Nachrichten empfangen
Mcp2515_write_register Rxm0sidh , 0
Mcp2515_write_register Rxm0sidl , 0
Mcp2515_write_register Rxm0eid8 , 0
Mcp2515_write_register Rxm0eid0 , 0
Mcp2515_write_register Rxm1sidh , 0
Mcp2515_write_register Rxm1sidl , 0
Mcp2515_write_register Rxm1eid8 , 0
Mcp2515_write_register Rxm1eid0 , 0
'Einstellen der Pin Funktionen
'Deaktivieren der Pins RXnBF Pins (High Impedance State)
Mcp2515_write_register Bfpctrl , 0
'TXnRTS Bits als Input schalten
Mcp2515_write_register Txrtsctrl , 0
'Device zurück in den normalen Modus versetzen
Mcp2515_bitmodify Canctrl , &B11100000 , &B00000000
'ende MCP2515_init
Return
'-------------- MCP2515 schreiben ----------------------------
Sub Mcp2515_write_register(byval Adress As Byte , Byval Daten As Byte)
'Print "Reg: " ; Adress ; " Val: " ; Daten
Mcphilf = Spi_write
Reset Css
Spdr = Mcphilf
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = Daten
Do
Loop Until Spsr.spif = 1
Set Css
End Sub
'-------------- MCP2515 lesen ----------------------------
Sub Mcp2515_read_register(byval Adress As Byte )
Mcphilf = Spi_read
Reset Css
Spdr = Mcphilf
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = 0
Do
Loop Until Spsr.spif = 1
Canin = Spdr
Set Css
End Sub
'-------------- MCP2515 bitmodify ----------------------------
Sub Mcp2515_bitmodify(reg_add , Reg_mask , Reg_val)
'Local Can_tmp2 As Byte
Reset Css
Mcphilf = Spi_bit_modify
Spdr = Mcphilf 'Can_tmp2
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 Css
End Sub
End
Lesezeichen