[vc_row][vc_column width=”1/1″][vc_column_text]
This demo will show you how to use touch sensor to control LED.
[/vc_column_text][vc_tour][vc_tab title=”Hardware list” tab_id=”1450924304-1-11″][vc_column_text]
- 96board
- Liner Base Mezzanine Card
- LED module of Linker Kit
- 2 x 4-pin cable
- Touch sensor module of Linker Kit
[/vc_column_text][/vc_tab][vc_tab title=”Steps” tab_id=”1450924304-2-68″][vc_column_text]Connect the touch sensor to D1 port and LED module to D2.
[/vc_column_text][/vc_tab][vc_tab title=”Program” tab_id=”1450924321301-2-8″][vc_column_text]
#include <stdio.h> #include <stdlib.h> #include "libsoc_gpio.h" #include "libsoc_debug.h" #define GPIO_LED 13 #define GPIO_BUTTON 36 int main() { gpio *gpio_led,*gpio_button; libsoc_set_debug(1); gpio_led = libsoc_gpio_request(GPIO_LED,LS_SHARED); gpio_button = libsoc_gpio_request(GPIO_BUTTON,LS_SHARED); if((gpio_led == NULL) || (gpio_button == NULL)) { goto fail; } libsoc_gpio_set_direction(gpio_led,OUTPUT); libsoc_gpio_set_direction(gpio_button,INPUT); if((libsoc_gpio_get_direction(gpio_led) != OUTPUT) || (libsoc_gpio_get_direction(gpio_button) != INPUT)) { goto fail; } while(1) { int n = libsoc_gpio_get_level(gpio_button); if(n == HIGH) { while(1) { libsoc_gpio_set_level(gpio_led,HIGH); sleep(1); libsoc_gpio_set_level(gpio_led,LOW); sleep(1); n = libsoc_gpio_get_level(gpio_button); if(n == HIGH) break; } } libsoc_gpio_set_level(gpio_led,LOW); usleep(1000000); } fail: if(gpio_led || gpio_button) { printf("apply gpio resource fail!\n"); libsoc_gpio_free(gpio_led); libsoc_gpio_free(gpio_button); } return EXIT_SUCCESS; }
[/vc_column_text][/vc_tab][vc_tab title=”Compile and run” tab_id=”1450924322299-3-8″][vc_column_text]gcc ButtonControlLed.c -o ButtonControlLed -lsoc
sudo ./ButtonControlLed
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.