## Displaying ADC value on 7 Segment ?

27 posts / 0 new
Author
Message

Guys,

I have :

```d = ntc_get_temp(adcresistance, (double)0.947070725e-3, (double)2.500662058e-4, (double)1.859992838e-7);
sprintf(tempCelcius,"%.2f",d);```

how can I display it on seven segment ?
the function :

`DigitTo7SegEncoder(unsigned char digit, unsigned char common)`

Thanks

`DigitTo7SegEncoder(tempCelcius,0)`

?

Are we supposed to guess what the two parameters to the function are?

Is it something you wrote or something you retrieved from the internet? If the latter did it not come with user manual?

OK, the internet then:

http://www.avr-tutorials.com/projects/atmega16-based-digital-clock

So, two things about that page. One is that there's a pretty good description of the routine at the point of implementation:

```/*
* Function Description:
* Encode a Decimal Digit 0-9 to its Seven Segment Equivalent.
*
* Function Arguments:
* digit - Decimal Digit to be Encoded
* common - Common Anode (0), Common Cathode(1)
* SegVal - Encoded Seven Segment Value
*
* Connections:
* Encoded SegVal is return in the other G-F-E-D-C-B-A that is A is the least
* significant bit (bit 0) and G bit 6.
*/```

and two, there are even sample invocations:

```  SegDataPort = DigitTo7SegEncoder(seconds%10,1);
SegCntrlPort = ~0x01;
SegDataPort = DigitTo7SegEncoder(seconds/10,1);
SegCntrlPort = ~0x02;
```

I simply fail to see how you are unable to work out how to use the function given those things?!?

I do sometimes wonder if you are a deliberate troll as no one could surely be so clueless?

What I don't understand, is how can I separate the first digit and the second one ??
For example I want to display 20 C?
tempCelcius value is 20 and seven segment 2 and 0, how to separate them ??

Oh for goodness sake! Are you blind? Do you not see the "% 10" and "/ 10" in their example invocations there? What do you think they are doing exactly?

There is a potential optimisation you could do as the C library has div() and ldiv() in <stdlib.h>:

That gives both the division and the remainder in one operation. The return is div_t or ldiv_t:

Am I right this way ?

``` sprintf(tempCelcius,"%.0f",d);
//display the value to seven segment
SegDataPort = DigitTo7SegEncoder(tempCelcius[0],0);
SegCntrlPort = ~0x02;
_delay_ms(1);
SegDataPort = DigitTo7SegEncoder(tempCelcius[1],0);
SegCntrlPort = ~0x01;
_delay_ms(1);```

The sprintf() delivers ASCII digits. You need a binary index (0..9). To convert '0'..'9' to 0..9 you subtract '0'.

But you don't seem to have handling for the decimal point? If you subtract '0' from '.' and use that to index segment data you are likely to get a very odd result!

do you mean :

`sprintf(tempCelcius,"%.0f",d); `

suppose to be

`sprintf(tempCelcius,"%.2f",d);`

?

You tell me. You know how you want this display to look, not me.

I tested with

```     usart_pstr("temperature: \n");
usart_transmit(tempCelcius[0]);
usart_pstr("\n");
usart_transmit(tempCelcius[1]);
usart_pstr("\n");```

I got on terminal:

temperature:
3
0

but when I displayed to 7 segment I got "88"

code :

``` SegDataPort = DigitTo7SegEncoder(tempCelcius[0],0);
SegCntrlPort = ~0x02;
_delay_ms(10);
SegDataPort = DigitTo7SegEncoder(tempCelcius[1],0);
SegCntrlPort = ~0x01;
_delay_ms(10);```

Anything missing here ? thanks

You are missing an ASCII chart.... Get one, print it and place it in your field of view. You will be surprised at how often you will use it.

Try and understand the difference between the binary value that is used to represent a human-readable character and the representation of a decimal value in binary.
for exmaple., the decimal value 123 can be represented in at least the following forms ;
- binary byte 01111011
- ASCII-encoded human-readable binary bytes 00110001 00110010 00110011

Learn how to use the various standard library routines such as atoi(), itoa(), sprintf(), etc etc etc etc, to convert from one form to another.

But first, read any documentation/comments/examples that comes with whatever routine you manage to find.

