Skip to content

Commit

Permalink
Merge pull request #6 from nseidle/master
Browse files Browse the repository at this point in the history
Support for SevSeg library and S7S hardware
  • Loading branch information
Jim Lindblom committed Nov 5, 2012
2 parents 1cc46f9 + 9f94cb6 commit 79ec0f9
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
11-2-2012
Spark Fun Electronics
Nathan Seidle
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Serial7Segment is an open source seven segment display.
This is example code that shows how to send data over SPI to the display.
For more information about the commands, be sure to visit:
http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands
To get this code to work, attached an OpenSegment to an Arduino Uno using the following pins:
Pin 10 on Uno (CS) to CS on OpenSegment
Pin 11 to MOSI
Pin 12 to MISO
Pin 13 to SCK
VIN to PWR
GND to GND
*/

#include <SPI.h>

int csPin = 10; //You can use any IO pin but for this example we use 10

int cycles = 0;

void setup()
{
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH); //By default, don't be selecting OpenSegment

Serial.begin(9600); //Start serial communication at 9600 for debug statements
Serial.println("OpenSegment Example Code");

SPI.begin(); //Start the SPI hardware
SPI.setClockDivider(SPI_CLOCK_DIV64); //Slow down the master a bit

//Send the reset command to the display - this forces the cursor to return to the beginning of the display
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer('v'); //Reset command
}

void loop()
{
cycles++; //Counting cycles! Yay!
Serial.print("Cycle: ");
Serial.println(cycles);

spiSendValue(cycles); //Send the four characters to the display

delay(1); //If we remove the slow debug statements, we need a very small delay to prevent flickering
}

//Given a number, spiSendValue chops up an integer into four values and sends them out over spi
void spiSendValue(int tempCycles)
{
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment

SPI.transfer(tempCycles / 1000); //Send the left most digit
tempCycles %= 1000; //Now remove the left most digit from the number we want to display
SPI.transfer(tempCycles / 100);
tempCycles %= 100;
SPI.transfer(tempCycles / 10);
tempCycles %= 10;
SPI.transfer(tempCycles); //Send the right most digit

digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
11-2-2012
Spark Fun Electronics
Nathan Seidle
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Serial7Segment is an open source seven segment display.
This is example code that shows how to send data over SPI to the display.
For more information about the commands, be sure to visit:
http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands
To get this code to work, attached an OpenSegment to an Arduino Uno using the following pins:
Pin 10 on Uno (CS) to CS on OpenSegment
Pin 11 to MOSI
Pin 12 to MISO
Pin 13 to SCK
VIN to PWR
GND to GND
*/

#include <SPI.h>

int csPin = 10; //You can use any IO pin but for this example we use 10

int cycles = 0;

void setup()
{
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH); //By default, don't be selecting OpenSegment

Serial.begin(9600); //Start serial communication at 9600 for debug statements
Serial.println("OpenSegment Example Code");

SPI.begin(); //Start the SPI hardware
SPI.setClockDivider(SPI_CLOCK_DIV16); //Slow down the master a bit

//Send the reset command to the display - this forces the cursor to return to the beginning of the display
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer('v'); //Reset command
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
}

void loop()
{
Serial.println("Low brightness"); //Just a debug statement
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer(0x7A); // Brightness control command
delay(1); //Small delay between bytes so S7S can buffer them
SPI.transfer(0); // Set brightness level: 0% to 100%
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
delay(1); //Small delay between bytes so S7S can buffer them

spiSendString("b000"); //Send the four characters to the display
delay(2000); //Hang out for a bit before we go to the next brightness level

Serial.println("Mid brightness"); //Just a debug statement
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer(0x7A); // Brightness control command
delay(1); //Small delay between bytes so S7S can buffer them
SPI.transfer(50); // Set brightness level: 0% to 100%
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
delay(1); //Small delay between bytes so S7S can buffer them

spiSendString("b050"); //Send the four characters to the display
delay(2000); //Hang out for a bit before we go to the next brightness level

Serial.println("High brightness"); //Just a debug statement
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer(0x7A); // Brightness control command
delay(1); //Small delay between bytes so S7S can buffer them
SPI.transfer(100); // Set brightness level: 0% to 100%
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
delay(1); //Small delay between bytes so S7S can buffer them

spiSendString("b100"); //Send the four characters to the display
delay(2000); //Hang out for a bit before we go to the next brightness level
}

