首页 > 编程知识 正文

树莓派arduino通信,树莓派nrf24l01通信

时间:2023-05-06 02:09:43 阅读:172573 作者:4649

thispostwalksthroughsettingupacancontrollerontheraspberrypi.mygoalistohelpdemystifytheprocess, andprovidesimpleinstructionsthatarelativebeginnershouldbeabletofollow.particularly,includinginformationthatiwishwasmoresmorerereareare

bytheendofthistutorialyoushouldhaveaworkingcanconnectedtotheraspberrypiviathespibus.usingsocketcanitwilbepossible tosendandandresible linuxipstack.thismeansyoucanalysecanpacketsjustasyouwouldwithtcp/IP,usingstandardutilitiesliketcpdumpandwireshark.add usingutilitieslikecandump/cansendprovidedbycan-utils,youcanremoveanextralayerofabstraction,andfocuspurelyonthecandatalecandatals

Getting the Hardware

youhaveafairamountofchoiceswhenitcomestohardware.themostdiscussedandmostpopularmodulesusethemcp 2515 can-SPI chip,soI'llFCP

Some options are:

-picanbySKyydz32.28Incvat(~$46 ) -链接

- can-bussheldbysparkfun---- $ 25-link

- ebay-less than $2(search for ' MCP 2515 module ' ) )。

ichosetogowiththecheapebaymodule (pictured above )、 notthattheotheralternativesweren ' t great.thesparkfunshieldlookedveryinterestingwithamicrosdslotfordatalogging,and hook up fup butsinceiwaslearninganddidn ' twanttoendupbrickinganicepieceofhardwarejustyet,I went the cheaper route。

youshouldbeabletofollowthisguidefortheothermodulesaswell,you ' lljusthavetodeterminethewiringbylookingattheschematicsforyour

ifoundsomeschematicsfortheebaymodule-pdf

Note,theebaymoduleusesthetja 1050 can transceiver,whiletheothersusethemcp 2551.notsureifitmakesmuchofadifference,butthoughoughetthorence

Wiring it Up

thisshouldapplytoallthemodulesimentioned,thoughthelabelingmaybeabitdifferent.pinout.xyzhasasupercoolinteractiveractiverpipinout

RPi Pin RPi Label CAN Module

02------ 5v-----VCC

06------ gnd-----gnd

19--------gpio10----mosi () ) ) )。

21------ gpio9---

-----MISO (SO)

22---------GPIO25--------INT

23---------GPIO11--------SCK

24---------GPIO8---------CS

Enabling the SPI Bus and Loading the Kernel Modules

Edit /boot/config.txt and add the following lines, setting the oscillator parameter to that of the oscillator present on the module. Most of the modules use a 16MHz oscillator for the clock, though the Ebay module uses 8MHz oscillator=8000000. You can determine the frequency by looking at the chip.

Reboot.

If everything is hooked up and working the can0 interface should be visible with ifconfig can.

pi@raspberrypi:~ $ ifconfig can

can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00

NOARP MTU:16 Metric:1

collisions:0 txqueuelen:10

The MCP2515 device tree overlay parameters are described in /boot/overlays/README.

Name: mcp2515-can0

Info: Configures the MCP2515 CAN controller on spi0.0

Load: dtoverlay=mcp2515-can0,=

Params: oscillator Clock frequency for the CAN controller (Hz)

spimaxfrequency Maximum SPI frequence (Hz)

interrupt GPIO for interrupt signal

Compiling Can-utils

You may need to install some prerequisites:

sudo apt-get install git autoconf libtool

Run the following commands to compile can-utils:

git clone https://github.com/linux-can/can-utils.git

cd can-utils

./autogen.sh

./configure

make

sudo make install

You may also install via the repository:

sudo apt-get install can-utils

Bring Up the Interface

You can bring up the interface with:

sudo ip link set can0 up type can bitrate 50000

You'll need to select the proper bitrate for the network you're connecting to. Common bitrates are: 10000, 20000, 50000, 100000, 125000, 250000, 500000, 800000, 1000000.

If you need to change a setting, first bring down the interface:

sudo ip link set can0 down

You can see a list of available settings by appending help to the command:

pi@raspberrypi:~ $ ip link set can0 up type can bitrate 50000 help

Usage: ip link set DEVICE type can

phase-seg2 PHASE-SEG2 [ sjw SJW ] ]

dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]

Where: BITRATE := { 1..1000000 }

SAMPLE-POINT := { 0.000..0.999 }

TQ := { NUMBER }

PROP-SEG := { 1..8 }

PHASE-SEG1 := { 1..8 }

PHASE-SEG2 := { 1..8 }

SJW := { 1..4 }

RESTART-MS := { 0 | NUMBER }

The loopback setting enables loopback mode on the MCP2515, which can be useful for verifying that the RPi is communicating with the CAN module without having to connect to an actual CAN bus. All messages transmitted in loopback mode will be echoed back over the interface.

Listen-only can be useful if you're interested in sniffing CAN traffic, and want to lessen the risk of messing something up by ensuring the device doesn't send any packets.

Send and Receive CAN Messages

Using cansend/candump it's possible to respectively send and receive CAN messages.

Send

cansend can0 001#1122334455667788

And receive:

candump can0

On some cars you may not see any traffic through the CAN-bus on the OBD2 port. This is due to the fact that there is a 'gateway' present between the OBD2 port and the various CAN networks. The gateway acts like a firewall between the networks, and it's necessary to send a request for data before it can be received.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。