2017년 8월 22일 화요일

[4] 각 하드웨어를 연결할 수 있는 모듈 만들기(2) – SPI 통신 선택 방법

[4] 각 하드웨어를 연결할 수 있는 모듈 만들기(2) – SPI 통신 선택 방법
(4.1) SPI 테스트
1)wiringPi 설치하기

2)spi 코드 작성
 1  #include <stdio.h>
 2  #include <string.h>
 3  #include <errno.h>
 4  #include <wiringPi.h>
 5  #include <wiringPiSPI.h>
 6
 7  #define SPI_CHANNEL 0
 8  #define SPI_SPEED 10
 9
10  int main(void)
11  {
12     if(wiringPiSetup() == -1)
13      {
14         fprintf (stdout, "Unable to start wiringPi: %s\n", strerror(errno));
15         return 1 ;
16       }
17
18     if(wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1)
19     {
20         fprintf (stdout, "wiringPiSPISetup Failed: %s\n", strerror(errno));
21         return 1 ;
22     }
23
24    while(1)
25     {
26         char data = 0x65;
27         wiringPiSPIDataRW(SPI_CHANNEL, &data, 1);
28     }
29
30     return 0;
31   }

<출처 - file:///home/an/Downloads/%EB%9D%BC%EC%A6%88%EB%B2%A0%EB%A6%AC+%EC%84%B8%EB%AF%B8%EB%82%98+7%ED%9A%8C.pdf>
3)라즈베리파이 spi 통신 설정

4)SPI 파형 OSC로 확인
(SPI 최고 속도가 필요하다.)
3.png
BCM2835 ARM Peripherals –

The CDIV (Clock Divider) field of the CLK register sets the SPI clock speed
2.png
Cortex -A7 MPCore -
(4.2) 라즈베리 파이와 아두이노의 SPI 통신 테스트]
라즈베리파이가 slave에 데이터를 잘 전달하는지 확인해보았다.
20160903_052548.jpgKqnyD.jpg
라즈베리파이 아두이노
SCK(23)                       SCK(52)
MOSI(19) MOSI(51)
GND   GND
  1. 라즈베리파이 코드
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <errno.h>
 4 #include <wiringPi.h>
 5 #include <wiringPiSPI.h>
 6
 7 #define SPI_CHANNEL 0
 8 #define SPI_SPEED 10000
 9 #define SIZE 12
10
11
12
13 int main(void)
14 {
15
16     char buf[SIZE] = "RaspberryPi";
17     int i = 0;
18
19     if(wiringPiSetup() == -1)
20     {
21         fprintf(stdout,"Unable to start wiringPi: %s\n", strerror(errno));
22         return 1;
23     }
24
25     if(wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1)
26     {
27         fprintf(stdout, "wiringPiSetup Failed: %s\n", strerror(errno));
28         return 1;
29     }
30
31     while(1)
32     {
33         char data = buf[i];
34         wiringPiSPIDataRW(SPI_CHANNEL, &data, 1);
35
36         delay(100);
37
38         i++;
39
40         if(i==SIZE-1)
41             i=0;
42     }
43
44     return 0;


  1. 아두이노 코드
#include <SPI.h>

char buf = 'z';
volatile bool process_it;
void setup (void)
{
 Serial.begin (115200);   // debugging

 // turn on SPI in slave mode
 SPCR |= bit (SPE);

 // have to send on master in, *slave out*
 pinMode (MISO, OUTPUT);

 // get ready for an interrupt
 process_it = false;

 // now turn on interrupts
 SPI.attachInterrupt();
}  // end of setup

// SPI interrupt routine
ISR (SPI_STC_vect)
{
 byte c = SPDR;  // grab byte from SPI Data Register
 buf = c;

 process_it = true;
}  // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
 if (process_it)
   {
   Serial.println (buf);
   process_it = false;
   }  // end of flag set
}  // end of loop

20160903_052535.jpg

6.png

(4.3] 칩을 선택할 수 있는 디코더
1)4-16 디코더
6.png

8.png

7.png

댓글 없음:

댓글 쓰기

clear images were obtained