CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC16F1619 RS232 UART Interrupts ISSUE

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dokeiby



Joined: 12 Jun 2018
Posts: 3

View user's profile Send private message

PIC16F1619 RS232 UART Interrupts ISSUE
PostPosted: Tue Jun 12, 2018 11:58 pm     Reply with quote

Hello.

I am having very strange problems with serial interrupts.
Let me try to explain the situation:

The device is PIC16F1619.
If interrupt is not used, RS232 communication is done.
But if using interrupt then is not working INT_RDA.

Please let me know how to use INT_RDA.

Not using interrupt:
Code:
 
#use delay(internal=8MHz)
#define USART_RX        PIN_B5
#define USART_TX        PIN_B7
#use rs232(baud=9600,parity=N,xmit=USART_TX,rcv=USART_RX,bits=8, ERRORS, stream=PORT1)

void main()
{
   while(1)
   {
      if(kbhit())
      {
         c  = getc(PORT1);
         RcvBuffer[RcvCnt] = c;
         RcvCnt++;
         putc(c,PORT1);
      }
   }
}


Using interrupt (INT_RDA):
Code:
 
#use delay(internal=8MHz)
#define USART_RX        PIN_B5
#define USART_TX        PIN_B7
#use rs232(baud=9600,parity=N,xmit=USART_TX,rcv=USART_RX,bits=8, ERRORS, stream=PORT1)

int8  RcvCnt;
int8 RcvBuffer[100];

#INT_RDA
void  RDA_isr(void)
{
   char c;
   
   c = getc(PORT1);

   RcvBuffer[RcvCnt] = c;
   
   RcvCnt++;
}

void main()
{
   RcvCnt = 0;
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   while(1)
   {
      if(RcvCnt >= 0)
      {
         putc(RcvBuffer[RcvCnt], PORT1);
         RcvCnt  --;
      }
   }
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Jun 13, 2018 1:11 am     Reply with quote

There are examples supplied with the compiler which show you how to use interrupts for serial I/O, and how to handle a buffer.

Also use the code button, it preserves your formatting and makes code easier to read.

Mike


+++++++++++++++++++++
Code blocks added to O.P.
- Forum Moderator
+++++++++++++++++++++
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jun 13, 2018 5:07 am     Reply with quote

this
Quote:
putc(RcvBuffer[RcvCnt], PORT1);

apprears to be the wrong syntax when using streams and 'putc' function.

If you press F11 while your project is open, you'll have the CCS Manual onscreen and can search it.... I always have it open as I'm old and can't memeber stuff and my typing is bad.

I believe you need
Code:
fputc(RcvBuffer[RcvCnt], PORT1);

also
for some unknown reason you decrement the counter variable. Now, I think CCS defaults int8 variable to unsigned ,so 0...255, when you start with 0 then decrement, the counter is 255, so your if(.....) will always be true ??

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 13, 2018 7:21 am     Reply with quote

You have to use PPS with this PIC. This is stated in the data sheet.
Add the two lines shown below in bold:
Quote:

#define USART_RX PIN_B5
#define USART_TX PIN_B7

#pin_select U1RX=USART_RX
#pin_select U1TX=USART_TX

#use rs232(baud=9600,parity=N, xmit=USART_TX,rcv=USART_RX,bits=8, ERRORS, stream=PORT1)


2nd thing. You have two compiler warnings, concerning "Condition always
true". One of them is caused by doing while(1). This can easily be fixed
by changing it to:
Code:
while(TRUE)

It's important to do that, so that when you see "Condition always true",
you will actually look at the line, rather than blowing it off because you
think it's just another while(1) warning.

The 2nd warning about "Condition always true" concerns this line:
Quote:
if(RcvCnt >= 0)

RcvCnt is an int8. This is unsigned by default in CCS, for this compiler.
So an unsigned number will always be greater than or equal to 0.
That's a bug in your code. You most likely want this:
Code:
if(RcvCnt > 0)
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Jun 13, 2018 11:36 am     Reply with quote

Just to explain a little your existing code is _not_ using the UART. It is using a software UART, so no INT_RDA.
PCM_programmer shows how to actually use the UART.
dokeiby



Joined: 12 Jun 2018
Posts: 3

View user's profile Send private message

PIC16F1619 RS232 UART Interrupts ISSUE
PostPosted: Mon Jun 18, 2018 12:23 am     Reply with quote

to PCM programmer
I am try below. but Same situation.

#use delay(internal=8MHz)

#define USART_RX PIN_B5
#define USART_TX PIN_B7

#pin_select U1RX=USART_RX
#pin_select U1TX=USART_TX

#use rs232(baud=9600,parity=N,xmit=USART_TX,rcv=USART_RX,bits=8, ERRORS, stream=PORT1)

int8 RcvCnt;
int8 RcvBuffer[100];

#INT_RDA
void RDA_isr(void)
{
char c;

c = getc(PORT1);

RcvBuffer[RcvCnt] = c;

RcvCnt++;
}

void main()
{
RcvCnt = 0;

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(1)
{
if(RcvCnt > 0)
{
fputc(RcvBuffer[RcvCnt], PORT1);
RcvCnt --;
}
}
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 1:18 am     Reply with quote

Use this:
Code:

#define USART_RX PIN_B5
#define USART_TX PIN_B7

#pin_select U1RX=USART_RX
#pin_select U1TX=USART_TX

#use rs232(baud=9600,parity=N,UART1,bits=8, ERRORS, stream=PORT1)


This is shown in the 'sticky' at the head of the forum about #PIN_SELECT.

I don't know why. On some chips, once you have done the select, you can use the pin names, and the #use will work. But on others it only selects the hardware UART, if you tell it to use this with the 'UART1'. The UARTx syntax is the 'safe' one that always forces the physical UART to be used.
dokeiby



Joined: 12 Jun 2018
Posts: 3

View user's profile Send private message

Re: PIC16F1619 RS232 UART Interrupts ISSUE-SOLVED
PostPosted: Mon Jun 18, 2018 8:00 pm     Reply with quote

JUST Disconnect PICKit3.
Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group