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 CCS Technical Support

RS232 receive

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
info@ckmintech.com



Joined: 16 May 2006
Posts: 39

View user's profile Send private message

RS232 receive
PostPosted: Mon May 22, 2006 2:24 am     Reply with quote

I used the following simple program for an experiment

#include <16F628A.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES NOLVP
#FUSES HS
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,enable=PIN_B3)

void main()
{



printf("Enter a char");
while (true) {
putc(getc());
}
}

PC received the string "Enter a char" but the progrma stuck on getc(). No data received. Wiring and RS232 interface were carfully checked and 100% no problem. I used 18F1320 for test again, the result was exactly the same. I am using PCWH 3.249.

Is there a possibility this is caused by the compiler bug?
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon May 22, 2006 2:36 am     Reply with quote

By design, getc() waits until a character is received. The program will hang in the getc() until it receives a character. This is a normal behavior and not a bug. If you don't want the program to wait for the character, use kbhit() to check if there is a character available.

Code:

while (1)
{
  if (kbhit())
  {
    c = getc();
  }
  // do somethig useful
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon May 22, 2006 3:23 am     Reply with quote

Quote:
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,enable=PIN_B3)
Why have you added the enable keyword? This confuses me a bit and suggests to me you are not using a standard 3-wire setup (Tx, Rx, gnd), do you have other control lines like CTS or RTS connected as well?

When replacing the 16F628A by a 18F1320 you are aware that B5 (transmit) is at the same physical pin, but your B2 (receive) has to be changed to another pin?

Many pins in the PIC processors are combined with several hardware features. It is good practice to disable the features you don't use. For example the analog input ports often have priority over the digital input function, so add a call to SETUP_ADC_PORTS(NO_ANALOGS).

Both the 16F628A and the 18F1320 have a hardware UART, why don't you use it?
info@ckmintech.com



Joined: 16 May 2006
Posts: 39

View user's profile Send private message

PostPosted: Mon May 22, 2006 3:30 am     Reply with quote

Sorry. The description is not clear. The program stuck on getc() no matter how many times i hit the keyboard. kbhit() funtion always return zero.
info@ckmintech.com



Joined: 16 May 2006
Posts: 39

View user's profile Send private message

PostPosted: Mon May 22, 2006 4:08 am     Reply with quote

I am using LT1280 for RS232 interface. The On/off pin is connected to RB3, therefore I need to add enable=PIN_B3 to #rs232 clause

For testing with 18F1320, the tx and rx pins are updated in #rs232 clause.

The problem is why kbhit() always return zero and getc() is keep on waiting for input (because kbhit() reurns zero) no matter how many times i hit the keyboard?
Ttelmah
Guest







PostPosted: Mon May 22, 2006 4:37 am     Reply with quote

The reason is simple. You are turning off the LT1280, by using the enable pin.
If you want to receive, the chip _must_ be turned on. The 'enable' output on the serial connection, is for things like RS485 drivers, where you can turn off the _driver_ component only, and leave the line idle, when not sending, with the reciever still wroking. The PIC is doing exactly what you are telling it, and switching the driver off, when you are not sending. On the LT1280, the on/off pin, controls both the driver and the receiver, so you never see the incoming characters.

Best Wishes
Guest








PostPosted: Mon May 22, 2006 6:00 am     Reply with quote

Does this means that teh following program will work?

#include <16F628A.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES NOLVP
#FUSES HS
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)

void main()
{

set_adc_ports(no_analogus)l
output_high(PIN_B3);
printf("Enter a char");
while (true) {
putc(getc());
}
}
nasbyc



Joined: 27 Apr 2009
Posts: 50

View user's profile Send private message AIM Address

PostPosted: Fri Nov 18, 2011 6:01 am     Reply with quote

Ttelmah wrote:
The reason is simple. You are turning off the LT1280, by using the enable pin.
If you want to receive, the chip _must_ be turned on. The 'enable' output on the serial connection, is for things like RS485 drivers, where you can turn off the _driver_ component only, and leave the line idle, when not sending, with the reciever still wroking. The PIC is doing exactly what you are telling it, and switching the driver off, when you are not sending. On the LT1280, the on/off pin, controls both the driver and the receiver, so you never see the incoming characters.


does it means pin_b5(enable pin that connected to pic) has to set low in order to receive character from pc (getc()). currently i can transmit data to pc. however couldn't able to received any data from pc(kbhit()). fyi one max485 connected to pic and another max485 connected to max232 -pc
temtronic



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

View user's profile Send private message

PostPosted: Fri Nov 18, 2011 6:39 am     Reply with quote

Your hardware is too complicated.
Just use the folowing.

PIC<- max232-> PC.

example wiring is in the CCS Help files.

There is no need for the RS485 chips in this setup.

Also, you can get rid of the 'enable' option in the use RS232(...) code.
nasbyc



Joined: 27 Apr 2009
Posts: 50

View user's profile Send private message AIM Address

PostPosted: Fri Nov 18, 2011 7:08 am     Reply with quote

temtronic wrote:
Your hardware is too complicated.
Just use the folowing.

PIC<- max232-> PC.

example wiring is in the CCS Help files.

There is no need for the RS485 chips in this setup.

Also, you can get rid of the 'enable' option in the use RS232(...) code.


i need to use the max485 because i want this pic (master )to communicate with other several pic (slave). or it is posibble that i use this configuration:
pc <-> max232 <-> pic (master) <-> max485 <-> max485 <-> pic (slave)
temtronic



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

View user's profile Send private message

