Showing
30 changed files
with
737 additions
and
39 deletions
lab2-2-1
0 → 100755
No preview for this file type
lab2-2-1.c
0 → 100644
1 | +#include<wiringPi.h> | ||
2 | +#define LED_ON 1 | ||
3 | +#define LED_OFF 0 | ||
4 | +const int Led[16]={4,17,18,27,22,23,24,25,6,12,13,16,19,20,26,21}; | ||
5 | + | ||
6 | +void LEDControl(int pinnum){ | ||
7 | + int i; | ||
8 | + for(i=0;i<16;i++){ | ||
9 | + if(i==pinnum) | ||
10 | + digitalWrite(Led[i],LED_ON); | ||
11 | + else | ||
12 | + digitalWrite(Led[i],LED_OFF); | ||
13 | + } | ||
14 | +} | ||
15 | + | ||
16 | +int main(void){ | ||
17 | + int i; | ||
18 | + if(wiringPiSetupGpio()==-1) | ||
19 | + return 1; | ||
20 | + for(i=0;i<16;i++){ | ||
21 | + pinMode(Led[i],OUTPUT); | ||
22 | + digitalWrite(Led[i],LED_OFF); | ||
23 | + } | ||
24 | + while(1){ | ||
25 | + for(i=0;i<16;i+=2){ | ||
26 | + LEDControl(i); | ||
27 | + delay(500); | ||
28 | + } | ||
29 | + for(i=1;i<16;i+=2){ | ||
30 | + LEDControl(i); | ||
31 | + delay(500); | ||
32 | + } | ||
33 | + } | ||
34 | + return 0; | ||
35 | +} | ||
36 | + |
lab2-2-2
0 → 100755
No preview for this file type
lab2-2-2.c
0 → 100644
1 | +#include<wiringPi.h> | ||
2 | +#define LED_ON 1 | ||
3 | +#define LED_OFF 0 | ||
4 | +const int Led[16]={4,17,18,27,22,23,24,25,6,12,13,16,19,20,26,21}; | ||
5 | + | ||
6 | +void LEDControl(int pinnum){ | ||
7 | + int i; | ||
8 | + for(i=0;i<16;i++){ | ||
9 | + if(i==pinnum || i==pinnum+1) | ||
10 | + digitalWrite(Led[i],LED_ON); | ||
11 | + else | ||
12 | + digitalWrite(Led[i],LED_OFF); | ||
13 | + } | ||
14 | +} | ||
15 | + | ||
16 | +int main(void){ | ||
17 | + int i; | ||
18 | + if(wiringPiSetupGpio()==-1) | ||
19 | + return 1; | ||
20 | + for(i=0;i<16;i++){ | ||
21 | + pinMode(Led[i],OUTPUT); | ||
22 | + digitalWrite(Led[i],LED_OFF); | ||
23 | + } | ||
24 | + while(1){ | ||
25 | + for(i=0;i<16;i+=2){ | ||
26 | + LEDControl(i); | ||
27 | + delay(500); | ||
28 | + } | ||
29 | + } | ||
30 | + return 0; | ||
31 | +} | ||
32 | + |
lab2-2-3
0 → 100755
No preview for this file type
lab2-2-3.c
0 → 100644
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 GPFSEL1 0x04 | ||
10 | +#define GPSET0 0x1C | ||
11 | +#define GPCLR0 0x28 | ||
12 | + | ||
13 | +int main() { | ||
14 | + int fd = open( "/dev/mem", O_RDWR|O_SYNC ); | ||
15 | + if ( fd < 0 ){ | ||
16 | + printf( "can't open /dev/mem \n" ); | ||
17 | + exit(-1); | ||
18 | + } | ||
19 | + | ||
20 | + char *gpio_memory_map | ||
21 | + = (char *)mmap(0, 4096, PROT_READ|PROT_WRITE, | ||
22 | + MAP_SHARED, fd, GPIO_BASE ); | ||
23 | + | ||
24 | + if ( gpio_memory_map == MAP_FAILED ) { | ||
25 | + printf( " Error : mmap \n" ); | ||
26 | + exit(-1); | ||
27 | + } | ||
28 | + | ||
29 | + volatile unsigned int* gpio | ||
30 | + = (volatile unsigned int*)gpio_memory_map; | ||
31 | + | ||
32 | + gpio[GPFSEL1/4] = (1<<24); | ||
33 | + | ||
34 | + int i; | ||
35 | + for ( i=0; i<5; i++ ) { | ||
36 | + gpio[GPCLR0/4] = (1<<18); | ||
37 | + sleep(1); | ||
38 | + | ||
39 | + gpio[GPSET0/4] = (1<<18); | ||
40 | + sleep(1); | ||
41 | + } | ||
42 | + | ||
43 | + munmap( gpio_memory_map, 4096); | ||
44 | + return 0; | ||
45 | +} | ||
46 | + | ||
47 | + | ||
48 | + | ||
49 | + |
lab2-2-4
0 → 100755
No preview for this file type
lab2-2-4.c
0 → 100644
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 GPFSEL1 0x04 | ||
10 | +#define GPSET0 0x1C | ||
11 | +#define GPCLR0 0x28 | ||
12 | + | ||
13 | +int main() { | ||
14 | + int fd = open( "/dev/mem", O_RDWR|O_SYNC ); | ||
15 | + if ( fd < 0 ){ | ||
16 | + printf( "can't open /dev/mem \n" ); | ||
17 | + exit(-1); | ||
18 | + } | ||
19 | + | ||
20 | + char *gpio_memory_map | ||
21 | + = (char *)mmap(0, 4096, PROT_READ|PROT_WRITE, | ||
22 | + MAP_SHARED, fd, GPIO_BASE ); | ||
23 | + | ||
24 | + if ( gpio_memory_map == MAP_FAILED ) { | ||
25 | + printf( " Error : mmap \n" ); | ||
26 | + exit(-1); | ||
27 | + } | ||
28 | + | ||
29 | + volatile unsigned int* gpio | ||
30 | + = (volatile unsigned int*)gpio_memory_map; | ||
31 | + | ||
32 | + gpio[GPFSEL1/4] = (1<<18); | ||
33 | + | ||
34 | + int i; | ||
35 | + for ( i=0; i<5; i++ ) { | ||
36 | + gpio[GPCLR0/4] = (1<<16); | ||
37 | + sleep(1); | ||
38 | + | ||
39 | + gpio[GPSET0/4] = (1<<16); | ||
40 | + sleep(1); | ||
41 | + } | ||
42 | + | ||
43 | + munmap( gpio_memory_map, 4096); | ||
44 | + return 0; | ||
45 | +} | ||
46 | + | ||
47 | + | ||
48 | + | ||
49 | + |
lab2-2_3
0 → 100755
No preview for this file type
lab2-2_3.c
0 → 100755
1 | +#include<wiringPi.h> | ||
2 | +#define LED_ON 1 | ||
3 | +#define LED_OFF 0 | ||
4 | +const int Led[16]={4,17,18,27,22,23,24,25,6,12,13,16,19,20,26,21}; | ||
5 | + | ||
6 | +void LEDControl(int pinnum){ | ||
7 | + int i; | ||
8 | + for(i=0;i<16;i++){ | ||
9 | + if(i==pinnum) | ||
10 | + digitalWrite(Led[i],LED_ON); | ||
11 | + else | ||
12 | + digitalWrite(Led[i],LED_OFF); | ||
13 | + } | ||
14 | +} | ||
15 | + | ||
16 | +int main(void){ | ||
17 | + int i; | ||
18 | + if(wiringPiSetupGpio()==-1) | ||
19 | + return 1; | ||
20 | + for(i=0;i<16;i++){ | ||
21 | + pinMode(Led[i],OUTPUT); | ||
22 | + digitalWrite(Led[i],LED_OFF); | ||
23 | + } | ||
24 | + while(1){ | ||
25 | + for(i=0;i<16;i++){ | ||
26 | + LEDControl(i); | ||
27 | + delay(500); | ||
28 | + } | ||
29 | + } | ||
30 | + return 0; | ||
31 | +} | ||
32 | + |
lab2-3-1
0 → 100755
No preview for this file type
lab2-3-1.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 }; | ||
3 | +const int Keypad[8] = { 16, 13, 12, 6, 21, 26, 20, 19 }; | ||
4 | +int LedON[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | ||
5 | +int lastKey[8] = { 1, 1, 1, 1, 1, 1, 1, 1 }; | ||
6 | + | ||
7 | +int KeypadRead() { | ||
8 | + int i, keypadnum=-1; | ||
9 | + for(i=0; i<8; i++) { | ||
10 | + if(!digitalRead(Keypad[i]) && lastKey[i]==1) { | ||
11 | + keypadnum = i; | ||
12 | + lastKey[i]=0; | ||
13 | + break; | ||
14 | + } | ||
15 | + else if(digitalRead(Keypad[i]) && lastKey[i]==0) | ||
16 | + lastKey[i]=1; | ||
17 | + } | ||
18 | + return keypadnum; | ||
19 | +} | ||
20 | +void LedControl(int keypadnum) { | ||
21 | + int i; | ||
22 | + for(i=0; i<8; i++) { | ||
23 | + if(i==keypadnum) { | ||
24 | + LedON[i]=!LedON[i]; | ||
25 | + digitalWrite(LedRed[i], LedON[i]);} | ||
26 | + } | ||
27 | +} | ||
28 | +int main(void) { | ||
29 | + int i, keypadnum=-1; | ||
30 | + | ||
31 | + if(wiringPiSetupGpio() == -1) | ||
32 | + return 1; | ||
33 | + | ||
34 | + for(i=0; i<8; i++) { | ||
35 | + pinMode(LedRed[i], OUTPUT); | ||
36 | + digitalWrite(LedRed[i], LedON[i]); | ||
37 | + } | ||
38 | + | ||
39 | + for(i=0; i<8; i++) | ||
40 | + pinMode(Keypad[i], INPUT); | ||
41 | + | ||
42 | + while(1) { | ||
43 | + keypadnum = KeypadRead(); | ||
44 | + LedControl(keypadnum); | ||
45 | + } | ||
46 | + return 0; | ||
47 | +} |
lab2-3-2
0 → 100755
No preview for this file type
lab2-3-2.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 }; | ||
3 | +const int Keypad[5] = { 6, 12, 13, 16, 19 }; | ||
4 | +int state=0; | ||
5 | + | ||
6 | +void checkKey3(){ | ||
7 | + if(KeypadRead()==2) | ||
8 | + state=0; | ||
9 | +} | ||
10 | +int KeypadRead() { | ||
11 | + int i, keypadnum=-1; | ||
12 | + for(i=0; i<8; i++) { | ||
13 | + if(!digitalRead(Keypad[i])) { | ||
14 | + keypadnum = i; | ||
15 | + break; | ||
16 | + } | ||
17 | + } | ||
18 | + return keypadnum; | ||
19 | +} | ||
20 | +void LedControl(int num) { | ||
21 | + int i; | ||
22 | + for(i=0; i<8; i++) { | ||
23 | + checkKey3(); | ||
24 | + if(state==0) | ||
25 | + return; | ||
26 | + if(i==num) | ||
27 | + digitalWrite(LedRed[i], HIGH); | ||
28 | + else | ||
29 | + digitalWrite(LedRed[i], LOW); | ||
30 | + } | ||
31 | +} | ||
32 | +void LedON(int order){ | ||
33 | + int i; | ||
34 | + if(order==1){ | ||
35 | + for(i=0; i<8; i++) { | ||
36 | + LedControl(i); | ||
37 | + if(state==0) | ||
38 | + return; | ||
39 | + delay(500); | ||
40 | + } | ||
41 | + LedControl(-1); | ||
42 | + } | ||
43 | + else if (order==-1){ | ||
44 | + for(i=7; i>=0; i--) { | ||
45 | + LedControl(i); | ||
46 | + if(state==0) | ||
47 | + return; | ||
48 | + delay(500); | ||
49 | + } | ||
50 | + LedControl(-1); | ||
51 | + } | ||
52 | +} | ||
53 | +int main(void) { | ||
54 | + int i, keypadnum=-1; | ||
55 | + if(wiringPiSetupGpio() == -1) | ||
56 | + return 1; | ||
57 | + for(i=0; i<8; i++) { | ||
58 | + pinMode(LedRed[i], OUTPUT); | ||
59 | + digitalWrite(LedRed[i], LOW); | ||
60 | + } | ||
61 | + for(i=0; i<5; i++) | ||
62 | + pinMode(Keypad[i], INPUT); | ||
63 | + while(1) { | ||
64 | + state=0; | ||
65 | + keypadnum = KeypadRead(); | ||
66 | + if(keypadnum== 3) { | ||
67 | + state=1; | ||
68 | + LedON(1); | ||
69 | + } | ||
70 | + else if(keypadnum== 4) { | ||
71 | + state=1; | ||
72 | + LedON(-1); | ||
73 | + } | ||
74 | + else if(keypadnum== 0){ | ||
75 | + while(1){ | ||
76 | + state=1; | ||
77 | + LedON(1); | ||
78 | + if(state==0) | ||
79 | + break; | ||
80 | + } | ||
81 | + } | ||
82 | + else if(keypadnum== 1){ | ||
83 | + while(1){ | ||
84 | + state=1; | ||
85 | + LedON(-1); | ||
86 | + if(state==0) | ||
87 | + break; | ||
88 | + } | ||
89 | + } | ||
90 | + } | ||
91 | + return 0; | ||
92 | +} | ||
93 | + | ||
94 | + |
lab2-3_1
0 → 100755
No preview for this file type
lab2-3_1.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 }; | ||
3 | +const int Keypad[8] = { 16, 13, 12, 6, 21, 26, 20, 19 }; | ||
4 | + | ||
5 | +int KeypadRead() { | ||
6 | + int i, keypadnum=-1; | ||
7 | + for(i=0; i<8; i++) { | ||
8 | + if(!digitalRead(Keypad[i])) { | ||
9 | + keypadnum = i; | ||
10 | + break; | ||
11 | + } | ||
12 | + } | ||
13 | + return keypadnum; | ||
14 | +} | ||
15 | +void LedControl(int keypadnum) { | ||
16 | + int i; | ||
17 | + for(i=0; i<8; i++) { | ||
18 | + if(i==keypadnum) | ||
19 | + digitalWrite(LedRed[i], HIGH); | ||
20 | + else | ||
21 | + digitalWrite(LedRed[i], LOW); | ||
22 | + } | ||
23 | +} | ||
24 | +int main(void) { | ||
25 | + int i, keypadnum=-1; | ||
26 | + | ||
27 | + if(wiringPiSetupGpio() == -1) | ||
28 | + return 1; | ||
29 | + | ||
30 | + for(i=0; i<8; i++) { | ||
31 | + pinMode(LedRed[i], OUTPUT); | ||
32 | + digitalWrite(LedRed[i], LOW); | ||
33 | + } | ||
34 | + | ||
35 | + for(i=0; i<8; i++) | ||
36 | + pinMode(Keypad[i], INPUT); | ||
37 | + | ||
38 | + while(1) { | ||
39 | + keypadnum = KeypadRead(); | ||
40 | + LedControl(keypadnum); | ||
41 | + } | ||
42 | + return 0; | ||
43 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lab2-4-1
0 → 100755
No preview for this file type
lab2-4-1.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +#include <stdio.h> | ||
3 | +#include <stdlib.h> | ||
4 | + | ||
5 | +const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 }; | ||
6 | +const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 }; | ||
7 | +const int FndFont[6] = { 0x00, 0x76, 0x79, 0x38, 0x38, 0x3F }; | ||
8 | + | ||
9 | +void init(){ | ||
10 | + int i; | ||
11 | + if( wiringPiSetupGpio() == -1 ){ | ||
12 | + printf( "wiringPiSetupGpio() error\n" ); | ||
13 | + exit(-1); | ||
14 | + } | ||
15 | + for(i=0;i<6;i++){ | ||
16 | + pinMode( FndSelectPin[i], OUTPUT ); | ||
17 | + digitalWrite( FndSelectPin[i], HIGH ); | ||
18 | + } | ||
19 | + for(i=0;i<8;i++){ | ||
20 | + pinMode( FndPin[i], OUTPUT ); | ||
21 | + digitalWrite( FndPin[i], LOW ); | ||
22 | + } | ||
23 | +} | ||
24 | +void FndSelect(int position){ | ||
25 | + int i; | ||
26 | + for(i=0;i<6;i++){ | ||
27 | + if(i==position){ | ||
28 | + digitalWrite(FndSelectPin[i],LOW); | ||
29 | + } | ||
30 | + else{ | ||
31 | + digitalWrite(FndSelectPin[i],HIGH); | ||
32 | + } | ||
33 | + } | ||
34 | +} | ||
35 | +void FndDisplay(int position, int num){ | ||
36 | + int i; | ||
37 | + int flag=0; | ||
38 | + int shift=0x01; | ||
39 | + | ||
40 | + for(i=0;i<8;i++){ | ||
41 | + flag=(FndFont[num]&shift); | ||
42 | + digitalWrite(FndPin[i], flag); | ||
43 | + shift<<=1; | ||
44 | + } | ||
45 | + FndSelect(position); | ||
46 | +} | ||
47 | +int main(){ | ||
48 | + int pos; | ||
49 | + int data[6]={5,4,3,2,1,0}; | ||
50 | + init(); | ||
51 | + int i=0; | ||
52 | + unsigned long prevtime=millis(); | ||
53 | + while(1){ | ||
54 | + unsigned long curtime=millis(); | ||
55 | + if(curtime-prevtime > 500){ | ||
56 | + i+=1; | ||
57 | + i=i%6; | ||
58 | + prevtime=curtime; | ||
59 | + } | ||
60 | + for(pos=0;pos<6;pos++){ | ||
61 | + FndDisplay((pos+i)%6,data[pos]); | ||
62 | + delay(1); | ||
63 | + } | ||
64 | + } | ||
65 | + return 0; | ||
66 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lab2-4-2
0 → 100755
No preview for this file type
lab2-4-2.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +#include <stdio.h> | ||
3 | +#include <stdlib.h> | ||
4 | + | ||
5 | +const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 }; | ||
6 | +const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 }; | ||
7 | +const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, | ||
8 | + 0x6D, 0x7D, 0x07, 0x7F, 0x67 }; | ||
9 | + | ||
10 | +void init(){ | ||
11 | + int i; | ||
12 | + if( wiringPiSetupGpio() == -1 ){ | ||
13 | + printf( "wiringPiSetupGpio() error\n" ); | ||
14 | + exit(-1); | ||
15 | + } | ||
16 | + for(i=0;i<6;i++){ | ||
17 | + pinMode( FndSelectPin[i], OUTPUT ); | ||
18 | + digitalWrite( FndSelectPin[i], HIGH ); | ||
19 | + } | ||
20 | + for(i=0;i<8;i++){ | ||
21 | + pinMode( FndPin[i], OUTPUT ); | ||
22 | + digitalWrite( FndPin[i], LOW ); | ||
23 | + } | ||
24 | +} | ||
25 | +void FndSelect(int position){ | ||
26 | + int i; | ||
27 | + for(i=0;i<6;i++){ | ||
28 | + if(i==position){ | ||
29 | + digitalWrite(FndSelectPin[i],LOW); | ||
30 | + } | ||
31 | + else{ | ||
32 | + digitalWrite(FndSelectPin[i],HIGH); | ||
33 | + } | ||
34 | + } | ||
35 | +} | ||
36 | +void FndDisplay(int position, int num){ | ||
37 | + int i,number; | ||
38 | + int flag=0; | ||
39 | + int shift=0x01; | ||
40 | + number=FndFont[num]; | ||
41 | + if(position==2) | ||
42 | + number|=0x80; | ||
43 | + | ||
44 | + for(i=0;i<8;i++){ | ||
45 | + flag=(number&shift); | ||
46 | + digitalWrite(FndPin[i], flag); | ||
47 | + shift<<=1; | ||
48 | + } | ||
49 | + FndSelect(position); | ||
50 | +} | ||
51 | +int main(){ | ||
52 | + int pos; | ||
53 | + int time=0; | ||
54 | + init(); | ||
55 | + unsigned long prevtime=millis(); | ||
56 | + while(1){ | ||
57 | + unsigned long curtime=millis(); | ||
58 | + if(curtime-prevtime > 10){ | ||
59 | + prevtime=curtime; | ||
60 | + time++; | ||
61 | + } | ||
62 | + int data[6]={time%10, (time%100)/10, (time%1000)/100, | ||
63 | + (time/1000)%10, (time/10000)%10, time/100000}; | ||
64 | + for(pos=0;pos<6;pos++){ | ||
65 | + FndDisplay(pos,data[pos]); | ||
66 | + delay(1); | ||
67 | + } | ||
68 | + } | ||
69 | + return 0; | ||
70 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lab2-4_1
0 → 100755
No preview for this file type
lab2-4_1.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +#include <stdio.h> | ||
3 | +#include <stdlib.h> | ||
4 | + | ||
5 | +const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 }; | ||
6 | +const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 }; | ||
7 | +const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, | ||
8 | + 0x6D, 0x7D, 0x07, 0x7F, 0x67 }; | ||
9 | + | ||
10 | +void init(){ | ||
11 | + int i; | ||
12 | + if( wiringPiSetupGpio() == -1 ){ | ||
13 | + printf( "wiringPiSetupGpio() error\n" ); | ||
14 | + exit(-1); | ||
15 | + } | ||
16 | + for(i=0;i<6;i++){ | ||
17 | + pinMode( FndSelectPin[i], OUTPUT ); | ||
18 | + digitalWrite( FndSelectPin[i], HIGH ); | ||
19 | + } | ||
20 | + for(i=0;i<8;i++){ | ||
21 | + pinMode( FndPin[i], OUTPUT ); | ||
22 | + digitalWrite( FndPin[i], LOW ); | ||
23 | + } | ||
24 | +} | ||
25 | +void FndDisplay(int position, int num){ | ||
26 | + int i; | ||
27 | + int flag=0; | ||
28 | + int shift=0x01; | ||
29 | + | ||
30 | + for(i=0;i<8;i++){ | ||
31 | + flag=(FndFont[num]&shift); | ||
32 | + digitalWrite(FndPin[i], flag); | ||
33 | + shift<<=1; | ||
34 | + } | ||
35 | + digitalWrite( FndSelectPin[position], LOW); | ||
36 | +} | ||
37 | +int main(int argc, char **argv){ | ||
38 | + if(argc != 3){ | ||
39 | + printf("Usage: %s [position][number]",argv[0]); | ||
40 | + exit(-1); | ||
41 | + } | ||
42 | + init(); | ||
43 | + FndDisplay(atoi(argv[1]), atoi(argv[2])); | ||
44 | + return 0; | ||
45 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lab2-4_2
0 → 100755
No preview for this file type
lab2-4_2.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +#include <stdio.h> | ||
3 | +#include <stdlib.h> | ||
4 | + | ||
5 | +const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 }; | ||
6 | +const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 }; | ||
7 | +const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, | ||
8 | + 0x6D, 0x7D, 0x07, 0x7F, 0x67 }; | ||
9 | + | ||
10 | +void init(){ | ||
11 | + int i; | ||
12 | + if( wiringPiSetupGpio() == -1 ){ | ||
13 | + printf( "wiringPiSetupGpio() error\n" ); | ||
14 | + exit(-1); | ||
15 | + } | ||
16 | + for(i=0;i<6;i++){ | ||
17 | + pinMode( FndSelectPin[i], OUTPUT ); | ||
18 | + digitalWrite( FndSelectPin[i], HIGH ); | ||
19 | + } | ||
20 | + for(i=0;i<8;i++){ | ||
21 | + pinMode( FndPin[i], OUTPUT ); | ||
22 | + digitalWrite( FndPin[i], LOW ); | ||
23 | + } | ||
24 | +} | ||
25 | +void FndSelect(int position){ | ||
26 | + int i; | ||
27 | + for(i=0;i<6;i++){ | ||
28 | + if(i==position){ | ||
29 | + digitalWrite(FndSelectPin[i],LOW); | ||
30 | + } | ||
31 | + else{ | ||
32 | + digitalWrite(FndSelectPin[i],HIGH); | ||
33 | + } | ||
34 | + } | ||
35 | +} | ||
36 | +void FndDisplay(int position, int num){ | ||
37 | + int i; | ||
38 | + int flag=0; | ||
39 | + int shift=0x01; | ||
40 | + | ||
41 | + for(i=0;i<8;i++){ | ||
42 | + flag=(FndFont[num]&shift); | ||
43 | + digitalWrite(FndPin[i], flag); | ||
44 | + shift<<=1; | ||
45 | + } | ||
46 | + FndSelect(position); | ||
47 | +} | ||
48 | +int main(){ | ||
49 | + int pos; | ||
50 | + int data[6]={0,1,2,3,4,5}; | ||
51 | + init(); | ||
52 | + while(1){ | ||
53 | + for(pos=0;pos<6;pos++){ | ||
54 | + FndDisplay(pos,data[pos]); | ||
55 | + delay(1); | ||
56 | + } | ||
57 | + } | ||
58 | + return 0; | ||
59 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lab2-5-1
0 → 100755
No preview for this file type
lab2-5-1.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +#include <softPwm.h> | ||
3 | + | ||
4 | +#define MOTOR_MT_N_PIN 17 | ||
5 | +#define MOTOR_MT_P_PIN 4 | ||
6 | + | ||
7 | +#define LEFT_ROTATE 1 | ||
8 | +#define RIGHT_ROTATE 2 | ||
9 | + | ||
10 | +void MotorStop() { | ||
11 | + softPwmWrite(MOTOR_MT_N_PIN, 0); | ||
12 | + softPwmWrite(MOTOR_MT_P_PIN, 0); | ||
13 | +} | ||
14 | + | ||
15 | +void MotorControl(int rotate, int speed) { | ||
16 | + if (rotate == LEFT_ROTATE) { | ||
17 | + digitalWrite(MOTOR_MT_P_PIN, LOW); | ||
18 | + softPwmWrite(MOTOR_MT_N_PIN, speed); | ||
19 | + } | ||
20 | + else if (rotate == RIGHT_ROTATE) { | ||
21 | + digitalWrite(MOTOR_MT_N_PIN, LOW); | ||
22 | + softPwmWrite(MOTOR_MT_P_PIN, speed); | ||
23 | + } | ||
24 | +} | ||
25 | +int main(void){ | ||
26 | + if (wiringPiSetupGpio() == -1) | ||
27 | + return 1; | ||
28 | + | ||
29 | + pinMode(MOTOR_MT_N_PIN, OUTPUT); | ||
30 | + pinMode(MOTOR_MT_P_PIN, OUTPUT); | ||
31 | + | ||
32 | + softPwmCreate(MOTOR_MT_N_PIN, 0, 128); | ||
33 | + softPwmCreate(MOTOR_MT_P_PIN, 0, 128); | ||
34 | + int i; | ||
35 | + for(i=1;i<5;i++){ | ||
36 | + MotorControl(LEFT_ROTATE,i*32); | ||
37 | + delay(2000); | ||
38 | + MotorStop(); | ||
39 | + delay(2000); | ||
40 | + } | ||
41 | + for(i=4;i>0;i--){ | ||
42 | + MotorControl(RIGHT_ROTATE,i*32); | ||
43 | + delay(2000); | ||
44 | + MotorStop(); | ||
45 | + delay(2000); | ||
46 | + } | ||
47 | + return 0; | ||
48 | +} | ||
49 | + | ||
50 | + | ||
51 | + |
lab2-5-2
0 → 100755
No preview for this file type
lab2-5-2.c
0 → 100644
1 | +#include <wiringPi.h> | ||
2 | +#include <softPwm.h> | ||
3 | + | ||
4 | +#define LedPin 22 | ||
5 | + | ||
6 | +int main(void){ | ||
7 | + if (wiringPiSetupGpio() == -1) | ||
8 | + return 1; | ||
9 | + | ||
10 | + pinMode(LedPin, OUTPUT); | ||
11 | + softPwmCreate(LedPin, 0, 128); | ||
12 | + int i; | ||
13 | + while(1){ | ||
14 | + for(i=0;i<129;i+=8){ | ||
15 | + softPwmWrite(LedPin, i); | ||
16 | + delay(100); | ||
17 | + } | ||
18 | + for(i=128;i>-1;i-=8){ | ||
19 | + softPwmWrite(LedPin,i); | ||
20 | + delay(100); | ||
21 | + } | ||
22 | + } | ||
23 | + return 0; | ||
24 | +} | ||
25 | + | ||
26 | + | ||
27 | + |
lab2-5_1
0 → 100755
No preview for this file type
1 | -#include <wiringPi.h> // GPIO Access Library 헤더파일 선언 | 1 | +#include <wiringPi.h> |
2 | -#include <softPwm.h> // Software PWM library 헤더파일 선언 | 2 | +#include <softPwm.h> |
3 | -// Motor 핀 설정 | 3 | + |
4 | #define MOTOR_MT_N_PIN 17 | 4 | #define MOTOR_MT_N_PIN 17 |
5 | #define MOTOR_MT_P_PIN 4 | 5 | #define MOTOR_MT_P_PIN 4 |
6 | -// Motor 회전 방향 정의 | 6 | + |
7 | #define LEFT_ROTATE 1 | 7 | #define LEFT_ROTATE 1 |
8 | #define RIGHT_ROTATE 2 | 8 | #define RIGHT_ROTATE 2 |
9 | -// Motor 정지 함수 | 9 | + |
10 | void MotorStop() { | 10 | void MotorStop() { |
11 | -softPwmWrite(MOTOR_MT_N_PIN, 0); | 11 | + softPwmWrite(MOTOR_MT_N_PIN, 0); |
12 | -softPwmWrite(MOTOR_MT_P_PIN, 0); | 12 | + softPwmWrite(MOTOR_MT_P_PIN, 0); |
13 | } | 13 | } |
14 | -// Motor 회전 함수 | 14 | + |
15 | void MotorControl(int rotate) { | 15 | void MotorControl(int rotate) { |
16 | -if (rotate == LEFT_ROTATE) { | 16 | + if (rotate == LEFT_ROTATE) { |
17 | -digitalWrite(MOTOR_MT_P_PIN, LOW); | 17 | + digitalWrite(MOTOR_MT_P_PIN, LOW); |
18 | -softPwmWrite(MOTOR_MT_N_PIN, 50); | 18 | + softPwmWrite(MOTOR_MT_N_PIN, 50); |
19 | + } | ||
20 | + else if (rotate == RIGHT_ROTATE) { | ||
21 | + digitalWrite(MOTOR_MT_N_PIN, LOW); | ||
22 | + softPwmWrite(MOTOR_MT_P_PIN, 50); | ||
23 | + } | ||
19 | } | 24 | } |
20 | -else if (rotate == RIGHT_ROTATE) { | 25 | +int main(void){ |
21 | -digitalWrite(MOTOR_MT_N_PIN, LOW); | 26 | + if (wiringPiSetupGpio() == -1) |
22 | -softPwmWrite(MOTOR_MT_P_PIN, 50); | 27 | + return 1; |
28 | + | ||
29 | + pinMode(MOTOR_MT_N_PIN, OUTPUT); | ||
30 | + pinMode(MOTOR_MT_P_PIN, OUTPUT); | ||
31 | + | ||
32 | + softPwmCreate(MOTOR_MT_N_PIN, 0, 100); | ||
33 | + softPwmCreate(MOTOR_MT_P_PIN, 0, 100); | ||
34 | + while (1){ | ||
35 | + MotorControl(LEFT_ROTATE); | ||
36 | + delay(2000); | ||
37 | + MotorStop(); | ||
38 | + delay(1000); | ||
39 | + MotorControl(RIGHT_ROTATE); | ||
40 | + delay(2000); | ||
41 | + MotorStop(); | ||
42 | + delay(1000); | ||
43 | + } | ||
44 | + return 0; | ||
23 | } | 45 | } |
24 | -} | ||
25 | -int main(void) | ||
26 | -{ | ||
27 | -if (wiringPiSetupGpio() == -1) | ||
28 | -return 1; | ||
29 | -// Motor 핀 출력으로 설정 | ||
30 | -pinMode(MOTOR_MT_N_PIN, OUTPUT); | ||
31 | -pinMode(MOTOR_MT_P_PIN, OUTPUT); | ||
32 | -// Motor 핀 PWM 제어 핀으로 설정 | ||
33 | -// 주기: 100ms | ||
34 | -softPwmCreate(MOTOR_MT_N_PIN, 0, 100); | ||
35 | -softPwmCreate(MOTOR_MT_P_PIN, 0, 100); | ||
36 | -while (1) | ||
37 | -{ | ||
38 | -MotorControl(LEFT_ROTATE); // Motor 왼쪽으로 회전 | ||
39 | -delay(2000); | ||
40 | -MotorStop(); // Motor 정지 | ||
41 | -delay(1000); | ||
42 | -MotorControl(RIGHT_ROTATE); // Motor 오른쪽으로 회전 | ||
43 | -delay(2000); | ||
44 | -MotorStop(); // Motor 정지 | ||
45 | -delay(1000); | ||
46 | -} | ||
47 | -return 0; | ||
48 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
46 | + | ... | ... |
-
Please register or login to post a comment