TODOPIC
Microcontroladores PIC => Primeros pasos - Iniciación a los microcontroladores => Mensaje iniciado por: eldelaesquina en 25 de Mayo de 2013, 18:03:25
-
Hola.
¿Alguien tiene alguna rutina en C para convertir y mostrar los valores CCPR1 (CCPR1H:CCPR1L) en algún tipo de salida, LCD, RS232, Les 7 segmentos..etc.?
Estoy trabajando con PIC16F887 aunque la rutina serviría para cualquiera.
-
No entiendo que es lo que quieres.
Puedes convertir el número del registro CCP a string y luego mostrarlo en una LCD. O si sueles utilizar "printf" -yo no la utilizo porque consume mucho memoria de programa- puedes hacer uso de dicha función para mostrar en LCD.
-
Hola AngelGris.
Me refiero a, cono el resultado del módulo te lo da en dos bytes por separado CCPR1H y CCPR1L , lo que necesito es obtener el valor completo y luego sacarlo por pantalla. He probado ByteToStr(CCPR1H+CCPR1L, txt) , con & en vez de + y (CCPR1H:CCPR1L).
También me interesa el modo para manejar el valor completo en el modo que , si mal no recuerdo, había que dividir por 1000, luego por 100 pero que no recuerdo más.
Gracias.
-
Prueba con algo así:
union {
unsigned int w;
struct {
unsigned char lob;
unsigned char hib;
};
} CCPR1;
CCPR1.lob = CCPR1L;
CCPR1.hib = CCPR1H;
printf("%u", CCPR1.w)
Saludos.
-
Hola Picuino.
Gracias , me ha parecido una buena solución. Lo probaré aunque no creo que tenga ningún problema.
-
Hola.
He intentado adaptar tu solución a MikroC pero por muchos intentos que hago todo son errores. Incluso con rutinas como esta en MikroC, sigue ídem de ídem.
This same kind of union can also work the other way around when you want to combine 2-bytes into 1 word (16-bits). This technique is handy for reading 16-bit timer registers or ADC registers... ect... basically anytime two bytes are read out and are needed as a combined word.
Code: Select all
unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
...
...
Bytes2Int.byte[1] = HiInbyte; // combine bytes
Bytes2Int.byte[0] = LoInbyte; //
...
return (Bytes2Int.word); // return the combined bytes as a 16-bit Word
El error es indefined unión
¡Help!
}
-
Puedes probar con una sentencia más simple (aunque no sé si el compilador creará código optimizado):
int CCPR1;
CCPR1 = (CCPR1H<<8) & CCPR1L;
Saludos.
-
Hola a todos.
Basándome en el criterio que me habéis dado, he dado con un ejemplo de Mikroelectronika referente a esto mismo en concreto.
Create the Union template somwhere before "void main ()":
union U16_
{
unsigned int word; // For accessing the whole 16-bit unsigned int
unsigned char byte[2]; // For accessing 16-bits as individual bytes
};
y luego...
Next create the instance where you need to use the Union in a function and load it with data:
void WriteMyEE_Bytes (unsigned int MyInWord)
{
union U16_ Int2Bytes;
...
Int2Bytes.word = MyInWord;
EEPROM_WR (Int2Bytes.byte[1]); // write the hi byte
EEPROM_WR (Int2Bytes.byte[0]); // write the lo byte to eeprom
...
}
Como no es mi caso, hay esto a continuación.
This same kind of union can also work the other way around when you want to combine 2-bytes into 1 word (16-bits). This technique is handy for reading 16-bit timer registers or ADC registers... ect... basically anytime two bytes are read out and are needed as a combined word.
unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
...
...
Bytes2Int.byte[1] = HiInbyte; // combine bytes
Bytes2Int.byte[0] = LoInbyte; //
...
return (Bytes2Int.word); // return the combined bytes as a 16-bit word
}
Lo adapto a mis necesidades como sigue....
union U16_
{
unsigned int word; // For accessing the whole 16-bit unsigned int
unsigned char byte[2]; // For accessing 16-bits as individual bytes
};
unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
Bytes2Int.byte[1] = HiInbyte; // combine bytes
Bytes2Int.byte[0] = LoInbyte; //
return (Bytes2Int.word); // return the combined bytes as a 16-bit word
}
Si esta última parte la pongo dentro de una función, me da un error diciendo que "return" no puede estar dentro de la función y si la dejo fuera los errores son de redefinición, el punto de separación Bytes2Int.byte no lo acepta, y al final, sabiendo que es una buena solución, no se como aplicarlo.
¿Alguien me da una solución?
Gracias.
P.D.
La página del ejemplo está en esta dirección
http://www.mikroe.com/forum/viewtopic.php?f=88&t=25930