This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LPC17xx UART TX problem

Good Afternoon to all
I'm working with LCP1768 and uVsion 5, and i'm having a little problem sending BYTE (unsigned char, 0x00 to 0xFF), every BYTE with coding greater than 0x7F are being treated like char (0x00 to 0x7F), for example 0x80 turn to 0x00, 0xCA turn to 0x4A.
Can someone give me light in this situation?

Here is my code...

#include <LPC17xx.h>
#include "Serial.h"

#define SW_FIFO_SIZE            512
#define RLS_INTERRUPT           0x03
#define RDA_INTERRUPT           0x02
#define CTI_INTERRUPT           0x06
#define THRE_INTERRUPT          0x01
#define LSR_RDR                 (1<<0)
#define LSR_OE                  (1<<1)
#define LSR_PE                  (1<<2)
#define LSR_FE                  (1<<3)
#define LSR_BI                  (1<<4)
#define LSR_THRE                (1<<5)
#define LSR_TEMT                (1<<6)
#define LSR_RXFE                (1<<7)
#define UART_DISABLED           0x00
#define UART_OPERATIONAL        0x01
#define UART_OVERFLOW           0x02
#define UART_PARITY_ERROR       0x03
#define UART_FRAMING_ERROR      0x04
#define UART_BREAK_DETECTED     0x05
#define UART_CHAR_TIMEOUT       0x06

// Variáveis Serial 0

bool LedTx0;
bool LedRx0;
BYTE  UART0_RxBuf[UART0_RX_BUFFER_SIZE];
BYTE  UART0_TxBuf[UART0_RX_BUFFER_SIZE];
BYTE  UART0_RxTail;
BYTE  UART0_RxHead;
BYTE uart0_tx_sts;
BYTE uart0_rx_sts;
uint32_t rdaInterrupts = 0;
uint32_t ctiInterrupts = 0;
static FIFO tx0Fifo;
static FIFO rx0Fifo;

void Serial0Init( unsigned int baudRate ) {
  BYTE x;
  int pclk, fdiv;

  LedTx0 = false;
  LedRx0 = false;
  LPC_SC->PCONP |= (1 << 3);
  LPC_PINCON->PINSEL0 |= (1 << 4);             /* Pin P0.2 used as TXD0 (Com0) */
  LPC_PINCON->PINSEL0 |= (1 << 6);             /* Pin P0.3 used as RXD0 (Com0) */

  LPC_SC->PCLKSEL0 &= ~(3 << 6); // clear bits
  LPC_SC->PCLKSEL0 |=  (1 << 6); // set to "01" (full speed)
  pclk = SystemCoreClock;

  LPC_UART0->LCR = 0x03;             /* 8 bits, no Parity, 1 Stop bit */
  LPC_UART0->LCR |= (1 << 7);
  LPC_UART0->DLM = 0;//Fdiv / 256;
  LPC_UART0->DLL = 108;//Fdiv % 256;
  LPC_UART0->FDR = 0x21;///

  LPC_UART0->LCR &= ~(1 << 7);
  LPC_UART0->FCR |= (0x3 << 6); //(0x3 = 14, 0x00 = 1)
  LPC_UART0->FCR |= 0x01;
  LPC_UART0->FCR |= 0x06;

  NVIC_EnableIRQ(UART0_IRQn);
  NVIC_SetPriority(UART0_IRQn,30);
  LPC_UART0->IER = 0x07; //Enable
  LPC_UART0->LCR    = 0x03;

  FifoInit(&tx0Fifo, SW_FIFO_SIZE, (BYTE*)UART0_TxBuf);
  FifoInit(&rx0Fifo, SW_FIFO_SIZE, (BYTE*)UART0_RxBuf);
}

void Serial0Done(void) {
  FifoClear(&tx0Fifo);
  FifoClear(&rx0Fifo);
}

void UART0_IRQHandler(void) __irq {
        BYTE intId, lsrReg;
        BYTE byData,bTmpTail;

        intId = ((LPC_UART0->IIR)>>1)& 0x7;
        if(intId == RLS_INTERRUPT) { //RLS
                lsrReg = LPC_UART0->LSR;
                if (lsrReg & LSR_OE)
                        uart0_rx_sts = UART_OVERFLOW;
                else if (lsrReg & LSR_PE)
                        uart0_rx_sts = UART_PARITY_ERROR;
                else if (lsrReg & LSR_FE)
                        uart0_rx_sts = UART_FRAMING_ERROR;
                else if (lsrReg & LSR_BI)
                        uart0_rx_sts = UART_BREAK_DETECTED;
        } else if (intId == RDA_INTERRUPT) {
                while((LPC_UART0->LSR) & 0x1) {
                        byData = (BYTE)(LPC_UART0->RBR);
                        FifoPut(&rx0Fifo,byData);
                        UART0_RxBuf[UART0_RxHead] = byData;
            UART0_RxHead += 1;
                        if(LedRx0) {
                                LPC_GPIO2->FIOPIN &= ~(1 << 2);
                                LedRx0 = false;
                        } else {
                                LPC_GPIO2->FIOPIN |= (1 << 2);
                                LedRx0 = true;
                        }
                        if (UART0_RxHead == UART0_RX_BUFFER_SIZE)
        UART0_RxHead = 0;
                }
                rdaInterrupts++;
        } else if (intId == CTI_INTERRUPT) {
                while((LPC_UART0->LSR) & 0x1) {
                        byData = (BYTE)(LPC_UART0->RBR);
                        FifoPut(&rx0Fifo,byData);
                        UART0_RxBuf[UART0_RxHead] = byData;
            UART0_RxHead += 1;
                        if(LedRx0) {
                                LPC_GPIO2->FIOPIN &= ~(1 << 2);
                                LedRx0 = false;
                        } else {
                                LPC_GPIO2->FIOPIN |= (1 << 2);
                                LedRx0 = true;
                        }
                        if (UART0_RxHead == UART0_RX_BUFFER_SIZE)
        UART0_RxHead = 0;
                }
                ctiInterrupts++;
        }
}

void Serial0Transmit(PBYTE data, WORD length)  {
  while (length != 0) {
    Serial0Transmit_Byte(*data);
    data++;
    length--;
  }
}

void Serial0Transmit_Byte(BYTE byData) {
  thr_empty;
  LPC_UART0->IER = 0;
  thr_empty = (LPC_UART0->LSR & LSR_THRE);

  LPC_UART0->THR = byData;
  if (LedTx0) {
    LPC_GPIO2->FIOPIN &= ~(1 << 3);
    LedTx0 = false;
  } else {
    LPC_GPIO2->FIOPIN |= (1 << 3);
    LedTx0 = true;
  }
}