About how to use 74HC595 to control the Seven-segment display

excuse me I want to use 74HC595 to control the Seven-segment display with imp,and i follow the website


but i don’t know how to connect the 74HC595 with my imp ,and i also don’t know how to work the imp pin to send data to 74HC595 ,i also test the arduino to do this,and it work well,i follow this website

i need the help like how to connect the pin ,like this in arduino

DS (pin 14) connect Arduino pin 11
ST_CP (pin 12, latch pin) connect Arduino pin 8
SH_CP (pin 11, clock pin) connect Arduino pin 12

and how the imp send data to 74HC595,like this in arduino

`void sevenSegWrite(byte digit) {
digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);

digitalWrite(latchPin, HIGH);
}`

thank you very much

Take a look at the pin mux page, which will tell you which imp pins support SPI, but by the looks of it, you just need three general-purpose pins of your choice, wired up to the
NXP 74HC595 shift register and the LED as shown in the blog post. BTW, the 74HC595’s datasheet can be found here.

You’ll need to adapt the blog’s Arduino code, so take a look at our porting guide. To start you off, the above code becomes something like:

`function sevenSegWrite(digit)
{
hardware.pin1.write(0)
shiftOut(hardware.pin2, hardware.pin5, 0, seven_seg_digits[digit])
hardware.pin1.write(1)
}

function shiftOut(data_pin, clock_pin, constant, data)
{
if (constant == 1)
{
for (local i = 8 ; i > 0 ; i–)
{
clock_pin.write(0);
data_pin.write(data & 0x80);
data = data << 1;
clock_pin.write(1);
}
}
else
{
for (local i = 0 ; i < 8 ; i++)
{
clock_pin.write(0);
data_pin.write(data & 0x01);
data = data >> 1;
clock_pin.write(1);
}
}
}`

This assume imp pin 1 is wired up to the 74HC595’s latch pin (ST_CP), pin 2 to the data pin (DS) and pin 5 to the clock pin (SH_CP). You would configure these with:

hardware.pin1.configure(DIGITAL_OUT) hardware.pin2.configure(DIGITAL_OUT) hardware.pin5.configure(DIGITAL_OUT)

thank you for your help ,i learn very much from this ,and i also use this to do something like follow

,this is thing to got facebook likes and show it on Seven-segment display
thank you very much

Nice!

willy810473, can you post the code u used for the imp?

this is the code i use
agent:

`function HttpGetWrapper (url, headers) {
local request = http.get(url, headers);
local response = request.sendsync();
return response;
}

function start_time(likes)
{

// register the HTTP handler
local resp = HttpGetWrapper("https://graph.facebook.com/CAVEEducation?fref=ts",{ "Content-Type" : "text/xml" });
//server.log(resp.body);
local data = http.jsondecode(resp.body)
server.log(data["likes"]);
//server.log(data["cover"]["source"]);
// Send the device a 'pong' message immediately

local likes = data["likes"];

device.send("pong", likes);
//device.send("pong", data);

}

// When we get a ‘ping’ message from the device, call start_time()
device.on(“ping”, start_time);
`
////////////////////////////////////////////////////////////////////
Device:

`hardware.pin1.configure(DIGITAL_OUT);
hardware.pin2.configure(DIGITAL_OUT);
hardware.pin5.configure(DIGITAL_OUT);

local seven_seg_digits = [
0xFC, // = 0
0x60, // = 1
0xDA, // = 2
0xF2, // = 3
0x66, // = 4
0xB6, // = 5
0xBE, // = 6
0xE0, // = 7
0xFE, // = 8
0xE6 // = 9
];

function sevenSegWrite(digit)
{
//hardware.pin1.write(0);
shiftOut(hardware.pin2, hardware.pin5, 0, seven_seg_digits[digit]);
//hardware.pin1.write(1);
}

function shiftOut(data_pin, clock_pin, constant, data)
{
if (constant == 1)
{
for (local i = 8 ; i > 0 ; i–)
{
clock_pin.write(0);
data_pin.write(data & 0x80);
data = data << 1;
clock_pin.write(1);
}
}
else
{
for (local i = 0 ; i < 8 ; i++)
{
clock_pin.write(0);
data_pin.write(data & 0x01);
data = data >> 1;
clock_pin.write(1);
}
}
}

function ping()
{
// Send a ‘ping’ message to the server with the current millis counter
agent.send(“ping”, hardware.millis());
}

function return_from_imp(likes)
{

hardware.pin1.write(0);
sevenSegWrite(likes/1000);
sevenSegWrite((likes%1000)/100);
sevenSegWrite((likes%100)/10);
sevenSegWrite(likes%10);
hardware.pin1.write(1);

server.log(likes);
// Wake up in 5 seconds and ping again
imp.wakeup(5.0, ping);

}

// When we get a ‘pong’ message from the agent, call return_from_imp()
agent.on(“pong”, return_from_imp);
// Start the ping-pong
ping();`

Hi!

I’m trying to do a simple task with a 74HC595. I just want to control 6 leds, on/off command. I’ve read the actual thread and was sure that your code will work for me. But, unfortunately it doesn’t. Leds are randomly on and off. That’s not good )) So i am wrong when i think that the shiftOut function that you use is the way to go for my simple task? I’m not an expert with squirrel coding so any help would ben appreciated.

Alain

Working code to control 6 LEDs. First bitd in bytes are counting from left to right, starting from 0, so if you want to power the first LED ( Q0 on shift register ) , you need this byte: 00000001 ( hex 0x01 ), to power the second led : 00000010 ( hex 0x02 ) , to power the first and second : 00000011 ( 0x03 ). Code ported from Arduino
`
storageClockInput <- hardware.pin1; // pinn 1 of shift register
shiftClockInput <- hardware.pin2; // pinn 11 of shift register
serialIn <- hardware.pin5; // pinn 14 of shift register
data <- 0x03;

storageClockInput.configure(DIGITAL_OUT,0);
shiftClockInput.configure(DIGITAL_OUT,0);
serialIn.configure(DIGITAL_OUT,0);
//
imp.wakeup(2.0, function() {
local i=0;
local pinState;
// hold low for as long as you are transmitting
storageClockInput.write(0);
for( i=7; i>=0; i–) {
shiftClockInput.write(0);
if ( data & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
server.log("Bits: "+pinState);
//Sets the pin to HIGH or LOW depending on pinState
serialIn.write(pinState);
//register shifts bits on upstroke of clock pin
shiftClockInput.write(1);
// //zero the data pin after shift to prevent bleed through
serialIn.write(0);
}
//stop shifting
shiftClockInput.write(0);
// send to pins/storage
//no longer needs to listen for information
storageClockInput.write(1);
});

`