Autor Tema: Error en comunicacion Bluetooth con PIC18f4455  (Leído 2885 veces)

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

Desconectado P3teR

  • PIC10
  • *
  • Mensajes: 6
Error en comunicacion Bluetooth con PIC18f4455
« en: 28 de Febrero de 2019, 18:21:59 »
Puedo dar este tema por solucionado
Mil gracias a remi04

Mensaje Original:
Hola, y gracias por tomarse el tiempo de leer mi primera publicación en el foro.
Estoy tratando de enviar el resultado de mi conversion AD via Bluetooth usando un PIC18f4455 y un modulo HC-06.
La señal a digitalizar entra en el pin A0.
La recepcion de los datos la realizo desde un celular con la aplicacion Serial Bluetooth o Bluetooth Terminal HC-06. Sin embargo lo que obtengo es una salida de 5V continua en el pin TX del PIC.
El código se realizó en MPLAB 8.92, lo cambié ligeramente para que enviara un 1 cada vez que le pasara un dato por RX y aun asi no funciona.
Soy nuevo en programación con PIC así que apreciaría cualquier ayuda que puedan darme

El código íntegro es:

Código: C
  1. #define _XTAL_FREQ 80000000
  2. #include <p18f4455.h>
  3. #include <adc.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <delays.h>
  7. #include <portb.h>
  8. #include <usart.h>
  9. #include <timers.h>
  10.  
  11. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  12. #pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)
  13. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  14. #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
  15. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  16. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
  17. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  18. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
  19. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
  20. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
  21. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  22. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  23. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
  24. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
  25. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
  26. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  27. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
  28. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  29. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
  30. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
  31. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
  32. #pragma config EBTRB = OFF
  33. #pragma config FOSC = INTOSC_EC // Interno, puerto en RA6, EC usado por USB
  34. #pragma config VREGEN = OFF       // USB voltage regulator disabled
  35. #pragma config WDT = OFF         //detecta si el micro esta en ciclo infinito
  36. #pragma config IESO = OFF      // Habilita cambio intclk  OUTCLK
  37. #pragma config PWRT = OFF   // demora al Power Up
  38. #pragma config MCLRE = OFF       // MCLR se vuelve la entrada digital RE3
  39.  
  40. void high_isr(void);
  41.  
  42. typedef unsigned char bool;
  43. #define true    1
  44. #define false   0
  45.  
  46. unsigned int data = 0;
  47. bool got_data_bool = false;
  48.  
  49.  
  50. //UART
  51. void uart_init (unsigned int gen_reg, unsigned int sync, unsigned int brgh, unsigned int brg16) {
  52.  
  53. // pines de tx y rx como entradas
  54. TRISCbits.RC7=1;        TRISCbits.RC6=1;
  55. //*******************************
  56.  
  57. //Generar reloj para uart, baud rate
  58. SPBRGH = (gen_reg & 0xFF00 >>8);
  59. SPBRG = (gen_reg & 0x00FF);
  60. //***********************************
  61.  
  62. //Habilitar rece ptor
  63. RCSTAbits.CREN=1;     RCSTAbits.SPEN=1;     BAUDCONbits.BRG16=brg16;
  64. //***********************************
  65.  
  66. //Transmision
  67. TXSTAbits.SYNC=sync;        TXSTAbits.BRGH=brgh;      TXSTAbits.TXEN=1;
  68. //*********************************
  69.  
  70. //Habilitar interrupciones para la recepción
  71. IPR1bits.RCIP=1; //prioridad
  72. PIE1bits.RCIE=1; //habilitar
  73.  
  74. }
  75. void uart_send(unsigned int *c) {
  76.   TXREG=*c;
  77.   //esperar a que el buffer de TX se vacie (TXREG se vacie)
  78.   while (TXSTAbits.TRMT==0);      }
  79.  
  80.  
  81. void uart_receiver(unsigned int *c, bool *rx_flag){                                                  
  82.         if(RCSTAbits.FERR){unsigned int er = RCREG;}  // Framing error
  83.         else if(RCSTAbits.OERR){                  //Overrun error
  84.                         RCSTAbits.CREN=0;
  85.                         RCSTAbits.CREN=1;}
  86.         else{*c = RCREG;   *rx_flag = true;}}
  87.  
  88.  
  89. //INTERRUPCION
  90.     #pragma code high_vetor=0x8
  91.     void  interrupt_at_high_vector(void)
  92.     {  _asm GOTO high_isr _endasm    }
  93.     #pragma code
  94.  
  95.     #pragma interrupt high_isr
  96.     void high_isr (void){
  97.         INTCONbits.GIEH = 0;
  98.       if (PIR1bits.RCIF){
  99.         uart_receiver(&data,&got_data_bool);
  100.           PIR1bits.RCIF=0;}
  101.       INTCONbits.GIEH = 1;}
  102.      #pragma code
  103.  
  104.  
  105.  
  106. void main( void ){
  107.  
  108.   OSCCONbits.IRCF = 0x07;
  109.   OSCCONbits.SCS = 0x03;
  110.  while (OSCCONbits.IOFS!=1); //wait for oscillator to stabilize
  111.  
  112. uart_init(51,0,1,0);
  113.  
  114. //cad
  115. TRISAbits.RA0 = 1 ;//entrada
  116. ADCON1bits.VCFG0 = 0  ;// referencia positiva Vdd
  117. ADCON1bits.VCFG1 = 0 ; // referencia positiva Vss
  118. ADCON1bits.PCFG = 0x0E ; // rconfig como digitales An12-An01
  119.  
  120. ADCON2bits.ADFM = 1 ; // justificado derecha
  121. ADCON2bits.ACQT = 0x04; // tiempo de adquisicion 8 TAD
  122. ADCON2bits.ADCS = 0x04; // clock source fosc/4
  123.  
  124. ADCON0bits.CHS = 0; // canal analógico An0
  125. ADCON0bits.ADON = 1; // enable ADC*/
  126.  
  127. RCONbits.IPEN = 1;
  128. /*  INTCONbits.GIEH = 1;
  129.   INTCONbits.GIEL = 1;*/
  130. INTCONbits.PEIE = 1;
  131.  
  132. while(1){  
  133. ADCON0bits.GO = 1;
  134. while (ADCON0bits.GO);
  135.  
  136. //Para enviar el resultado del ADC
  137. /*data=(ADRESH << 6) | (ADRESL >> 2);
  138. uart_send (&data);*/
  139.  
  140.  
  141. if(got_data_bool){
  142. data='1';              //para probar solamente
  143.     uart_send(&data);
  144.     got_data_bool = false;}}}

