SergioNeto:
[...] So my teacher said it was ok to use arduino and do this by software, so I started to look for some codes out there but I only found codes that convert it directlly to the 7 segment display, but I just want the output from the Arduino to be in BCD format, like 4bits BCD for the units, and 4 bits for the tens, and so on.
TL;DR: I need a code that converts the analog output from the LM35 to BCD 4 bits for units and 4 bits to Tens.
That means you want to use external BCD-to-7 segment decoder like 4511. If so, your setup might become like this:
Figure-1: Non-multiplexed display unit
You may now follow these steps to acquire temperature signal from LM35 sensor and show only the integer part of the temperature onto DP0-DP1 positions of the 7-segment display unit.
1. First of all, upload the following codes to see the temperature value on Serial Monitor.
void setup()
{
Serial.begin(9600);
analogReference(INTERNAL);
}
void loop()
{
unsigned int tempC;
tempC = (int)100*(1.1/1024)*analogRead(A5); //tempC contains value in binary format
Serial.println(tempC); //print() command converts the binary value of tempC into decimal/BCD format
delay(1000);
}
2. Assume that the Serial Monitor shows: 23; it means that the value of the variable tempC = 000010111b (0x17 = 23 decimal). Now, to show the temperature value on DP0-DP1 positions of the 7-segment display unit, we have to convert the binary value 00010111b into 00100011 (23). How can we do it? The conversion can be done using modulus(%) and division (/) operators as has been suggested by @adwsystems in his Post#4.
tempC = 00010111; //assume
Let us divide tempC by 10 (0x0A); we will get 8-bit remainder (R1) as 00000011 (0x03 = 3 = 0011) and 8-bit quotient (Q1) as 00000010 (0x02 = 0010). The following operation will give us the R1: byte R1 = tempC%10. Now, send the value of R1 onto PB3 - PB0 by executing this instruction: PORTB = R1. The 4511 will automatically decode this BCD pattern and will show 3 at DP1 position of the display.
The following operation will give us the Q1: byte Q1 = tempC/10. Perform this modulus operation on Q1 to get R0 (00000010 = 0x02 = 0010): byte R0 = Q1%10. Now, send the value of R0 onto PC3 - PC0 by executing this instruction: PORTC = R0. The 4511 will automatically decode this BCD pattern and will show 2 at DP0 position of the display.
The codes are (untested): (you need to set the directions of PB3-PB0 and PC3-PC0 lines as outputs and copy other codes as needed from Step-1.)
unsigned tempC;
tempC = (int)100*(1.1/1024)*analogRead(A5);
byte R1 = tempC%10;
PORTB = R1;
byte Q1 = tempC/10; //or tempC = tempC/10;
byte R0 = Q1%10;
PORTC = R0
BTW: As you have been asked to use Arduino and software, you (probably) have to remove the 2x4511 decoders from the circuit of Fig-1. Now, the necessary cc-codes for the 4-bit BCD codes of unit/ten positions are to be collected from a lookup table. Are you familiar with the concept of lookup table? Something else more -- the display will become a multiplexed one of the following type.
Figure-2: Multiplexed display unit