博客專欄

        EEPW首頁 > 博客 > Controlling an SPI device with the Raspberry Pi

        Controlling an SPI device with the Raspberry Pi

        發布人:電子禪石 時間:2019-11-26 來源:工程師 發布文章

        The Raspberry Pi has a Broadcom BCM 2835 chip allowing it to interface with SPI devices on its GPIO pins. There are two chip select pins meaning that the Pi can control two devices simultaneously.



        19MOSI – master output slave input
        21MISO – master input slave output
        23SCLK – clock
        24CE0 – chip enable 0
        26CE1 – chip enable 1

        Step 1: Enable SPI on the Raspberry Pi

        1. In your Pi’s terminal, run

          sudo raspi-config
        2. Go to Advanced Options > SPI

        3. Choose “Yes” for both questions then select Finish to exit raspi-config

        4. Either reboot your Pi or run this command to load the kernel module

          sudo modprobe spi-bcm2708

        Step 2: Install spidev

        Spidev is a python module that allows us to interface with the Pi’s SPI bus.Watch movie online The Transporter Refueled (2015)

        sudo apt-get update
        sudo apt-get upgrade
        sudo apt-get install python-dev python3-dev
        cd ~git clone https://github.com/doceme/py-spidev.gitcd py-spidev
        make
        sudo make install

        Step 3: Python script

        Finally, we can write and run a python script to control the SPI device.

        1. Create a file called spi-test.py in your favorite editor

          #!/usr/bin/pythonimport spidevimport time
          
          spi = spidev.SpiDev()spi.open(0, 0)spi.max_speed_hz = 7629# Split an integer input into a two byte array to send via SPIdef write_pot(input):
              msb = input >> 8
              lsb = input & 0xFF
              spi.xfer([msb, lsb])# Repeatedly switch a MCP4151 digital pot off then onwhile True:
              write_pot(0x1FF)
              time.sleep(0.5)
              write_pot(0x00)
              time.sleep(0.5)
        2. Make the file executable and run it

          chmod +x spi-test.py
          sudo ./spi-test.py

        Notes on spidev

        Unless the spi.max_speed_hz field is a value accepted by the driver, the script will fail when you run it. The field can be set to these values on the raspberry pi:



        125.0 MHz125000000
        62.5 MHz62500000
        31.2 MHz31200000
        15.6 MHz15600000
        7.8 MHz7800000
        3.9 MHz3900000
        1953 kHz1953000
        976 kHz976000
        488 kHz488000
        244 kHz244000
        122 kHz122000
        61 kHz61000
        30.5 kHz30500
        15.2 kHz15200
        7629 Hz7629

        Two SPI devices can be controlled in python by creating two SpiDev objects, one for each device.

        spi = spidev.SpiDev()
        spi.open(0, 0)
        spi.max_speed_hz = 976000
        spi2 = spidev.SpiDev()
        spi2.open(0, 1)
        spi2.max_speed_hz = 976000

        https://www.takaitra.com/spi-device-raspberry-pi/

        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞:

        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 星子县| 乌鲁木齐县| 凤城市| 黔东| 长寿区| 双城市| 寻乌县| 三都| 通城县| 盐池县| 富阳市| 涟水县| 文登市| 五指山市| 广河县| 姚安县| 桂平市| 新野县| 怀宁县| 阿拉善盟| 乳源| 盈江县| 杨浦区| 阜城县| 黄平县| 梨树县| 日喀则市| 华宁县| 湖州市| 满城县| 湛江市| 天祝| 郎溪县| 陆川县| 都安| 泰来县| 铜山县| 丰城市| 莱西市| 全州县| 汕尾市|