convertir un int ( ou unsigned char )

A

Andre

to ascii ..
Qq chose comme ferais
printf("%d",truc);
ou plutôt avec scanf(..)
seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
l'espace mémoire pour utiliser scanf.
Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
format UTC.
Qq un a une idée??
merci d'avance
André
 
J

Johann Klammer

Andre said:
to ascii ..
Qq chose comme ferais
printf("%d",truc);
ou plutôt avec scanf(..)
seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
l'espace mémoire pour utiliser scanf.
Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
format UTC.
Qq un a une idée??
merci d'avance
André

Something along these lines? Was used with an atmel uC... Converts
unsigned... Outputs characters directly to a lcd... The division by ten
may be a bit costly... not sure how to avoid those...

<code>

#include <stdint.h>

static void print_v(uint32_t value)
{
uint8_t s=0,c=9,digit;
uint32_t cv=1000000000;
while(c--)
{
digit = '0';
while( value >= cv )
{
s=1;
digit++;// Increment first digit
value -= cv;
}
if(s)
{
lcd_putchar(digit,0);
}
else lcd_putchar(' ',0);
cv/=10;
}
lcd_putchar('0' + value,0);
lcd_putchar('\n',0);
}

</code>
 
S

Shao Miller

to ascii ..
Qq chose comme ferais
printf("%d",truc);
ou plutôt avec scanf(..)
seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
l'espace mémoire pour utiliser scanf.
Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
format UTC.
Qq un a une idée??
merci d'avance

Google translated as:
to ascii ..
'd Like sth
printf ("% d", foo);
or rather with scanf (..)
just as it should work on pic16f877 u I do not have the space to use scanf memory.
The goal is to display the time on a lcd. Time being available in UTC.
Qq one has a idea?
thank you in advance

Do you want 'asctime' in the time.h header? Are you trying to ask a
user for a decimal number?

- Shao Miller
 
N

Noob

Andre said:
to ascii ..
Qq chose comme ferais
printf("%d",truc);
ou plutôt avec scanf(..)
seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
l'espace mémoire pour utiliser scanf.
Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
format UTC.
Qq un a une idée??

Hello André,

I think you meant to post to fr.comp.lang.c (where they speak French).

If you only have to deal with base-10 numbers:

char convert_val_to_text_representation(char val)
{
return val + '0';
}

or even a lookup table : char map[10] = "0123456789"

char c = map[val];

Regards.
 
A

Andre

Le 19/12/2012 11:53, Shao Miller a écrit :
Google translated as:

Do you want 'asctime' in the time.h header? Are you trying to ask a user
for a decimal number?

- Shao Miller
Sorry I post this by mistake on the wrong group.
Many thanks for your answers ( to every body)
I have the hours as a int (0-24 ) or 0x00 to 0x18 )
so I need to display this on a LCDn two numbers 1 and 2 if time hour is 12.
without using scanf as I don't have enough memory to implement scanf on
a PIC16..
a lookup table is a solution but that require a lot of memory in the
'data space ' limited to 368 bytes..

André
 
B

BartC

I have the hours as a int (0-24 ) or 0x00 to 0x18 )
so I need to display this on a LCDn two numbers 1 and 2 if time hour is
12.
without using scanf as I don't have enough memory to implement scanf on
#include <stdio.h>
#include <stdlib.h>

void showtime(hours) {

if (hours<10) {
putchar('0');
putchar('0'+hours);
}
else if (hours<20) {
putchar('1');
putchar('0'+(hours-10));
}
else {
putchar('2');
putchar('0'+(hours-20));
}
}

int main(void) {
int i;

for (i=0; i<=24; ++i) {
showtime(i);
puts("");
}
}
 
A

Andre

Le 19/12/2012 11:37, Andre a écrit :
to ascii ..
Qq chose comme ferais
printf("%d",truc);
ou plutôt avec scanf(..)
seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
l'espace mémoire pour utiliser scanf.
Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
format UTC.
Qq un a une idée??
merci d'avance
André
Many thanks to everybody..
André
 
B

BartC

I have the hours as a int (0-24 ) or 0x00 to 0x18 )
so I need to display this on a LCDn two numbers 1 and 2 if time hour is
12.
without using scanf as I don't have enough memory to implement scanf on a
PIC16..

You keep mentioning scanf. scanf is normally used to convert characters into
numbers. Do you mean printf? This converts numbers into characters.

What does the LCD interface need anyway: actual ASCII codes ('0' to '9'),
BCD codes (0 to 9/15), 7-segment codes, or something else entirely?

There is anyway another group comp.arch.embedded which I think specialises
in this.
 
K

Keith Thompson

BartC said:
#include <stdlib.h>

void showtime(hours) {

if (hours<10) {
putchar('0');
putchar('0'+hours);
}
else if (hours<20) {
putchar('1');
putchar('0'+(hours-10));
}
else {
putchar('2');
putchar('0'+(hours-20));
}
}

int main(void) {
int i;

for (i=0; i<=24; ++i) {
showtime(i);
puts("");
}
}

Simpler:

putchar('0'+hours/10);
putchar('0'+hours%10);

Your approach might make sense if integer division is horribly
expensive, but any performance gains are likely to be lost in the
time it takes to perform the output.
 
B

BartC

Simpler:

putchar('0'+hours/10);
putchar('0'+hours%10);

Your approach might make sense if integer division is horribly
expensive, but any performance gains are likely to be lost in the
time it takes to perform the output.

Yeah...

I didn't have division in mind at all, unless I was just subconsciously
avoiding it (this is a PIC after all).

However lookups will also work, although the OP thinks they will use up data
memory (ram presumably). But I'd be surprised if the PIC compiler couldn't
somehow put const data into read-only-memory which is usually more generous.
 
P

Phil Carmody

Keith Thompson said:
Simpler:

putchar('0'+hours/10);
putchar('0'+hours%10);

Your approach might make sense if integer division is horribly
expensive, but any performance gains are likely to be lost in the
time it takes to perform the output.

If integer division is expensive, most likely from a code-space PoV,
and I presume space is a more valuable commodity than time, then do
what everyone did 30 years ago...

void show2digits(int i) /* precondition 0 <= i <= 99 */
{
char tens='0';
while((i-=10) >= 0) {
++tens;
}
putchar(tens);
putchar(i+(10+'0'));
}

Depending on how the PIC handles flags, this may be better:

void show2digits(int i) /* precondition 0 <= i <= 99 */
{
char tens='0'-1;
do { ++tens } while ((i-=10) >= 0);
putchar(tens);
putchar(i+(10+'0'));
}

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top