//Given a string, spiSendString chops up the string and sends out the first four characters over spi
void spiSendString(char *toSend)
{
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer('v'); //Reset command
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment

digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
for(byte x = 0 ; x < 4 ; x++)
{
delay(1); //Small delay between bytes so S7S can buffer them
SPI.transfer(toSend[x]); //Send a character from the array out over I2C
}
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void loop()

Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

// delay(10);
delay(10);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
11-4-2012
Spark Fun Electronics
Nathan Seidle
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Serial7Segment is an open source seven segment display.
This is example code that shows how to display basic numbers and control the decimal, colon, and AM/PM dot.
Note: This code works with displays the support the center colon and extra dots (AM/PM, not decimals).
Note: This code expects the display to be listening at 9600bps. If your display is not at 9600bps, you can
do a software or hardware reset. See the Wiki for more info:
http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands#wiki-baud
To get this code to work, attached an Serial7Segment to an Arduino Uno using the following pins:
Pin 7 on Uno (software serial RX) to TX on Serial7Segment
Pin 8 on Uno to RX on Serial7Segment
VIN to PWR
GND to GND
*/

#include <SoftwareSerial.h>

SoftwareSerial Serial7Segment(7, 8); //RX pin, TX pin

int cycles = 0;

//From https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands#wiki-decimal
#define APOSTROPHE 5
#define COLON 4
#define DECIMAL4 3
#define DECIMAL3 2
#define DECIMAL2 1
#define DECIMAL1 0

long millisTimer;
byte seconds = 0;
byte minutes = 0;
byte hours = 0;
boolean amTime = true;
boolean colonOn = false;

char tempString[100]; //Used for sprintf

void setup() {

Serial.begin(9600);
Serial.println("OpenSegment Example Code");

Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display

Serial7Segment.print("0822"); //Send the hour and minutes to the display

millisTimer = millis();

//For testing, we initialize the variables to the current time
seconds = 57;
minutes = 59;
hours = 12;
}

void loop()
{
//Every second update the various variables and blink colon/apos/decimal
if( (millis() - millisTimer) > 1000)
{
millisTimer += 1000; //Adjust the timer forward 1 second

seconds++;
if(seconds > 59)
{
seconds -= 60; //Reset seconds and increment minutes
minutes++;
if(minutes > 59)
{
minutes -= 60; //Reset minutes and increment hours
hours++;
if(hours > 12)
{
hours -= 12; //Reset hours and flip AM/PM
if(amTime == true)
amTime = false; //Flip AM to PM
else
amTime = true;
}
}
}

//Blink the colon every other second
if(colonOn == true)
{
colonOn = false;
Serial7Segment.write(0x77); // Decimal, colon, apostrophe control command
Serial7Segment.write((byte) 0); // Turns off colon, apostrophoe, and all decimals
}
else
{
colonOn = true;
Serial7Segment.write(0x77); // Decimal, colon, apostrophe control command
Serial7Segment.write( (1<<APOSTROPHE) | (1<<COLON) | (1<<DECIMAL4) ); // Turns on colon, apostrophoe, and far-right decimal
}
}

//Debug print the time
sprintf(tempString, "HH:MM:SS %02d:%02d:%02d", hours, minutes, seconds);
Serial.println(tempString);

sprintf(tempString, "%02d%02d", minutes, seconds);
Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

delay(100);
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
9-23-2012
Spark Fun Electronics
Nathan Seidle
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Serial7Segment is an open source seven segment display.
Did you set your Serial7Segment display to a baud rate you don't know? This example sketch
should get you fixed up! This example code sends the factory reset command at 12 different baud rates.
For more information see: http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands#wiki-reset
To get this code to work, attached an Serial7Segment to an Arduino Uno using the following pins:
Pin 8 on Uno to RX on Serial7Segment
VIN to PWR
GND to GND
*/

#include <SoftwareSerial.h>

SoftwareSerial Serial7Segment(7, 8); //RX pin, TX pin

int cycles = 0;

void setup()
{
Serial.begin(9600); //Setup the debug terminal at regular 9600bps
Serial.println("Attempting to reset display to 9600bps...");

//Step through each available serial baud rate and send the factory reset command
int baudRates[12] = {2400, 4800, 9600, 14400, 19200, 38400, 57600, 7600, 115200, 250000, 500000, 1000000};
for (int i = 0 ; i < 12 ; i++)
{
Serial7Segment.begin(baudRates[i]); // Set new baud rate
delay(10); // Arduino needs a moment to setup serial
Serial7Segment.write(0x81); // Send factory reset command
}

Serial7Segment.begin(9600);
delay(10); // Arduino needs a moment to setup serial
Serial7Segment.print("0000"); //Send some characters out .print to clear out the buffer
Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
Serial7Segment.print("test"); //Display a test message on the screen

Serial.println("Attempt is now done. If the display says 'test', it has been reset to 9600bps");
}

void loop()
{
//Do nothing
}



Loading

0 comments on commit 79ec0f9

Please sign in to comment.