I got
tempCelcius[1] = 0x33 Hex
tempCelcius[0] = 0x32 Hex

How can I convert then to 3 and 2 on 'char' ?

I tried :
temp1 = atoi(tempCelcius[0]);
temp2 = atoi(tempCelcius[1]);

now I got 0 0 on seven segment...should I convert again integer to char ?

Thanks

I tried :

```d = ntc_get_temp(adcresistance, (double)0.947070725e-3, (double)2.500662058e-4, (double)1.859992838e-7);
sprintf(tempCelcius,"%.2c",d);```

I got 80 on seven segment now...??

See posts #4 and #6

If you don't know what the format codes in sprintf() actually do then don't randomly try things in the hope that something will work, because that is a bad way to learn anything.

The display routine you found will take a byte and display the binary value of that byte.
Look at the actual code of that routine., if the value you give it is not in the range of 0 to 9 then it will return rubbish.
(I will let you figure out why it returns rubbish in that scenario)

Anyway..., you need to give it a value in the range of binary 0 and binary 9 inclusive., and post #6 gives you two methods.

Learn what the '/' and '%' operators do. Those operators (and their equivalents in other languages) are absolutely fundamental to any programming that you will ever do.

div(,] is a way of doing the '/' and '%' in one statement., for the moment just use the '/' and '%' operators.

Sit down with pen and paper, do the process and see what you get when you do 24 / 10 and 24 % 10

I think it will be :

```sprintf(tempCelcius,"%.2f",d);
temp1 = atoi(tempCelcius)/10;
temp2 = atoi(tempCelcius)%10;
//display the value to seven segment
SegDataPort = DigitTo7SegEncoder(temp1,0);
SegCntrlPort = ~0x02;
_delay_ms(1);
SegDataPort = DigitTo7SegEncoder(temp2,0);
SegCntrlPort = ~0x01;
_delay_ms(1);```

isn't it ?

I want to display Perth....but got only the last 'h'...
if I put in never ending loop it works...

Quote:
while(1)
{
display_perth();

}

I don't want to use never ending loop,
how can I get that ?

Quote:

isn't it ?

WILL YOU EVER GET IT?!?: Tested it? How? Result?

Quote:

I do sometimes wonder if you are a deliberate troll as no one could surely be so clueless?

Sometimes? Lucky you..

Dear bianchi77: You are step by step losing the trust of the residents here at AVRfreaks. You need to shape up or you will eventually be abandoned completely. If you have some kind of impairment that causes your lack of focus, inability to read, failing memory and absent empathy etc then please tell us about it. We really are a tolerant and understanding bunch, and given an explanation we will understand any difficulties. Lacking an explanatiomn there will at some point be a limit to your strange behaviour.

Please react and respond. It will determine my future involvement in your threads. Lacking a response I will default to not participating.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Johan, you said it much nicer than i would have. Mr bianchi burned me a while ago, thus my limited responses.

@bianchi77:

Does this [url]https://www.google.de/maps/@-31.9688837,115.9313409,9z[/url] work for you?

For me, it displays Perth well enough...

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

As can be seen in my previous reply, for some posters here i abandoned "helpful mode" long ago. In my mind, it always takes two to obey to a given protocol to make communication work...

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

It's just a question, if you don't want to answer, you don't have to...and don't have to be angry or upset...take it easy mate...;)

Then you have no respect for our time and effort. You suck our life force. Do you like it when people screw you around?

I saw that movie. The pod people were grown in giant pea pods with a bunch of sticky goop all over them. After a while there weren't anymore normal people left. Like the embedded c world.

Imagecraft compiler user

Although unrelated, the ISR could be made a bit more efficient by using:

```
ISR(TIMER1_COMPA_vect){
if(++seconds == 60){
seconds = 0;
if(++minutes == 60){
minutes = 0;
if(++hours == 24)
hours = 0;
}
}
}
```

Rather than:

```
ISR(TIMER1_COMPA_vect)
{
seconds++;
if(seconds == 60)
{
seconds = 0;
minutes++;
}
if(minutes == 60)
{
minutes = 0;
hours++;
}
if(hours > 23)
hours = 0;
}
```

So most of the time we only have 1 increment and 1 logic check per interrupt. But for this application it wont change anything.