Sodele, also schreiben klappt jetzt ganz hervorragend, ich habe den code erstmal auf das nötigste reduziert. Nachdem ich nun den Prescaler geändert habe, wollte ich das Register CANCTRL auslesen - das klappt irgendwie nicht. Muss ich dann noch irgendwie Nullen senden ? In der Help von SPIIN steht irgendwie so etwas, aber das verstehe ich nun überhaupt nicht. Jedenfalls bleibt die MISO Leitung high. Wenn ich da nochmal um einen Tip bitten dürfte ?
Code:'******************************************************************************* '* '* '* Projekt: CAN Testboard '* Zielhardware: CAN Testboard V1.0 '* Stand: 03.03.2008 '* '* CAN Test10.bas '* '******************************************************************************* 'Inspirationsliste ' ' '**** Deklarationen ************************************************************ $hwstack = 32 ' default use 32 for the hardware stack $swstack = 8 'default use 10 for the SW stack $framesize = 24 'default use 40 for the frame space $regfile = "m8def.dat" 'Chipdefinition $crystal = 16000000 'Ext. crystal Osc. Speed 16Mhz $baud = 2400 'Speed COM1 Declare Sub Mcpinit() Declare Sub Canwrite(byval Reg_add As Byte , Byval Reg_val As Byte) Declare Sub Canmod(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte) Declare Function Canread(byval Reg_add As Byte ) As Byte '**** Portinitialisierung ****************************************************** Portb = &H00 'Port B hochohmig schalten Portc = &H00 'Port C hochohmig schalten Portd = &H00 'Port D hochohmig schalten Config Portb.0 = Output 'LED gelb Led_gelb Alias Portb.0 Led_gelb = 0 Config Portb.1 = Output 'LED rot Led_rot Alias Portb.1 Led_rot = 0 Config Portb.2 = Input '/SS Eingang Config Portc = Output 'Aux Config Pind.2 = Input 'MCP Interrupt MCP2515 /INT Config Portd.4 = Output 'MCP_CS Mcp_cs Alias Portd.4 Set Mcp_cs 'low aktiv Config Portd.6 = Input 'Taster S1 Portd.6 = 1 'Pullup für S1 S1 Alias Pind.6 Config Portd.7 = Input 'Taster S2 Portd.7 = 1 'Pullup für S2 S2 Alias Pind.7 Led_rot = 1 'Resetmerker und Zeit zum ISP Stecker abziehen ;) Wait 3 Led_rot = 0 '**** Variablen deklarieren **************************************************** Const Cmd_read = &H03 'Read Command Const Cmd_write = &H02 'Write Command Const Cmd_bitmodify = &H05 'Bit-modify Command Const Cmd_reset = &HC0 'Reset Command Const Canctrl = &H0F 'Control Register Dim Data_tmp As Byte 'Temp Datenbyte Dim Canstatus As Byte 'Statuswort Dim Spi_write_buff(3) As Byte 'Sendebuffer Dim Spi_mod_buff(4) As Byte 'Modbuffer Dim Spi_read_buff(2) As Byte 'empfangsbuffer '**** Schnittstellen konfigurieren ********************************************* 'SPI Config Portb.3 = Output 'MOSI Config Portb.4 = Input 'MISO Config Portb.5 = Output 'SCK Config Portb.2 = Output '/SS - /CS Config Spi = Hard , Data Order = Msb , Master = Yes , Noss = 0 , Clockrate = 4 Spiinit '******************************************************************************* Print " " Print "Neustart !" Print " " Print "Call mcpinit..." Call Mcpinit() Print "MCP init done!" Data_tmp = Canread(canctrl) Print "Canctrl=" ; Bin(data_tmp) '******************************************************************************* '* Hauptprogramm * '******************************************************************************* Do If S1 = 0 Then Led_rot = 1 Waitms 500 Led_rot = 0 End If If S2 = 0 Then Led_rot = 1 Waitms 500 Led_rot = 0 End If Loop '******************************************************************************* Sub Mcpinit() Local Can_tmp As Byte 'Lokale Variable Print "Reset 2515..." 'MCP Resetten Can_tmp = Cmd_reset Reset Mcp_cs Waitus 1 Spiout Data_tmp , 1 Waitus 1 Set Mcp_cs Waitus 20 'Warten bis Reset erledigt, evtl. Abfrage MCP einbauen ? Print "done!" Print "change prescaler..." Spi_write_buff(1) = Cmd_write Spi_write_buff(2) = Canctrl Spi_write_buff(3) = &B00000100 'Prescaler auf Takt/1 Reset Mcp_cs Waitus 1 Spiout Spi_write_buff(1) , 3 Waitus 1 Set Mcp_cs Print "done!" End Sub Sub Canwrite(reg_add , Reg_val) 'Subroutine "schreiben" Spi_write_buff(1) = Cmd_write Spi_write_buff(2) = Reg_add Spi_write_buff(3) = Reg_val Spiout Spi_write_buff(1) , 3 End Sub Sub Canmod(reg_add , Reg_mask , Reg_val) Spi_mod_buff(1) = Cmd_bitmodify Spi_mod_buff(2) = Reg_add Spi_mod_buff(3) = Reg_mask Spi_mod_buff(4) = Reg_val Spiout Spi_mod_buff(1) , 4 End Sub Function Canread(reg_add) Spi_read_buff(1) = Cmd_read Spi_read_buff(2) = Reg_add Spiout Spi_read_buff(1) , 2 Spiin Canread , 1 End Function End







Zitieren

Lesezeichen