TODOPIC

Microcontroladores PIC => Lenguaje C para microcontroladores PIC => Mensaje iniciado por: Nocturno en 17 de Octubre de 2011, 15:56:16

Título: C32: uso de USARTs alternativas
Publicado por: Nocturno en 17 de Octubre de 2011, 15:56:16
Hola amigos, en un proyecto en el que uso un PIC32MX795F512H quiero usar las 4 USART que trae, las 2 principales y las 2 alternativas.

Sin embargo, por más que intento activar las alternativas, siempre funcionan las principales.

Este es mi código:

Código: C
  1. #include <string.h>
  2. #include <plib.h>
  3.  
  4. // Configuration Bits
  5.  
  6. #pragma config FNOSC    = FRCPLL                // Oscilador interno
  7. #pragma config FPLLIDIV = DIV_1         // PLL Input Divider
  8. #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
  9. #pragma config FPLLODIV = DIV_1         // PLL Output Divider
  10. #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
  11. #pragma config FWDTEN   = OFF           // Watchdog Timer
  12. #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
  13. #pragma config FCKSM    = CSECMD        // Clock Switching & Fail Safe Clock Monitor
  14. #pragma config OSCIOFNC = OFF           // CLKO Enable
  15. #pragma config IESO     = OFF           // Internal/External Switch-over
  16. #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
  17. #pragma config CP       = OFF           // Code Protect
  18. #pragma config BWP      = OFF           // Boot Flash Write Protect
  19. #pragma config PWP      = OFF           // Program Flash Write Protec
  20. #pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
  21. #pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
  22.  
  23. #define SYSTEM_FREQUENCY 80000000L
  24. #define BAUDRATE 57600
  25. static const char* cadenaHello="Hello World desde la G2...\n";
  26.  
  27. int main (void)
  28. {
  29.         int nc;
  30.         int pbFreq;
  31.         CheKseg0CacheOn();
  32.         mCheConfigure(CHE_CONF_WS3 | CHE_CONF_PF_DISABLE);
  33.        
  34.         pbFreq=SYSTEMConfigPerformance(SYSTEM_FREQUENCY);
  35.         OpenUART2(UART_ALTRX_ALTTX|UART_EN|UART_NO_PAR_8BIT|UART_1STOPBIT,UART_RX_ENABLE|UART_TX_ENABLE,(pbFreq/16/BAUDRATE)-1);
  36.         //OpenUART2(UART_RX_TX|UART_EN|UART_NO_PAR_8BIT|UART_1STOPBIT,UART_RX_ENABLE|UART_TX_ENABLE,(pbFreq/16/BAUDRATE)-1);
  37.  
  38.         while (1)       putsUART2(cadenaHello);
  39. }

¿Alguna sugerencia?

MUCHAS GRACIAS
Título: Re: C32: uso de USARTs alternativas
Publicado por: MerLiNz en 17 de Octubre de 2011, 16:26:23
con openuart3, openuart4, putsuart4.... no te funcionan las otras?
Título: Re: C32: uso de USARTs alternativas
Publicado por: Nocturno en 17 de Octubre de 2011, 18:37:40
No, si paso de 2 ya no compila.
Título: Re: C32: uso de USARTs alternativas
Publicado por: MerLiNz en 17 de Octubre de 2011, 19:55:31
veo que no has puesto el #include <p32.....h>

ni el usart.h

me bajo el C32 y te comento el problema...
Título: Re: C32: uso de USARTs alternativas
Publicado por: MerLiNz en 17 de Octubre de 2011, 20:24:17
segun he visto la libreria legacy (la de toda la vida) solo acepta 2. La extendida pone los 6 pero en la .h pone lo siguiente:

#ifdef _UART4
    // UART4 Module ID (not available if UART1 CTS/RTS used).
    UART4,
#endif
#ifdef _UART5
    // UART5 Module ID (not available if UART2 CTS/RTS used).
    UART5,
#endif
#ifdef _UART6
    // UART6 Module ID (not available if UART3 CTS/RTS used).
    UART6,
#endif

lo de no aviaible sera por si usas otros pines que tiene el CTS/RTS o algo asi

C:\Program Files (x86)\Microchip\MPLAB C32 Suite\pic32-libs\include\peripheral\uart.h

ahi tienes la libreria moderna
Título: Re: C32: uso de USARTs alternativas
Publicado por: KALLITOS en 17 de Octubre de 2011, 22:44:13
Hola, yo la tengo por aca:

C:\Program Files\Microchip\MPLAB C32 Suite\pic32mx\include\peripheral

saludos
Título: Re: C32: uso de USARTs alternativas
Publicado por: Nocturno en 18 de Octubre de 2011, 01:44:07
Al incluir esta línea
#include <plib.h>

ya no hace falta incluir cada librería por separado porque se incluyen todas.

Respecto al micro, he probado a hacer esto
#include <p32xxxx.h>

y también esto
#include <proc/p32mx795f512h.h>

