• AVR Freaks

AnsweredHot!Problems using uart

Author
felipe_ferreira
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2019/09/10 07:48:12
  • Location: 0
  • Status: offline
2020/01/22 11:39:56 (permalink)
0

Problems using uart

Greetings,
Initially I apologize for the English, as it is not my native language.
I have been having a problem for weeks to be able to use the UART serial communication module of pic32mx530f128h. I use MPLAB v5.25 with Harmony V3.
I simply want to print a value from any register in uart, but I'm not having success. I looked for a lot of information in the related forums, but I was not successful.
I am using a serial-to-usb converter, and the terminal software for viewing the USB communication.
I tried to do the test by programming the registers directly, as well as using harmony
I will present the code I used.
 

/* Obj UART2*/
UART Uart2Obj;

/* ************************************************************************** */
void init_UART_2()
{
       
    U2MODE = 0x8; // Aticar o modulo UAR1.
       
    /* Active Transmission Reception*/
    U2STAbits.UTXEN = 1;
    U2STAbits.URXEN = 1;
    
    /* Registro bood-rade. */

    U2BRG = 1562; //9600
    
    /* Flags de Interruptions */
    IFS1bits.U2RXIF = 0;
    IFS1bits.U2TXIF = 0;
    
    /*Determines Interrupt Mode for / Transmit and Receive*/
    U2STAbits.UTXISEL = 1;
    U2STAbits.URXISEL = 1;
    
     
    /*Set Interrupt Priority*/
    IPC9bits.U2IP = 7;
    IPC9bits.U2IS = 1;
    
    /*Enable Interruption Reception*/
    IEC1bits.U2TXIE = 0;
    IEC1bits.U2RXIE = 1;
   
    
    /*Onn Uart2*/
    U2MODE = 0x08000;
}
/* ************************************************************************** */

/* ************************************************************************** */
/*
* [Application Functions Application]
*/
void write_UART_2_toInteger(unsigned int n)

{
    char s[21];
    int i;
    sprintf(s, "%d", n);
    for(i = 0; s[i] != '\0'; i++) {
        U2STAbits.UTXEN = 1;
        U2TXREG = s[i];
       
        if(U2STAbits.UTXBF) {
            U2STAbits.UTXEN = 1;
            U2STAbits.UTXEN = 0;
        }
        
        U2STAbits.UTXEN = 0;
    }
    U2STAbits.UTXEN = 1;
    U2TXREG = '\n';
    U2STAbits.UTXEN = 0;
}

void write_UART_2_toWorld(char * buffer, int size)
{
   
 IEC1bits.U2TXIE = 1;
    int i;
    for(i = 0; i < size; i++) {
        //IEC1bits.U2TXIE = 0;
        U2STAbits.UTXEN = 1;
        //IEC1bits.U2TXIE = 1;
        
        U2TXREG = buffer[i];
        U2STAbits.UTXEN = 0;
          /*
        if(U2STAbits.UTXBF) {
            U2STAbits.UTXEN = 1;
            U2STAbits.UTXEN = 0;
        }*/
    }
          
      
  U2STAbits.UTXEN = 0;
        
    
    U2STAbits.UTXEN = 1;
    U2TXREG = '\n';
    U2STAbits.UTXEN = 0;
}

void UART_2_InterruptHandler_teste()


 
 Important to say, I use pickkit3, and when using the simulator, activating the uart2 option, I can see the printing values ​​on the debbug screen perfectly.
However, I can't see on the tera term, which I use as a terminal.
And I realize that I am also not having an interruption when receiving a character.
My main focus was if could tell me if at least in the settings apparently the registrars are being configured right. So that my can eliminate due code problems and focus only on hadware problems.
Also, I tested it by generating the own code with harmony 3 
 
