helmi_mjd
Member level 2
- Joined
- Feb 20, 2011
- Messages
- 45
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,288
- Activity points
- 1,668
Hi guys!
I would like to receive string through UART RX using interrupt. I have trouble in receiving the string. Below is my code. Please if anybody could pointed out the problem in my codes.
Thanks in advance.:-?
I would like to receive string through UART RX using interrupt. I have trouble in receiving the string. Below is my code. Please if anybody could pointed out the problem in my codes.
Thanks in advance.:-?
Code:
void uart1a_initialize(void)
{
// Set the Baud Rate.
U1BRG = 129; //((Fosc/4)/(16 * Baud rate)) - 1
//129 = 9600bps baudrate for
//10MHz crystal with 8xPLL
U1MODEbits.ALTIO = 1; //1 = Communicate using //U1ATX and U1ARX pins (alternative).
U1STAbits.URXISEL = 0; //Interrupt flag bit is set //for every character received.
IPC2bits.U1RXIP = 4; //UART1 Receive Interrupt //Priority = 4.
U1STAbits.OERR = 0; //Clear the Receive //Overflow Flag.
IFS0bits.U1RXIF = 0; //Clear the UART1 Receive //Interrupt flag.
IEC0bits.U1RXIE = 1; //Disable UART1 Receive //Interrupt
U1MODEbits.UARTEN = 1; //Enable UART1.
U1STAbits.UTXEN = 0; //Enable UART1 Transmit*
}
void __attribute__((__interrupt__, no_auto_psv)) _U1RXInterrupt(void)
{
#define RX_BUFF 64
static char rxstr[RX_BUFF];
static char* temp;
temp = rxstr;
// Clear the overflow bit to recieve data
if(U1STAbits.OERR ==1){
U1STAbits.OERR ==0;
}
else if ((U1STAbits.FERR ==0) && (U1STAbits.PERR ==0)){
while((*temp = U1RXREG) != '\n'){
++temp;
}
}
//Host setup timestamp for recording
if(rxstr[0]=='&'){
yrs = atoi(&rxstr[1]);
mon = atoi(&rxstr[2]);
day = atoi(&rxstr[3]);
hrs = atoi(&rxstr[4]);
min = atoi(&rxstr[5]);
sec = atoi(&rxstr[6]);
}
//Host would like to setup new threshold value
if(rxstr[0] == '@'){
dB_thres = atoi(&rxstr[1]);
}
//Host request for data transmission
if(rxstr[0]=='#'){
transmitFlag = 1;
}
//Reset the receive buffer
int n;
for(n = 0;n<RX_BUFF;n++){
rxstr[n] = 0;
}
//Clear RX interrupt flag
IFS0bits.U1RXIF = 0;
}