pero no cambia nada.
Título: SOLUCIONADO - C32: uso de USARTs alternativas
Publicado por: Nocturno en 18 de Octubre de 2011, 02:41:26
¡Lo conseguí!

Según parece las macros OpenUSART y asociadas están obsoletas y sólo se mantienen la 1 y la 2.

En uno de los ejemplos de C32 encontré otra forma de hacerlo y ya tengo configurada mi USART nº 6  :D

Lo próximo será modificar las funciones para que acepten el nº de USART como parámetro, pero ya os dejo aquí el ejemplo funcionando:
Código: C
  1. #include <proc/p32mx795f512h.h>
  2. #include <string.h>
  3. #include <plib.h>
  4.  
  5. // Configuration Bits
  6.  
  7. #pragma config FNOSC    = FRCPLL                // Oscilador interno
  8. #pragma config FPLLIDIV = DIV_1         // PLL Input Divider
  9. #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
  10. #pragma config FPLLODIV = DIV_1         // PLL Output Divider
  11. #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
  12. #pragma config FWDTEN   = OFF           // Watchdog Timer
  13. #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
  14. #pragma config FCKSM    = CSECMD        // Clock Switching & Fail Safe Clock Monitor
  15. #pragma config OSCIOFNC = OFF           // CLKO Enable
  16. #pragma config IESO     = OFF           // Internal/External Switch-over
  17. #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
  18. #pragma config CP       = OFF           // Code Protect
  19. #pragma config BWP      = OFF           // Boot Flash Write Protect
  20. #pragma config PWP      = OFF           // Program Flash Write Protec
  21. #pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
  22. #pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
  23.  
  24. // *****************************************************************************
  25. // *****************************************************************************
  26. // Section: System Macros
  27. // *****************************************************************************
  28. // *****************************************************************************
  29. #define   GetSystemClock()          (80000000ul)
  30. #define   GetPeripheralClock()      (GetSystemClock()/(1 << OSCCONbits.PBDIV))
  31. #define   GetInstructionClock()      (GetSystemClock())
  32.  
  33. // *****************************************************************************
  34. // *****************************************************************************
  35. // Section: Function Prototypes
  36. // *****************************************************************************
  37. // *****************************************************************************
  38. void SendDataBuffer(const char *buffer, UINT32 size);
  39. void delay_us(WORD us);
  40. void delay_ms(WORD msec);
  41.  
  42. #define BAUDRATE 115200
  43. #define UARTx   UART6
  44.  
  45. const char cadenaHello[]="Hello World desde la G2...\r\n";
  46.  
  47. // *****************************************************************************
  48. // int main (void)
  49. // *****************************************************************************
  50. int main (void)
  51. {
  52.         int nc;
  53.         int pbFreq;
  54.         CheKseg0CacheOn();
  55.         mCheConfigure(CHE_CONF_WS3 | CHE_CONF_PF_DISABLE);
  56.        
  57.         pbFreq=SYSTEMConfigPerformance(GetSystemClock());
  58.        
  59.         UARTConfigure(UARTx, UART_ENABLE_PINS_TX_RX_ONLY);
  60.         UARTSetFifoMode(UARTx, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
  61.     UARTSetLineControl(UARTx, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
  62.     UARTSetDataRate(UARTx, GetPeripheralClock(), BAUDRATE);
  63.     UARTEnable(UARTx, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
  64.  
  65.         while (1) {
  66.                 SendDataBuffer(cadenaHello, sizeof(cadenaHello)-1);;
  67.                 delay_ms(1000);
  68.         }      
  69. }
  70.  
  71.  
  72. // *****************************************************************************
  73. // void delay_us(WORD us)
  74. // *****************************************************************************
  75. void delay_us(WORD us)
  76. {
  77.  DWORD start = _CP0_GET_COUNT();
  78.  DWORD end = start + GetSystemClock() / 1000000 / 2 * us;
  79.  if (end > start) while (_CP0_GET_COUNT() < end);
  80.  else while (_CP0_GET_COUNT() > start || _CP0_GET_COUNT() < end);
  81. }
  82.  
  83. // *****************************************************************************
  84. // void delay_ms(WORD msec)
  85. // *****************************************************************************
  86. void delay_ms(WORD msec)
  87. {
  88.         unsigned int i;
  89.  
  90.         for(i=0; i<msec; i++)
  91.         delay_us(1000);
  92. }
  93.  
  94. // *****************************************************************************
  95. // void SendDataBuffer(char *buffer, UINT32 size)
  96. // *****************************************************************************
  97. void SendDataBuffer(const char *buffer, UINT32 size)
  98. {
  99.     while(size)
  100.     {
  101.         while(!UARTTransmitterIsReady(UARTx))
  102.             ;
  103.  
  104.         UARTSendDataByte(UARTx, *buffer);
  105.  
  106.         buffer++;
  107.         size--;
  108.     }
  109.  
  110.     while(!UARTTransmissionHasCompleted(UARTx))
  111.         ;
  112. }

(http://img534.imageshack.us/img534/4196/helloworldg2.png) (http://imageshack.us/photo/my-images/534/helloworldg2.png/)