|
|
View previous topic :: View next topic |
Author |
Message |
dokeiby
Joined: 12 Jun 2018 Posts: 3
|
PIC16F1619 RS232 UART Interrupts ISSUE |
Posted: Tue Jun 12, 2018 11:58 pm |
|
|
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
|
|
Posted: Wed Jun 13, 2018 1:11 am |
|
|
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: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Jun 13, 2018 5:07 am |
|
|
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
|
|
Posted: Wed Jun 13, 2018 7:21 am |
|
|
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:
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:
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:
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Jun 13, 2018 11:36 am |
|
|
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
|
PIC16F1619 RS232 UART Interrupts ISSUE |
Posted: Mon Jun 18, 2018 12:23 am |
|
|
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: 19551
|
|
Posted: Mon Jun 18, 2018 1:18 am |
|
|
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
|
Re: PIC16F1619 RS232 UART Interrupts ISSUE-SOLVED |
Posted: Mon Jun 18, 2018 8:00 pm |
|
|
JUST Disconnect PICKit3.
|
|
|
|
|
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
|