Autor Tema: sprintf me come muchos recursos en xc8  (Leído 2050 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado micro_pepe

  • Moderadores
  • DsPIC30
  • *****
  • Mensajes: 3206
sprintf me come muchos recursos en xc8
« en: 09 de Junio de 2016, 19:58:57 »
Si pongo esto en xc8 me come casi todos los recursos de un PIC16F88:

Código: [Seleccionar]
sprintf(texto, "%5.1fV %5.2fA", volt, amp);

Alguna otra forma de hacerlo, o me despido de mi 16F y le doy la vienvenida a un 18F.

Saludos!!!
Se obtiene más en dos meses interesandose por los demás, que en dos años tratando de que los demás se interesen por ti.

新年快乐     的好奇心的猫死亡

Desconectado elgarbe

  • Moderadores
  • PIC24H
  • *****
  • Mensajes: 2178
Re:sprintf me come muchos recursos en xc8
« Respuesta #1 en: 10 de Junio de 2016, 08:22:41 »
me parece que no hay otra, quizá alguna version reducida de la librería, muchas funciones de la stdio.h ocupan mucho espacio en flash...
No se si se linquea a la librería completa o solo a las funciones utilizadas... acá killer sabrá decir bien.
Sino, alguna implementacion no standar de sprintf si es que existe...

yo en su momento usaba estas funciones (para la uart, pero se puede usar para cualquier cosa) para imprimir bytes, flotantes, etc.

Código: C
  1. // ***********************
  2. // Funcion para enviar un caracter por el UART 2
  3. void UART2_Sendchar(char c)
  4. {
  5.         while( (LPC_UART2->LSR & LSR_THRE) == 0 );      // Esperamos hasta que Transmit Holding Register Empty esté en 1
  6.  
  7.         LPC_UART2->THR = c;             //Escrivo en el Transmit Holding Register para que se envíe el caracter
  8. }
  9.  
  10. // ***********************
  11. // Funcion para leer un caracter desde el UART 2
  12. char UART2_Getchar()
  13. {
  14.         char c;
  15.         while( (LPC_UART2->LSR & LSR_RDR) == 0 );       // Espero hasta que haya datos mirando el Receive Data Register
  16.         c = LPC_UART2->RBR;                                             // Leo el Receive Buffer Register
  17.         return c;
  18. }
  19.  
  20. // ***********************
  21. // Funcion para enviar una cadena de caracteres por el UART 2
  22. void UART2_PrintString(char *pcString)
  23. {
  24.         int i = 0;
  25.         // loop through until reach string's zero terminator
  26.         while (pcString[i] != 0) {
  27.                 UART2_Sendchar(pcString[i]); // print each character
  28.                 i++;
  29.         }
  30. }
  31.  
  32. void uart2_printBytes(uint8_t* buf, uint32_t len) {
  33.     // transfer all bytes to HW Tx FIFO
  34.     while ( len != 0 ) {
  35.         // send next byte
  36.         UART2_Sendchar(*buf);
  37.  
  38.         // update the buf ptr and length
  39.         buf++;
  40.         len--;
  41.     }
  42. }
  43. void uart2_printUint32(uint32_t n, uint8_t base) {
  44.     uint32_t i = 0;
  45.     uint8_t buf[8 * sizeof(uint32_t)]; // binary is the largest
  46.  
  47.     // check for zero case, print and bail out if so
  48.     if (n == 0) {
  49.         UART2_Sendchar((uint8_t)'0');
  50.         return;
  51.     }
  52.  
  53.     while (n > 0) {
  54.         buf[i] = n % base;
  55.         i++;
  56.         n /= base;
  57.     }
  58.  
  59.     for (; i > 0; i--) {
  60.         if (buf[i - 1] < 10)
  61.                 UART2_Sendchar((uint8_t)('0' + buf[i - 1]));
  62.         else
  63.                 UART2_Sendchar((uint8_t)('A' + buf[i - 1] - 10));
  64.     }
  65. }
  66. void uart2_printInt32(int32_t n, uint8_t base) {
  67.     uint32_t i = 0;
  68.  
  69.     // print '-' for negative numbers, also negate
  70.     if (n < 0) {
  71.         UART2_Sendchar((uint8_t)'-');
  72.         n = ((~n) + 1);
  73.     }
  74.  
  75.     // cast to unsigned and print using uint32_t printer
  76.     i = n;
  77.     uart2_printUint32(i, base);
  78. }
  79.  
  80. void uart2_printDouble(double n, uint8_t frac_digits) {
  81.     uint8_t i;
  82.     uint32_t i32;
  83.     double rounding, remainder;
  84.  
  85.     // test for negatives
  86.     if (n < 0.0) {
  87.         UART2_Sendchar((uint8_t)'-');
  88.         n = -n;
  89.     }
  90.  
  91.     // round correctly so that print(1.999, 2) prints as "2.00"
  92.     rounding = 0.5;
  93.     for (i=0; i<frac_digits; i++)
  94.         rounding /= 10.0;
  95.     n += rounding;
  96.  
  97.     // extract the integer part of the number and print it
  98.     i32 = (uint32_t)n;
  99.     remainder = n - (double)i32;
  100.     uart2_printUint32(i32, 10);
  101.  
  102.     // print the decimal point, but only if there are digits beyond
  103.     if (frac_digits > 0)
  104.         UART2_Sendchar((uint8_t)'.');
  105.  
  106.     // extract digits from the remainder one at a time
  107.     while (frac_digits-- > 0) {
  108.         remainder *= 10.0;
  109.         i32 = (uint32_t)remainder;
  110.         uart2_printUint32(i32, 10);
  111.         remainder -= i32;
  112.     }
  113. }
-
Leonardo Garberoglio

Desconectado micro_pepe

  • Moderadores
  • DsPIC30
  • *****
  • Mensajes: 3206
Re:sprintf me come muchos recursos en xc8
« Respuesta #2 en: 10 de Junio de 2016, 14:51:07 »
Al final hice arreglos separando en digitos el contenido de las variables, queda poco elegante pero funciona y ocupa poco.

Saludos!!!
Se obtiene más en dos meses interesandose por los demás, que en dos años tratando de que los demás se interesen por ti.

新年快乐     的好奇心的猫死亡

Desconectado Picuino

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5883
    • Picuino
Re:sprintf me come muchos recursos en xc8
« Respuesta #3 en: 10 de Junio de 2016, 17:51:06 »
Yo utilizo enteros y luego los convierto a bcd. Ocupa poco, da mucho trabajo y es muy flexible.

Para los decimales, utilizo coma fija.

Desconectado Picuino

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5883
    • Picuino
Re:sprintf me come muchos recursos en xc8
« Respuesta #4 en: 10 de Junio de 2016, 17:52:39 »
Se puede trabajar también con float, pero la aritmética flotante también ocupa lo suyo.

Saludos.

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re:sprintf me come muchos recursos en xc8
« Respuesta #5 en: 18 de Junio de 2016, 16:13:46 »
El mayor problema son los flotantes, yo tambien prefiero manejarme con enteros de ser posible, y como bien dice picuino ir por coma fija de ultima


 

anything