neilsonster
13-02-2005, 23:27
Hi, after getting my automous scripting and autonomous camera codes working independently, I have been having quite a bit of trouble putting the two together. The problems have seemed to start coming in User_Initialization when I call Serial_Driver_Initialize() along with Initialize_Encoders() and Initialize_Gyro() (from Kevin Watson's code).
I examined this and noticed it deals with interrupts, which I have had no experience with (It's my first and only year since I'm graduating this year :( ). Would anyone please be able to explain to me where the conflict lies? Here is the code from the three functions:
Serial_Driver_Initialize()
void Serial_Driver_Initialize(void)
{
Initialize_uart();
}
(which calls this) ---v
static void Initialize_uart(void)
{
uword8 flush;
/* Setup the device control block (dcb) */
dcb[PROGRAM_PORT].myRCREG = &RCREG1;
dcb[PROGRAM_PORT].myTXREG = &TXREG1;
dcb[PROGRAM_PORT].portId = 1; /* setup unique ID for PROGRAM_PORT */
Reset_Control_Block(&dcb[PROGRAM_PORT]);
dcb[TTL_PORT].myRCREG = &RCREG2;
dcb[TTL_PORT].myTXREG = &TXREG2;
dcb[TTL_PORT].portId = 2; /* setup unique ID for TTL_PORT */
Reset_Control_Block(&dcb[TTL_PORT]);
/* Initialize the PROGRAM_PORT port */
#if USE_BUFFERED_PRINTF
Open1USART(USART_TX_INT_ON & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, baud_115);
#else
Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, baud_115);
#endif
IPR1bits.TXIP = PRIORITY;
IPR1bits.RCIP = PRIORITY;
TRISCbits.TRISC6 = 0; /*TX1 output*/
TRISCbits.TRISC7 = 1; /*RX1 input */
flush = RCREG1; /* flush data */
flush = RCREG1; /* flush data */
RXINTF = 0;
/* Initialize the TTL_PORT port */
Open2USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, baud_115);
IPR3bits.RC2IP = PRIORITY;
IPR3bits.TX2IP = PRIORITY;
TRISGbits.TRISG1 = 0; /*TX2 output*/
TRISGbits.TRISG2 = 1; /*RX2 input */
flush = RCREG2; /* flush data */
flush = RCREG2; /* flush data */
RXINTF2 = 0;
aBreakerWasTripped = FALSE;
}
Initialize_Encoders()
void Initialize_Encoders(void)
{
// Initialize the left encoder's interrupt. INT2 on the user PIC18F8520
// is mapped to the interrupt one pin on the EDU-RC and digital I/O one
// on the FRC-RC.
TRISBbits.TRISB2 = 1; // make sure the RB2/INT2 pin is configured as an input [108]
//
INTCON3bits.INT2IP = 0; // 0: interrupt 1 is low priority (leave at 0 for IFI controllers) [91]
// 1: interrupt 1 is high priority
//
INTCON2bits.INTEDG2 = 1; // 0: trigger on the falling-edge [90]
// 1: trigger on the rising-edge
//
INTCON3bits.INT2IE = 1; // 0: disable interrupt 1 [91]
// 1: enable interrupt 1
// Initialize the right encoder's interrupt. INT3 on the user PIC18F8520
// is mapped to the interrupt two pin on the EDU-RC and digital I/O two
// on the FRC-RC.
TRISBbits.TRISB3 = 1; // make sure the RB3/CCP2/INT3 pin is configured as an input [108]
//
INTCON2bits.INT3IP = 0; // 0: interrupt 2 is low priority (leave at 0 for IFI controllers) [90]
// 1: interrupt 2 is high priority
//
INTCON2bits.INTEDG3 = 1; // 0: trigger on the falling-edge [90]
// 1: trigger on the rising-edge
//
INTCON3bits.INT3IE = 1; // 0: disable interrupt 2 [91]
// 1: enable interrupt 2
}
Initialize_Gyro()
void Initialize_Gyro(void)
{
// reset the heading angle
Reset_Gyro_Angle();
// force a gyro bias calculation
Calc_Gyro_Bias();
// initialize the analog to digital converter
Initialize_ADC();
// initialize and start timer 2
Initialize_Timer_2();
}
Then Initialize_Timer_2() does this:
void Initialize_Timer_2(void)
{
//
// use these parameters for a 200Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_200HZ
// use a 1:16 prescaler
T2CONbits.T2CKPS0 = 0;
T2CONbits.T2CKPS1 = 1;
PR2 = 221; // 223.21 - 2 is ideal
// use a 1:14 postscaler
T2CONbits.T2OUTPS0 = 1;
T2CONbits.T2OUTPS1 = 0;
T2CONbits.T2OUTPS2 = 1;
T2CONbits.T2OUTPS3 = 1;
#endif
//
// use these parameters for a 400Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_400HZ
// use a 1:16 prescaler
T2CONbits.T2CKPS0 = 0;
T2CONbits.T2CKPS1 = 1;
PR2 = 140; // 142.05 - 2 is ideal
// use a 1:11 postscaler
T2CONbits.T2OUTPS0 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS3 = 1;
#endif
//
// use these parameters for a 800Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_800HZ
// use a 1:16 prescaler
T2CONbits.T2CKPS0 = 0;
T2CONbits.T2CKPS1 = 1;
PR2 = 69; // 71.02 - 2 is ideal
// use a 1:11 postscaler
T2CONbits.T2OUTPS0 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS3 = 1;
#endif
//
// use these parameters for a 1600Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_1600HZ
// use a 1:4 prescaler
T2CONbits.T2CKPS0 = 1;
T2CONbits.T2CKPS1 = 0;
PR2 = 140; //142.05 - 2 is ideal
// use a 1:11 postscaler
T2CONbits.T2OUTPS0 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS3 = 1;
#endif
// make sure the timer 2 register starts at zero
TMR2 = 0x00;
// timer 2 interrupt is low priority
IPR1bits.TMR2IP = 0;
// to prevent a spurious interrupt, make sure the interrupt flag is reset
PIR1bits.TMR2IF = 0;
// enable the timer 2 interrupt
PIE1bits.TMR2IE = 1;
// enable timer 2
T2CONbits.TMR2ON = 1;
}
Ok well.. thank you very much for any help you have to offer.
I examined this and noticed it deals with interrupts, which I have had no experience with (It's my first and only year since I'm graduating this year :( ). Would anyone please be able to explain to me where the conflict lies? Here is the code from the three functions:
Serial_Driver_Initialize()
void Serial_Driver_Initialize(void)
{
Initialize_uart();
}
(which calls this) ---v
static void Initialize_uart(void)
{
uword8 flush;
/* Setup the device control block (dcb) */
dcb[PROGRAM_PORT].myRCREG = &RCREG1;
dcb[PROGRAM_PORT].myTXREG = &TXREG1;
dcb[PROGRAM_PORT].portId = 1; /* setup unique ID for PROGRAM_PORT */
Reset_Control_Block(&dcb[PROGRAM_PORT]);
dcb[TTL_PORT].myRCREG = &RCREG2;
dcb[TTL_PORT].myTXREG = &TXREG2;
dcb[TTL_PORT].portId = 2; /* setup unique ID for TTL_PORT */
Reset_Control_Block(&dcb[TTL_PORT]);
/* Initialize the PROGRAM_PORT port */
#if USE_BUFFERED_PRINTF
Open1USART(USART_TX_INT_ON & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, baud_115);
#else
Open1USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, baud_115);
#endif
IPR1bits.TXIP = PRIORITY;
IPR1bits.RCIP = PRIORITY;
TRISCbits.TRISC6 = 0; /*TX1 output*/
TRISCbits.TRISC7 = 1; /*RX1 input */
flush = RCREG1; /* flush data */
flush = RCREG1; /* flush data */
RXINTF = 0;
/* Initialize the TTL_PORT port */
Open2USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, baud_115);
IPR3bits.RC2IP = PRIORITY;
IPR3bits.TX2IP = PRIORITY;
TRISGbits.TRISG1 = 0; /*TX2 output*/
TRISGbits.TRISG2 = 1; /*RX2 input */
flush = RCREG2; /* flush data */
flush = RCREG2; /* flush data */
RXINTF2 = 0;
aBreakerWasTripped = FALSE;
}
Initialize_Encoders()
void Initialize_Encoders(void)
{
// Initialize the left encoder's interrupt. INT2 on the user PIC18F8520
// is mapped to the interrupt one pin on the EDU-RC and digital I/O one
// on the FRC-RC.
TRISBbits.TRISB2 = 1; // make sure the RB2/INT2 pin is configured as an input [108]
//
INTCON3bits.INT2IP = 0; // 0: interrupt 1 is low priority (leave at 0 for IFI controllers) [91]
// 1: interrupt 1 is high priority
//
INTCON2bits.INTEDG2 = 1; // 0: trigger on the falling-edge [90]
// 1: trigger on the rising-edge
//
INTCON3bits.INT2IE = 1; // 0: disable interrupt 1 [91]
// 1: enable interrupt 1
// Initialize the right encoder's interrupt. INT3 on the user PIC18F8520
// is mapped to the interrupt two pin on the EDU-RC and digital I/O two
// on the FRC-RC.
TRISBbits.TRISB3 = 1; // make sure the RB3/CCP2/INT3 pin is configured as an input [108]
//
INTCON2bits.INT3IP = 0; // 0: interrupt 2 is low priority (leave at 0 for IFI controllers) [90]
// 1: interrupt 2 is high priority
//
INTCON2bits.INTEDG3 = 1; // 0: trigger on the falling-edge [90]
// 1: trigger on the rising-edge
//
INTCON3bits.INT3IE = 1; // 0: disable interrupt 2 [91]
// 1: enable interrupt 2
}
Initialize_Gyro()
void Initialize_Gyro(void)
{
// reset the heading angle
Reset_Gyro_Angle();
// force a gyro bias calculation
Calc_Gyro_Bias();
// initialize the analog to digital converter
Initialize_ADC();
// initialize and start timer 2
Initialize_Timer_2();
}
Then Initialize_Timer_2() does this:
void Initialize_Timer_2(void)
{
//
// use these parameters for a 200Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_200HZ
// use a 1:16 prescaler
T2CONbits.T2CKPS0 = 0;
T2CONbits.T2CKPS1 = 1;
PR2 = 221; // 223.21 - 2 is ideal
// use a 1:14 postscaler
T2CONbits.T2OUTPS0 = 1;
T2CONbits.T2OUTPS1 = 0;
T2CONbits.T2OUTPS2 = 1;
T2CONbits.T2OUTPS3 = 1;
#endif
//
// use these parameters for a 400Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_400HZ
// use a 1:16 prescaler
T2CONbits.T2CKPS0 = 0;
T2CONbits.T2CKPS1 = 1;
PR2 = 140; // 142.05 - 2 is ideal
// use a 1:11 postscaler
T2CONbits.T2OUTPS0 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS3 = 1;
#endif
//
// use these parameters for a 800Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_800HZ
// use a 1:16 prescaler
T2CONbits.T2CKPS0 = 0;
T2CONbits.T2CKPS1 = 1;
PR2 = 69; // 71.02 - 2 is ideal
// use a 1:11 postscaler
T2CONbits.T2OUTPS0 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS3 = 1;
#endif
//
// use these parameters for a 1600Hz gyro sample rate
//
#ifdef GYRO_SAMPLE_RATE_1600HZ
// use a 1:4 prescaler
T2CONbits.T2CKPS0 = 1;
T2CONbits.T2CKPS1 = 0;
PR2 = 140; //142.05 - 2 is ideal
// use a 1:11 postscaler
T2CONbits.T2OUTPS0 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS3 = 1;
#endif
// make sure the timer 2 register starts at zero
TMR2 = 0x00;
// timer 2 interrupt is low priority
IPR1bits.TMR2IP = 0;
// to prevent a spurious interrupt, make sure the interrupt flag is reset
PIR1bits.TMR2IF = 0;
// enable the timer 2 interrupt
PIE1bits.TMR2IE = 1;
// enable timer 2
T2CONbits.TMR2ON = 1;
}
Ok well.. thank you very much for any help you have to offer.