Okay, so I created a code so that whenever external interrupt is activated (output from one encoder), it increments the current angle variable and display the current angle via serial terminal. The problem is that whenever the external interrupt activates, it completely disrupts the UART transmission and the PIC stops transmitting serial messages. Here's the code
Rich (BB code):
//=================================================================================================
// Includes, Defines, and Global Variables
//=================================================================================================
#include <p18F2420.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
int current_angle=0;
char message[30];
//=================================================================================================
//
//=================================================================================================
//=================================================================================================
// Function Prototypes
//=================================================================================================
void CONFIG(void);
//=================================================================================================
//
//=================================================================================================
//=================================================================================================
// External Interrupt Handling
//=================================================================================================
#pragma interrupt ISR
void ISR ()
{
if(INTCONbits.INT0IF==1) // Checks to see if the external interrupt has been triggered.
{
current_angle+=1;
INTCONbits.INT0IF=0; // Clear the external interrupt flag
}
}
#pragma code InterruptVectorHigh = 0x08 // This function simply jumps to the ISR code shown above.
void InterruptVectorHigh (void)
{
_asm
goto ISR //jump to interrupt routine
_endasm
}
#pragma code
//=================================================================================================
//
//=================================================================================================
//=================================================================================================
// Main Loop
//=================================================================================================
void main(void)
{
CONFIG();
while(1) // Run the program forever and let the interrupts take over.
{
sprintf(message,"Current Angle: %d \r\n",current_angle);
putsUSART(message);
Delay10KTCYx(125);
}
}
//=================================================================================================
//
//=================================================================================================
//=================================================================================================
// Configuration Function
//=================================================================================================
void CONFIG(void)
{
/* Code for handling all of the interrupt */
RCONbits.IPEN=1; // Enable interrupt priority
INTCONbits.GIE=1; // Enable global interrupts
INTCONbits.INT0IE=1; // Enable INT0 interrupts
INTCONbits.INT0IF=0; // Set external interrupt flag to zero
INTCON2bits.RBPU=1; // Disable Pull up
INTCON2bits.INTEDG0=1; // Interrupt enable on rising edge
INTCON3bits.INT1IE=1; // Enable External interrupt 1
INTCON2bits.INTEDG1=1; // Enable External interrupt on rising edge
INTCON3bits.INT1IF=0; // Set flag to zero
TRISBbits.TRISB0=1; // Set PORTB0 to input
TRISBbits.TRISB1=1; // Set PORTB1 to input
TRISBbits.TRISB2=0; // Set PORTB2 to output
PORTB=0x03; // Enable high on inputs
/* USART Configuration */
TRISC=0x00;
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 129);
}
//=================================================================================================
//
//=================================================================================================