[vc_row][vc_column width=”1/1″][vc_column_text]
This demo will tell you to how to use photoresistor to control LED, if the environment is too dark, the LED will turn on.
[/vc_column_text][vc_tour][vc_tab title=”Hardware list” tab_id=”1450923552-1-12″][vc_column_text]
- 96board
- Liner Base Mezzanine Card
- Photoresistor module of Linker Kit
- LED module of Linker kit
- 4-pin cable x 2
[/vc_column_text][/vc_tab][vc_tab title=”Steps” tab_id=”1450923552-2-98″][vc_column_text]
Connect
Connect the photoresistor module to ADC1 port on Linker Base Mezzanine Card using 4-pin cable. This let SIG interface of photoresistor module connect to A0 port. Then, connect LED module to D2 port.
[/vc_column_text][/vc_tab][vc_tab title=”Program” tab_id=”1450923566047-2-7″][vc_column_text]#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/spi/spidev.h>
#include <linux/types.h>
#include <inttypes.h>
#include “libsoc_spi.h”
#include “libsoc_gpio.h”
#include “libsoc_debug.h”
#define GPIO_CS 18
#define GPIO_LED 13
static uint8_t tx[3],rx[3];
int main()
{
int adc_value;
gpio *gpio_cs,*gpio_led;
libsoc_set_debug(0);
gpio_cs = libsoc_gpio_request(GPIO_CS,LS_SHARED);
gpio_led = libsoc_gpio_request(GPIO_LED,LS_SHARED);
if((gpio_cs == NULL) || gpio_led == NULL)
{
goto fail;
}
libsoc_gpio_set_direction(gpio_cs,OUTPUT);
libsoc_gpio_set_direction(gpio_led,OUTPUT);
if((libsoc_gpio_get_direction(gpio_cs) != OUTPUT)
|| libsoc_gpio_get_direction(gpio_led) != OUTPUT)
{
printf(“Failed to set direction to OUTPUT!\n”);
goto fail;
}
spi *spi_dev = libsoc_spi_init(32766,0);
if(!spi_dev)
{
printf(“Failed to get spidev device!\n”);
return EXIT_FAILURE;
}
libsoc_spi_set_mode(spi_dev,MODE_0);
libsoc_spi_get_mode(spi_dev);
libsoc_spi_set_speed(spi_dev,10000);
libsoc_spi_get_speed(spi_dev);
libsoc_spi_set_bits_per_word(spi_dev,BITS_8);
libsoc_spi_get_bits_per_word(spi_dev);
tx[0] = 0x01;
tx[1] = 0x80;
tx[2] = 0x00;
while(1)
{
libsoc_gpio_set_level(gpio_cs,HIGH);
usleep(10);
libsoc_gpio_set_level(gpio_cs,LOW);
libsoc_spi_rw(spi_dev,tx,rx,3);
libsoc_gpio_set_level(gpio_cs,HIGH);
adc_value = (rx[1]<<8)&0b1100000000;
adc_value |= (rx[2]&0xff);
printf(“adc_value:%d\n”,adc_value);
if(adc_value == 0)
{
libsoc_gpio_set_level(gpio_led,HIGH);
}
if(adc_value <= 1023)
{
libsoc_gpio_set_level(gpio_led,LOW);
}
sleep(1);
}
free:
libsoc_spi_free(spi_dev);
fail:
if(gpio_cs)
{
libsoc_gpio_free(gpio_cs);
libsoc_gpio_free(gpio_led);
}
return EXIT_SUCCESS;
}
[/vc_column_text][/vc_tab][vc_tab title=”Compile and run” tab_id=”1450923567217-3-7″][vc_column_text]
gcc spi_test.c -o spi_test -lsoc sudo ./spi_test
The program will read the voltage of photoresistor and display on the terminal, if the value change to 0 when you cover photoresistor with something, the LED will light.
[/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.