Code:
'**** Schnittstellen konfigurieren *********************************************
'SPI
'Config Portb.4 = Input 'MISO Input
'Config Portb.3 = Output 'MOSI Output
'Config Portb.5 = Output 'SCK Output
'Spcr = &B01010011 'HardwareSPI AN, SPI Int off, MSB, Master, Low, Phase 0, Clock 16MHZ/8 = 2MHz
'Spsr = &B00000000 'SPI2X=0
'In BASCOM:
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16
Spiinit
Print " "
Print "Neustart !"
Print " "
Print "Call mcpinit..."
Call Mcpinit()
Print "done!"
Data_tmp = Canread(caninte)
Print "CANINTE:" ; Data_tmp
Data_tmp = Canread(canintf)
Print "CANINTF:" ; Data_tmp
Data_tmp = Canread(canctrl)
Print "Canctrl=" ; Data_tmp
Data_tmp = Canread(cnf1)
Print "CNF1=" ; Data_tmp
Data_tmp = Canread(cnf2)
Print "CNF2=" ; Data_tmp
Data_tmp = Canread(cnf3)
Print "CNF3=" ; Data_tmp
'*******************************************************************************
'* Hauptprogramm *
'*******************************************************************************
Do
Reset Watchdog
If S1 = 0 Then
Led_rot = 1
Waitms 500
'Prescaler auf clock/1 stellen
Reset Mcp_cs 'MCP Chip select an (aktiv)
Waitus 10 'Warten bis Pegel stabil
Data_tmp = Cmd_reset
Spdr = Data_tmp '&HC0 ins SPI Data Register laden
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Waitus 10 'Warten bis Pegel stabil
Set Mcp_cs 'Chip select wieder an (inaktiv)
Led_gelb = 1
Waitms 500
Canmod Canctrl , &B00000011 , &B00000000 'Device zurück in den normalen Modus versetzen
Canmod Canctrl , &B11100000 , &B00000000 'Device zurück in den normalen Modus versetzen
Led_gelb = 0
Led_rot = 0
End If
If S2 = 0 Then
Led_rot = 1
Waitms 500
Led_rot = 0
End If
'If Can_post_holen = 1 Then
' Print "Empfangen"
' Data_tmp = Canread(canintf)
' Print "CANINTF: " ; Data_tmp
' Can_post_holen = 0
' Led_gelb = 0
'End If
Loop
'*******************************************************************************
'*******************************************************************************
Sub Mcpinit()
Local Can_tmp As Byte 'Lokale Variable
Reset Mcp_cs 'MCP Chip select an (aktiv)
Waitus 10 'Warten bis Pegel stabil
Can_tmp = Cmd_reset
Spdr = Can_tmp '&HC0 ins SPI Data Register laden
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Waitus 10 'Warten bis Pegel stabil
Set Mcp_cs 'Chip select wieder an (inaktiv)
Led_gelb = 1
Waitms 500
Canwrite Cnf1 , &B00000100 'forum &B00010011 'selbst:&B00000100
Canwrite Cnf2 , &B11100010 'forum &B10010000 'selbst:&B11100010 '
Canwrite Cnf3 , &B00000011 '&B00000010 'selbst: &B00000011 '
Canwrite Caninte , &B00000011 'Interrupt einstellen
Canwrite Rxb0ctrl , &B01100000 'Buffer 0: Empfangen aller Nachrichten
Canwrite Rxb1ctrl , &B01100000 'Buffer 1: Empfang aller Nachrichten
Canwrite Rxm0sidh , 0 'Kein Filter, Empfängeradresse Null
Canwrite Rxm0sidl , 0
Canwrite Rxm0eid8 , 0
Canwrite Rxm0eid0 , 0
Canwrite Rxm1sidh , 0
Canwrite Rxm1sidl , 0
Canwrite Rxm1eid8 , 0
Canwrite Rxm1eid0 , 0
Canwrite Bfpctrl , 0 'Deaktivieren der RXnBF Pins (High Impedance)
Canwrite Txrtsctrl , 0 'TXnRTS Bits als Inputs schalten
Canmod Canctrl , &B11100000 , &B00000000 'Device zurück in den normalen Modus versetzen
Led_gelb = 0
End Sub
Sub Canwrite(reg_add , Reg_val)
Local Can_tmp2 As Byte
Reset Mcp_cs 'MCP Chip select an (aktiv)
Waitus 10
Can_tmp2 = Cmd_write
' In Bascom:
' Spiout Can_tmp2 , 1
' Spiout Reg_add , 1
' Spiout Reg_val , 1
Spdr = Can_tmp2 '&H02 ins SPI Data Register laden
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Spdr = Reg_add
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Spdr = Reg_val
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Waitus 10
Set Mcp_cs 'Chip select wieder an (inaktiv)
End Sub
Sub Canmod(reg_add , Reg_mask , Reg_val )
Local Can_tmp2 As Byte
Reset Mcp_cs
Waitus 10
Can_tmp2 = Cmd_bitmodify '&H05
' In Bascom:
' Spiout Can_tmp2 , 1
' Spiout Reg_add , 1
' Spiout Reg_mask , 1
' Spiout Reg_val , 1
Spdr = Can_tmp2 'ins SPI Data Register laden
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Spdr = Reg_add
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Spdr = Reg_mask
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Spdr = Reg_val
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Waitus 10
Set Mcp_cs
End Sub
Function Canread(reg_add )
Local Can_tmp2 As Byte
Reset Mcp_cs
Waitus 10
Can_tmp2 = Cmd_read '&H03
' In Bascom:
' Spiout Can_tmp2 , 1
' Spiout Reg_add , 1
' Spiin canread , 1
Spdr = Can_tmp2 'ins SPI Data Register laden
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Spdr = Reg_add
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Canread = Spdr 'Datenübernahme
Spdr = 0
Do 'Warte bis
Loop Until Spsr.spif = 1 'SPI Interrupt cleared (Transfer abgeschlossen)
Waitus 10
Set Mcp_cs
End Function
End
'*** ISR ***********************************************************************
Int_mcp: 'ISR für Empfang
Led_gelb = 1
Can_post_holen = 1
Return
Bit 7 in CNF2 habe ich geändert, ich hatte das so verstanden, das bei Null die Länge von PS2 > PS1 und IPT ist. Aber naja, ist halt mies beschrieben
Lesezeichen