void UART2_Initialize( void )
{
    /* Set up UxMODE bits */
    /* STSEL = 0 */
    /* PDSEL = 0 */

    U2MODE = 0x8;

    /* Enable UART2 Receiver and Transmitter */
    U2STASET = (_U2STA_UTXEN_MASK | _U2STA_URXEN_MASK);

    /* BAUD Rate register Setup */
    U2BRG = 1562;

    /* Disable Interrupts */
    IEC1CLR = _IEC1_U2EIE_MASK;

    IEC1CLR = _IEC1_U2RXIE_MASK;

    IEC1CLR = _IEC1_U2TXIE_MASK;

    /* Initialize instance object */
    uart2Obj.rxBuffer = NULL;
    uart2Obj.rxSize = 0;
    uart2Obj.rxProcessedSize = 0;
    uart2Obj.rxBusyStatus = false;
    uart2Obj.rxCallback = NULL;
    uart2Obj.txBuffer = NULL;
    uart2Obj.txSize = 0;
    uart2Obj.txProcessedSize = 0;
    uart2Obj.txBusyStatus = false;
    uart2Obj.txCallback = NULL;

    /* Turn ON UART2 */
    U2MODESET = _U2MODE_ON_MASK;
}

 
e para usar uma função booleana UART2_Write (void * buffer, const size_t size) ,
Também não tenho sinal de retorno no meu terminal.
Em termos de código, usando harmonia, existem mais configurações a serem feitas, além de incluir o módulo uart e configurá-lo?
Muito obrigado pela sua ajuda agora.
post edited by felipe_ferreira - 2020/01/22 12:03:18
#1
ric
Super Member
  • Total Posts : 28640
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Problems using uart 2020/01/22 12:56:34 (permalink)
0
Why is your transmit code fiddling around with the TXEN bit?
You should set it once in your initialise code, then never touch it again.
Also, NEVER enable interrupts if you don't have an interrupt service routine. You don't need them for simple poolling output like this.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
arpananand
Super Member
  • Total Posts : 498
  • Reward points : 0
  • Joined: 2009/11/18 04:35:42
  • Location: 0
  • Status: offline
Re: Problems using uart 2020/01/22 23:12:49 (permalink)
0
how about trying one of the UART PLIB demos present in H3 in following location? 
"\csp\apps\uart"
#3
felipe_ferreira
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2019/09/10 07:48:12
  • Location: 0
  • Status: offline
Re: Problems using uart 2020/01/23 06:19:44 (permalink)
0
So, but actually from what I saw, this bit is responsible for activating the transmission, so much so that it is possible to transmit a byte when the active one. Wouldn't that be?
UTXEN: Transmit Enable bit
1 = UARTx transmitter is enabled. UxTX pin is controlled by UARTx (if ON = 1)
0 = UARTx transmitter is disabled. Any pending transmission is aborted and buffer is reset. UxTX pin is
controlled by the port
When using debug mode, view the UAR2T I'm using. I'm sending this picture 
 
Thanks for your response!
 
post edited by felipe_ferreira - 2020/01/23 06:26:26

Attached Image(s)

#4
felipe_ferreira
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2019/09/10 07:48:12
  • Location: 0
  • Status: offline
Re: Problems using uart 2020/01/23 06:25:56 (permalink)
0
So in the second code I posted, it was generated by the harmony itself, it created all initialization as writing functions,
and invoked the function "bool UART2_Write (void * buffer, const size_t size)" but to no avail
NOTE: I am using a BPS and selected pins 31 / RX 32 / TX
are the serial communication pins
 
Thanks for your response!
#5
ric
Super Member
  • Total Posts : 28640
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Problems using uart 2020/01/23 12:16:40 (permalink) ☼ Best Answerby felipe_ferreira 2020/01/23 13:04:34
5 (1)
felipe_ferreira
So, but actually from what I saw, this bit is responsible for activating the transmission, so much so that it is possible to transmit a byte when the active one. Wouldn't that be?

You can't transmit when it's off.
You will corrupt the transmit that is happening if you turn it off while the transfer is happening.
As I said, turn it on and leave it on.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
felipe_ferreira
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2019/09/10 07:48:12
  • Location: 0
  • Status: offline
Re: Problems using uart 2020/01/23 13:03:03 (permalink)
0
 
