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

getchar() hanging.

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



Joined: 05 Jan 2004
Posts: 17
Location: hamilton, NEW ZEALAND

View user's profile Send private message Visit poster's website

getchar() hanging.
PostPosted: Thu Oct 28, 2004 8:47 pm     Reply with quote

I have a PIC16F84 and run it on a 32kHz watch crystal.
I have compiled the code at the end of this post and connected the pcb to my PC. Using a terminal program I see that the PIC transmits characters correctly but it hangs on the getchar().

I know that the characters are arriving at RB6 (although the PIC is being powered by 3V and the Max 233 levels (with a resistor and zener are a bit high (0-4V)).
Questions: Have I missed out anything obvious.?

Can anybody explain in words the machine code generated by the PCM 2.731 for the getchar() function? I have very little experience in machine code and there seems to be an awful lot of GOTO's.



#include <stdlib.h>
#include "16F84.h"
#fuses LP, NOWDT, NOPROTECT, NOPUT
#USE DELAY(clock = 32760)
#USE RS232(BAUD=300, XMIT = PIN_B7, RCV = PIN_B6)

void main(void){

int character;

set_tris_b(0b01110111); printf("hello world");
while(1){
putchar('a');
while (!kbhit()) ;
character = getchar();
putchar(character);
}
}
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

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

PostPosted: Thu Oct 28, 2004 9:45 pm     Reply with quote

You are missing the errors keyword from the use RS232 line. If a receive overrun error occurs you will lockup the serial interface. No need for the
Code:
While(!kbhit());
- instead of looping on this you may as well block on getchar().

At 32768Hz the processor can only execute 27 instructions per rs232 bit time. You might find removing the while is enough to get it working.

I did not understand what you meant by the MAX232 voltage being high. Have you powered the Max232 from a different power supply rail to the PIC? If so then feeding 4V into a PIC powered at 3V is asking for trouble -possible latchup of the pic. Do you have series resistors limiting the current into the PIC RX line?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 28, 2004 10:00 pm     Reply with quote

Quote:
You are missing the errors keyword from the use RS232 line. If a receive overrun error occurs you will lockup the serial interface.

Um, the 16F84 doesn't have a hardware usart. So this statement is wrong.

Quote:
No need for the Code:
While(!kbhit());
- instead of looping on this you may as well block on getchar().

Since there is no usart, this statement is also incorrect.
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

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

PostPosted: Thu Oct 28, 2004 10:19 pm     Reply with quote

Quote:
Um, the 16F84 doesn't have a hardware usart. So this statement is wrong.


Opps - Fingers engaged without the brain in gear :-)

Fom the CCS manual on the kbhit()...

Quote:
Note that in the case of software RS232 this function should be called at least 10 times the bit rate to ensure incoming data is not lost.


Based on your crystal frequency, the kbhit() function would need to be executed every 2.5 machines cycles. This isn't going to work
Humberto



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

View user's profile Send private message

PostPosted: Fri Oct 29, 2004 8:27 am     Reply with quote

Quote:

(although the PIC is being powered by 3V and the Max 233 levels (with a resistor and zener are a bit high (0-4V)).


I would test the code feeding the PIC with 5V and being sure that the RCV
out of the Max 233 (in iddle state) is a solid HIGH close to rail, without any zener.
I would add a 10K pull up in PIN_B6.

Best wishes, Very Happy

Humberto
pgaastra



Joined: 05 Jan 2004
Posts: 17
Location: hamilton, NEW ZEALAND

View user's profile Send private message Visit poster's website

getchar() hanging.
PostPosted: Sun Oct 31, 2004 3:34 pm     Reply with quote

Thanks for all the ideas.

I changed the baud rate to 110 baud and now the program works.

I only need to send the micro some parameters so 110 baud is OK for now.
pgaastra



Joined: 05 Jan 2004
Posts: 17
Location: hamilton, NEW ZEALAND

View user's profile Send private message Visit poster's website

getchar() hanging.
PostPosted: Sun Oct 31, 2004 4:04 pm     Reply with quote

I said I changed the baud rate to 110 from 300 and it now works.

I had a look at the machine code produced by the compiler for each baud rate, for getchar() and it's identical. How can that be? I thought the software uart would look for an edge of the start bit and then look at the RX pin at the mid point of the subsequent bits. If the code is identical, how can the timing be right for both baud rates?
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