PIC.man
Newbie level 1
Hi,
I'm trying to receive data from the PC via RealTerm terminal software to a PIC18F4520 microcontroller. Transmission is working, but when I try to receive I keep getting the ascii 'k' over and over (and im not sending it). I'm pretty new to programming with C (which is what im using) on the MPLAB IDE. I'm trying to send data from Realterm to the pic and then receive it back and display it. My code is shown below -
// Declarations
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) {
//Set PORT pins as I/O
TRISCbits.TRISC6 = 1; //Sets RC6 as output pin - TX
TRISCbits.TRISC7 = 1; //Sets RC7 as input pin - RX
TRISBbits.TRISB2 = 0; //Set LED 2 as output
//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
//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);
//Set interrupt pins for USART
IPR1bits.RCIP = 1; // Make the UART Rx interrupt a high priority interrupt
RCONbits.IPEN = 0; // Enable interrupt priorities
INTCONbits.GIEH = 1; // All GLOBAL interrupts are high
PIE1bits.RCIE = 1; // Enable Receive interrupt
/***************** Main LOOP 2 - USART***********************/
LED = 1;
while(1)
{
TXREG = receivedata;
Delay10KTCYx(100);
}
CloseUSART();
}
#pragma interrupt high_isr
void high_isr(void)
{
if(PIR1bits.RCIF)
{
if (RCSTAbits.OERR==1) //check for overflow error
{ RCSTAbits.CREN=0; //if error present, reset OERR so that reception can continue
RCSTAbits.CREN=1; }
else if(RCSTAbits.FERR == 1){ //Handling Framing error
char dummy;
dummy = RCREG;
TXSTAbits.TXEN=0;
TXSTAbits.TXEN=1;
RCSTAbits.CREN=0; //if error present, reset OERR so that reception can continue
RCSTAbits.CREN=1;
}
receivedata = RCREG;
}
}
From this I believe whenever i send data from RealTerm it should raise an interrupt and wait till the receive register is loaded, transfer data to the 'receivedata' variable and pass it to TXREG.
If anyone has any insight or can spot the problem here it would be extremely helpful. Thanks!
I'm trying to receive data from the PC via RealTerm terminal software to a PIC18F4520 microcontroller. Transmission is working, but when I try to receive I keep getting the ascii 'k' over and over (and im not sending it). I'm pretty new to programming with C (which is what im using) on the MPLAB IDE. I'm trying to send data from Realterm to the pic and then receive it back and display it. My code is shown below -
// Declarations
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) {
//Set PORT pins as I/O
TRISCbits.TRISC6 = 1; //Sets RC6 as output pin - TX
TRISCbits.TRISC7 = 1; //Sets RC7 as input pin - RX
TRISBbits.TRISB2 = 0; //Set LED 2 as output
//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
//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);
//Set interrupt pins for USART
IPR1bits.RCIP = 1; // Make the UART Rx interrupt a high priority interrupt
RCONbits.IPEN = 0; // Enable interrupt priorities
INTCONbits.GIEH = 1; // All GLOBAL interrupts are high
PIE1bits.RCIE = 1; // Enable Receive interrupt
/***************** Main LOOP 2 - USART***********************/
LED = 1;
while(1)
{
TXREG = receivedata;
Delay10KTCYx(100);
}
CloseUSART();
}
#pragma interrupt high_isr
void high_isr(void)
{
if(PIR1bits.RCIF)
{
if (RCSTAbits.OERR==1) //check for overflow error
{ RCSTAbits.CREN=0; //if error present, reset OERR so that reception can continue
RCSTAbits.CREN=1; }
else if(RCSTAbits.FERR == 1){ //Handling Framing error
char dummy;
dummy = RCREG;
TXSTAbits.TXEN=0;
TXSTAbits.TXEN=1;
RCSTAbits.CREN=0; //if error present, reset OERR so that reception can continue
RCSTAbits.CREN=1;
}
receivedata = RCREG;
}
}
From this I believe whenever i send data from RealTerm it should raise an interrupt and wait till the receive register is loaded, transfer data to the 'receivedata' variable and pass it to TXREG.
If anyone has any insight or can spot the problem here it would be extremely helpful. Thanks!