Autor Tema: UART2 XC32  (Leído 3343 veces)

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

Desconectado bitpic

  • PIC18
  • ****
  • Mensajes: 402
    • BitPIC
UART2 XC32
« en: 31 de Diciembre de 2012, 16:56:02 »
Hola,

Estoy intentando hacer un simple programa que envíe un dato por el UART de un PIC32MX101 pero no hay manera.

El programa es el siguiente:

Código: [Seleccionar]
/*
 * File:   main.c
 * Author: BitPIC
 *
 * Created on 30 de diciembre de 2012, 20:55
 */
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <plib.h>

// PIC32MX110F016B Configuration Bit Settings

// DEVCFG3
// USERID = No Setting
#pragma config PMDL1WAY = ON            // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON             // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
// SYSCLK = 10 MHz (10MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
#pragma config FPLLIDIV = DIV_10         // PLL Input Divider (10x Divider)
#pragma config FPLLMUL = MUL_20         // PLL Multiplier (20x Multiplier)
#pragma config FPLLODIV = DIV_2         // System PLL Output Clock Divider (PLL Divide by 2)

// DEVCFG1
#pragma config FNOSC = PRI              // Oscillator Selection Bits (Primary Osc (XT,HS,EC))
#pragma config FSOSCEN = ON             // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON                // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1              // Watchdog Timer Postscaler (1:1)
#pragma config WINDIS = OFF             // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WISZ_25      // Watchdog Timer Window Size (Window Size is 25%)

// DEVCFG0
#pragma config JTAGEN = OFF             // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF                 // Code Protect (Protection Disabled)

#define SYS_FREQ (10000000)      //le indicamos la frecuencia del sistema

#define config1 UART_EN | UART_IDLE_CON | UART_RX_TX | UART_DIS_WAKE | UART_DIS_LOOPBACK | UART_DIS_ABAUD | UART_NO_PAR_8BIT | UART_1STOPBIT | UART_IRDA_DIS | UART_DIS_BCLK_CTS_RTS| UART_NORMAL_RX | UART_BRGH_SIXTEEN
#define config2 UART_TX_PIN_LOW | UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR | UART_ADR_DETECT_DIS | UART_RX_OVERRUN_CLEAR
#define DESIRED_BAUDRATE 9600

// Function Prototypes
int main(void);
void delay(int time);


int main(void) {
    unsigned int pbClk;

    pbClk = SYSTEMConfigPerformance(SYS_FREQ);

    mPORTBSetPinsDigitalIn(BIT_11|BIT_2 | BIT_4);
    mPORTBSetPinsDigitalOut(BIT_10 |BIT_12 | BIT_13 | BIT_14 | BIT_15);

    //Configruación UART
    U2RXRbits.U2RXR = 0b0011; // Set RX to RPB11
    RPB10Rbits.RPB10R = 0b0010; // Set TX to RPB10

    OpenUART2( config1, config2, pbClk/16/DESIRED_BAUDRATE-1); // calculate actual BAUD generate value.

    while(1){
        
        if(_RB2){
            delay(10000);
            if(!_RB2){
                _RB12 = !_RB12;
            }
        }

        while (U2STAbits.UTXBF);
        putcUART2('a');
        putcUART2(0x0A);
    }
}

void delay(int time){
    int i;
    for(i=0;i<time;i++);
}


Para comprobar que el programa funciona le he puesto un trozo de codigo que lee un pulsador y cambia el estado de un led en cada pulsación. Esta parte funciona, lo único que no me envia el dato (a) por el puerto serie.

Sospecho que pueda ser algo de configuración. Puede que haya configurado mal el UART o tenga algo que ver con la configuración del reloj del micro y no este funcionando bien el UART o el baudrate, pero no encuentro que es.

Es la primera vez que uso el UART en un PIC32.

¿Alguien ve algo que este mal en el código?

Un saludo.
« Última modificación: 31 de Diciembre de 2012, 17:03:56 por bitpic »
BitPIC

Desconectado Suky

  • Moderador Local
  • DsPIC33
  • *****
  • Mensajes: 6758
Re: UART2 XC32
« Respuesta #1 en: 31 de Diciembre de 2012, 19:44:03 »
Proba con las nuevas funciones de configuración, que puede resultar menos engorroso:

Código: C
  1. UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); // Habilita pines Tx y Rx.
  2. UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); // INT_Rx cuando el buffer tiene un byte.
  3. UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); // 8:N:1
  4. UARTSetDataRate(UART2, GetPeripheralClock(), 19200);                                                                    // 19200       
  5. UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));                              // Modulo encedido, Tx y Rx encendido

Saludos!
No contesto mensajes privados, las consultas en el foro

Desconectado bitpic

  • PIC18
  • ****
  • Mensajes: 402
    • BitPIC
Re: UART2 XC32
« Respuesta #2 en: 01 de Enero de 2013, 08:12:47 »
Gracias Suky, pero me acabo de dar cuenta que el diseño del hardware está mal, tiene los pines TX y RX invertidos.

Es una tarjeta que compre para unas clases de PIC32 que hice y resulta que la tarjeta estaba diseñada para DSPic y le pusieron un PIC32 pensando que sería 100% compatible pero resulta que no.

Nada, tirare de soldador y estaño a ver si la puedo modificar.

Un saludo.
BitPIC

Desconectado bitpic

  • PIC18
  • ****
  • Mensajes: 402
    • BitPIC
Re: UART2 XC32
« Respuesta #3 en: 01 de Enero de 2013, 09:03:50 »
He arreglado el Hardware y ahora si que recibo datos en el PC pero estos datos no son correctos.

Estoy enviando una A y estoy recibiendo una Ž.

Supongo que el problema puede estar en que tenga mal el baudrate o este enviando datos con niveles invertidos... pero no encuentro el porque...

¿alguien tiene alguna idea de que hago mal?

Un saludo.
BitPIC