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

unable to communicate with pic16f877a through rs232

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



Joined: 09 Mar 2007
Posts: 14

View user's profile Send private message

unable to communicate with pic16f877a through rs232
PostPosted: Fri Mar 09, 2007 7:59 am     Reply with quote

Hi,
I'm trying to communicate via rs232 with a 16f877a for the first time and i can't get anything. this is the code i'm using:

Code:

#include <16F877.h>
#device ADC=8
#include <stdio.h>
#fuses  PUT, XT, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)

void main() {
   int8 x;
   bit_set(x,0);


   SETUP_ADC_PORTS(NO_ANALOGS);
   SETUP_ADC(ADC_OFF);
   SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_1);
   SETUP_TIMER_1(T1_DISABLED);
   SETUP_TIMER_2(T2_DISABLED, 127, 1);
   //SETUP_TIMER_3(T3_DISABLED);
   SETUP_CCP1(CCP_OFF);
   SETUP_CCP2(CCP_OFF);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   output_high(PIN_A0);
   delay_ms(1000);


   while (TRUE) {
     putc('U');

      if(bit_test(x,0)){
         output_low(PIN_A0);
         bit_clear(x,0);
         delay_ms(100);
      }
      else{
         output_high(PIN_A0);
         bit_set(x,0);
         delay_ms(100);
      }
   }
}


i'm using hyperterminal with settings 8,N,1

what am i missing?
thanks a lot;)
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

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

PostPosted: Fri Mar 09, 2007 8:08 am     Reply with quote

Hi,

what I think is missing is that you enable the int_rda interrupt, but you do not have any code handling the interrupt.

try a code as this:
Code:
#int_rda
void rs232handling()
{
   c = getc();
}

(with this example you also need to add a global variable: char c)

I hope this helps,

Cheers, Jos
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 8:15 am     Reply with quote

Quote:

what am i missing?

You are missing to write a code to echoing a received character, that should be
the first attempt to comunicate.
Your code only print the letter "U" and toggle the PIN_A0.

If you search in this forum, for sure you will find hundreds of examples like this:

Code:

#include <16F877.h>
#device ADC=8
#fuses  PUT, XT, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)

void main()
{
char c;

while(1)
 {
  c = getc();
  putc(c);
 }
}


Humberto
Guest








PostPosted: Fri Mar 09, 2007 9:22 am     Reply with quote

thanks for the help,

i have tried booth suggestions and it didn't work!
is there anything else i can try?...i'm using a max232 for level shifting and i have checked the connections several times i don't see here the problem is...

thanks a lot.
xokolatecake



Joined: 09 Mar 2007
Posts: 14

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 9:24 am     Reply with quote

Quote:

thanks for the help,

i have tried booth suggestions and it didn't work!
is there anything else i can try?...i'm using a max232 for level shifting and i have checked the connections several times i don't see here the problem is...

thanks a lot.


i wrote this, just forgot to log on;)
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 10:11 am     Reply with quote

Did you check your hardware? Check if the 232 level converter generate the appropiate
+V and -V voltages. In the regular ICL232/MAX232 converter type, they are PIN2 and PIN6.
Also, try with another terminal emulator, Hiperterminal + Window$... is not the best
choise to test RS232 communications. Most users reported negative comments here.

Humberto
xokolatecake



Joined: 09 Mar 2007
Posts: 14

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 11:13 am     Reply with quote

thanks a lot for your answer,

i read 9V on V+ pin and -11V on V- pin, which i belive to be fine(according to data sheet). i have tried eltima's advanced serial port terminal and the serial port monitor that is part of ccs ide, and the result is the same:no data transfer!
i don't know what else to do..let me just make a few questions to clear my mind out:

-the pins not at use on the 232 chip should be grounded?
-the crystal i'm usong is not exactly 4mhz but close, does it matter?
-i only made 2connections between the pic and the 232 chip, pins rc6 and rc7, are those enough?


thanks a lot for your help
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 11:34 am     Reply with quote

Quote:
-the pins not at use on the 232 chip should be grounded?



You don't need to ground spare transmitter and receiver inputs

Quote:
-the crystal i'm usong is not exactly 4mhz but close, does it matter?


You do need to accurately reflect your oscillator frequency. The compiler will use your #use delay command to calculate the correct values to write into the PIC's UART registers to give a certain baud rate. If what you put in the #use delay statement does not match your oscillator frequency, the baud rate generated will not be 9600 baud as you would like. That may be enough to print garbage or cause other odd things to happen.

Quote:
-i only made 2connections between the pic and the 232 chip, pins rc6 and rc7, are those enough


Assuming you made the right connections, those two should be enough.

Have you looked at the output of the PIC/MAX232 with a scope, logic analyzer or something like that? If you are getting absolutely nothing at all, I'd check the output of the PIC AND that you have continuity in the RS232 cable AND that is the cable is the correct type (i.e. straight-through or NULL modem)

r.b.
xokolatecake



Joined: 09 Mar 2007
Posts: 14

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 12:20 pm     Reply with quote

thanks for your answer,

i've tried with a 10mhz crystal, changing this part of the code:
Code:

(...)

#fuses  PUT, HS, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=10000000)

(...)


and it didn't work...
i checked my cable and i got:
6-4,2-3,1-8,5-5 connections.

Does this look right to you? i could not find what kind of handshaking (loop-back,partial,full) my cable has...

thanks a lot for the help;)
xokolatecake



Joined: 09 Mar 2007
Posts: 14

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 2:14 pm     Reply with quote

another thing,

i noted that my 232chip r1out(pin12) outputs 5v when not connected to the rc7 pin on the pic, and when i connect it, the voltage comes down to about 2v...is this a hint of something going wrong?

thanks a lot for all the help;)
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 2:34 pm     Reply with quote

Your explanation of your connections is not very clear, and without seeing your actual board, I can't really tell how you have things connected.

In your description, you say you have pin 2 connected to pin three. Pin 2 of what and Pin 3 of what? In a normal RS232 system Pin3 of the PC (DTE) is a Tx pin and pin 2 of a peripheral (DCE) is also Tx. These are both outputs and therefore no RS232 transaction will take place.

What you must do is ensure that there is connectivity between the Tx pin of the PIC and Pin 2 (Rx) of the PC's RS232 port. Check your MAX232 connection, its connection to the DB9 connector, and check the RS232 cable, which may have the Rx and Tx pins cross over on the way to the PC.

If the connection is correct, and nothing is happening, then you've got to trace the signal with something like an oscilloscope.


And the MAX232 inverts signals, so the behaviour you see is not necessarily unusual.

r.b.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 2:35 pm     Reply with quote

Quote:

checked my cable and i got:
6-4,2-3,1-8,5-5 connections.

Your cable should have the following connections:
2-3,3-2,5-5


Quote:

Does this look right to you? i could not find what kind of handshaking (loop-back,partial,full) my cable has...

To comunicate a PIC with a PC it is not necesary any handshaking.

Use a cable as shown in the lower example:
http://www.isotech.co.uk/images/rs232.gif

Humberto
gdl_pro



Joined: 01 Mar 2007
Posts: 1

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 4:11 pm     Reply with quote

This will help you debug your hardware first and then debug the real failure.

1.- disconnect you rs232 cable from your circuit and make a short in pins 2-3 of the cable. Use your hyperterminal to send text. Verify if the data is returned thru the short.

2. If cable works remove the short and reconnect it to the circuit and a short between pics C6 & C7 of the PIC. Use your hyperterminal to send text. Verify if the data is returned thru the short.


If both passsed thenm your hardware is correct and it is time to look deeper in the code or try a new chip. If not, then you can first solving the HW problems
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