TODOPIC

Microcontroladores PIC => Lenguaje C para microcontroladores PIC => Mensaje iniciado por: LucasBols en 01 de Abril de 2014, 17:05:28

Título: XC8 por qué testbit no funciona?
Publicado por: LucasBols en 01 de Abril de 2014, 17:05:28
hola, buenas tardes,

este código no funciona, por qué?

Código: C
  1. unsigned int variable;
  2.  
  3.     variable = 21046; // 1010010 00110110
  4.  
  5.     for ( unsigned int i=0; i<16; i++)
  6.     {
  7.         PORTBbits.RB4 = variable & ((unsigned int)1 << i);
  8.  
  9.         PORTBbits.RB3 = 1;  __delay_ms( 500 );
  10.         PORTBbits.RB3 = 0;  __delay_ms( 500 );
  11.     }

es lo mismo que

Código: C
  1. #define testbit(var,bit)    ((var) & ((unsigned int) 1<<(bit)))

que tampoco funciona

muchas gracias,

saludos
Título: Re: XC8 por qué testbit no funciona?
Publicado por: AKENAFAB en 01 de Abril de 2014, 18:06:42
Revisa este link.

http://www.todopic.com.ar/foros/index.php?topic=39799.0


Código: [Seleccionar]
#define testbit(var,bit)    ((var) & ((unsigned long) 1<<(bit)))
#define setbit(var,bit)     ((var)|= ((unsigned long) 1<<(bit)))
#define clrbit(var,bit)     ((var)&=~((unsigned long) 1<<(bit)))
Título: Re: XC8 por qué testbit no funciona?
Publicado por: LucasBols en 02 de Abril de 2014, 13:59:38
hola AKENAFAB, buenas tardes,

tampoco me funciona

el header tiene esto

Código: C#
  1. #define LCD_LUZ         PORTBbits.RB4
  2.  
  3. typedef union
  4. {
  5.     unsigned int variable;
  6.     struct
  7.     {
  8.         unsigned d15:1, d14:1, d13:1, d12:1, d11:1, d10:1, d9:1, d8:1, d7:1, d6:1, d5:1, d4:1, d3:1, d2:1, d1:1, d0:1;
  9.     };
  10. } t_int;
  11.  
  12. t_int dato;

en el XC8 (del man):
        Type         Size (bits)
    unsigned int         16

probé con esto y lo único que funciona es bit por bit de la union

Código: C#
  1. void procesar_comando(void)
  2. {
  3.     for ( char i=0; i<16; i++)
  4.     {
  5.         LCD_LUZ = (bool)((unsigned int)dato.variable & ((unsigned int)1 << i));
  6.  
  7.         PORTBbits.RB3 = 1; __delay_ms( 500 );
  8.         PORTBbits.RB3 = 0; __delay_ms( 500 );
  9.     }
  10. //    #######################
  11. //    #### ESTO FUNCIONA ####
  12. //    #######################
  13. //    LCD_LUZ = dato.d0;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  14. //    LCD_LUZ = dato.d1;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  15. //    LCD_LUZ = dato.d2;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  16. //    LCD_LUZ = dato.d3;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  17. //    LCD_LUZ = dato.d4;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  18. //    LCD_LUZ = dato.d5;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  19. //    LCD_LUZ = dato.d6;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  20. //    LCD_LUZ = dato.d7;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  21. //    LCD_LUZ = dato.d8;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  22. //    LCD_LUZ = dato.d9;   PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  23. //    LCD_LUZ = dato.d10;  PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  24. //    LCD_LUZ = dato.d11;  PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  25. //    LCD_LUZ = dato.d12;  PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  26. //    LCD_LUZ = dato.d13;  PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  27. //    LCD_LUZ = dato.d14;  PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  28. //    LCD_LUZ = dato.d15;  PORTBbits.RB3 = 1; __delay_ms( 500 ); PORTBbits.RB3 = 0; __delay_ms( 500 );
  29. }
Título: Re: XC8 por qué testbit no funciona? [RESUELTO: no acepta asignaciones]
Publicado por: LucasBols en 02 de Abril de 2014, 14:59:26
hola AKENAFAB,

vi que en el hilo que me indicaste lo usas en un if y probe, parece que el resultado de un AND no puede utilizarse como asignación, solamente como comparación,

quedó así y anda

Código: C++
  1. for ( char i=0; i<16; i++)
  2.     {
  3.         if ( (unsigned int)dato.variable & ((unsigned int)1 << i) )
  4.             LCD_LUZ = 1;
  5.         else
  6.             LCD_LUZ = 0;
  7.         led_clock();
  8.     }

muchas gracias,

saludos
Título: Re: XC8 por qué testbit no funciona?
Publicado por: BrunoF en 02 de Abril de 2014, 19:44:46
vi que en el hilo que me indicaste lo usas en un if y probe, parece que el resultado de un AND no puede utilizarse como asignación, solamente como comparación,

Hola, lo que pasa es que PORTBbits.RB4 tiene tamaño de bit. Por ende sólo aceptaría valores 0 y 1. Otros valores estarían técnicamente indefinidos y a la merced de lo que el compilador considere.

Más rápido:
Código: C#
  1. for ( unsigned int i=1; i!=0; i<<=1)
  2.    {
  3.        if ( (unsigned int)dato.variable & i )
  4.            LCD_LUZ = 1;
  5.        else
  6.            LCD_LUZ = 0;
  7.        led_clock();
  8.    }

Saludos!