« Última modificación: 11 de Marzo de 2019, 10:21:07 por P3teR »

Desconectado remi04

  • PIC24F
  • *****
  • Mensajes: 657
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #1 en: 28 de Febrero de 2019, 20:48:23 »
Ahora mismo con poco tiempo para revisar bien el código decirte que es normal que la salida del pic esté a nivel alto, cuando inicias el módulo, este configura los dos pines tx y rx como salida y entrada respectivamente y pone a nivel alto tx, por lo que no es necesario que definas los TRIS de ambos pines.

   Ten en cuenta que lo que estás enviando a la terminal son enteros de 8 bits no imprimibles.  No son caracteres ascii por lo que tienes que configurar el terminal que uses para que te muestre los datos como enteros en decimal o hexadecimal.

  Los punteros en este programa no son necesarios.



 

   


Desconectado P3teR

  • PIC10
  • *
  • Mensajes: 6
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #2 en: 01 de Marzo de 2019, 10:21:45 »
Gracias por su respuesta. Aprecio realmente la ayuda.
Modifiqué el código según sus recomendaciones:
-Abandoné los puneteros
-Comenté los TRIS
-Configuré el BT Terminal en HEX

Aun así, en lugar de '1' obtengo a la salida (y solo de vez en cuando) 'FE' o 'FF' Lo cual me imagino tenga que ver con el hecho de que mi pin de TX del PIC tiene su salida en el valor  máximo de 5V.
El código ahora es:

