Go Down

Topic: 7 Segment Display using SPI (Read 12129 times) previous topic - next topic

Busaboi

Anybody have any experience with this 4 digit display using SPI?
It's from Sparkfun, item COM-09230
http://www.sparkfun.com/commerce/product_info.php?products_id=9230
I've looked at the SPI tutorials on Arduino.cc but can't quite make the jump when it comes to sending 4 byte hex packets.


Busaboi

Well now...after talking to myself late into the the night and with a great deal of help from fellow Arduinophile Quazar, we have this sample code for y'all to use to help get up the learning curve for this Serial LED Display from Sparkfun (COM-09230).

Hmmm, now lemme see what's cold and frosty in the fridge  ;)

Wiring:
Display MOSI <--> Mini pin 11 (Across from D8) Blue
Display CSN <--> Mini pin 10 (Across from D9) Green
Display SCK <--> Mini pin 13 (Across from D6) Yellow
Display Vcc <--> Mini Vcc (Across from GND) Red
Display GND <--> Mini GND (Across from RXI) Black

Colors refer to a jpeg that I have somewhere....


Code: [Select]

// Send alphanumeric data to the Sparkfun Serial LED Display (COM-09230) using SPI
// Tested using Arduino Pro Mini w/ ATMega168 @ 5V
// July 21, 2009  - Quazar & Busaboi
// No guarantees expressed or implied just a good starting point
// Based upon the many SPI tutorials on Arduino.cc
//
// "num" specifies the number to display
// "base" specifies the base to use (2-16).
//    Use 2 for binary, 8 for octal, 10 for decimal, or 16 for hex
// "pad" indicates whether leading zeros should be replaced with spaces.
//    pad==0 means spaces ("   0"), pad==1 means zeros ("0000")
//
// Notes: The display's decimal/punctuation indicators are not changed.
// Numbers that don't fit into 4 digits show as " OF " for "Overflow".
// Assumptions: "unsigned short" is assumed to be at least 16b wide.

#define DATAOUT 11 //MOSI
#define DATAIN 12 //MISO - not used, but part of builtin SPI
#define SPICLOCK 13 //sck
#define SLAVESELECT 10 //ss

char spi_transfer(volatile char data)
{
 SPDR = data;                    // Start the transmission
 while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
 {
 };
 return SPDR;                    // return the received byte
}

void setup()
{
 byte clr;
 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK, OUTPUT);
 pinMode(SLAVESELECT, OUTPUT);
 digitalWrite(SLAVESELECT, HIGH); //disable device
 // SPCR = 01010010
 //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
 //sample on leading edge of clk,system clock/64
 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
 clr=SPSR;
 clr=SPDR;
 delay(10);
 write_led_numbers(0x78,0x78,0x78,0x78); //Blank display
 write_led_decimals(0x00); // All decimal points off
}

void write_led_decimals(int value)
{
  digitalWrite(SLAVESELECT, LOW);
  delay(10);
  spi_transfer(0x77);     // Decimal Point OpCode
  spi_transfer(value);    // Decimal Point Values
  digitalWrite(SLAVESELECT, HIGH); //release chip, signal end transfer
}
void write_led_numbers(int digit1, int digit2, int digit3, int digit4)
{
  digitalWrite(SLAVESELECT, LOW);
  delay(10);
  spi_transfer(digit1);    // Thousands Digit
  spi_transfer(digit2);    // Hundreds Digit
  spi_transfer(digit3);    // Tens Digit
  spi_transfer(digit4);    // Ones Digit
  digitalWrite(SLAVESELECT, HIGH); //release chip, signal end transfer
}
void write_led(unsigned short num, unsigned short base, unsigned short pad)
{
   unsigned short digit[4] = { 0, ' ', ' ', ' ' };
   unsigned short place = 0;

   if ( (base<2) || (base>16) || (num>(base*base*base*base-1)) ) {
       write_led_numbers(' ', 0x00, 0x0f, ' ');  // indicate overflow
   } else {
       while ( (num || pad) && (place<4) ) {
           if ( (num>0)  || pad )
               digit[place++] = num % base;
           num /= base;
       }
       write_led_numbers(digit[3], digit[2], digit[1], digit[0]);
   }
}

void loop()
{
for (int i = 0; i < 9999; i++) {
    write_led (i,10,0);
    delay(100);
  }

}

Giannis

