1

I want to make a simple UART transmission / reception from the PIC to hyperterminal in proteus. I will start by showing my code, then state the problem:

#include <stdbool.h>   
#include <stdint.h>
#include "xc.h"

// Set the configuration words

_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & 0xFFFF & FWDTEN_OFF & ICS_PGx2 )
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRI )

#define BRG_DIV2          16
#define BRGH2                0

int UART1Init(const unsigned long int BAUDRATE) {
// reconfigure the I/O pins if required
  U1BRG = 25;// set up baudrate
  U1MODE = 0;// clear mode register
  U1MODEbits.BRGH = 0;//divider is 16
  U1STA = 0;// clear status
  U1MODEbits.UARTEN = 1;// enable UART
  U1STAbits.UTXEN = 1;// enable reansmit
  _U1RXIF = 0;// clear interrupt flag
  return 1;
}

//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UART1CharReady() {
  return _U1RXIF;                   // none zero if UART has received data
}

//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
char UART1GetChar() {
  while (!_U1RXIF)
    ;                       // wait for data
  _U1RXIF = 0;                           // clear interrupt flag
  return U1RXREG;
}

// Function:UART GetString - read a string until \n received or length reached
void UART1GetString(char *str, int length) {
  char ch = 0;
  while (ch != '\n')
    if (UART1CharReady()) {
      ch = *str = UART1GetChar();
      str++;
      *str = 0;
      if (--length < 1)
        return;
    }
}

// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UART1PutChar(const char ch) {
  while (U1STAbits.TRMT == 0)
    ;   // wait for transmit ready
  U1TXREG = ch;               // transmit character
}

//Function:UART PrintString - prints a string of characters to the UART.
void UART1PrintString(const char *str) {
  char c;
  while ((c = *str++))
    UART1PutChar(c);
}

int UART2Init(const unsigned long int BAUDRATE) {
  U2BRG = 25;       // set up baudrate
  U2MODE = 0;                   // clear mode register
  U2MODEbits.BRGH = 0;         //divider is 16
  U2STA = 0;                   // clear status
  U2MODEbits.UARTEN = 1;       // enable UART
  U2STAbits.UTXEN = 1;       // enable reansmit
  _U2RXIF = 0;       // clear interrupt flag
  return 1;
}

//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UART2CharReady() {
  return _U2RXIF;                   // none zero if UART has received data
}

//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
unsigned char UART2GetChar() {
  while (!_U2RXIF)
    ;                       // wait for data
  _U2RXIF = 0;                           // clear interrupt flag
  return U2RXREG;
}

// Function:UART GetString - read a string until \n received or length reached
void UART2GetString(char *str, int length) {
  char ch = 0;
  while (ch != '\n')
    if (UART2CharReady()) {
      ch = *str = UART2GetChar();
      str++;
      *str = 0;
      if (--length < 1)
        return;
    }
}

// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UART2PutChar(const unsigned char ch) {
  while (U2STAbits.TRMT == 0)
    ;   // wait for transmit ready
  U2TXREG = ch;               // transmit character
}

//Function:UART PrintString - prints a string of characters to the UART.
void UART2PrintString(const char *str) {
  char c;
  while ((c = *str++))
    UART2PutChar(c);
}

void vApplicationStackOverflowHook() {
}

void delx() {
  unsigned int x = 100;
  while (x--)
    ;
}

int main(void) {
  TRISA = 0;
  UART1Init(9600);
  UART2Init(9600);
  PORTA = 0;

  // Application Loop
  UART2PutChar('a');
  while (1) {
    unsigned char c = UART1GetChar();
    if (c == 'a')
      PORTA = c;
    else {
      PORTA ^= 0xFF;
      delx();
    }
  }

  return 0;
}

The simulation setup on proteus is very straight forward the PIC24 and a hyperterminal correctly set up with 9600 and other default parameter 1 stop bit RX/TX polarity same like what my professor have, I had made a wire from UART2TX (RF5) to the RX pin of the hyperterminal and also to the UART1RX (for testing purposes), apparently the hyperterminal only outputs junk values basically the euro sign I had googled and went everywhere I couldn't find someone with the same problem as me, so what Í did is going to test, I sent data from UART2 and catch it in UART1, the data is correct because PORTA pins (the first 8 pins) shows the correct binary 8-bit representation of my character that I sent, any help is appreciated.

8
  • 1
    "I had made a wire from UART2TX (RF5) to the RX pin of the hyperterminal and also to the UART1RX (for testing purposes)" --> Do you have a 3rd ground wire connected? Commented May 21 at 11:06
  • @chux Hello thanks for the feedback, i dont understand wym by 3rd ground wire, what component need ground beside the PIC24F? Commented May 21 at 18:40
  • How many wires connected between machines: I'd expect transmit, receive and ground.. Commented May 22 at 18:13
  • but im only connecting the RX pin to the TX UART2 of the PIC the TX RTS & CTS arent connected to anything @chux Commented May 22 at 20:23
  • @sawdust thanks for the feedback, where do you suggest i should go? exactly i think grounding isnt a problem? Commented May 23 at 2:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.