[라즈베리파이가 0~255 밝기값 받기 ]
아두이노
|
라즈베리파이
|
2
|
16
|
3
|
17
|
4
|
18
|
5
|
19
|
6
|
20
|
7
|
21
|
8
|
22
|
9
|
23
|
GND
|
GND
|
* 아두이노 A0에 가변저항 연결
* 라즈베리파이의 핀 번호는 BCM 핀번호 표현을 따른 것임.
|
아두이노 코드
int analogPin = 0;// potentiometer wiper (middle terminal) connected to analog pin 3
int digitalPin[] = {2,3,4,5,6,7,8,9};// outside leads to ground and +5V
int val = 0; // variable to store the value read
char val_8b = 0;
int j = 0;
void setup()
{
for(j=0; j<8; j++)
pinMode(digitalPin[j], OUTPUT);
Serial.begin(9600); // setup serial
}
void printBit(){
int i = 0;
for(i = 7; i>=0; i--){
int bitVal = bitRead(val_8b, i);
digitalWrite(digitalPin[i], bitVal);
Serial.print(bitVal);
}
Serial.println();
}
void loop()
{
val = analogRead(analogPin);
val_8b = val/4; // read the input pin
// Serial.println(val_8b, HEX); // debug value
printBit();
delay(100);
}
|
라즈베리파이 코드
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <sys/mman.h>
7
8 #define GPIO_BASE 0x3F200000
9 #define GPLEV0 0x34
10
11 int main()
12 {
13 int fd = open("/dev/mem", O_RDWR|O_SYNC);
14 if(fd<0){
15 printf("can't open /dev/mem \n");
16 exit(-1);
17 }
18
19 char *gpio_memory_map = (char*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_S HARED, fd, GPIO_BASE);
20
21 if(gpio_memory_map == MAP_FAILED)
22 {
23 printf("ERROR: mmap\n");
24 exit(-1);
25 }
26
27 volatile unsigned int* gpio = (volatile unsigned int*)gpio_memory_map;
28
29 while(1)
30 {
31 int input = gpio[GPLEV0/4] & (0xFF<<16);
32
33 input = input >> 16;
34 input = input & 0xFF;
35 printf("%d\n", input);
36
37 delay(100);
38 }
39
40 munmap(gpio_memory_map, 4096);
41
42
43
44
45 return 0;
46 }
|
댓글 없음:
댓글 쓰기