Código: C
  1. #define _XTAL_FREQ 80000000
  2.  
  3. #include <p18f4455.h>
  4. #include <adc.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <delays.h>
  8. #include <portb.h>
  9. #include <usart.h>
  10. #include <timers.h>
  11.  
  12.  
  13. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  14. #pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)
  15. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  16. #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
  17. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  18. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
  19. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  20. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
  21. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
  22. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
  23. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  24. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  25. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
  26. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
  27. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
  28. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  29. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
  30. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  31. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
  32. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
  33. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
  34. #pragma config EBTRB = OFF
  35. #pragma config FOSC = INTOSC_EC // Interno, puerto en RA6, EC usado por USB
  36. #pragma config VREGEN = OFF       // USB voltage regulator disabled
  37. #pragma config WDT = OFF         //detecta si el micro esta en ciclo infinito
  38. #pragma config IESO = OFF      // Habilita cambio intclk  OUTCLK
  39. #pragma config PWRT = OFF   // demora al Power Up
  40. #pragma config MCLRE = OFF       // MCLR se vuelve la entrada digital RE3
  41.  
  42. void high_isr(void);
  43.  
  44. typedef unsigned char bool;
  45. #define true    1
  46. #define false   0
  47.  
  48. unsigned int data = 0;
  49. bool got_data_bool = false;
  50.  
  51.  
  52. //UART
  53. void uart_init (unsigned int gen_reg, unsigned int sync, unsigned int brgh, unsigned int brg16) {
  54.  
  55. // pines de tx y rx como entradas
  56. //TRISCbits.RC7=1;        TRISCbits.RC6=1;
  57. //*******************************
  58.  
  59. //Generar reloj para uart, baud rate
  60. SPBRGH = (gen_reg & 0xFF00 >> 8);         SPBRG = (gen_reg & 0x00FF);
  61. //***********************************
  62.  
  63. //Habilitar rece ptor
  64. RCSTAbits.CREN=1;     RCSTAbits.SPEN=1;     BAUDCONbits.BRG16=brg16;
  65. //***********************************
  66.  
  67. //Transmision
  68. TXSTAbits.SYNC=sync;        TXSTAbits.BRGH=brgh;      TXSTAbits.TXEN=1;
  69. //*********************************
  70.  
  71. //Habilitar interrupciones para la recepción
  72. IPR1bits.RCIP=1; //prioridad
  73. PIE1bits.RCIE=1; //habilitar
  74.  
  75. }
  76. void uart_send(unsigned int c) {
  77.   TXREG=c;
  78.   //esperar a que el buffer de TX se vacie (TXREG se vacie)
  79.   while (TXSTAbits.TRMT==0);      }
  80.  
  81.  
  82. void uart_receiver(unsigned int c, bool rx_flag){                                                  
  83.         if(RCSTAbits.FERR){unsigned int er = RCREG;}  // Framing error
  84.         else if(RCSTAbits.OERR){                  //Overrun error
  85.                         RCSTAbits.CREN=0;
  86.                         RCSTAbits.CREN=1;}
  87.         else{c = RCREG;  rx_flag = true;}}
  88.  
  89. //INTERRUPCION
  90.     #pragma code high_vetor=0x8
  91.     void  interrupt_at_high_vector(void)
  92.     {  _asm GOTO high_isr _endasm    }
  93.     #pragma code
  94.  
  95.     #pragma interrupt high_isr
  96.     void high_isr (void){
  97.         INTCONbits.GIEH = 0;
  98.       if (PIR1bits.RCIF){
  99.         uart_receiver(data,got_data_bool);
  100.           PIR1bits.RCIF=0;}
  101.       INTCONbits.GIEH = 1;}
  102.      #pragma code
  103.  
  104. void main( void ){
  105.  
  106.   OSCCONbits.IRCF = 0x07;
  107.   OSCCONbits.SCS = 0x03;
  108.  while (OSCCONbits.IOFS!=1); //wait for oscillator to stabilize
  109.  
  110. uart_init(51,0,1,0);
  111.  
  112. //cad
  113. TRISAbits.RA0 = 1 ;//entrada
  114. ADCON1bits.VCFG0 = 0  ;// referencia positiva Vdd
  115. ADCON1bits.VCFG1 = 0 ; // referencia positiva Vss
  116. ADCON1bits.PCFG = 0x0E ; // rconfig como digitales An12-An01
  117.  
  118. ADCON2bits.ADFM = 1 ; // justificado derecha
  119. ADCON2bits.ACQT = 0x04; // tiempo de adquisicion 8 TAD
  120. ADCON2bits.ADCS = 0x04; // clock source fosc/4
  121.  
  122. ADCON0bits.CHS = 0; // canal analógico An0
  123. ADCON0bits.ADON = 1; // enable ADC*/
  124.  
  125. RCONbits.IPEN = 1;
  126. /*  INTCONbits.GIEH = 1;
  127.   INTCONbits.GIEL = 1;*/
  128. INTCONbits.PEIE = 1;
  129.  
  130. while(1){  
  131. ADCON0bits.GO = 1;
  132. while (ADCON0bits.GO);
  133.  
  134. //Para enviar el resultado del ADC
  135. /*data=(ADRESH << 6) | (ADRESL >> 2);
  136. uart_send (&data);*/
  137.  
  138. if(got_data_bool){
  139. data='1';              //para probar solamente
  140.     uart_send(data);
  141.     got_data_bool = false;}}}
     
     
