Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   convertir un int ( ou unsigned char ) (http://www.velocityreviews.com/forums/t955639-convertir-un-int-ou-unsigned-char.html)

Andre 12-19-2012 10:37 AM

convertir un int ( ou unsigned char )
 
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é

Johann Klammer 12-19-2012 10:50 AM

Re: convertir un int ( ou unsigned char )
 
Andre wrote:
> 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>


Shao Miller 12-19-2012 10:53 AM

Re: convertir un int ( ou unsigned char )
 
On 12/19/2012 05:37, Andre wrote:
> 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

Noob 12-19-2012 10:54 AM

Re: convertir un int ( ou unsigned char )
 
Andre wrote:
> 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.


Andre 12-19-2012 11:04 AM

Re: convertir un int ( ou unsigned char )
 
Le 19/12/2012 11:53, Shao Miller a écrit :
> On 12/19/2012 05:37, Andre wrote:
>> 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

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é

BartC 12-19-2012 11:37 AM

Re: convertir un int ( ou unsigned char )
 


"Andre" <pas.mois@ailleur.be> wrote in message
news:50d19f28$0$3104$ba620e4c@news.skynet.be...

> 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("");
}
}

--
Bartc

Andre 12-19-2012 12:48 PM

Re: convertir un int ( ou unsigned char )
 
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é

BartC 12-19-2012 12:55 PM

Re: convertir un int ( ou unsigned char )
 
"Andre" <pas.mois@ailleur.be> wrote in message
news:50d19f28$0$3104$ba620e4c@news.skynet.be...

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.

--
Bartc



Keith Thompson 12-19-2012 11:43 PM

Re: convertir un int ( ou unsigned char )
 
"BartC" <bc@freeuk.com> writes:
> "Andre" <pas.mois@ailleur.be> wrote in message
> news:50d19f28$0$3104$ba620e4c@news.skynet.be...
>
>> 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("");
> }
> }


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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

BartC 12-20-2012 12:26 AM

Re: convertir un int ( ou unsigned char )
 
"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnmwx9r4kr.fsf@nuthaus.mib.org...
> "BartC" <bc@freeuk.com> writes:
>> "Andre" <pas.mois@ailleur.be> wrote in message


>>> 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.


>> 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));
>> }


> 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.

--
Bartc



All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.