hier mal ein Beispielcode von mir:
Code:
$regfile = "m16def.dat" ' ATMega16
$crystal = 14745600 ' Baudratenquarz
$hwstack = 40 ' default use 32 for the hardware stack
$swstack = 80 ' default use 10 for the SW stack
$framesize = 90
Declare Sub Can_init()
Declare Sub Mcp2515_write(byval Reg_add As Byte , Byval Reg_val As Byte)
Declare Sub Mcp2515_bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Declare Function Mcp2515_read(byval Reg_add As Byte) As Byte
Mcp2515cs Alias Portb.4
Config Mcp2515cs = Output
Const Cmd_read = &H03 ' Read Command
Const Cmd_write = &H02 ' Write Command
Const Cmd_bitmodify = &H05 ' Bit-modify Command
Const Cmd_readstatus = &HA0 ' Read Status Command (poll)
Const Cmd_read_rx_status = &HB0
Const Cmd_reset = &HC0 ' Reset Command
Const Cmd_rts0 = &H81
Const Cmd_rts1 = &H82
Const Caninte = &H2B
Const Canctrl = &H0F
Const Canstat = &H0E ' Statusregister
Const Eflg = &H2D ' Error Flag Register
Const Cnf3 = &H28 ' Configuration Register 3
Const Cnf2 = &H29 ' Configuration Register 2
Const Cnf1 = &H2A ' Configuration Register 1
Const Txb0ctrl = &B00110000 ' Transmit Buffer 0 Control Register
Const Txb0sidh = &B00110001 ' Transmit Buffer 0 Std Identifier High
Const Txb0sidl = &B00110010 ' Transmit Buffer 0 Std Identifier Low
Const Txb0eid8 = &B00110011 ' Transmit Buffer 0 Ext Identifier High
Const Txb0eid0 = &B00110100 ' Transmit Buffer 0 Ext Identifier Low
Const Txb0dlc = &B00110101 ' Transmit Buffer 0 Data Length Code
Const Txb0d0 = &B00110110 ' Transmit Buffer 0 Data Byte 0
Const Txb0d1 = &B00110111 ' Transmit Buffer 0 Data Byte 1
Const Txb0d2 = &B00111000 ' Transmit Buffer 0 Data Byte 2
Const Txb0d3 = &B00111001 ' Transmit Buffer 0 Data Byte 3
Const Txb0d4 = &B00111010 ' Transmit Buffer 0 Data Byte 4
Const Txb0d5 = &B00111011 ' Transmit Buffer 0 Data Byte 5
Const Txb0d6 = &B00111100 ' Transmit Buffer 0 Data Byte 6
Const Txb0d7 = &B00111101 ' Transmit Buffer 0 Data Byte 7
Const Rxm0sidh = &B00100000 ' Receive Buffer 0 Std Identifier High
Const Rxm0sidl = &B00100001 ' Receive Buffer 0 Std Identifier Low
Const Rxm0eid8 = &B00100010 ' Receive Buffer 0 Ext Identifier High
Const Rxm0eid0 = &B00100011 ' Receive Buffer 0 Ext Identifier low
Const Rxm1sidh = &B00100100 ' Receive Buffer 1 Std Identifier High
Const Rxm1sidl = &B00100101 ' Receive Buffer 1 Std Identifier Low
Const Rxm1eid8 = &B00100110 ' Receive Buffer 1 Ext Identifier High
Const Rxm1eid0 = &B00100111 ' Receive Buffer 1 Ext Identifier low
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
' Hardware SPI-Schnittstelle konfigurieren
Config Portb.6 = Input
Config Portb.5 = Output
Config Portb.7 = Output
Spcr = &B01010010
Config Pind.2 = Input
Config Int0 = Falling
On Int0 Int_canint
Enable Int0
Enable Interrupts
Call Can_init()
Do
' Zu übertragende Daten setzen:
Mcp2515_bitmodify Txb0ctrl , &B00000011 , &B00000011 ' TX-Konfiguration
Mcp2515_write Txb0sidh , &B01010101 ' Empfängeradresse setzen
Mcp2515_write Txb0sidl , &B01010101 ' Empfängeradresse setzen
Mcp2515_write Txb0dlc , &H04 ' Paketlänge festlegen
Mcp2515_write Txb0d0 , &B01010101 ' Zu übertragende Daten setzen
Mcp2515_write Txb0d1 , &B01010101 ' Zu übertragende Daten setzen
' Übertragung auslösen
Reset Mcp2515cs
Waitus 10
Spdr = Cmd_rts0
Do
Loop Until Spsr.spif = 1
Waitus 10
Set Mcp2515cs
Loop
Sub Can_init()
Local Can_tmp As Byte
Reset Mcp2515cs
' Reset MCP2515
Can_tmp = Cmd_reset
Spdr = Can_tmp
Do
Loop Until Spsr.spif = 1
Set Mcp2515cs
Waitms 2000
Mcp2515_write Canctrl , &B10001000 ' low nibble: 3=OSM 2=CLK-Pin 1-0=CLK Divider
Print "speed change done"
' F MCP2515 = 16MHz
Can_tmp = Eram_param_baudrate
Select Case Can_tmp
Case 0:
' CAN 31,25 KHz
Mcp2515_write Cnf1 , &B00001111
Case 1:
' CAN 62,5 KHz
Mcp2515_write Cnf1 , &B00000111
Case 2:
' CAN 125 KHz
Mcp2515_write Cnf1 , &B00000011
Case 3:
' CAN 250 KHz
Mcp2515_write Cnf1 , &B00000001
Case 4:
' CAN 500 KHz
Mcp2515_write Cnf1 , &B00000000
Case Else :
Mcp2515_write Cnf1 , &B00001111
End Select
Mcp2515_write Cnf2 , &B10010000 ' Cnf2 = &H29
Mcp2515_write Cnf3 , &B00000010 ' Cnf3 = &H28
Mcp2515_write Caninte , &B00000011
Mcp2515_write Rxb0ctrl , &B01100000
Mcp2515_write Rxb1ctrl , &B01100000
Mcp2515_write Rxm0sidh , 0 ' Empfängeradressen auf 0, Filter aus
Mcp2515_write Rxm0sidl , 0
Mcp2515_write Rxm0eid8 , 0
Mcp2515_write Rxm0eid0 , 0
Mcp2515_write Rxm1sidh , 0
Mcp2515_write Rxm1sidl , 0
Mcp2515_write Rxm1eid8 , 0
Mcp2515_write Rxm1eid0 , 0
Mcp2515_bitmodify Canctrl , &B11100000 , &B00000000
'Mcp2515_write Canctrl , &B00000000 ' low nibble: 3=OSM 2=CLK-Pin 1-0=CLK Divider
'Mcp2515_write Canctrl , &HE0
End Sub
Sub Mcp2515_bitmodify(reg_add , Reg_mask , Reg_val)
Local Can_tmp2 As Byte
Mcp2515cs = 0
Can_tmp2 = Cmd_bitmodify
Spdr = 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
Mcp2515cs = 1
End Sub
Sub Mcp2515_write(reg_add , Reg_val)
Local Can_tmp2 As Byte
Mcp2515cs = 0
Can_tmp2 = Cmd_write
Spdr = Can_tmp2
Do
Loop Until Spsr.spif = 1
Waitms 2
Spdr = Reg_add
Do
Loop Until Spsr.spif = 1
Spdr = Reg_val
Do
Loop Until Spsr.spif = 1
Mcp2515cs = 1
End Sub
Function Mcp2515_read(reg_add)
Local Can_tmp2 As Byte
Mcp2515cs = 0
Can_tmp2 = Cmd_read
Spdr = Can_tmp2
Do
Loop Until Spsr.spif = 1
Spdr = Reg_add
Do
Loop Until Spsr.spif = 1
Spdr = 0
Do
Loop Until Spsr.spif = 1
Mcp2515_read = Spdr
Mcp2515cs = 1
End Function
Lesezeichen