It really was the problem,
but I also need to add a delay time
So, I need to check better, what is the ideal time for the program to wait for the transmission of a byte, do you know anything about it?
But in advance, thank you very much for your help
 
But solved the problem, I modified it to.
thank you
 

void escrever_UART_2_toWorld(char * buffer, int size)
{
   unsigned int i, maxwt;
   
   maxwt = 10000;
        for(i = 0; i < size; i++) {
             U2STAbits.UTXEN = 1;
            U2TXREG = buffer[i];
            while (!(U2STA & _U2STA_TRMT_MASK) && maxwt--);
             maxwt = 10000;
        }
   U2TXREG = '\n';
   maxwt = 10000;
   while (!(U2STA & _U2STA_TRMT_MASK) && maxwt--);
   U2TXREG = '\r';
   
}

#7
ric
Super Member
  • Total Posts : 28640
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Problems using uart 2020/01/23 14:24:35 (permalink)
0
Did you read my comment in that other topic where you learnt about TRMT?
That is NOT the best flag to use.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
felipe_ferreira
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2019/09/10 07:48:12
  • Location: 0
  • Status: offline
Re: Problems using uart 2020/01/24 07:03:46 (permalink)
0
ric
Did you read my comment in that other topic where you learnt about TRMT?
That is NOT the best flag to use.
 




Thank you so much for your answer,
hadn't read your observation in that post
In case just to analyze if I really understood, you meant
it is not ideal to wait for the TRMT bit used in "U2STA & _U2STA_TRMT_MASK", since it only indicates that the buffer is empty?
but evaluate the UTXBF bit, which evaluates whether a character can still be sent?
it really wasn't so clear the difference between the two for me,
Does UTXBF not only indicate the number of characters that can still be sent in the transmission?
in this case it would be something like
if (! UxSTAbits.UTXBF) U2TXREG = dat; ?
 
Isn't it necessary to evaluate TRMT to ensure that it can send a new character?
 
thank you very much, sorry for the english
 
<edited>

it worked for me
 
unsigned int i;
    
    while (U2STAbits.UTXBF);
    for(i = 0; i < size; i++) {
        U2TXREG = buffer[i];
        while (U2STAbits.UTXBF);
   }
    
    U2TXREG = '\n';
    while (U2STAbits.UTXBF);
    U2TXREG = '\r';

post edited by felipe_ferreira - 2020/01/24 09:55:17
#9
ric
Super Member
  • Total Posts : 28640
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Problems using uart 2020/01/24 13:14:16 (permalink)
4.5 (2)
felipe_ferreira
it is not ideal to wait for the TRMT bit used in "U2STA & _U2STA_TRMT_MASK", since it only indicates that the buffer is empty?

No, that is NOT what it indicates.
It indicates that the "transmit shift register" is empty.
That is an entirely different register. The data in TXBUF is transferred to the transmit shift register, then it is sent out the TX pin one bit at a time. As soon as it has been copied to the shift register, TXBUF is available to hold more data.
 

Isn't it necessary to evaluate TRMT to ensure that it can send a new character?

No!
 

it worked for me
 
unsigned int i;
    
    while (U2STAbits.UTXBF);
    for(i = 0; i < size; i++) {
        U2TXREG = buffer[i];
        while (U2STAbits.UTXBF);
   }
    
    U2TXREG = '\n';
    while (U2STAbits.UTXBF);
    U2TXREG = '\r';


You're doing too many tests. Try:
    
    while (U2STAbits.UTXBF);    // wait until TXBUF is free
    U2TXREG = buffer[i];
    while (U2STAbits.UTXBF);    // wait until TXBUF is free
    U2TXREG = '\n';
    while (U2STAbits.UTXBF);    // wait until TXBUF is free
    U2TXREG = '\r';

or better still, make a function to send one character
   
void putch(char data)
{
    while (U2STAbits.UTXBF);    // wait until TXBUF is free
    U2TXREG = data;    //send the character
}

 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
Jump to:
© 2020 APG vNext Commercial Version 4.5