Skip to content

Commit eea80a8

Browse files
committed
Added I2C interface example.
Added a really simple I2C example sketch. Cleaned up some residual stuff in the main firmware. Moved the I2C receive event down below the main loop because it's not really an ISR.
1 parent ad900d9 commit eea80a8

File tree

2 files changed

+83
-23
lines changed

2 files changed

+83
-23
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
11-2-2012
3+
Spark Fun Electronics
4+
Nathan Seidle
5+
6+
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
Serial7Segment is an open source seven segment display.
9+
10+
This is example code that shows how to send data over I2C to the display.
11+
12+
Note: This code expects the display to be listening at the default I2C address. If your display is not at 0x71, you can
13+
do a software or hardware reset. See the Wiki for more info:
14+
http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands
15+
16+
To get this code to work, attached an Serial7Segment to an Arduino Uno using the following pins:
17+
A5 to SCL
18+
A4 to SDA
19+
VIN to PWR
20+
GND to GND
21+
22+
*/
23+
24+
#include <Wire.h>
25+
26+
#define DISPLAY_ADDRESS1 0x71 //This is the default address of the OpenSegment with both solder jumpers open
27+
28+
int cycles = 0;
29+
30+
void setup()
31+
{
32+
Wire.begin(); //Join the bus as master
33+
34+
Serial.begin(9600); //Start serial communication at 9600 for debug statements
35+
Serial.println("OpenSegment Example Code");
36+
37+
//Send the reset command to the display - this forces the cursor to return to the beginning of the display
38+
Wire.beginTransmission(DISPLAY_ADDRESS1);
39+
Wire.write('v');
40+
Wire.endTransmission();
41+
}
42+
43+
void loop()
44+
{
45+
cycles++; //Counting cycles! Yay!
46+
Serial.print("Cycle: ");
47+
Serial.println(cycles);
48+
49+
i2cSendValue(cycles); //Send the four characters to the display
50+
51+
delay(1); //If we remove the slow debug statements, we need a very small delay to prevent flickering
52+
}
53+
54+
//Given a number, i2cSendValue chops up an integer into four values and sends them out over I2C
55+
void i2cSendValue(int tempCycles)
56+
{
57+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
58+
Wire.write(tempCycles / 1000); //Send the left most digit
59+
tempCycles %= 1000; //Now remove the left most digit from the number we want to display
60+
Wire.write(tempCycles / 100);
61+
tempCycles %= 100;
62+
Wire.write(tempCycles / 10);
63+
tempCycles %= 10;
64+
Wire.write(tempCycles); //Send the right most digit
65+
Wire.endTransmission(); //Stop I2C transmission
66+
}

firmware/Serial 7-Segment Display/Serial_7_Segment_Display_Firmware/Serial_7_Segment_Display_Firmware.ino

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,6 @@ ISR(SPI_STC_vect)
6868
interrupts(); // Fine, you were saying?
6969
}
7070

71-
// I2C byte receive interrupt routine
72-
// Note: this isn't an ISR. I'm using wire library (because it just works), so
73-
// Wire.onReceive(twiReceive); should be called
74-
void twiReceive(int rxCount)
75-
{
76-
while(Wire.available() > 0) // Do this while data is available in Wire buffer
77-
{
78-
unsigned int i = (buffer.head + 1) % BUFFER_SIZE; // read buffer head position and increment
79-
unsigned char c = Wire.read(); // Read data byte into c, from Wire data buffer
80-
81-
if (i != buffer.tail) // As long as the buffer isn't full, we can store the data in buffer
82-
{
83-
buffer.data[buffer.head] = c; // Store the data into the buffer's head
84-
buffer.head = i; // update buffer head, since we stored new data
85-
}
86-
}
87-
}
88-
8971
// The display data is updated on a Timer interrupt
9072
ISR(TIMER1_COMPA_vect)
9173
{
@@ -128,24 +110,36 @@ void loop()
128110
}
129111

130112
// This is effectively the UART0 byte received interrupt routine
131-
//ISR(USART_RX_vect)
132113
void serialEvent()
133114
{
134115
while (Serial.available())
135116
{
136-
//noInterrupts(); // We'll be quick...
137-
138117
unsigned int i = (buffer.head + 1) % BUFFER_SIZE; // read buffer head position and increment
139-
// unsigned char c = UDR0; // Read data byte into c, from UART0 data register
140118
unsigned char c = Serial.read(); // Read data byte into c, from UART0 data register
141119

142120
if (i != buffer.tail) // As long as the buffer isn't full, we can store the data in buffer
143121
{
144122
buffer.data[buffer.head] = c; // Store the data into the buffer's head
145123
buffer.head = i; // update buffer head, since we stored new data
146124
}
125+
}
126+
}
147127

148-
//interrupts(); // Okay, resume interrupts
128+
// I2C byte receive interrupt routine
129+
// Note: this isn't an ISR. I'm using wire library (because it just works), so
130+
// Wire.onReceive(twiReceive); should be called
131+
void twiReceive(int rxCount)
132+
{
133+
while(Wire.available()) // Do this while data is available in Wire buffer
134+
{
135+
unsigned int i = (buffer.head + 1) % BUFFER_SIZE; // read buffer head position and increment
136+
unsigned char c = Wire.read(); // Read data byte into c, from Wire data buffer
137+
138+
if (i != buffer.tail) // As long as the buffer isn't full, we can store the data in buffer
139+
{
140+
buffer.data[buffer.head] = c; // Store the data into the buffer's head
141+
buffer.head = i; // update buffer head, since we stored new data
142+
}
149143
}
150144
}
151145

0 commit comments

Comments
 (0)