한국어
Embedded
 

통신 Raspberry Pi 와 ATtiny85 간 I²C 통신

makersweb 2023.03.18 15:04 조회 수 : 587

Raspberry Pi 의 I²C 주변 장치는 기본적으로 켜져 있지 않다. raspi-config를 사용하여 활성화할 수 있다.

아래 명령을 입력했을 때,

ls /dev/*i2c*

다음과 같이 출력되어야 한다.

/dev/i2c-1

 

Utilities

I2C 인터페이스를 작동시키는 데 도움이 되는 일련의 명령줄 유틸리티 프로그램이 있다. apt 패키지 관리자를 사용하여 얻을 수 있다.

sudo apt install -y i2c-tools

특히 i2cdetect 은 버스의 모든 주소를 조사하고 장치가 있는지 여부를 표시해준다.

다음 명령줄을 입력해본다. -y 플래그는 확인을 기다릴 필요가 없도록 한다. 1은 I2C 버스 1(예: i2c-1)에서 I2C 장치를 스캔하고 있음을 나타낸다.

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 

 

I²C 통신 Example

Arduino (ATtiny85) 측 slave 예제

// Wire Slave Sender
#include <TinyWire.h>
#include <twi.h>

void setup() {
  // config TinyWire library for I2C slave functionality
  TinyWire.begin( 0x8 );
  // register a handler function in case of a request from a master
  TinyWire.onRequest( requestEvent );
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  String data = "hello makersweb.net";
  TinyWire.send(1);
  TinyWire.send(data.c_str(), data.length());
}

라이브러리: https://github.com/lucullusTheOnly/TinyWire

배선

Raspberry Pi PIN Map
GPIO 2 - I²C 핀 중 SDA(I2C1 데이터), GPIO 3 - I²C 핀 중 SCL(I2C1 클럭).

raspberrypi_pin.png

ATtiny85 PIN Map
ATtiny85 마이크로컨트롤러에서 SDA 핀은 PB0이고 SCL 핀은 PB2이다.

attiny85_pin.png

ATtiny85 (Slave) 장치와 연결하고 Raspberry Pi 에서 다시 i2cdetect 명령줄을 입력해본다.

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- 08 -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --     

 

Raspberry Pi 측 master 예제

#include "pi2c.h"

int main(){
    Pi2c arduino(8); //Create a new object "arduino" using address "0x08"
    char data[20] = {0,}; //Create a buffer of char (single bytes) for the data

    //Receive from the Arduino and put the contents into the "data" char array
    arduino.i2cRead(data, sizeof(data));
    //Print out what the Arduino is sending...
    std::cout << "Arduino Says: " << data << std::endl;

    //Send an 16 bit integer
    //arduino.i2cWriteArduinoInt(4356);

    return 0;
}

라이브러리: https://github.com/JohnnySheppard/Pi2c

실행

i2c_master.png

번호 제목 글쓴이 날짜 조회 수
52 [Uboot 명령어 및 환경 변수 요약]U-Boot에 Command 및 Parameter에 대한 설명 pjk 2014.01.09 10387
51 printk() makersweb 2014.02.27 5154
50 GNU C 레퍼런스 메뉴얼 - 부록 D secret makersweb 2014.02.28 5
49 임베디드 시스템 개발 환경 선택 makersweb 2014.03.05 3528
48 부트로더의 start.S 분석 file makersweb 2014.03.23 3614
47 시리얼 인터페이스 커넥터를 위한 핀아웃 file pjk 2014.10.10 4798
46 USB 핀아웃 file pjk 2014.10.11 8432
45 폴링(Polling), 인터럽트(Interrupt), DMA(Direct Memory Access) file pjk 2014.10.24 6012
44 USB OTG 기술의 개념 file pjk 2014.11.03 15254
43 실시간 운영 체제 또는 RTOS(Real Time Operating System) pjk 2014.12.02 5847
42 AVR(AT90USB162)을 USB to Serial 로 이용하기 file makersweb 2015.02.14 4821
41 ST, STM32 MCU용 ‘통합 개발 환경(IDE)’ 무료 제공 makersweb 2015.03.04 8859
40 JFlashARM으로 MCU에 bin(바이너리)다운로드 file makersweb 2015.06.07 4301
39 이클립스에서 IAR프로젝트 사용방법 file makersweb 2015.07.09 8779
38 AVRISP mkII 펌웨어 업그레이드 file makersweb 2015.07.22 6936
37 윈도우10에서 Prolific USB to Serial 드라이버 인식문제 file makersweb 2016.01.24 22792
36 STM32와 CAN(Controller Area Network) Loop Back file makersweb 2017.01.23 5414
35 STM32(Cortex-M3) 개발환경구축 with Eclipse file makersweb 2018.11.08 3591
34 yocto project, 라즈베리파이를 위한 Qt + 임베디드리눅스 빌드 file makersweb 2019.02.01 11018
33 욕토 프로젝트를 이용한 Qt SDK 빌드 for 라즈베리파이3 file makersweb 2019.03.19 3067