0
votes

I'm trying to simply send characters trough my UART Interface by calling the funktion: UART_Write_Text("hello");

this is the code executed in the the uart.c file:

void UART_Init(void)
{
    //115200bps deafult value for RN4678
    BRGH = 0;                       //Setting High Baud Rat
    BRG16 = 0;                      //8-Bit mode
    SPBRG = 8;                      //Writing SPBRG Register
    TXSTAbits.SYNC = 0;             //Setting Asynchronous Mode, ie UART
    RCSTAbits.SPEN = 1;             //Enables Serial Port
    RCSTAbits.CREN = 1;             //Enables Reception
    TXSTAbits.TXEN = 1;             //Enables Transmission
}

/******************************************************************************/
//-----UART write byte
/******************************************************************************/
void UART_Write(char data)
{
    while(!TRMT);
    TXREG = data;
}

/******************************************************************************/
//-----UART check tx queue
/******************************************************************************/
char UART_TX_Empty(void)
{
    return TRMT;
}

/******************************************************************************/
//-----UART write string
/******************************************************************************/
void UART_Write_Text(char *text)
{
  for(int i=0; text[i]!='\0' || text[i] !=0x00; i++){
    UART_Write(text[i]);
  }
  UART_Write('\n');
}

/******************************************************************************/
//-----UART data ready to read
/******************************************************************************/
char UART_Data_Ready(void)
{
  return RCIF;
}

/******************************************************************************/
//-----UART read byte
/******************************************************************************/
char UART_Read(void)
{
  while(!RCIF);
  return RCREG;
}

/******************************************************************************/
//-----UART read string
/******************************************************************************/
void UART_Read_Text(char *Output, unsigned int length)
{
  unsigned int i;
  for(int i=0;i<length;i++)
  Output[i] = UART_Read();
}

Now when I'm debugging it, I see that it writes the wright charakters into the TXREG. At the end it sended the following characters: 'h', 'e', 'l', 'l', 'o', '\n'. I send them to the bluetooth module RN4678 with a baud rate of 115200bps which is also the default baud rate of the BT module. However when I read the sended character on my phone with a Bluetooth terminal i get only some characters wright and the other ones are questionmarks, so it doesn't recognise them (not always the same character unrecognised).

I already experimented with the baud rate, but it looks like its the right value I writed into the SPBRG. I'm also polling the TRMT so there shouldn't be any collisions...

Someone knows what I'm doing wrong?

1
Which ones are coming as question marks - the e and o or the h and l. If it is one of those pairs, then you have an odd/even parity problem. - cup
Unfortunately I couldn't run the BT-module any more, but I remember that the question mark characters were always the succesive ones... - Ignacio Neuhaus

1 Answers

0
votes
#include "Configuration_bits.h"
#include <xc.h>
#include <stdio.h>
#include <p18f67k22.h>
#include <string.h>
#include <stdlib.h>
#define _XTAL_FREQ 16000000

void UART_Init_1(){  
    TRISCbits.TRISC6=1;  // TX1 Pin-31
    TRISCbits.TRISC7=1; //  RX1 Pin-32
    RC1IE=1;

    SPBRG1=34;                // Baud Rate 115200
    BAUDCON1bits.BRG16=1;
    TXSTA1bits.BRGH=1;


   //TXSTA 1 Register 
    TXSTA1bits.TX9=0;    // 8-bit transmission
    TXSTA1bits.TXEN=0;
    TXSTA1bits.TXEN=1;  // Transmit is enabled
    TXSTA1bits.SYNC=0;  // Asynchronous mode
    TXSTA1bits.TRMT=1;  // Transmit Shift Register Status bit

   //RXSTA 1 Register
    RCSTA1bits.SPEN=1;   // Serial Port Enable bit
    RCSTA1bits.RX9=0;    // 8-bit reception
    RCSTA1bits.CREN=1;   // Continuous Receive Enable bit

}  
void UART_Tx_1(unsigned char z[])
{
unsigned int uart_tx1=0;
while(z[uart_tx1]!='\0')
{
  while(!TX1IF);
  TXREG1=z[uart_tx1];
  uart_tx1++;
}
}
void UART_Rx_1()
{
    string_rx1[uart_rx1]=RCREG1;
    uart_rx1++;
}

void interrupt ISR(void)
{
     if(RC1IF && RC1IE)  // UART_1
        {     

        }



}
void main(void)
{ 
    System_Initialization();
    UART_Init_1();


     while(1) 
   {   

     UART_Tx_1("HELLO\r\n"); // Transmit Hello

   }
}