spi 통신 > 라즈베리파이

본문 바로가기

회원로그인

회원가입

라즈베리파이

spi 통신

페이지 정보

profile_image
작성자 최고관리자
댓글 0건 조회 76회 작성일 20-09-28 09:36

본문

>raspi-config

5. Interfacing Options -

P4 SPI 와 P5 I2C 롤 활성화 시킨다.


>vi /etc/modprobe.d/raspi-blacklist.conf

# blacklist spi and i2c by default (many users don't need them)


#blacklist spi-bcm2708
#blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804


> vi /etc/modules

snd-bcm2835
i2c-dev
i2c-bcm2708
spidev
spi-bcm2708


> reboot



* SPI 활성화 확인

1.

> cd /etc/modules

> vi modules.conf

i2c-dev

spi-dev

확인


2. * 두 채널을 위한 디바이스파일
> ls /dev/spi*

/dev/spidev0.0 

/dev/spidev0.1

확인


3. lsmod | grep spi

spidev ~~

spi_bcm* ~~


모듈 확인

> modinfo spi_bcm2835


4.

$ cat /boot/config.txt

dtparam=spi=on


=========================================



> sudo apt-get install python-dev

> sudo apt-get install python3-dev


> cd /적당한 위치

> sudo apt-get install git

> git clone git://github.com/doceme/py-spidev

> cd py_spidev

> sudo python setup.py install


> gpio load spi

> mkdir python-spi

> cd python-spi


> wget https://raw.github.com/doceme/py-spidev/master/setup.py
> wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c
> sudo python setup.py install



파이썬 코드

===============================================================================
import
spidev

spi = spidev.SpiDev()
spi.open(0, 0) # open(bus, device)

# SPI 버스 0와 디바이스 0를 열고,
# 동작 모드는 3으로 최대 속도는 1MHz로 설정하고
# 0x00, 0x01, 0x02 값을 보냄
spi.mode = 3
spi.max_speed_hz = 1000000 # set transfer speed
spi_result = spi.xfer2([0x00, 0x01, 0x02])

print(spi_result)

spi.close()


===============================================================================

출처 : https://kalten.tistory.com/46

import spidev
import time
import sys, os

spi_0 = spidev.SpiDev()
spi_0.open(0, 0)

#this fucntion can be used to find out the ADC value on ADC 0
def readadc_0(adcnum_0):
    if adcnum_0 > 7 or adcnum_0 < 0:
        return -1

    r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0])
    adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]

    return adcout_0


#first ADC setup on SPI port 1
spi_1 = spidev.SpiDev()
spi_1.open(0, 1)

#this fucntion can be used to find out the ADC value on ADC 1
def readadc_1(adcnum_1):
    if adcnum_1 > 7 or adcnum_1 < 0:
        return -1

    r_1 = spi_1.xfer2([1, 8 + adcnum_1 << 4, 0])
    adcout_1 = ((r_1[1] & 3) << 8) + r_1[2]

    return adcout_1


while 1:
    for x in range (0, 1):    #0은 아니라고 1부터 실행인듯
        print ('Port ' + str(x) + ' | '  + 'adc_0: ' + str(readadc_0(x)).zfill(4) + ' | ')
    print ('----------------------------------')

    time.sleep(1)

===============================================================================

댓글목록

등록된 댓글이 없습니다.