Code:
'Mikrocontroller
$regfile = "m32def.dat"
$crystal = 16000000
'Stacks
$hwstack = 200
$swstack = 200
$framesize = 400
'UART konfigurieren
$baud = 19200
'SPI konfigurieren
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1 , Clockrate = 16
Spiinit
'Unterprogramme
Declare Sub Read_register(byval Adress As Byte )
Declare Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Declare Sub Bitmodify(byval Reg_add As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
Declare Sub Write_tx
Declare Sub Register
Declare Sub Mcp2515_init
Declare Sub Send_can
'Befehle
Const Spi_read = &H03
Const Spi_write = &H02
Const Spi_reset = &HC0
Const Spi_rts0 = &H81
Const Spi_bitmodify = &H05
'Registeradressen
Const Cnf1 = &H2A
Const Cnf2 = &H29
Const Cnf3 = &H28
Const Canctrl = &H0F
Const Caninte = &H2B
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 Txb0eid8 = &H33 'Transmit Buffer 0 Ext Identifier High
Const Txb0eid0 = &H34 'Transmit Buffer 0 Ext 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 Rxm0sidh = &H20 'Receive Buffer 0 Std Identifier High
Const Rxm0sidl = &H21 'Receive Buffer 0 Std Identifier Low
Const Rxm0eid8 = &H22 'Receive Buffer 0 Ext Identifier High
Const Rxm0eid0 = &H23 'Receive Buffer 0 Ext Identifier low
Const Rxm1sidh = &BH24 'Receive Buffer 1 Std Identifier High
Const Rxm1sidl = &H25 'Receive Buffer 1 Std Identifier Low
Const Rxm1eid8 = &H26 'Receive Buffer 1 Ext Identifier High
Const Rxm1eid0 = &H27 'Receive Buffer 1 Ext Identifier low
Const Rxb0ctrl = &H60
Const Rxb1ctrl = &H70
'I/O Einstellungen
Config Portb.4 = Output
'Namen vergeben
Cs Alias Portb.4
'Variablen
Dim Canin As Byte
'Interrupts aktivieren
Enable Interrupts
'Chipselect auf High setzen
Set Cs
Wait 1
'MCP2515 initialisieren
Call Mcp2515_init
'Hauptprogramm
Do
Call Write_tx 'Zu übertragende Daten setzen
Call Send_can 'Senden auslösen
Call Register
'Register auslesen
Waitms 100
Loop
End
'Unterprogramme
'-------------------------------------------------------------------------------------------------------------------
Sub Read_register(byval Adress As Byte )
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_read 'Inhalt von "SPI_Read" im Datenregister speichern 'Read Instruction (0000 0011) senden
Spiout Spdr , 1
Spiout Adress , 1
Spiin Canin , 1 'Registerinhalt einlesen
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Write_register(byval Adress As Byte , Byval Daten As Byte)
Reset Cs 'Chipselect auf Low ziehen
Spdr = Spi_write 'Inhalt von "SPI-Write" ins SPI_Data_Register schieben
Do 'Schleife bis SPI Interrupt Flag gesetzt wurde, also bis
Loop Until Spsr.spif = 1 'die Übertragung abgeschlossen ist
Spdr = Adress
Do
Loop Until Spsr.spif = 1
Spdr = Daten
Do
Loop Until Spsr.spif = 1
Set Cs 'Chipselect auf High ziehen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Mcp2515_init
Reset Cs
'MCP2515 reseten
Spdr = Spi_reset
Do
Loop Until Spsr.spif = 1
Waitms 10
Set Cs '10ms warten, da sonst das Auslesen von CNF1
'nicht richtig erfolgen kann
'Control Register beschreiben
Write_register Canctrl , &H8F 'Device in Configuration Mode versetzen, OSC-Pin
'aktivieren, Fclk = F / 8 einstellen
'Interrupts einstellen
Write_register Caninte , &H04 'Transmit Buffer 0 Interrupt aktivieren
'TX-Konfiguration
Bitmodify Txb0ctrl , &H03 , &H03
'Bittiming einstellen
Write_register Cnf1 , &H03 'Baudrate einstellen
Write_register Cnf2 , &HA0 'Phasensegmenteinstellungen
Write_register Cnf3 , &H02
'Rx Buffer einstellen
Write_register Rxb0ctrl , &H60
Write_register Rxb1ctrl , &H60
Write_register Rxm0sidh , 0 ' Empfängeradressen auf 0, Filter aus
Write_register Rxm0sidl , 0
Write_register Rxm0eid8 , 0
Write_register Rxm0eid0 , 0
Write_register Rxm1sidh , 0
Write_register Rxm1sidl , 0
Write_register Rxm1eid8 , 0
Write_register Rxm1eid0 , 0
'CAN-Controller in den "Normal Mode" versetzen
Bitmodify Canctrl , &HE0 , &H0
'Priorität einstellen
Write_register Txb0ctrl , &H03 'Highest Message Priority für Transmit Buffer 0
'Message Identifier einstellen
Write_register Txb0sidh , &H00
Write_register Txb0sidl , &H03
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Bitmodify(reg_add , Reg_mask , Reg_val)
Reset Cs
Spdr = Spi_bitmodify
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 Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Send_can
Reset Cs
Spdr = Spi_rts0 'Übertragung auslösen
Do
Loop Until Spsr.spif = 1
Set Cs
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Register
'Terminalausgabe
Print "Registereinstellungen:"
Read_register Cnf1
Print "Registerinhalt CNF1: " ; Canin
Read_register Cnf2
Print "Registerinhalt CNF2: " ; Canin
Read_register Cnf3
Print "Registerinhalt CNF3: " ; Canin
Read_register Canctrl
Print "Registerinhalt CAN Control: " ; Canin
Read_register Txb0ctrl
Print "Registerinhalt TXB0 Control: " ; Canin
Read_register Txb0sidl
Print "Registerinhalt TXB0 Identifier Low: " ; Canin
Read_register Txb0sidh
Print "Registerinhalt TXB0 Identifier High: " ; Canin
Read_register Txb0dlc
Print "Registerinhalt TXB0 Data Lenght: " ; Canin
Read_register Caninte
Print "Registerinhalt CAN Interrupt Enable: " ; Canin
Print "----------------------------------------------------------"
End Sub
'-------------------------------------------------------------------------------------------------------------------
Sub Write_tx
Write_register Txb0sidh , &H55 'Empfängeradresse setzen
Write_register Txb0sidl , &H55 'Empfängeradresse setzen
Write_register Txb0dlc , &H04 'Paketlänge festlegen
Write_register Txb0d0 , &HFF 'Zu übertragende Daten setzen
Write_register Txb0d1 , &HFF 'Zu übertragende Daten setzen
End Sub
'-------------------------------------------------------------------------------------------------------------------
Und hier nochmal eins in C für Cypress Chips:
Code:
//----------------------------------------------------------------------------
// C main line
//----------------------------------------------------------------------------
#include <m8c.h> // Part specific Constants and Macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
char Button = 0x00;
void I2C_Init(void);
void main(void)
{
M8C_EnableGInt;
CSD_Start();
CSD_InitializeBaselines();
CSD_SetDefaultFingerThresholds();
EzI2Cs_SetRamBuffer(1, 1, (char *)&Button);
I2C_Init();
while (1)
{
CSD_ScanAllSensors();
CSD_UpdateAllBaselines();
// Sensor 1
if(CSD_bIsSensorActive(0))
{
PRT2DR |= 0x01;
Button = 0x01;
}
else
{
PRT2DR &= ~0x01;
}
// Sensor 2
if(CSD_bIsSensorActive(1))
{
PRT2DR |= 0x02;
Button = 0x02;
}
else
{
PRT2DR &= ~0x02;
}
// Sensor 3
if(CSD_bIsSensorActive(2))
{
PRT2DR |= 0x04;
Button = 0x03;
}
else
{
PRT2DR &= ~0x04;
}
// Sensor 4
if(CSD_bIsSensorActive(3))
{
PRT2DR |= 0x08;
Button = 0x04;
}
else
{
PRT2DR &= ~0x08;
}
}
}
void I2C_Init(void)
{
EzI2Cs_Start(); // I²C Modul starten
EzI2Cs_EnableInt(); // I²C Interrupts aktivieren
}
Eine Befehlsliste für z.B. den Asuro hast du hier:
Lesezeichen