View previous topic :: View next topic |
Author |
Message |
shawnperkins
Joined: 13 Apr 2011 Posts: 2
|
Interrupt problems 24F16KA102 |
Posted: Wed Apr 13, 2011 12:49 pm |
|
|
Hello Folks,
I seem to be having a problem getting an interrupt to fire when serial data is available on UART2 of my PIC processor. I'm sure my hardware is good, and serial data has been verified with a scope on the UART input pin. Attached is my code. It's a modified sample I found here on the BBS. Any help or suggestions would be greatly appreciated. Using PCD 4.114.
Thanks,
Shawn
Code: |
#include "C:\Users\Public\Documents\main4.h"
#include "string.h"
#include "stdio.h"
enum RECEIVE_STATES {IDLE, RECEIVING};
enum RECEIVE_STATES receive_state = IDLE;
#INT_RDA2
#define STX 0x31
#define LINEFEED 0x0A
#define MAX_STR_LEN 40
char RxBuff[MAX_STR_LEN + 1]; // +1 for terminating zero.
int8 rxBuffPtr = 0;
int8 new_string_received = FALSE;
char StringBuffer[MAX_STR_LEN + 1]; // +1 for terminating zero.
#use rs232( baud=1200, UART2, parity=N, bits=8)
// Compare the new received string to the previous string.
// If a new string is found than the string is copied for comparison next time.
void CheckForNewString()
{
if (strcmp(RxBuff, StringBuffer) != 0)
{
// New string found, copy the string and set flag.
strcpy(StringBuffer, RxBuff);
new_string_received = TRUE;
}
}
#int_RDA2
void RDA2_isr(void)
{
char data_in;
output_low(PIN_B15); //flash LED to know that the interrupt got triggered
delay_ms(700);
output_high(PIN_B15); //flash LED to know that the interrupt got triggered
data_in = getc(); // Get character from rs232
putc( data_in ); // Echo character to PC2
switch (receive_state)
{
case IDLE:
if (data_in == STX)
receive_state = RECEIVING;
break;
case RECEIVING:
if (data_in == LINEFEED) // End of string terminator found?
{
RxBuff[rxBuffPtr] = '\0'; // Terminate string
CheckForNewString();
receive_state = IDLE;
rxBuffPtr = 0;
}
else
{
// Only process readable characters
if ((data_in >= 0x20) && (data_in < 127))
{
if (RxBuffPtr < MAX_STR_LEN)
{
RxBuff[rxBuffPtr] = data_in;
rxBuffPtr++;
}
}
}
break;
}
}
void main()
{
setup_spi( FALSE );
setup_adc_ports(sAN12);
setup_adc(ADC_OFF | ADC_TAD_MUL_0);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
enable_interrupts(int_rda2);
StringBuffer[0] = 0; // Terminate string.
while (1)
{
if (new_string_received == TRUE)
printf("New string found: %s\n", StringBuffer);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
shawnperkins
Joined: 13 Apr 2011 Posts: 2
|
|
Posted: Wed Apr 13, 2011 1:26 pm |
|
|
That did it. I was trying to put 'enable_interrupts(GLOBAL)' in my code, but it was throwing an error, 'Unidentified identifier GLOBAL'. By placing 'enable_interrupts(intr_global)' fixed the problem. Thanks for the fast reply!
Shawn |
|
![](templates/subSilver/images/spacer.gif) |
|