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.

PIC16F628a Problem generating a RCIE interrupt

Status
Not open for further replies.

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

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);
  }
}
 
Oh, also I've found a post on this forum saying that PEIE has to be set for this. However, looking at the datasheet for the PIC16F628a I could not see this, specifically looking at section 12.2.2
So I went and set PEIE to see if it would help. With the code I have, setting PEIE seems to "hang" the PIC
 
So it did come down to being PEIE, I set this but got a hang, so I cleared TXIE since I'm not using a TX interrupt at the moment and it got rid of the hang and got the program working normally...
 
It hangs because you are setting TXIE but you don't have any service code for it and so the interrupt is constantly retriggered. Change it back to just setting RCIE, PEIE and GIE. Also, both tris bits have to be set to input for the UART to function properly (see step 1 - page 82) .

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top