Quote
Well now...after talking to myself late into the the night and with a great deal of help from fellow Arduinophile Quazar, we have this sample code for y'all to use to help get up the learning curve for this Serial LED Display from Sparkfun (COM-09230).


Hey!! It worked like a charm! Thank you!

mmmmm.
tried it on a Duemilanove,
MOSI and CSN on a pwn pin (10 and 11) and SCK on pin 12.
only 0000 on the display. (which are not always the numbers I'm looking for ;-) ) anybody tries it on a Duemilanove board?

Busaboi

Fred, try changing the spi bus freq by sending (<<SSPR0) instead of (1<<SPR1).  This will divide by 16 and not 64.

Also, check these pins (ref Arduino.cc SPI library page)

pin 13      SCK      SPI clock      
pin 12      MISO      SPI master in, slave out
pin 11      MOSI      SPI master out, slave in
pin 10      SS      SPI slave select

Hope this helps!

Blake Arrington

Hey, I got 4 of these and am still floundering a little with the SPI.

Have you used this in a project yet?  I got your code to work great, but I am trying to do some other stuff..

I have 1 arduino I want to display
      1)  Elapsed Time
      2)  Temperature
      3)  Calculation (based on those first two numbers)

The whole Master/Slave coding I haven't discovered yet.  Any help guys?

Thanks,

BLake

pwillard

#7
Feb 20, 2010, 04:12 pm Last Edit: Feb 20, 2010, 04:16 pm by pwillard Reason: 1
You do have a CHIP SELECT pin assigned that is unique to each device, right?

http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

Ran Talbott

Quote
Hey, I got 4 of these and am still floundering a little with the SPI.

Do you want to want to connect multiple displays to the Arduino?

If so,  you need to wire MOSI and SCLK on all chips together,  and wire the SS pin (which Sparkfun calls "CSN") of each display to a different digital output on the Arduino.

Then,  instead of using a #define'd constant like "SLAVESELECT" as Busaboi's code does,  you'd use a variable,  and set it to the digital output pin number for the display you want to update.


Blake Arrington

Thanks guys.  As far as the wiring goes I am all set.  I have the three displays wires all on the same bus except for the Slave "CSN" wire which is on a different pin on the Arduino.  For me, that was the easy part.

The code on the other hand is what I am trying to learn how to do.  I was looking for more example code to help me figure this SPI thing out.  

@Ran, I was wondering if Slaveselect in Busaboi's code was a special function from the library, so you cleared that up for me.   And yes, I need all three displays to simultaneously display constantly updating information.

Also, Busaboi doesn't call up the SPI.h library that I have seen.  Is it unnecessary to do so?

Ran Talbott

Quote
Also, Busaboi doesn't call up the SPI.h library that I have seen.

The SPI library is very small.  Busaboi just put the code he needed inline.

Assuming his code is good for 1 display (which I think has already been confirmed),  simply changing the slave select from a constant to a variable should be all you need to get it to work for 3.

There's not a whole lot to understand with SPI:  the really hard part is figuring out the "mode" (clock polarity and phasing),  because SPI didn't start out as an official standard.  So different chip makers did their own variations,  and SPI never got formalized like I2C or USB.  Sometimes you also need to be careful with the timing of slave select,  or the pacing of transmitted bytes,  but usually you can just go as fast as possible.

You've already got that right,  so it's just a matter of stuffing bytes into the SPI transmit register in groups of 4 (and,  in this case,  ignoring the received data).


cowanrg

works just fine for me but i can't figure out how to send commands to change brightness or turn on the colon.  anyone know how to do this?

2xyo

Hi every body,

I'm new with arduino. I tried to display "1234" with serial like this :
Code: [Select]

RX      ->  PIN TX  
VCC   ->  PIN  3.3V
GND   ->  PIN GND


Code: [Select]
void setup(){
 Serial.begin(9600);
 delay(100);
 Serial.println(0x79);
 delay(100);
 Serial.println(0x02);
}
void loop() {
delay(1000);
Serial.println(1,BIN);
Serial.println(2,BIN);
Serial.println(3,BIN);
Serial.println(4,BIN);
}


It display lot of thing but but "1234" !

I writed this dirty code from the documentation on sparkfun website.


sys_spud

@2xyo - These 7 segment displays have two physical interfaces. This topic thread is about SPI, and you're trying to use the Serial interface.

See the thread at: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249525950/2 about using Serial with this display. Working code is shown in that thread.

Go Up