View previous topic :: View next topic |
Author |
Message |
pgaastra
Joined: 05 Jan 2004 Posts: 17 Location: hamilton, NEW ZEALAND
|
getchar() hanging. |
Posted: Thu Oct 28, 2004 8:47 pm |
|
|
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
|
|
Posted: Thu Oct 28, 2004 9:45 pm |
|
|
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 - 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
|
|
Posted: Thu Oct 28, 2004 10:00 pm |
|
|
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
|
|
Posted: Thu Oct 28, 2004 10:19 pm |
|
|
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
|
|
Posted: Fri Oct 29, 2004 8:27 am |
|
|
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,
Humberto |
|
|
pgaastra
Joined: 05 Jan 2004 Posts: 17 Location: hamilton, NEW ZEALAND
|
getchar() hanging. |
Posted: Sun Oct 31, 2004 3:34 pm |
|
|
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
|
getchar() hanging. |
Posted: Sun Oct 31, 2004 4:04 pm |
|
|
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? |
|
|
|