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.

Pic16F7XX transmission interrupt problem

Status
Not open for further replies.

Tzvik

New Member
Hi to all,
I have written a C program that transmits 4 byes of data in UART using interrupt routine.
My problem is that the interrupt is always ON, how I wrote a right routine that transmit 1 byte wait until the end of the byte transmitted (go to main loop), and go back to the interrupt routine to send the next byte ??

Code:
  TXIE = 1;
  TXEN = 0;
  ei();
/*------------------------------------------------*/
 buff[0]=0x5;
 buff[1]=0x6;
 buff[2]=0x7;
 buff[3]=0x8;
 
 BuffCounter = 0;
 
 TXEN = 1;//Start interrupt
 
...
void interrupt sys_int(void)
{
  if (TXIF == 1)
  {
	if (TRMT == 1)	
	{
		if (BuffCounter <4)
		{
			TXREG = buff[BuffCounter++];
			TXEN = 1;
		}
		else
			TXEN = 0;
	}	
  }
}
 
If you swap around your TXEN and TXIE so that TXEN is always set and you turn TXIE on and off to send data it should work.

Your code should look something like this.
Code:
  TXIE = 0; 
  TXEN = 1; 
  ei(); 
/*------------------------------------------------*/ 
 buff[0]=0x5; 
 buff[1]=0x6; 
 buff[2]=0x7; 
 buff[3]=0x8; 
  
 BuffCounter = 0; 
  
 TXIE = 1;//Start interrupt 
  
... 
void interrupt sys_int(void) 
{ 
  if (TXIF == 1) 
  { 
   if (TRMT == 1)    
   { 
      if (BuffCounter <4) 
      { 
         TXREG = buff[BuffCounter++]; 
      } 
      else 
         TXIE = 0; 
   }    
  } 
}

With the above code you will get 5 interrupts, one for each byte sent and a final one that will disable the transmit interrupt.


HTH

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top