|
|
View previous topic :: View next topic |
Author |
Message |
Darren Logan Guest
|
Hard + soft USART query |
Posted: Wed Mar 05, 2003 7:31 am |
|
|
Hello all,
PIC = 16F870, PCM = 3.136
I am using both hardware and software USART.
Hardware is used for RS485 and software for RS232.
When I power the unit up, I can communicate with the hardware (RS485 port) no problem straight away.
If I power up and use the software (RS232) the unit does not seem to respond.
If however I use the RS485 port first and then switch over to the RS232 .. it works and continues to work.
Therefore, there's something odd in the way the PIC handles the software USART on power up. The hardware USART is fine.
This problem is driving me nuts (as have other problems) and I'd really appreciate some help!
I guess I need to reset or clear something in the software USART on power up to get it to a ready condition.
Regards,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12373 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 11:23 am |
|
|
:=Hello all,
:=
:=PIC = 16F870, PCM = 3.136
:=
:=I am using both hardware and software USART.
:=Hardware is used for RS485 and software for RS232.
:=
:=When I power the unit up, I can communicate with the hardware (RS485 port) no problem straight away.
:=If I power up and use the software (RS232) the unit does not seem to respond.
-------------------------------------------------------------
Post a very short program that demonstrates the problem.
Then we can test it and decide what's happening.
Make sure you post a complete program: #fuse statement,
all #use statements, #include statement, etc.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12382 |
|
|
Darren Logan Guest
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 1:39 pm |
|
|
Hello,
Thanks for your reply. Files posted below are separated with a line thus --------------------------------------------------
As you will see, the RS485 reception is performed through hardware interrupts. RS232 reception is performed through a timed_getc() routine.
It is the timed_getc() causing the problems.
I've missed out my global.h and other small .c includes.
the main header file:
--------------------------------------------------
#include <16F870.h>
#use delay(clock=4000000)
#fuses XT,NOWDT,PUT,NOPROTECT,BROWNOUT,NOLVP,NOCPD,NOWRT
#use fixed_io(A_OUTPUTS=PIN_A4)
#define PIN_UTX 51 // RS232 Transmit
#define PIN_URX 52 // RS232 Receive
#define PIN_RS485FLO 53 // RS485 Flow control
#use fixed_io(B_OUTPUTS=PIN_B0,PIN_B1,PIN_B2,PIN_UTX,PIN_RS485FLO,PIN_B6,PIN_B7)
#define PIN_DI 62 // RS485 Transmit
#define PIN_RO 63 // RS485 Receive
#use fixed_io(C_OUTPUTS=PIN_C0,PIN_C1,PIN_C2,PIN_C3,PIN_C4,PIN_C5,PIN_C6)
#use rs232(baud=4800,parity=N,xmit=PIN_UTX,rcv=PIN_URX,stream=RS232,ERRORS) // RS232 serial comms pre-processor
#use rs232(baud=4800,parity=N,enable=PIN_RS485FLO,xmit=PIN_DI,rcv=PIN_RO,stream=RS485,ERRORS) // RS485 serial comms pre-processor
--------------------------------------------------
relevant code from my main .c file:
// below is the RS485 interrupt handler
#int_rda
rda_isr() { // RS485 serial char in service routine
string[RS485StrPointer]=fgetc(RS485);
if(string[RS485StrPointer]==':') {
RS485StrPointer=1;
}
else {
RS485StrPointer++;
if(RS485StrPointer>=22) {
RS485StrPointer=0;
if(string[0]==':') {
DoSerialProcess=true;
}
}
}
}
void main() {
do {
// other code
if(DoSerialProcess) {
DoSerialProcess=false;
process_serial(DoRS485);
}
while(true);
}
void timed_getc(void) {
long timeout=0;
int strpointer;
fprintf(RS232,">\r");
while(!kbhit(RS232) && (++timeout<30000)) { // approx. 400mS
delay_us(5);
}
if(kbhit(RS232)) {
string[0]=fgetc(RS232);
if(string[0]!=':') {
return;
}
else {
for(strpointer=1;strpointer<21;strpointer++) {
timeout=0;
while(!kbhit(RS232) && (++timeout<200)) {
delay_us(5);
}
if(kbhit(RS232)) {
string[strpointer]=fgetc(RS232);
}
else {
return;
}
}
process_serial(DoRS232);
return;
}
}
else {
return;
}
}
URGENT HELP IS REQUIRED !!!!!!
Regards,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12386 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 2:55 pm |
|
|
<font face="Courier New" size=-1>:=void timed_getc(void) {
:=
:= long timeout=0;
:= int strpointer;
:=
:= fprintf(RS232,">\r");
:=
:= while(!kbhit(RS232) && (++timeout<30000)) { := delay_us(5);
:= }
-----------------------------------------------------
The basic problem is in the code above.
You're running a fairly slow clock (4 MHz), which gives
you 1 instruction per usec. Then, you're using 4800 baud
for your RS-232 rate. One bit time at that baud rate is
about 20.8 usec.
Your code above, in the typical case, takes about 20 usec
to execute a loop. Well, that's about the same as a bit time.
Suppose a start bit occurs just after the test for kbhit().
By the time you go through the whole loop and get back up
to the top, for another test, most of your start bit is gone.
You've got to do something to shorten that loop.
---
___________________________
This message was ported from CCS's old forum
Original Post ID: 12391 |
|
|
Darren Logan Guest
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 3:15 pm |
|
|
Hello,
Thank you for your reply.
Are you sure about the bit rate? I thought you calculate it thus:
1 second/4800 = 208uS not 20.8
or have I been Mr thicky somewhere? ... it has been known 8-)
If you are correct then am I correct in saying my options are:
1. Decrease the baud rate (to say 2400)
2. Increase the clock speed (to say 8MHz)
any other options?
Regards,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12396 |
|
|
Darren Logan Guest
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 3:24 pm |
|
|
Hello again,
I've just lowered the baud to 2400 and the problem remains.
I'd like to add something though...
I said above that on power up, the RS232 does not work. Well I've found that after using RS485 comms, then powering down and up again the RS232 sometimes works, but I HAVE to use the RS485 first.
It may work after the 1st power cycle, the 2nd, 3rd etc. but once it stops working it remains not working.
If I don't use the RS485, no matter how many times I power cycle and request data on the RS232 - I get nothing.
As ever, a most odd phenomina.
Regards,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12399 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 3:35 pm |
|
|
:=Hello,
:=
:=Thank you for your reply.
:=Are you sure about the bit rate? I thought you calculate it thus:
:=
:=1 second/4800 = 208uS not 20.8
:=or have I been Mr thicky somewhere? ... it has been known 8-)
:=
:=If you are correct then am I correct in saying my options are:
:=
:=1. Decrease the baud rate (to say 2400)
:=2. Increase the clock speed (to say 8MHz)
:=
:=any other options?
:=
:=Regards,
:=Darren
---------------------------------------------------
I did a boo-boo. I'm sorry. I jumped to a conclusion
by reading my calculator too quickly. I'll examine your
code again and look for other problems.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12401 |
|
|
Darren Logan Guest
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 4:31 pm |
|
|
Hello,
Sorry I did a boo-boo to!!
A major boo-boo actually - I've found the fault, it was a silly mistake with indexes in my string array.
Problem solved.
I very much appreciate your time and efforts all the same.
Regards,
Darren (sneaks out the back door with a very red face...)
___________________________
This message was ported from CCS's old forum
Original Post ID: 12403 |
|
|
Eugene Onishi Guest
|
Re: Hard + soft USART query |
Posted: Wed Mar 05, 2003 5:28 pm |
|
|
I've actually had problems as my main loop as the program got bigger and used this routine for the second soft port
setup_ccp2(CCP_CAPTURE_FE);
#use rs232(XX)
#int_ccp2
void serial_isr2() {
t=getc();
}
It's almost like having a hardware usart, except the program execution stops for a full character.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12406 |
|
|
|
|
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
|