PostPosted: Fri Nov 18, 2011 8:37 am     Reply with quote

That 'system' is much better and far easier to work with .
Work in stages..
You can get the 'master' PIC 'up and running' , then add the slave PICs as required.Just be sure to add the RS485 bus resistors.
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Fri Nov 18, 2011 9:08 am     Reply with quote

The problem here, is that the thread is about RS232, not RS485. The problem of hijacking a thread....

Now, step back. Original thread is about RS232, using a LT1280 driver. The LT1280, has an 'enable' pin, which turns the whole chip on/off, for power saving. If this is connected to an 'enable' output from the PIC, then when the chip is turned off, you can't receive.

RS485 devices are different. They normally have separate 'receive enable', and 'transmit enable' pins, and these are usually opposite polarity. If you look at the standard MAX485, it has a /RE (signal low, enables receive), and DE (signal high enables transmit) pins. With these, you need the following wiring:

1) Connect /RE and DE together.
2) Connect these to the enable output of the PIC.
3) Have a _pull up_ resistor on the RO pin of the device, connecting to he RX pin of the PIC.
4) Have a terminating resistor between the A & B lines at each end of the bus.
5) Have two bias resistors, ensuring that the 'B' line is 200mV above the 'A' line when the bus is not driven. Typically the three resistors can be (for instance) 1.2KR from 5v to line B, then 120R between A and B, then 1.2KR to ground from A. This gives 100R termination (ideal for twisted pair wiring), and the bias as well.

Now on this biasing, with _some_ driver IC's it is not needed (devices exist where an undriven bus is warranted to give a 'high' output), but in general it is safer to have it. Maxim do also do active termination devices, that also provide the bias as well.

What happens then is that the DE pin is driven high _only_ when the PIC is sending data. When this happens the /RE pin turns off the receiver, and the pull up resistor ensures the PIC see nothing when the transmission is being done. As soon as the transmission finished, DE goes low (turning off the transmitter), and /RE also goes low _turning on the receiver_. The bias resistors ensure that nothing is received when the bus is idle, but since the receiver is now 'on', if somebody else sends something, this is received.

Remember though you should only have one set of terminators at each end of the bus.

If you are running with a low data rate, termination may not be needed, but biasing still is.

Best Wishes
nasbyc



Joined: 27 Apr 2009
Posts: 50

View user's profile Send private message AIM Address

PostPosted: Fri Nov 18, 2011 9:51 am     Reply with quote

Ttelmah wrote:
The problem here, is that the thread is about RS232, not RS485. The problem of hijacking a thread....

Now, step back. Original thread is about RS232, using a LT1280 driver. The LT1280, has an 'enable' pin, which turns the whole chip on/off, for power saving. If this is connected to an 'enable' output from the PIC, then when the chip is turned off, you can't receive.

RS485 devices are different. They normally have separate 'receive enable', and 'transmit enable' pins, and these are usually opposite polarity. If you look at the standard MAX485, it has a /RE (signal low, enables receive), and DE (signal high enables transmit) pins. With these, you need the following wiring:

1) Connect /RE and DE together.
2) Connect these to the enable output of the PIC.
3) Have a _pull up_ resistor on the RO pin of the device, connecting to he RX pin of the PIC.
4) Have a terminating resistor between the A & B lines at each end of the bus.
5) Have two bias resistors, ensuring that the 'B' line is 200mV above the 'A' line when the bus is not driven. Typically the three resistors can be (for instance) 1.2KR from 5v to line B, then 120R between A and B, then 1.2KR to ground from A. This gives 100R termination (ideal for twisted pair wiring), and the bias as well.

Now on this biasing, with _some_ driver IC's it is not needed (devices exist where an undriven bus is warranted to give a 'high' output), but in general it is safer to have it. Maxim do also do active termination devices, that also provide the bias as well.

What happens then is that the DE pin is driven high _only_ when the PIC is sending data. When this happens the /RE pin turns off the receiver, and the pull up resistor ensures the PIC see nothing when the transmission is being done. As soon as the transmission finished, DE goes low (turning off the transmitter), and /RE also goes low _turning on the receiver_. The bias resistors ensure that nothing is received when the bus is idle, but since the receiver is now 'on', if somebody else sends something, this is received.

Remember though you should only have one set of terminators at each end of the bus.

If you are running with a low data rate, termination may not be needed, but biasing still is.


sorry for that. i already connect the pic to max485 the same conf u described. and another max485 to max232-pc. the enable pin which connected to /re&de always high. and another max485 which connected to max232, /re&de always zero. i only can transmit data from pic to pc but not other way round. if iconnect pic (master) directly to max232 on pin c6 an c7. can i use the same pin (c6 & c7) to connect to rs485 <--> rs485 <--> pic(slave)
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Fri Nov 18, 2011 10:06 am     Reply with quote

You need to be operating the /RE and DE pins at the PC end.
Easiest solution, get yourself a RS485 adapter for the PC, rather than trying to go RS232-TTL-RS485. Devices that support half duplex, will already control the enable pin for you.
If you really 'must' use RS232 on the PC, then you need some extra circuitry. After you have the Rs232 converted to TTL async, you need to add a monostable multivibrator, or a simple 8pin PIC, setup so that when the falling edge on the TTL serial from the PC is seen, it operates the /RE and DE pins for 9.5 bit times. Remember you also need the pullup resistor on RO at this end.

Best Wishes
nasbyc



Joined: 27 Apr 2009
Posts: 50

View user's profile Send private message AIM Address

PostPosted: Fri Nov 18, 2011 10:10 am     Reply with quote

thanks
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