View previous topic :: View next topic |
Author |
Message |
tunatopraktan
Joined: 19 Mar 2021 Posts: 13
|
Receiving problem from RS232 controlled device |
Posted: Tue Mar 23, 2021 4:30 am |
|
|
Hello friends,
I control a rs232 controlled device (a hdmi switcher) via rs232 protocol. While i am sending the code, everything is fine. But when i sent the code, the device send the feedback data to me. And when i want to receive these datas, the program stop.
When i take out the RDA_interrupts, (when i dont want to take feedback from device, my circuit is working good. But i need feedback data from device.
For example:
I can send "SET SW in4 out1\x0D\x0A" (set input 4 to output 1) serial code to device. After that the device send "SWin4 out1<CR><LF>" (input4 to output1 setted) serial data to me. But my device blocks. How can i get this serial data from device correctly ?
My code is below:
Code: |
#include <16f877A.h>
#use delay(clock=20M)
#fuses HS, NOWDT, NOPUT, NOLVP, NOCPD, NOPROTECT, NODEBUG, NOBROWNOUT, NOWRT
#include <string.h>
#include <stdlib.h>
#use rs232 (baud=115200, xmit=pin_c6, rcv=pin_c7, parity=N, stop=1, errors, long_data)
char i;
int t, d, textsize;
char data[20];
#int_rda
void rs232_interrupt()
{
for(t=0; t<10; t++)
{
data[t] = getch();
}
}
void main()
{
set_tris_b(0xFF);
set_tris_c(0x00);
delay_ms(50);
enable_interrupts(GLOBAL);
enable_interrupts(int_rda); // when i make disable the rda interrupts,
// the program is working. But when i make
// enable it, the program block
while(1)
{
if(input(pin_b2) == 1)
{
printf("SET SW in3 out1\x0D\x0A");
delay_ms(50);
output_high(pin_c2);
delay_ms(100);
output_low(pin_c2);
delay_ms(20);
}
if(input(pin_b1) == 1)
{
printf("SET SW in4 out1\x0D\x0A");
delay_ms(50);
output_high(pin_c1);
delay_ms(100);
output_low(pin_c1);
delay_ms(20);
}
delay_ms(500);
if(data[5] == "4") // The "SWin4 out1<CR><LF>" serial code
// should come from device. And i asking data[5].
{
output_high(pin_c0);
delay_ms(500);
output_low(pin_c0);
}
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9255 Location: Greensville,Ontario
|
|
Posted: Tue Mar 23, 2021 4:40 am |
|
|
Have a look at ex_sisr.c, the CCS supplied program that shows how to receive data using the RDA_ISR and a buffer.
It'll be in the EXAMPLES folder.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19569
|
|
Posted: Tue Mar 23, 2021 4:42 am |
|
|
An RS232 interrupt means _one_ byte is available for you to receive.
Just one.
You can't sit in the interrupt loading data.
The interrupt should load just one byte and increment the count and exit.
When the count gets to however many bytes should be in the complete
message then set a flag that the main code uses to say the message is
complete. |
|
|
tunatopraktan
Joined: 19 Mar 2021 Posts: 13
|
|
Posted: Thu Mar 25, 2021 7:41 am |
|
|
Ttelmah wrote: | An RS232 interrupt means _one_ byte is available for you to receive.
Just one.
You can't sit in the interrupt loading data.
The interrupt should load just one byte and increment the count and exit.
When the count gets to however many bytes should be in the complete
message then set a flag that the main code uses to say the message is
complete. |
Thank you very much Ttelmah. Could you give a small example to me. I had solved my rs485 problem with your example. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9255 Location: Greensville,Ontario
|
|
Posted: Thu Mar 25, 2021 7:52 am |
|
|
again.....
Have a look at ex_sisr.c, the CCS supplied program that shows how to receive data using the RDA_ISR and a buffer.
It'll be in the EXAMPLES folder.... |
|
|
tunatopraktan
Joined: 19 Mar 2021 Posts: 13
|
|
Posted: Thu Mar 25, 2021 8:03 am |
|
|
temtronic wrote: | again.....
Have a look at ex_sisr.c, the CCS supplied program that shows how to receive data using the RDA_ISR and a buffer.
It'll be in the EXAMPLES folder.... |
Thank you for your advice temtronic. Unfortunately i am new at MCU so i did not understand this example😣😣😣. why am i using bkbhit function.😩 If you explain basicly , i will be grateful🙏
Thanks for all your help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19569
|
|
Posted: Thu Mar 25, 2021 9:10 am |
|
|
Ex_SISR, stores the incoming data into a circular buffer.
In your main code, a byte is available when bkbhit says there is a byte.
You would need to read the bytes when they exist into a message string,
and then when you have a complete message (presumably marked by
something like a line feed), interpret what is in this buffer. |
|
|
tunatopraktan
Joined: 19 Mar 2021 Posts: 13
|
|
Posted: Fri Mar 26, 2021 2:41 am |
|
|
Ttelmah wrote: | Ex_SISR, stores the incoming data into a circular buffer.
In your main code, a byte is available when bkbhit says there is a byte.
You would need to read the bytes when they exist into a message string,
and then when you have a complete message (presumably marked by
something like a line feed), interpret what is in this buffer. |
Hello friends,
I have integrated my code with "ex_sisr" example .
My code is shown below.
The problem is ; when i enable the rda interrupt ( enable_interrupts(int_rda); ) my circuit stop to send rs232 code too. Code loop is going on. only i can not send any rs232code.
Code: |
#include <16f877A.h>
#use delay(clock=20M)
#fuses HS, NOWDT, NOPUT, NOLVP, NOCPD, NOPROTECT, NODEBUG, NOBROWNOUT, NOWRT
#include <string.h>
#include <stdlib.h>
#use rs232 (baud=115200, xmit=pin_c6, rcv=pin_c7, parity=N, stop=1, errors)
#define buffer_size 32
BYTE buffer[buffer_size];
BYTE next_in;
BYTE next_out;
BYTE data[32];
#int_rda
void serial_isr()
{
int t;
buffer[next_in] = getc();
t = next_in;
next_in = (next_in + 1) % buffer_size;
if(next_in == next_out)
next_in = t; // buffer full
}
#define bkbhit (next_in != next_out)
BYTE bgetc()
{
BYTE c;
while(!bkbhit);
c = buffer[next_out];
next_out = (next_out + 1) % buffer_size;
return(c);
}
void main()
{
set_tris_b(0xFF);
set_tris_c(0x00);
delay_ms(50);
// enable_interrupts(int_rda); // when i make this enable, the circuit does not send rs232 dat to device.
enable_interrupts(GLOBAL);
do
{
delay_ms(1000);
while(bkbhit)
putc(bgetc());
if(buffer[0] == 0x53) // if buffer[0] is "S" //receiver
{
output_high(pin_c1);
delay_ms(50);
output_low(pin_c1);
}
if(input(pin_b1) == 1) // sending data
{
printf("SET SW in4 out1\x0D\x0A");
delay_ms(50);
output_high(pin_c2);
delay_ms(100);
output_low(pin_c2);
delay_ms(20);
}
if(input(pin_b2) == 1) // sending data
{
printf("SET SW in3 out1\x0D\x0A");
delay_ms(50);
output_high(pin_c2);
delay_ms(100);
output_low(pin_c2);
delay_ms(20);
}
}while (TRUE);
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9255 Location: Greensville,Ontario
|
|
Posted: Fri Mar 26, 2021 5:31 am |
|
|
quick comment...
your code....
BYTE next_in;
BYTE next_out;
change to ...
BYTE next_in=0;
BYTE next_out=0;
You should ALWAYS preset any variable to a KNOWN value.
Pretty sure that RAM can be any value upon powerup. Others will know for sure, haven't looked at the datasheet....
I know CCS has #ZERO_RAM PPdirective, to put '0' in all RAM locations, based upon the PIC . |
|
|
tunatopraktan
Joined: 19 Mar 2021 Posts: 13
|
|
Posted: Fri Mar 26, 2021 6:01 am |
|
|
temtronic wrote: | quick comment...
your code....
BYTE next_in;
BYTE next_out;
change to ...
BYTE next_in=0;
BYTE next_out=0;
You should ALWAYS preset any variable to a KNOWN value.
Pretty sure that RAM can be any value upon powerup. Others will know for sure, haven't looked at the datasheet....
I know CCS has #ZERO_RAM PPdirective, to put '0' in all RAM locations, based upon the PIC . |
I add #ZERO_RAM to my code. but same problem. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 26, 2021 6:17 am |
|
|
This is the problem:
Quote: | void main()
{
set_tris_b(0xFF);
set_tris_c(0x00);
|
You have caused the problem by setting the TRIS for Port C.
In section 10.0 of the 16F877A data sheet it says this:
Quote: | Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
set in order to configure pins RC6/TX/CK and RC7/RX/DT
as the Universal Synchronous Asynchronous Receiver
Transmitter |
This means the TRIS for pins C6 and C7 have to be set = 1.
But you have set them = 0, causing the program to fail.
Delete the line shown in bold. Let the compiler handle the TRIS. |
|
|
tunatopraktan
Joined: 19 Mar 2021 Posts: 13
|
|
Posted: Fri Mar 26, 2021 9:41 am |
|
|
Thank you Master.
One problem was solved. Another point I have to solve is I can not take feedback from device. Do you have any advice about that? |
|
|
|