You are on page 1of 5

Lab 3.

Programming with GPIO drivers

Lab 3. Programming with GPIO drivers


1. Mc ch: - Vit cc ng dng vi cc giao tip vo ra c bn (GPIO) trn KIT, s dng cc GPIO driver c sn, nh: giao tip led, button, - Bin dch, np v thc thi ng dng trn KIT - Pht trin cc ng dng t cc v d n gin. 2. Chun b: - PC Linux (Ubuntu) with arm-linux-gcc - B KIT thc hnh FriendlyArm mini/macro2440 - D liu: Cc v d tham kho 3. Ni dung thc hnh: 3.1. Vit chng trnh iu khin tt/bt led n 3.1.1. M t: - Dy 4 led n trn KIT ghp ni qua cng GPIO c sn driver trn Embedded Linux. - M hnh giao tip:

3.1.2. M ngun tham kho: (File tham kho: leds.c) #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ioctl.h> Lp trnh nhng ARM Linux Page 1

Lab 3. Programming with GPIO drivers int main(int argc, char **argv) { int on; int led_no; int fd; if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2],"%d", &on) != 1 || on < 0 || on > 1 || led_no < 0 || led_no > 3) { fprintf(stderr, "Usage: leds led_no 0|1\n"); exit(1); } fd = open("/dev/leds0", 0); if (fd < 0) { fd = open("/dev/leds", 0); } if (fd < 0) { perror("open device leds"); exit(1); } ioctl(fd, on, led_no); close(fd); return 0; } 3.1.3. Yu cu: Da trn v d trn, vit chng trnh iu khin tt, bt dy led n. Bin dch, np chng trnh ln KIT v thc thi, quan st kt qu. Ch : Mc nh trn KIT chy ng dng led_player trn nn Qtopia, cn tt ng dng ny quan st kt qu ng dng mi. Thc hin: . 3.2. Vit chng trnh c trng thi nt bm 3.2.1. M t: - Dy nt bm K1, K2, K3, K4, K5, K6 trn KIT c ghp ni qua GPIO, c sn driver trn h iu hnh Linux nhng. - C th c trng thi cc nt bm ny (pressed/release or not ?) v c x l thch hp. - M hnh lp trnh vi nt bm: Lp trnh nhng ARM Linux Page 2

Lab 3. Programming with GPIO drivers

3.2.2. M ngun tham kho (File buttons.c) #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/select.h> #include <sys/time.h> #include <errno.h> int main(void) { int buttons_fd; char buttons[6] = {'0', '0', '0', '0', '0', '0'}; buttons_fd = open("/dev/buttons", 0); if (buttons_fd < 0) { perror("open device buttons"); exit(1); } Lp trnh nhng ARM Linux Page 3

Lab 3. Programming with GPIO drivers for (;;) { char current_buttons[6]; int count_of_changed_key; int i; if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) { perror("read buttons:"); exit(1); } for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) { if (buttons[i] != current_buttons[i]) { buttons[i] = current_buttons[i]; printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down"); count_of_changed_key++; } } if (count_of_changed_key) { printf("\n"); } } close(buttons_fd); return 0; }

3.2.3. Yu cu. Da trn v d tham kho, vit chng trnh c trng thi ca cc nt bm, a ra thng bo nt no c nhn/nh. Bin dch, np v thc thi chng trnh trn KIT. 3.3. Vit chng trnh kt hp giao tip leds v buttons Yu cu: Da trn 2 v d trn vit chng trnh kt hp 2 giao tip vi leds v buttons. - S dng cc nt K1, K2, K3, K4 bt/tt 4 led n tng ng (mi ln bm nt, led tng ng s chuyn trng thi t tt -> bt hoc ngc li) - Hoc s dng 2 nt bm iu khin cho 1 led vi 1 nt dng tt v 1 nt dng bt. Lp trnh nhng ARM Linux Page 4

Lab 3. Programming with GPIO drivers Bi tp lm thm. Bi 1: Vit chng trnh LedDuoi to hiu ng led ui vi mt tham s i km l tc chy (tnh bng ms). Chng trnh cho php in ra thng bo hng dn s dng nu ngi dng la chn option help. LedDuoi help LedDuoi 1000 -> in ra hng dn s dng ->chy hiu ng led ui vi tr l 1000ms

Bi 2: Vit chng trnh to hiu ng led ui, c th thay i tc bng nt bm (1 nt tng tc , mt nt gim tc )

Lp trnh nhng ARM Linux

Page 5

You might also like