« Última modificación: 01 de Marzo de 2019, 16:22:14 por P3teR »

Desconectado P3teR

  • PIC10
  • *
  • Mensajes: 6
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #3 en: 01 de Marzo de 2019, 14:53:30 »
Para hacer más sencillo el código lo simplifiqué y dejé solo el enviar un 1 cuando se recibe un dato, eliminé también la rutina de interrupcion, aunque no estoy seguro de que sea lo mas indicado.
¿Por que no funciona?


Código: C
  1. #define _XTAL_FREQ 80000000
  2.  
  3. #include <p18f4455.h>
  4. #include <adc.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <delays.h>
  8. #include <portb.h>
  9. #include <usart.h>
  10. #include <timers.h>
  11.  
  12.  
  13. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  14. #pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)
  15. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  16. #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
  17. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  18. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
  19. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  20. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
  21. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
  22. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
  23. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  24. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  25. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
  26. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
  27. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
  28. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  29. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
  30. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  31. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
  32. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
  33. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
  34. #pragma config EBTRB = OFF
  35. #pragma config FOSC = INTOSC_EC // Interno, puerto en RA6, EC usado por USB
  36. #pragma config VREGEN = OFF       // USB voltage regulator disabled
  37. #pragma config WDT = OFF         //detecta si el micro esta en ciclo infinito
  38. #pragma config IESO = OFF      // Habilita cambio intclk  OUTCLK
  39. #pragma config PWRT = OFF   // demora al Power Up
  40. #pragma config MCLRE = OFF       // MCLR se vuelve la entrada digital RE3
  41.  
  42.  
  43. unsigned int UART_Read();
  44. void uart_send(unsigned int *c);
  45. unsigned int data = 0;
  46.  
  47.  
  48. //UART
  49. void uart_init (unsigned int gen_reg, unsigned int sync, unsigned int brgh, unsigned int brg16) {
  50.  
  51. // pines de tx y rx como entradas
  52. TRISCbits.RC7=1;        TRISCbits.RC6=1;
  53. //*******************************
  54.  
  55. //Generar reloj para uart, baud rate
  56. SPBRGH = (gen_reg & 0xFF00 >> 8);         SPBRG = (gen_reg & 0x00FF);
  57. //***********************************
  58.  
  59. //Habilitar rece ptor
  60. RCSTAbits.CREN=1;     RCSTAbits.SPEN=1;     BAUDCONbits.BRG16=brg16;
  61. //***********************************
  62.  
  63. //Transmision
  64. TXSTAbits.SYNC=sync;        TXSTAbits.BRGH=brgh;      TXSTAbits.TXEN=1;
  65. //*********************************
  66.  
  67. //Habilitar interrupciones para la recepción
  68. IPR1bits.RCIP=1; //prioridad
  69. PIE1bits.RCIE=1; //habilitar
  70. }
  71.  
  72. void uart_send(unsigned int *c) {
  73.   TXREG=*c;
  74.  while (TXSTAbits.TRMT==0);  }
  75.  
  76. unsigned int UART_Read()
  77. {
  78.   while(PIR1bits.RCIF==0);
  79. data='1';
  80. uart_send(&data);}
  81.  
  82.  
  83. void main( void ){
  84.  
  85.   OSCCONbits.IRCF = 0x07;
  86.   OSCCONbits.SCS = 0x03;
  87.  while (OSCCONbits.IOFS!=1); //wait for oscillator to stabilize
  88.  
  89. uart_init(51,0,1,0);
  90.  
  91. RCONbits.IPEN = 1;
  92. /*  INTCONbits.GIEH = 1;
  93.   INTCONbits.GIEL = 1;*/
  94. INTCONbits.PEIE = 1;
  95.  
  96.  
  97. while(1){  
  98.  
  99. UART_Read();}
« Última modificación: 01 de Marzo de 2019, 16:24:17 por P3teR »

Desconectado remi04

  • PIC24F
  • *****
  • Mensajes: 657
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #4 en: 01 de Marzo de 2019, 15:15:35 »
Hola. Ahora en el ordenador lo veo un poco mejor. Te recomiendo que uses:
Código: C
  1. ........
y sustituyes los puntos suspensivos por el codigo, asi es mucho mejor visible.

Viéndolo mejor ahora te digo que los punteros SI son imprescindibles de la forma en la que está planteada el programa.  Te voy comentando las cositas que veo en el mismo codigo.

 Tu código completo inicial sería este:

Código: C
  1. #define _XTAL_FREQ 80000000
  2. #include <p18f4455.h>
  3. #include <adc.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <delays.h>
  7. #include <portb.h>
  8. #include <usart.h>
  9. #include <timers.h>
  10.  
  11. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  12. #pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)
  13. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  14. #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
  15. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  16. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
  17. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  18. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
  19. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
  20. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
  21. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  22. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  23. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
  24. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
  25. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
  26. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  27. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
  28. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  29. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
  30. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
  31. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
  32. #pragma config EBTRB = OFF
  33. #pragma config FOSC = INTOSC_EC // Interno, puerto en RA6, EC usado por USB
  34. #pragma config VREGEN = OFF       // USB voltage regulator disabled
  35. #pragma config WDT = OFF         //detecta si el micro esta en ciclo infinito
  36. #pragma config IESO = OFF      // Habilita cambio intclk  OUTCLK
  37. #pragma config PWRT = OFF   // demora al Power Up
  38. #pragma config MCLRE = OFF       // MCLR se vuelve la entrada digital RE3
  39.  
  40. void high_isr(void);
  41.  
  42. typedef unsigned char bool;
  43. #define true    1
  44. #define false   0
  45.  
  46. unsigned int data = 0;
  47. bool got_data_bool = false;
  48.  
  49.  
  50. //UART
  51. void uart_init (unsigned int gen_reg, unsigned int sync, unsigned int brgh, unsigned int brg16) {
  52.  
  53. // pines de tx y rx como entradas
  54. TRISCbits.RC7=1;        TRISCbits.RC6=1;                                 // Eso vimos que no es necesario por que al activar el modulo eusart ya se configuran pero si lo dejas no afecta al programa, simplemente no es necesario
  55. //*******************************
  56.  
  57. //Generar reloj para uart, baud rate
  58. SPBRGH = (gen_reg & 0xFF00 >>8);                                     // No usas brg16, por lo que no es necesario iniciar SPBRGH
  59. SPBRG = (gen_reg & 0x00FF);                                              // Sería suficiente con poner SPBRG = gen_reg;            Asi, sin más.
  60. //**********************************
  61.  
  62.  
  63.  
  64. BAUDCONbits.ABDEN = 1;                                                 // Prueba al añadir esta linea aquí, esto habilitará autobaud. Cuando conectes el movil al HC06 lo primero que tienes que enviar es una 'U' que sería un 0x55. Si hay suerte lo que sucederá será
  65.                                                                                       // que te devuelva tu '1' que tienes abajo en la llamada a uart_send(unsigned char *c), que por cierto, si le quitas la asignación del '1' a dato, el programa debería hacer un ECO permanente.
  66.  
  67.  
  68.  
  69. //Habilitar rece ptor
  70. RCSTAbits.CREN=1;     RCSTAbits.SPEN=1;     BAUDCONbits.BRG16=brg16;
  71. //***********************************
  72.  
  73. //Transmision
  74. TXSTAbits.SYNC=sync;        TXSTAbits.BRGH=brgh;      TXSTAbits.TXEN=1;
  75. //*********************************
  76.  
  77. //Habilitar interrupciones para la recepción
  78. IPR1bits.RCIP=1; //prioridad
  79. PIE1bits.RCIE=1; //habilitar
  80.  
  81. }
  82. void uart_send(unsigned int *c) {
  83.   TXREG=*c;
  84.   //esperar a que el buffer de TX se vacie (TXREG se vacie)
  85.   while (TXSTAbits.TRMT==0);      }
  86.  
  87.  
  88. void uart_receiver(unsigned int *c, bool *rx_flag){                                                  
  89.    if(RCSTAbits.FERR){unsigned int er = RCREG;}  // Framing error
  90.    else if(RCSTAbits.OERR){                  //Overrun error
  91.                         RCSTAbits.CREN=0;
  92.                         RCSTAbits.CREN=1;}
  93.    else{*c = RCREG;   *rx_flag = true;}}
  94.  
  95.  
  96. //INTERRUPCION
  97.     #pragma code high_vetor=0x8
  98.     void  interrupt_at_high_vector(void)
  99.     {  _asm GOTO high_isr _endasm    }
  100.     #pragma code
  101.  
  102.     #pragma interrupt high_isr
  103.     void high_isr (void){
  104.         INTCONbits.GIEH = 0;
  105.       if (PIR1bits.RCIF){
  106.         uart_receiver(&data,&got_data_bool);
  107.           PIR1bits.RCIF=0;}
  108.       INTCONbits.GIEH = 1;}
  109.      #pragma code
  110.  
  111.  
  112.  
  113. void main( void ){
  114.  
  115.   OSCCONbits.IRCF = 0x07;
  116.   OSCCONbits.SCS = 0x03;
  117.  while (OSCCONbits.IOFS!=1); //wait for oscillator to stabilize
  118.  
  119. uart_init(51,0,1,0);                                                                                        // Aqui puedes probar poniendo BRGH a cero (Es el mismo valor SPBRG) tanto para BRGH1 que a cero.
  120.  
  121. //cad
  122. TRISAbits.RA0 = 1 ;//entrada
  123. ADCON1bits.VCFG0 = 0  ;// referencia positiva Vdd
  124. ADCON1bits.VCFG1 = 0 ; // referencia positiva Vss
  125. ADCON1bits.PCFG = 0x0E ; // rconfig como digitales An12-An01
  126.  
  127. ADCON2bits.ADFM = 1 ; // justificado derecha
  128. ADCON2bits.ACQT = 0x04; // tiempo de adquisicion 8 TAD
  129. ADCON2bits.ADCS = 0x04; // clock source fosc/4
  130.  
  131. ADCON0bits.CHS = 0; // canal analógico An0
  132. ADCON0bits.ADON = 1; // enable ADC*/
  133.  
  134. RCONbits.IPEN = 1;
  135. /*  INTCONbits.GIEH = 1;
  136.   INTCONbits.GIEL = 1;*/
  137. INTCONbits.PEIE = 1;
  138.  
  139. while(1){  
  140. ADCON0bits.GO = 1;
  141. while (ADCON0bits.GO);
  142.  
  143. //Para enviar el resultado del ADC
  144. /*data=(ADRESH << 6) | (ADRESL >> 2);
  145. uart_send (&data);*/
  146.  
  147.  
  148. if(got_data_bool){
  149. data='1';              //para probar solamente              // Esto lo puedes quitar, si lo quitas se enviará lo que hayas recibido por RX.  (ECO)
  150.     uart_send(&data);
  151.     got_data_bool = false;}}}


    Yo en este código no veo absolutamente nada por lo que no te deba funcionar.   Lo que hace tal cual está es un ECO.


  Y como te he puesto en el propio codigo, prueba también con el Autobaud a ver si resulta. Recuerda que el primer carácter a enviar desde el terminal es un 0x55 (en hexadecimal), en ASCII sería la letra 'U'.





 

 


 
« Última modificación: 01 de Marzo de 2019, 15:18:33 por remi04 »

Desconectado P3teR

  • PIC10
  • *
  • Mensajes: 6
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #5 en: 01 de Marzo de 2019, 16:15:11 »
Realmente aprecio la ayuda. Pero lamentablemente sigue sin funcionar. aun con sus sugerencias Por favor, remi04 , deme su opinion del código sin rutina de interrupción que coloqué antes. Muchas Gracias

Desconectado remi04

  • PIC24F
  • *****
  • Mensajes: 657
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #6 en: 01 de Marzo de 2019, 19:43:29 »
Realmente aprecio la ayuda. Pero lamentablemente sigue sin funcionar. aun con sus sugerencias Por favor, remi04 , deme su opinion del código sin rutina de interrupción que coloqué antes. Muchas Gracias

 Cambia el while(1) de tu main en el código simplificado por este y probamos.
 
Código: C
  1. while(1) {
  2.               while(PIR1bits.RCIF==0);
  3.               char dat = RCREG;
  4.               TXREG = dat;
  5.               while (TXSTAbits.TRMT==0);
  6.              }

 Esto hará un eco, recibe y devuelve lo que ha recibido.

Desconectado P3teR

  • PIC10
  • *
  • Mensajes: 6
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #7 en: 04 de Marzo de 2019, 11:35:42 »

[ Cambia el while(1) de tu main en el código simplificado por este y probamos.
 
Código: C
  1. while(1) {
  2.               while(PIR1bits.RCIF==0);
  3.               char dat = RCREG;
  4.               TXREG = dat;
  5.               while (TXSTAbits.TRMT==0);
  6.              }

 Esto hará un eco, recibe y devuelve lo que ha recibido.

En primer lugar, gracias remi04 por la ayuda.
Este código me da error de sintaxis.
Haciendo algunos cambios y usando mi variable unsigned int data (sin punteros) y comentando la línea de TRMT, entonces obtengo resultados raros. (de otra forma no recibo nada):

Código: C
  1. while(1){
  2.                while(PIR1bits.RCIF==0);
  3.               data = RCREG;
  4.               TXREG = data;
  5.           //    while (TXSTAbits.TRMT==0);

Enviando 01HEX tres veces recibo en este orden
01 0d 0a HEX
ff 0d 0a  HEX
01 0d fe HEX

Estoy extrañado, a pesar de ser un código relativamente sencillo no logro que funcione
¿Será un problema con la rutina de interrupción o los bits de configuración del micro? Siento que estoy cometiendo un error básico.


Desconectado remi04

  • PIC24F
  • *****
  • Mensajes: 657
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #8 en: 05 de Marzo de 2019, 08:05:26 »
Ahí ya hay que probar primero con una usart normal (un ch340) ftdi 232rl o similares para conectar con la terminal en un ordenador y ver si es el pic el que transmite mal o si es el módulo hc6.

 También pondría a enviar en loop infinito:

   while(1) TXREG = 0x55;

 Y miraría con el osciloscopio el timing del frame a ver qué periodo de bauerate está sacando el pic.

 

Desconectado P3teR

  • PIC10
  • *
  • Mensajes: 6
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #9 en: 08 de Marzo de 2019, 13:16:27 »
Muchas gracias a todos los que se han molestado en leer este post, en especial a remi04 por su ayuda.
Ya funciona!!!!
Al final acabé cambiando el compilador al mas moderno xc8 y ahora mi código es capaz de enviar el resultado de la conversion Analogica-Digital si recive 0x55 y lo detengo enviando 0x56 (los caracteres U y V respectivamente ):


Código: C
  1. #include <xc.h>
  2. #include <p18f4455.h>
  3. #include "stdint.h"
  4. #include "stdbool.h"
  5. #define _XTAL_FREQ 8000000
  6.  
  7.  
  8. #pragma config FCMEN = OFF
  9. #pragma config BORV = 3
  10. #pragma config WDTPS = 32768
  11. #pragma config LPT1OSC = OFF
  12. #pragma config STVREN = ON
  13. #pragma config LVP = ON
  14. #pragma config XINST = OFF
  15. #pragma config CP0 = OFF
  16. #pragma config CP1 = OFF
  17. #pragma config CP2 = OFF
  18. #pragma config CPB = OFF
  19. #pragma config CPD = OFF
  20. #pragma config WRT0 = OFF
  21. #pragma config WRT1 = OFF
  22. #pragma config WRT2 = OFF
  23. #pragma config WRTC = OFF
  24. #pragma config WRTB = OFF
  25. #pragma config WRTD = OFF
  26. #pragma config EBTR0 = OFF
  27. #pragma config EBTR1 = OFF
  28. #pragma config EBTR2 = OFF
  29. #pragma config EBTRB = OFF
  30. #pragma config FOSC = INTOSC_EC
  31. #pragma config VREGEN = OFF
  32. #pragma config WDT = OFF
  33. #pragma config IESO = OFF
  34. #pragma config PWRT = OFF
  35. #pragma config MCLRE = OFF
  36.  
  37. uint8_t data = 'A';
  38. uint8_t com = 'A';
  39. void interrupt high_isr(void);
  40. void interrupt low_priority low_isr(void);
  41.  
  42. void uart_init(uint16_t gen_reg, unsigned sync,unsigned brgh, unsigned brg16){
  43.    
  44.     TRISCbits.RC7=1;
  45.     TRISCbits.RC6=1;
  46.    
  47.     SPBRGH = (gen_reg & 0xFF00) >> 8;
  48.     SPBRG = gen_reg & 0x00FF;
  49.    
  50.     RCSTAbits.CREN = 1;
  51.     RCSTAbits.SPEN = 1;
  52.     BAUDCONbits.BRG16 = brg16;
  53.    
  54.     TXSTAbits.SYNC = sync;
  55.     TXSTAbits.BRGH = brgh;
  56.     TXSTAbits.TXEN = 1;
  57.    
  58. IPR1bits.RCIP=1;
  59.     PIE1bits.RCIE=1;
  60.  }
  61.  
  62. void main(void) {
  63.  
  64.     OSCCONbits.IRCF = 0x07;
  65.     OSCCONbits.SCS = 0x03;
  66.     uart_init(51,0,1,0);
  67.  
  68.     TRISAbits.RA0 = 1;
  69.     ADCON1bits.VCFG0 = 0;
  70.     ADCON1bits.VCFG1 = 0;
  71.     ADCON1bits.PCFG = 0x0E;
  72.    
  73.     ADCON2bits.ADFM = 1;
  74.     ADCON2bits.ACQT = 0x04;
  75.     ADCON2bits.ADCS = 0x04;    
  76.     ADCON0bits.CHS = 0;
  77.     ADCON0bits.ADON = 1;
  78.     RCONbits.IPEN = 1;
  79.     INTCONbits.GIEH = 1;
  80.     INTCONbits.GIEL = 1;
  81.    
  82.     while(1){
  83. while(com==0x55){
  84. ADCON0bits.GODONE = 1;
  85. while(ADCON0bits.GODONE);
  86. data=(ADRESH << 6) | (ADRESL >> 2);
  87. TXREG=data;  
  88. __delay_ms(30);  
  89. }
  90. }  
  91.     }
  92.  
  93. void interrupt high_isr(void){
  94.     INTCONbits.GIEH = 0;
  95.     if(PIR1bits.RCIF){
  96. if(RCREG==0x55)
  97. {com= 0x55;}
  98. if(RCREG==0x56)
  99. {com= 0x56;}
  100.        PIR1bits.RCIF=0;
  101.     }
  102.        INTCONbits.GIEH = 1;
  103. }
  104.  
  105. void interrupt low_priority low_isr(void){
  106.     INTCONbits.GIEH = 0;
  107.     if(PIR1bits.TXIF){
  108.         PIR1bits.TXIF=0;
  109.     }
  110.     INTCONbits.GIEH = 1;
  111. }

Sin embargo ahora me asalta otra duda. Me pidieron enviar los datos usando un header. Por ejemplo los detalles del ADC de forma
HDR+LSB+MSB donde HSR sería 0xFE y LSB y MSB son los bits menos y mas significativos
¿Como?
Y gracias por adelantado

Desconectado remi04

  • PIC24F
  • *****
  • Mensajes: 657
Re:Error en comunicacion Bluetooth con PIC18f4455
« Respuesta #10 en: 08 de Marzo de 2019, 19:47:30 »
Lo único que tienes es que poner antes del  TXREG = data;  todo el header

  TXREG = 0xfe;
  TXREG = ADRESL;
  TXREG = ADRESH;
  TXREG = data;

 
Si te da fallo los envíos mete entre ellos una espera de buffer libre por bit TRMT

Por ejemplo while (!TXSTAbits.TRMT);