0

I'm working on PIC18f4550. I want it to communicate through USART. I'm able to transmit a character but not able to receive any data. I check all the SFR's and r ri8 according to me. I'm using mplab c18 v3.46 compiler and MPLAB v8.40.

#include <p18f4550.h>
#include<usart.h>
#pragma config VREGEN = OFF         // Voltage regulator USB , is Suspended
#pragma config WDT = OFF                // Watchdog timer is suspended
#pragma config PLLDIV = 1                // Internal Oscillator engaged
#pragma config MCLRE = ON
#pragma config WDTPS = 32768
#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = INTOSCIO_EC
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config BORV = 3
#pragma config LPT1OSC = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config ICPRT = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF                                                                              
#pragma config CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF
#pragma config WRTB = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
#define a PORTD
int i,j;
unsigned char serial_data;
extern void delay(int);
extern void tx_data(unsigned char);
extern unsigned char rx_data(void);
void tx_data(unsigned char data1)
{
    TXREG=data1;                               
    while(PIR1bits.TXIF==0);                   
} 
unsigned char rx_data(void)
{
    while(PIR1bits.RCIF==0);                            // Wait until RCIF gets low
    return RCREG;                                
}
void main(void)
{
OSCCON=0x74;
TRISD= 0x00;
TRISC= 0x80;
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 12);
RCON=0x90;
INTCON=0xC0;
IPR1=0x00;
BAUDCON=0x00;
RCSTA=0x90;
tx_data('o');        // Transmit the same data back to PC
serial_data=rx_data();    // Receive data from PC
tx_data('k');       
}

I found this code on net and modified accordingly. It transmits 'o' and never respond again for 'k'

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

Off the top of my head... I don't use the C18 compiler, but if it acts like any regular compiler does, then it's probably due to something like this:

You activate the UART receive interrupt with the USART_RX_INT_ON flag, then you enable the GIEH/GIEL bits in INTCON.

But you don't provide an interrupt service routine. So my guess is that, sitting at the interrupt vector locations (typically PROGRAM ADDRESS 0x08 and 0x18 for PIC18), are NOP assembly instructions. And so when a Receive event hits, it jumps to the high priority interrupt vector at address 0x18 (because IPEN is enabled and IPR1 is cleared), and then it just overflows to whatever next valid instruction is from there, because there are no GOTO instruction at that vector address to properly make it go to a specific ISR function, and then properly return to the last known code location when the interrupt event occured...

epichorns
  • 1,278
  • 8
  • 9