|
| 1 | +/* |
| 2 | + OpenSegment is an 7-segment display with Serial/I2C/SPI interfaces. |
| 3 | + By: Nathan Seidle |
| 4 | + SparkFun Electronics |
| 5 | + Date: June 11th, 2015 |
| 6 | + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). |
| 7 | +
|
| 8 | + OpenSegment gives the user multiple interfaces (serial, I2C, and SPI) to control a four digit |
| 9 | + seven segment display. |
| 10 | + |
| 11 | + This example shows how to change the I2C or TWI address. |
| 12 | + |
| 13 | + Please Note: 0x71 is the 7-bit I2C address. If you are using a different language than Arduino you will probably |
| 14 | + need to add the Read/Write bit to the end of the address. This means the default read address for the OpenSegment |
| 15 | + is 0b.1110.0011 or 0xE3 and the write address is 0b.1110.0010 or 0xE2. |
| 16 | + For more information see https://learn.sparkfun.com/tutorials/i2c |
| 17 | +
|
| 18 | + Note: This code expects the display to be listening at the default I2C address. If your display is not at 0x71, you can |
| 19 | + do a software or hardware reset. See the Wiki for more info: |
| 20 | + http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands |
| 21 | + |
| 22 | + To get this code to work, attached a Serial7Segment to an Arduino Uno using the following pins: |
| 23 | + SCL (OpenSegment) to A5 (Arduino) |
| 24 | + SDA to A4 |
| 25 | + VIN to 5V |
| 26 | + GND to GND |
| 27 | +
|
| 28 | + For this example pull up resistors are not needed on SDA and SCL. If you have other devices on the |
| 29 | + I2C bus then 4.7k pull up resistors are recommended. |
| 30 | + |
| 31 | + OpenSegment will work at 400kHz Fast I2C. Use the .setClock() call shown below to set the data rate |
| 32 | + faster if needed. |
| 33 | +
|
| 34 | +*/ |
| 35 | + |
| 36 | +#include <Wire.h> |
| 37 | + |
| 38 | +#define DISPLAY_ADDRESS1 0x71 //This is the default address of the OpenSegment |
| 39 | +#define DISPLAY_ADDRESS_NEW 0x50 //This is the new I2C address we want to go to |
| 40 | + |
| 41 | +int cycles = 0; |
| 42 | + |
| 43 | +void setup() |
| 44 | +{ |
| 45 | + Wire.begin(); //Join the bus as master |
| 46 | + |
| 47 | + //By default .begin() will set I2C SCL to Standard Speed mode of 100kHz |
| 48 | + //Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz |
| 49 | + |
| 50 | + Serial.begin(9600); //Start serial communication at 9600 for debug statements |
| 51 | + Serial.println("OpenSegment Example Code"); |
| 52 | + |
| 53 | + //Send the reset command to the display - this forces the cursor to return to the beginning of the display |
| 54 | + Wire.beginTransmission(DISPLAY_ADDRESS1); |
| 55 | + Wire.write('v'); |
| 56 | + Wire.endTransmission(); |
| 57 | + |
| 58 | + //Send change address command |
| 59 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 60 | + Wire.write(0x80); //Send TWI address change command |
| 61 | + Wire.write(DISPLAY_ADDRESS_NEW); //New I2C address to use |
| 62 | + Wire.endTransmission(); //Stop I2C transmission |
| 63 | + |
| 64 | + Serial.println("I2C address changed to 0x50"); |
| 65 | + |
| 66 | + //Now we talk at this new address |
| 67 | + |
| 68 | + //Send the reset command to the display - this forces the cursor to return to the beginning of the display |
| 69 | + Wire.beginTransmission(DISPLAY_ADDRESS_NEW); |
| 70 | + Wire.write('v'); |
| 71 | + Wire.endTransmission(); |
| 72 | +} |
| 73 | + |
| 74 | +void loop() |
| 75 | +{ |
| 76 | + cycles++; //Counting cycles! Yay! |
| 77 | + Serial.print("Cycle: "); |
| 78 | + Serial.println(cycles); |
| 79 | + |
| 80 | + i2cSendValue(cycles); //Send the four characters to the display |
| 81 | + |
| 82 | + delay(1); //If we remove the slow debug statements, we need a very small delay to prevent flickering |
| 83 | +} |
| 84 | + |
| 85 | +//Given a number, i2cSendValue chops up an integer into four values and sends them out over I2C |
| 86 | +void i2cSendValue(int tempCycles) |
| 87 | +{ |
| 88 | + Wire.beginTransmission(DISPLAY_ADDRESS_NEW); // transmit to device at new I2C address |
| 89 | + Wire.write(tempCycles / 1000); //Send the left most digit |
| 90 | + tempCycles %= 1000; //Now remove the left most digit from the number we want to display |
| 91 | + Wire.write(tempCycles / 100); |
| 92 | + tempCycles %= 100; |
| 93 | + Wire.write(tempCycles / 10); |
| 94 | + tempCycles %= 10; |
| 95 | + Wire.write(tempCycles); //Send the right most digit |
| 96 | + Wire.endTransmission(); //Stop I2C transmission |
| 97 | +} |
0 commit comments