/*
---------------------------------------------------------
* |
Intel Edison C++ Example Code |
*
| CIRC-05 .: 8 More LEDs :.
(74HC595 Shift Register) |
*
---------------------------------------------------------
*
* We have already controlled 8 LEDs however this
does it in a slightly
* different manner. Rather than using 8 pins we
will use just three
*
and an additional chip.
*
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa/gpio.h>
//Pin Definitions
//The 74HC595 uses a serial communication
//link which has three pins
#define DIR_CLOCK 4
#define DIR_EN 7
#define DIR_SER 8
#define DIR_LATCH 12
#define LOW 0
#define HIGH 1
#define MSBFIRST 1
#define LSBFIRST 0
using namespace std;
sig_atomic_t volatile isrunning = 1;
void sig_handler(int signum);
void
shiftOut(mraa_gpio_context dataPin, mraa_gpio_context clockPin, uint8_t
bitOrder, uint8_t val);
//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
int main()
{
signal(SIGINT, &sig_handler);
mraa_init();
mraa_gpio_context
data_pin;
mraa_gpio_context
clock_pin;
mraa_gpio_context
latch_pin;
mraa_gpio_context
enable_pin;
data_pin
= mraa_gpio_init(DIR_SER);
clock_pin=
mraa_gpio_init(DIR_CLOCK);
latch_pin=
mraa_gpio_init(DIR_LATCH);
enable_pin = mraa_gpio_init(DIR_EN);
mraa_gpio_dir(data_pin, MRAA_GPIO_OUT);
mraa_gpio_dir(clock_pin,
MRAA_GPIO_OUT);
mraa_gpio_dir(latch_pin,
MRAA_GPIO_OUT);
mraa_gpio_dir(enable_pin,
MRAA_GPIO_OUT);
while (isrunning)
{
int delayTime = 100000; //the
number of microseconds to delay between LED updates
for (int i =
0; i < 256; i++)
{
mraa_gpio_write(latch_pin,
LOW); //Pulls the chips latch low
shiftOut(data_pin,
clock_pin, MSBFIRST, i);
//Shifts out the 8 bits to the shift register
mraa_gpio_write(latch_pin,
HIGH); //Pulls the latch high displaying
the data
usleep(delayTime);
printf("%d\n",i);
}
}
return MRAA_SUCCESS;
}
void sig_handler(int signum)
{
if (signum == SIGINT) isrunning = 0;
}
void
shiftOut(mraa_gpio_context dataPin,mraa_gpio_context clockPin,
uint8_t bitOrder,uint8_tval)
{
uint8_t
i;
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST)
mraa_gpio_write(dataPin, !!(val
& (1 << i)));
else
mraa_gpio_write(dataPin, !!(val
& (1 << (7 - i))));
mraa_gpio_write(clockPin, HIGH);
mraa_gpio_write(clockPin, LOW);
}
}
No comments:
Post a Comment