Pages: [1]   Go Down
Author Topic: Code for driving a 7-segment display  (Read 2295 times)
0 Members and 1 Guest are viewing this topic.
Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Electrical Engineers Rule the World.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Greetings,
I just got my first arduino board, the Nano, and for one of my first projects I wanted to make a digital thermometer. I have some C/C++ knowledge, but it is limited. I am using an LM34 temp sensor and I want to drive a 2-digit 7-segment LED display (Common Cathode).

The part number is obscure, and I cannot find a data sheet. Assume I can figure out which pin goes to what LED, which isn't a difficult task. That is not the problem.

I will be driving it with two 74LS48, which is a BCD to 7-segment display driver. This is because there are only 12 digital I/O pins (leaving the possibility for 3 displays)

Say the temp sensor reads 0.7V for 70F, and I use the 1.1V internal analogReference. The arduino would ideally detect (0.7V / 1.1V)*1023 = (651)10 or (1010001011)2 on its ADC output.

I would want to display 70 on the display, which would be BCD number 0111 0000 (This is the data which would be sent in parallel to the two 74LS48s)

Say the program does the following and converts the analogRead to a familiar decimal (integer) temperature reading:

Code:
 
temp = analogRead(LM34_pin);           // stores the digitized (0 - 1023) analog reading from the LM34
fahrenheit = (100*temp*(vref))/1023;   // calculates the actual fahrenheit temperature


How would I convert the decimal 70 to a form that will drive the 8 pins driving the display drivers (drive!)? (I can skip the conversion altogether if need be)

I have been searching the forums and internet for code to steal, but I have not had any luck. I would appreciate any help.

Thanks in advance.
Logged

- Scott

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How would I convert the decimal 70 to a form that will drive the 8 pins driving the display drivers
Assume the two numbers you want to output are msb and lsb (most significant bit & least significant bit)
Then:-

display = 70;
msb = display / 10;
lsb = display - (msb * 10);

would give msb = 7 and lsb = 0
Is that what you mean?
Logged

Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Electrical Engineers Rule the World.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is definitely helpful, thank you. Don't know why I didn't think of that part on my own, but then I need to convert that to BCD and send each digit to a separate digital output. I have thought of ways to do this, but I'd like the most efficient way someone can come up with, and my way isn't efficient.

Thanks.
Logged

- Scott

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but then I need to convert that to BCD and send each digit to a separate digital output

But that is BCD code. Each digit is in a separate variable. Each digit is a binary number. Just send each digit to the pins you have wired up your seven segment decoder up to.
Logged

Montreal
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

display = 70;
msb = display / 10;
lsb = display % 10;

would give msb = 7 and lsb = 0 also.

Logged

Patgadget
Montreal

Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Electrical Engineers Rule the World.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Say I had decimal 7 in the MSB variable, I would need to take that and turn it into it's BCD equivalent, 0111, and send it to 4 digital pins separately. Using a total of 8 pins to drive two BCD to 7-segment display drivers.

Again, for the number 70, I would want the following outputs:
(Pins chosen arbitrarily)

MSB = 7

2 = (MSB 1) = LOW
3 = HIGH
4 = HIGH
5 = (LSB 1) = HIGH

LSB = 0

6 = (MSB 2) = LOW
7 = LOW
8 = LOW
9 = (LSB 2) = LOW

Logged

- Scott

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try something like this:-

Code:
int msbPins[] = {2,3,4,5};  // pins to use for msb
int lsbPins[] = {6,7,8,9};
int val;

msb = 7;
lsb = 0;

for(int i=0; i<4; i++){
 val = msb & 1; //  isolate last bit of msb
digitalWrite(msbPin[i],val); // send to correct pin
 val = lsb & 1; //  isolate last bit of lsb
digitalWrite(lsbPin[i],val);
msb = msb >> 1; // shift the bits down so the next one is in position zero
lsb = lsb >> 1;
}

Use an array to define the order of your pins wired up to the driver.
Output high or low for the least significant bit of your number (msb and lsb)
then shift msb & lsb one place to the right so the next least significant bit becomes the least significant bit, loop round and do it again.

Note this is just written not compiled, it is not a complete programme.
« Last Edit: February 02, 2009, 10:40:29 am by Grumpy_Mike » Logged

Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Electrical Engineers Rule the World.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I'll take a look at that when I get off work.
Logged

- Scott

Pages: [1]   Go Up
Jump to: