Hi,
I'm fairly new to UART and i'm trying to do something fairly simple (or so i've been told) with a PIC18F4520 microcontroller.
I'm trying to send data via terminal software to the PIC and receive it back. I have no problems in getting data when its hardcoded onto the PIC (stored in variables and received through TXREG of the PIC) but when I try and send anything from the PC to the RX register RCREG I keep getting 0's and data never changes. I'm using a receive interrupt to do this. My code is shown below -
#include <delays.h>
#include <p18f4520.h>
#include <usart.h>
/****** Bit Variable Assignment *********/
#define LED_CHECK PORTBbits.RB2
void high_isr(void);
unsigned char receivedata;
/** Declare Interrupt Vector Sections ****************************/
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm goto high_isr _endasm
}
/******************* MAIN ******************/
#pragma code
void main(void) {
//Enable TX register and set TX pins for transmission
TXSTA = 0b00100000; // Make Tx pin of PORTC and output pin
RCSTA = 0b10010000; // Enable the serial port of PIC18
//Set interrupt pins for USART
RCONbits.IPEN = 1; // Enable interrupt priorities
INTCONbits.GIEH = 1; // All GLOBAL interrupts are high priority
PIE1bits.RCIE = 1; // Enable Receive interrupt
IPR1bits.RCIP = 1; // Make the UART Rx interrupt a high priority interrupt
//Configure the USART
OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51);
/***************** Main LOOP 2 - USART***********************/
LED_CHECK = 1;
while(1)
{
TXREG = receivedata;
while(!TXSTAbits.TRMT);
}
CloseUSART();
}
#pragma interrupt high_isr
void high_isr(void)
{
if(PIR1bits.RCIF)
{
receivedata = RCREG;
if (RCSTAbits.OERR==1) //check for overflow error
{ RCSTAbits.CREN=0; //if error present, reset OERR so that reception can continue
RCSTAbits.CREN=1;
LED_ERR = 1;
Delay10KTCYx(200);
LED_ERR = 0; }
else if(RCSTAbits.FERR == 1){ //Handling Framing error
char dummy, dummy1, dummy2;
TXSTAbits.TXEN=0;
TXSTAbits.TXEN=1;
dummy = RCREG;
dummy1 = RCREG;
dummy2 = RCREG;
LED_ERR = 1;
Delay10KTCYx(200);
LED_ERR = 0; }
PIR1bits.RCIF = 0;
TXSTAbits.TXEN=1;
}
}
I've checked baud rates on both sides and there seems to be no timing issue. I also have a Max232 circuit in between for the voltage level conversion.
As of now I have no idea whats wrong so any help would be greatly appreciated.
Thank you!
I'm fairly new to UART and i'm trying to do something fairly simple (or so i've been told) with a PIC18F4520 microcontroller.
I'm trying to send data via terminal software to the PIC and receive it back. I have no problems in getting data when its hardcoded onto the PIC (stored in variables and received through TXREG of the PIC) but when I try and send anything from the PC to the RX register RCREG I keep getting 0's and data never changes. I'm using a receive interrupt to do this. My code is shown below -
#include <delays.h>
#include <p18f4520.h>
#include <usart.h>
/****** Bit Variable Assignment *********/
#define LED_CHECK PORTBbits.RB2
void high_isr(void);
unsigned char receivedata;
/** Declare Interrupt Vector Sections ****************************/
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm goto high_isr _endasm
}
/******************* MAIN ******************/
#pragma code
void main(void) {
//Enable TX register and set TX pins for transmission
TXSTA = 0b00100000; // Make Tx pin of PORTC and output pin
RCSTA = 0b10010000; // Enable the serial port of PIC18
//Set interrupt pins for USART
RCONbits.IPEN = 1; // Enable interrupt priorities
INTCONbits.GIEH = 1; // All GLOBAL interrupts are high priority
PIE1bits.RCIE = 1; // Enable Receive interrupt
IPR1bits.RCIP = 1; // Make the UART Rx interrupt a high priority interrupt
//Configure the USART
OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51);
/***************** Main LOOP 2 - USART***********************/
LED_CHECK = 1;
while(1)
{
TXREG = receivedata;
while(!TXSTAbits.TRMT);
}
CloseUSART();
}
#pragma interrupt high_isr
void high_isr(void)
{
if(PIR1bits.RCIF)
{
receivedata = RCREG;
if (RCSTAbits.OERR==1) //check for overflow error
{ RCSTAbits.CREN=0; //if error present, reset OERR so that reception can continue
RCSTAbits.CREN=1;
LED_ERR = 1;
Delay10KTCYx(200);
LED_ERR = 0; }
else if(RCSTAbits.FERR == 1){ //Handling Framing error
char dummy, dummy1, dummy2;
TXSTAbits.TXEN=0;
TXSTAbits.TXEN=1;
dummy = RCREG;
dummy1 = RCREG;
dummy2 = RCREG;
LED_ERR = 1;
Delay10KTCYx(200);
LED_ERR = 0; }
PIR1bits.RCIF = 0;
TXSTAbits.TXEN=1;
}
}
I've checked baud rates on both sides and there seems to be no timing issue. I also have a Max232 circuit in between for the voltage level conversion.
As of now I have no idea whats wrong so any help would be greatly appreciated.
Thank you!
Last edited: