Norlin
New Member
Hello everyone,
I've been trying to get an interrupt to occur when data is received via the serial comm. Everything seems to be working as far as sending and receiving data, however when I switch the code to use an interrupt the interrupt never occurs, so the received data is never put into the rec_byte variable. I tried putting in a tmr0 interrupt just so it'd continually trigger the interrupt function and then everything works, but it's the tmr0 interrupt that is triggering and not the RCIE/RCIF...
Basically, RCIF is getting set, but an interrupt isn't being generated or there's something wrong with my interrupt function, can anyone help me out? Thanks in advance!
BTW, I'm using Microchip's MPLAB IDE with PICC
I've been trying to get an interrupt to occur when data is received via the serial comm. Everything seems to be working as far as sending and receiving data, however when I switch the code to use an interrupt the interrupt never occurs, so the received data is never put into the rec_byte variable. I tried putting in a tmr0 interrupt just so it'd continually trigger the interrupt function and then everything works, but it's the tmr0 interrupt that is triggering and not the RCIE/RCIF...
Basically, RCIF is getting set, but an interrupt isn't being generated or there's something wrong with my interrupt function, can anyone help me out? Thanks in advance!
BTW, I'm using Microchip's MPLAB IDE with PICC
Code:
#include <pic.h>
#include <string.h>
#include "delay.h"
__CONFIG(HS & WDTDIS & MCLREN & PWRTDIS & BORDIS & LVPDIS & UNPROTECT);
unsigned char rec_byte = '0';
void interrupt isr(void) @ 0x04
{
unsigned char x;
if(RCIF) // serial rx interrupt?
{
if (OERR == 1) // overrun, reset UART
{
CREN = 0; // disable UART
CREN = 1; // re-enable UART
}
else if (FERR == 1) // framing error (break?)
x = RCREG; // read and discard byte
rec_byte = RCREG;
}
}
void init_pic16f628a()
{
PORTA = 0x00; // Initialize PORTA
PORTB = 0x00; // Initialize PORTB
CMCON = 0x07; // comparators off
TRISA = 0x00; // PORTA Outputs
TRISB = 0x00; // PORTB Outputs
}
void init_usart(void)
{
TRISB |= 0x02; // Make RX pin an input (RB1)
TRISB &= 0xFB; // Make TX pin an output (RB2)
SPBRG = 129; // BaudRate=9600 @ 20Mhz as Table
// +0.16% error at this BaudRate
// PIE1 : EEIE CMIE RCIE TXIE —--- CCP1IE TMR2IE TMR1IE
PIE1 = 0b00110000; // replaces stuff below
// TXIE = 1; // enable serial tx interrupt
// RCIE = 1; // enable serial rx interrupt
GIE = 1; // enable global interrupts
// TXSTA : CSRC TX9 TXEN SYNC —--- BRGH TRMT TX9D
TXSTA=0b00100100; // Asynch,8bits,tx enb
// RCSTA : SPEN RX9 SREN CREN ADEN FERR OERR RX9D
RCSTA=0b10010000; // Cont receive enb
}
// send a single char
void usart_write_char(unsigned char ucChar)
{
TXREG = ucChar;
while(TRMT==0); // Wait for TX to finish
}
void main()
{
init_pic16f628a();
init_usart();
while(1)
{
//while(!RCIF); // Wait until data recieved
//rec_byte=RCREG; // Store for later
usart_write_char(rec_byte); // Transmit
delay(200);
}
}