Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic18f27j13 Recieve Interrupt not working

Status
Not open for further replies.

gwntd

New Member
Hello, I'm new to this forum and seeking help urgently. Can someone tell me why my interrupt receive is not working?

I'm communicating with my computer through real term at 115200 bps.
mplab IDE v8.70 compiler

thanks in advance for anyone helping.

I got my interrupt to work, but not correctly. The values that I am getting back from the interrupts are all mostly wrong. Can someone kind of enough help me.


I have two main problems. Whenever my RX interrupt assign data to the array (hs_hold[2]). The values are correct only sometimes , when they are outputted through the TX interrupt. I have also setup the TX interrupt to also display the hs_count which is used to increment the array. But even that has it's values non- consistent.

I am really confused, any helps is appreciated.
Code:
unsigned int hs_count;					// handshake counter
volatile int handshake;					// handshake flag
volatile int talk_continue; 				// continue flag
unsigned char temp_error;		// used in case to hold RC2REG in case of overrun error
	unsigned char ID_A[2];		// array used to hold motor id, angle byte1, angle byte2
	int i;
unsigned char hs_hold[2];

Code:
void setupUSART2(void)
{    //setup for EUART2 - target baudrate 115200 - used for communication with computer
   
   
    TXSTA2 =   0b00100000; //Enable transmit and low baud rate
    RCSTA2 =   0b10010000; //Enable serial port
    BAUDCON2 = 0b00001000; //Enable 16 bit baud generator
   
    SPBRGH2 = 0;
    SPBRG2 = 25;
   
    RCONbits.IPEN = 0;    //enable Interrupt - disable Interrupt priority levels
  //    PIR3bits.RC2IF = 0; // clears recieve interrupt flag
    INTCONbits.GIE = 1;    // enable Global Interrupts
    INTCONbits.PEIE = 1;    //enable Peripheral Interrupt
    PIE3bits.RC2IE = 1;    // enable EUSART Interrupt for Recieve
    IPR3bits.RC2IP = 0;
    IPR3bits.TX2IP = 0;
}

Code:
#pragma code isr=0x08
#pragma interrupt isr

void isr(void)
{	

	if((PIR3bits.RC2IF ==1) && (PIE3bits.RC2IE ==1))
		{ 

			
		if(RCSTA2&0x06) // check if(RCSTAbits.FERR==1 || RCSTAbits.OERR==1 ) // when OEER ==1 EUSART Recieve is disabled. FERR =1 Framign error
		{	//this shouldn't really happen since, handshake protocol would prevent overrun errors
			//just in case, will allow EUSART Recieve to function.
			RCSTA2bits.CREN =0; 					//overrun error - clear CREN 
			hs_hold[hs_count] = RCREG2	;			//clear Framing error
			RCSTA2bits.CREN = 1; 					//Renable CREN
		}
		else
		{
			hs_hold[hs_count] = RCREG2;
		}
			if(hs_count >= 2)		//used to reset hs_count 
			{ hs_count = 0;
			}
			else
			{
			 hs_count++;
			}
	
		talk_continue = 1;		//continue protocol communiaction with computer
		PIE3bits.TX2IE = 1;	// enable EUSART Interrupt for Transmit

		}
	

//----------------------------------------------------------------------------
// Transmisson Interrupt
//interrupt used to continue communication protocol with computer. By sending FA.


	if(talk_continue == 1)
		{
		if ((PIR3bits.TX2IF == 1) && (TXSTA2bits.TRMT == 1) &&(PIE3bits.TX2IE == 1))	//check if TX2REG is OPEN, and if TX interrupt flag is set
			{
			if(handshake == 0)
				{
				TXREG2 = 0xFA;						//FA is used to established inital handshake protocol of FF-AA-FF
				while(TXSTA2bits.TRMT == 0)
					{}
				TXREG2 = hs_count;				
				while(TXSTA2bits.TRMT == 0)
					{}
				TXREG2 = hs_hold[hs_count];
				while(TXSTA2bits.TRMT == 0)
					{}
				}
			else
				{
				TXREG2 = 0xFB;						//FB is used when ready to recieve motor ID/Angle
				}
			
			talk_continue = 0;						//resets talk_continue.
			PIE3bits.TX2IE = 0 ;	//  disable EUSART Interrupt for Transmit- to avoid continous interrupt
			}
			
		}
}
#pragma code

Code:
void main(void)
{    OSCCONbits.SCS = 0b00; // Selects Primary clock(FOSC) as the clock source

    TRISCbits.TRISC7 = 1;
    TRISCbits.TRISC6 = 0;
    TRISBbits.TRISB5 = 1;
    TRISBbits.TRISB4 = 0;
   

    PPSUnLock();                        // Reassign pins for EUSART2
    PPSOutput( PPS_RP7, PPS_TX2CK2);    //Remaps TX2 to RP7
    PPSInput(  PPS_RX2DT2, PPS_RP8);        //Remaps RX2 to RP8
    PPSLock();

    setupUSART1();
    setupUSART2();
    talk_continue = 0;
    handshake = 0;
    hs_count = 0;
    while(1){
        while(handshake == 0)
            { if((hs_hold[1]== 0xAA) && ( hs_hold[0] == hs_hold[2] == 0xFF) )        //check if handshake protocl activated FF-AA-FF
                  { handshake = 1;
                   PIE3bits.RC2IE = 0;    // Disable EUSART Interrupt for Recieve. Handshake protocol established.
                   hs_hold[0] = 0x00;                        //reset hs_hold
                   hs_hold[1] = 0x00;
                   hs_hold[2] = 0x00;
                 }
            }

        while(handshake == 1)
            {         //check if TXREG2 holds a 0xFE which ends transmit protocol.
                    for(i =0; i<2; i++)
                        {
                            while(PIR3bits.RC2IF == 0)    //waiting buffer, for when Recieve flag is set.
                                {}
                               
                        if(PIR3bits.RC2IF ==1)             //checks if Register flag is enabled, register holds a byte
                            {
                            ID_A[i] = RCREG2;
                            talk_continue = 1;
                            PIE3bits.TX2IE = 1;    // enable EUSART Interrupt for Transmit
                            }
                        }
                    if ( (ID_A[2] == 0xAA) && (ID_A[0] == ID_A[2] == 0xFF) )
                        {
                        action();            //tells motors to move
                        handshake = 0;
                        PIE3bits.RC2IE = 1;    // enable EUSART Interrupt for Recieve
                        PIE3bits.TX2IE = 1;    // enable EUSART Interrupt for Transmit
                        }
                    else
                        {
                        store_move(ID_A[0], ID_A[1], ID_A[2]);
                        }
            }
            }       
   

}

.
 
Last edited:
First off... I can't see where you get SPBRG2 = 25.... there isn't a frequency listed where this be the case... The closest is 23 @ 11,0592mhz.

Secondly... There was another posting about corrupt values inside the ISR. It was solved, but I cant remember the post... It was a while back.. Maybe someone will remember.

What xtal are you using?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top