User Profile

Collapse

Profile Sidebar

Collapse
nigelmercier
nigelmercier
Last Activity: Sep 11 '10, 06:32 PM
Joined: Dec 28 '09
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • nigelmercier
    started a topic C: Splitting an integer number into digits?
    in C

    C: Splitting an integer number into digits?

    I'm working on a PIC project, and my brain has almost exploded! I'm hoping that by writing down my problem, that the answer will come to me, but please feel free to help!

    Background (mostly irrelevant to an outsider): I'm storing a number in a variable (say r, an unsigned integer) which represents the ratio of a pulley system. The ratio itself is a real number and unlikely to be an integer, but I need to store it as an integer for...
    See more | Go to post

  • D'OH! Thanks for the replies, all working now. You can tell it's been a while since I did any math!
    See more | Go to post

    Leave a comment:


  • nigelmercier
    started a topic Mixed calculations with integers and floats in C?
    in C

    Mixed calculations with integers and floats in C?

    I'm trying to do a calculation that should be simple, but I keep getting a spurious answer of zero. It involves the rate of fuel use of an engine, the formula I'm using is:
    Rate of fuel use = (RPM / Max RPM)^3 * (Max Engine Power / 3.3)
    where 3.3 is the number of kWh produced by 1 litre of fuel). My code is as follows:

    Code:
    // following in typedefs.h file
    typedef unsigned short int byte;
    
    int
    ...
    See more | Go to post

  • nigelmercier
    replied to More problems with sprintf?
    in C
    Yes, that is interesting. It certainly explains why I was getting results like 256:257, as the passed values would have been 0 (hours) and the number of minutes.

    The sprintf() function came with my compiler, strLCD5XY() is my own function, but simply takes a string argument and displays it at co-ordinates x, y. It has worked fine until now, and is used throughout the project....
    See more | Go to post

    Leave a comment:


  • nigelmercier
    replied to More problems with sprintf?
    in C
    Curiouser and curiouser ...

    I have created a new buffer (char zonk[15]), turned off the interrupts, removed all the other processing, and hard coded 23 and 59 into the calls to sprintf.

    Code:
    char * formatHours(int seconds) // argument ignored for test
    { // convert integer seconds timer to string: " HH:MM"
       char zonk[15] ; // buffer with odd name to make sure it is unique
    
      sprintf(zonk,
    ...
    See more | Go to post

    Leave a comment:


  • nigelmercier
    replied to More problems with sprintf?
    in C
    OK, so the sprintf buffer needs to be long enough to hold the possible values of my type byte (unsigned char). The mins calculation limits this to 59, and the hours is unlikely to get above 12 or so. Even if it did, it can't go above 255, so I figured that a buffer of 4 was OK. I'll try a longer buffer and report back.

    The value of seconds starts at 0, and increments every second. After a couple of minutes, (with a value of around...
    See more | Go to post

    Leave a comment:


  • nigelmercier
    replied to More problems with sprintf?
    in C
    Thanks for your reply, but I'm a bit confused. I thought the sprintf buffer had to be large enough to contain the string that is returned? This would never be more than two characters: 59 minuits, or 24 hours. It certainly never got past a couple of minutes in testing.

    There is a single thread, and I have checked the value of the return value before it returns, still wrong. In fact the values of the buffer just after the sprintf calls...
    See more | Go to post

    Leave a comment:


  • nigelmercier
    replied to More problems with sprintf?
    in C
    The initial call to the function starts with 0, then increments every second. I let it run for a couple of minutes while I checked my code; does it look OK?

    Yes, I'm sure I could do it with a single call, but I'm eating my elephant one byte at a time :)
    See more | Go to post

    Leave a comment:


  • nigelmercier
    started a topic More problems with sprintf?
    in C

    More problems with sprintf?

    [Repost in new thread, as a forum bump doesn't seem to work :)]

    Having discovered sprintf (see here), I realised that it was idea for my time conversion function, see this thread. So I took the plunge, and converted it, and it worked for a while, but now something has gone wrong!

    This function is passed an integer number of seconds, and returns a text string " HH:MM" derived from it. But it doesn't work anymore;...
    See more | Go to post

  • Having found sprintf, I realised that it was idea for my time conversion function, see this thread. So I took the plunge, and converted it, and it worked for a while, but now something has gone wrong!

    This function is passed an integer number of seconds, and returns a text string " HH:MM" derived from it. But it doesn't work anymore! Note that txt4[4] and txt7[7] are global scratchpad variables.

    Code:
    char * formatHours(int
    ...
    See more | Go to post

    Leave a comment:


  • nigelmercier
    started a topic Convert a rounded float to a string with sprintf?
    in C

    Convert a rounded float to a string with sprintf?

    I need to convert a float to a string, after rounding it to 1 decimal place.

    For example: float f = 1.264 the string should be "1.3".

    I have just discovered that I do have access to sprintf, although in my compiler it doesn't actually "print" anything (its on a PIC microcontroller ). The only problem is that I have no idea how to drive it!

    I've attached the relevant page from the help...
    See more | Go to post

  • nigelmercier
    replied to Confused by passing character arrays?
    in C
    What I meant was that I got the function to do what I expected, now I need to consider the undefined behavior.

    I have changed my return statement to:

    return (strcpy(txt7, time)) ;

    where txt7 is a global scratch variable. Is this OK?
    See more | Go to post

    Leave a comment:


  • nigelmercier
    replied to Confused by passing character arrays?
    in C
    ISWYM about the pointer, but strangely I got it to work:

    Code:
      ByteToStr(hrs, txt4) ;
      strcat(time, txt4 +1) ;
      strcat(time, "h") ;
    debug1 = mins ;
      ByteToStr(mins, txt4) ;
      strcat(time, txt4 +1) ;
      strcat(time, "m") ;
    
      return time ;
    See more | Go to post

    Leave a comment:


  • nigelmercier
    started a topic Confused by passing character arrays?
    in C

    Confused by passing character arrays?

    I'm still not comfortable with passing character arrays, and I think this is the problem with this code:

    Code:
    char * formatHours(int decimin)
    { // convert integer deci-mins (6 seconds) to string: "XXhYYm"
      byte hrs, mins ;
      char time[7] ;  // global scratch variable txt4 is also char [7]
      hrs =  decimin / (60 * 10) ; // convert 1/10s of a minute into hours
      mins = decimin % (60 * 10) ; //
    ...
    See more | Go to post

  • nigelmercier
    replied to Centering text on a display?
    in C
    Does strlen ignore leading spaces then?
    See more | Go to post

    Leave a comment:


  • nigelmercier
    started a topic Centering text on a display?
    in C

    Centering text on a display?

    Now I've got a result for my text formatting problem [clicky] thanks to weaknessforcats , my next task is to centre the text on the display.

    This isn't a normal display, it is a small LCD with custom drivers that I have written. I can manipulate where the cursor is, but first I need to calculate the horizontal offset based on the number (0 - 9999) that I want to display.

    My first thought was a series of if statements...
    See more | Go to post

  • nigelmercier
    replied to Copy selected characters between strings?
    in C
    I figured strncpy would be the one, but I'm not 100% sure on now to do the txt7+2 bit. I had tried this and it didn't work:

    strncpy(txt4, txt7[2], 4) ;

    I guess pointers are involved?

    [Later] Ignore that, it seems that txt7+2 is actually what I need, just like you said.

    Many thanks.
    See more | Go to post

    Leave a comment:


  • nigelmercier
    started a topic Copy selected characters between strings?
    in C

    Copy selected characters between strings?

    I have two strings which are defined thus:

    char txt4[4], txt7[7];

    The string txt7 holds the character representation of an integer (0 to 9999), via IntToStr, which is defined thus:

    Converts input signed integer number to a string. The output string has fixed width of 7 characters including null character at the end (string termination). The output string is right justified and the remaining positions on...
    See more | Go to post

  • [QUOTE=nigelmerc ier;3549785]

    Code:
    for (i = 0; [B]i == 13[/B]; i++)
    //should be i <= 13
    I've also found the error in the loop, very surprised none of you guys spotted it!

    For some reason I can't get it into my head that the second expression in a for statement is a while rather than a terminating condition! Must be all that BASIC I did in the 1970s.
    See more | Go to post

    Leave a comment:


  • All sorted. Thanks for giving me the idea where to look in the string library, I also discovered memset:

    Code:
    void upDisp(void)
    { // Update display
      char bargraph[15] = "              ";
      char reserve[15] =  "*RESERVE TANK*";
      byte level;
    // set level here
      if (level == 0)
      {memcpy(bargraph, reserve, 14);}
      else
      {memset(bargraph, 0x5C, level);} //
    ...
    See more | Go to post

    Leave a comment:

No activity results to display
Show More
Working...