Code:
$regfile = "m32def.dat"
$crystal = 7372800
$baud = 9600
' Sonderzeichen und LCD Display ************************************************
Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.2 , Db6 = Porta.1 , Db7 = Porta.0 , E = Porta.4 , Rs = Porta.5
Config Lcdmode = Port
Config Lcdbus = 4 '4 bit mode
Config Lcd = 16 * 2 'LCD-Display mit 16 Zeichen in 2 Zeilen verwendet
' CAN Bus Init******************************************************************
Config Spi = Hard , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16 'Test mir Atmega wegen HardwareSPI
Spiinit
Config Pind.3 = Input
Mcpint Alias Pind.3 ' Mcpint Alias Pind.3
Css Alias Portd.4
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 Absender As Integer
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 Hilf1 As Word
Dim T As Integer
Dim L As Byte
Dim Verify As Byte
Verify = 10
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 Rxm1sidh = &H24
Const Rxm1sidl = &H25
Const Rxf0sidh = &H00
Const Rxf0sidl = &H01
Const Rxf1sidh = &H04
Const Rxf1sidl = &H05
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 1 Control Register
Const Txb1sidh = &H41 ' Transmit Buffer 1 Std Identifier High
Const Txb1sidl = &H42 ' Transmit Buffer 1 Std Identifier Low
Const Txb1dlc = &H45 ' Transmit Buffer 1 Data Length Code
Const Txb1d0 = &H46 ' Transmit Buffer 1 Data Byte 0
Const Txb1d1 = &H47 ' Transmit Buffer 1 Data Byte 1
Const Txb1d2 = &H48 ' Transmit Buffer 1 Data Byte 2
Const Txb1d3 = &H49 ' Transmit Buffer 1 Data Byte 3
Const Txb1d4 = &H4A ' Transmit Buffer 1 Data Byte 4
Const Txb1d5 = &H4B ' Transmit Buffer 1 Data Byte 5
Const Txb1d6 = &H4C ' Transmit Buffer 1 Data Byte 6
Const Txb1d7 = &H4D ' Transmit Buffer 1 Data Byte 7
Const Txb2ctrl = &H50 ' Transmit Buffer 2 Control Register
Const Txb2sidh = &H51 ' Transmit Buffer 2 Std Identifier High
Const Txb2sidl = &H52 ' Transmit Buffer 2 Std Identifier Low
Const Txb2dlc = &H55 ' Transmit Buffer 2 Data Length Code
Const Txb2d0 = &H56 ' Transmit Buffer 2 Data Byte 0
Const Txb2d1 = &H57 ' Transmit Buffer 2 Data Byte 1
Const Txb2d2 = &H58 ' Transmit Buffer 2 Data Byte 2
Const Txb2d3 = &H59 ' Transmit Buffer 2 Data Byte 3
Const Txb2d4 = &H5A ' Transmit Buffer 2 Data Byte 4
Const Txb2d5 = &H5B ' Transmit Buffer 2 Data Byte 5
Const Txb2d6 = &H5C ' Transmit Buffer 2 Data Byte 6
Const Txb2d7 = &H5D ' Transmit Buffer 2 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
$framesize = 32 'Stack
$swstack = 32
$hwstack = 64
'CAN Modul Identifikation
Id = 10
' Startanzeige
' LCD "1 5 10 16 20"
Cls
Locate 1 , 1 : Lcd "test1"
Locate 2 , 1 : Lcd "V1.1 05.01.2009"
Wait 2
Hilf1 = Id / 8
Idh = Hilf1
Hilf1 = Idh * 8
Hilf1 = Id - Hilf1
Hilf1 = Hilf1 * 32
Idl = Hilf1
Gosub Mcp2515_init
Mcp2515_read_register Caninte
Mcp2515_read_register Canintf
Mcp2515_read_register Canstat
Mcp2515_read_register Canctrl
Mcp2515_read_register Cnf1
Mcp2515_read_register Cnf2
Mcp2515_read_register Cnf3
Mcp2515_read_register Txb0ctrl
Mcp2515_read_register Txb1ctrl
Mcp2515_read_register Txb2ctrl
Mcp2515_read_register Rxb0ctrl
Mcp2515_read_register Rxb1ctrl
Mcp2515_read_register Bfpctrl
Mcp2515_read_register Txrtsctrl
Idi = 10
Locate 2 , 1 : Lcd "Eigene-ID " ; Id ; " o.k." ' Bestätigungsmeldung
' Haupt-Programm Ausführung
Wait 2
Cls
If Mcpint = 1 Then
' Id = Eigenid 'Identifer der zu sendenden Daten
Dfl = 8 'Nachrichtenlänge einstellen (DFL)
Db0(1) = 0 'Befehl 'Anweisung
Db0(2) = 0 'Ziel 'Empfänger
Db0(3) = 0
Db0(4) = 0
Db0(5) = 0
Db0(6) = 0
Db0(7) = 0
Db0(8) = Absender
Gosub Senden
Else
End If
Wait 3
Gosub Status 'Aktivmeldung an CAN Bus
Do
Loop
'**** CAN Sequenzen ************************************************************
Mcp2515_init:
Print "Mcp2515_init:"
'MCP2515 per Software Reset zurücksetzen
Mcphilf = Spi_reset
Reset Css
Waitus 10
Spdr = Mcphilf
Do
Print "do"
Loop Until Spsr.spif = 1
Waitus 10
Set Css
Waitus 20
Print "einstellen des Bit Timings "
'einstellen des Bit Timings +++++++++ UND HIER HÄNGT SICH DAS PROGRAMM DANN AUF ++++++++++++++++++++
Mcp2515_write_register Cnf1 , &H13
Mcp2515_write_register Cnf2 , &H90
Mcp2515_write_register Cnf3 , &H02
Print "einstellen des Bit Timings o.k."
'Interrupt einstellen
Mcp2515_write_register Caninte , &B00000011
Print "Interrupt einstellen "
Mcp2515_write_register Rxb0ctrl , &B01100000
' **
'Buffer 1: Empfang aller Nachrichten
Mcp2515_write_register Rxb1ctrl , &B01100000
' **
'****************************************************************************
'****************** Filter und Maske einstellen ********************
'****************************************************************************
'Maske setzen - alle Bits auf "1" führen zu einem eindeutigen Filtern einer ID
Mcp2515_write_register Rxm0sidh , &B00111111 '
Mcp2515_write_register Rxm0sidl , &B11100000 '
Mcp2515_write_register Rxm1sidh , &B00111111 '
Mcp2515_write_register Rxm1sidl , &B11100000 '
'Filter setzen
Mcp2515_write_register Rxf0sidh , &B00000011
Mcp2515_write_register Rxf0sidl , &B00100000
Mcp2515_write_register Rxf1sidh , &B00000011
Mcp2515_write_register Rxf1sidl , &B00100000
Mcp2515_write_register Bfpctrl , 0
Mcp2515_write_register Txrtsctrl , 0
Mcp2515_bitmodify Canctrl , &B11100000 , &B00000000
Print "return"
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
'******** Datenempfang ********************************************
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) ; "-#"
Gosub Auswertung
Return
'**************************************************
Auswertung:
Print "Empfang:" ; Db0(1) ; "-" ; Db0(2) ; "-" ; Db0(3) ; "-" ; Db0(4) ; "-" ; Db0(5) ; "-" ; Db0(6) ; "-" ; Db0(7) ; "-" ; Db0(8)
Return
'**************************************************
Status:
If Mcpint = 1 Then
' Id = Eigenid 'Identifer der zu sendenden Daten
Dfl = 8 'Nachrichtenlänge einstellen (DFL)
Db0(1) = 001 'Befehl 'Anweisung
Db0(2) = 000 'Ziel 'Empfänger
Db0(3) = 0
Db0(4) = 0
Db0(5) = 0
Db0(6) = 0
Db0(7) = 0
Db0(8) = Absender
Gosub Senden
Else
End If
Mcp2515_read_register Canintf 'Spi_rx_status
Canstat = Canin
' Print "CANINTF: " ; Canstat
Mcp2515_bitmodify Canctrl , &B00001000 , &B00001000
Waitms 500
Return
Senden:
Mcp2515_read_register Txb0ctrl '&HA0
Hilf1 = Id / 8
Idh = Hilf1
Hilf1 = Idh * 8
Hilf1 = Id - Hilf1
Hilf1 = Hilf1 * 32
Idl = Hilf1
'Print Id
'Print "SPI_read_Status0: " ; Canin
If Canin.3 = 0 Then Gosub Send_buffer0
Mcp2515_read_register Txb1ctrl '&HA0
'Print "Senden-Ende"
'********senden mit Buffer 0****************************************
Send_buffer0:
'Priorität einstellen
Mcp2515_bitmodify Txb0ctrl , &B00000011 , &B00000011
'Standard ID einstellen
Mcp2515_write_register Txb0sidh , Idh ' Empfängeradresse setzen
Mcp2515_write_register Txb0sidl , Idl ' Empfängeradresse setzen
'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
End
Lesezeichen