View previous topic :: View next topic |
Author |
Message |
kloppy_ Guest
|
printf with rs232 |
Posted: Wed Jul 28, 2004 5:19 pm |
|
|
can anybody tell me why this program only works when i omit the line
printf("received");?
i am using pcm with 3.110
Code: | #include <16F876.h>
#use delay (clock=20000000)
#use RS232 (baud=9600, xmit=pin_c6, rcv=pin_c7,bits=8,parity=n)
#use standard_io(c)
#fuses XT, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOLVP
char input=0;
#int_rda
void serial_isr()
{
input=getc();
}
main()
{
enable_interrupts(int_rda);
enable_interrupts(global);
while (1==1)
{
delay_ms(1000);
printf("%C",input);
printf("received");
}
} |
it also doesnt work if i use Code: | printf("%c received",input); |
instead of
Code: | printf("%C",input);
printf("received"); |
i am really confused..... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 28, 2004 5:35 pm |
|
|
What do you mean by "doesn't work" ?
Please explain what you expect to see on the terminal window,
and what you actually see. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jul 28, 2004 7:00 pm |
|
|
You should use HS instead of XT with 20MHz. |
|
|
kloppy
Joined: 28 Jul 2004 Posts: 32
|
|
Posted: Thu Jul 29, 2004 4:49 am |
|
|
doesn't work means i don't get anything back from my PIC (i use hyperterminal on my pc).
i expect that my program echoes what i typed on the keyboard with a "received" after every letter evey second.
everything works fine if i omit the printf("received");.....
(this is just a test-program if the rs232 works) |
|
|
prach Guest
|
example code |
Posted: Thu Jul 29, 2004 5:51 am |
|
|
Code: |
#include <16F628A.h>
#use delay (clock=4000000)
#use RS232 (baud=9600, xmit=pin_b2, rcv=pin_b1)
#fuses XT,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#include <input.c>
char input=0;
#int_rda
void serial_isr()
{
input=getc();
printf("%C",input);
printf("received");
}
main()
{
enable_interrupts(int_rda);
enable_interrupts(global);
while(true);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 29, 2004 11:22 am |
|
|
Quote: | Doesn't work means i don't get anything back from my PIC. |
What do you mean by "anything" ? Does that mean no characters
at all ? Or, does it mean you're getting the "received" word, but
not the echoed characters ?
I compiled your program for the 16F877 and ran it. I got this:
receivedreceivedareceivedareceivedareceivedareceivedareceivedareceived
Now, your program is not a very good test program, for many reasons.
But the main reason is that you have no spaces inbetween the echoed
character and the word "received", and you have no carriage-return/
linefeed after each character. It's all piled in together.
So in the displayed data above, it just looks like a bunch of "received"
words. But I did, in fact, press the "a" character and it was echoed back.
If I take the string above and put the "a" characters in bold,
then you can see them:
receivedreceivedareceivedareceivedareceivedareceivedareceivedareceived
Do you think that this is actually what's happening ? |
|
|
valemike Guest
|
|
Posted: Thu Jul 29, 2004 1:30 pm |
|
|
I couldn't help but notice in your code:
It isn't necessary to write (1==1).
It is sufficient to just write in:
or
|
|
|
Guest
|
|
Posted: Thu Jul 29, 2004 2:03 pm |
|
|
thanks - but these are only details.
i get nothing back from my pic. the terminal window is empty.
you are right it's no a good test program. originally this was a much bigger program which worked perfectly, excluded the rs232 communication. then i startet to delete more and more parts of my program til i found out that the only problem is this printf()!
it is really strange. i often used rs232 with 16f876 a few years ago and i never had a problem.
is it possible that the compiler doesn't work properly?
or maybe a hardware bug?
i also tried another pic with no success... |
|
|
kloppy
Joined: 28 Jul 2004 Posts: 32
|
|
Posted: Thu Jul 29, 2004 2:05 pm |
|
|
oops i forgot to log in.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 29, 2004 2:14 pm |
|
|
What's your compiler version ? |
|
|
kloppy
Joined: 28 Jul 2004 Posts: 32
|
|
Posted: Thu Jul 29, 2004 2:18 pm |
|
|
compiler version 3.110 |
|
|
kloppy
Joined: 28 Jul 2004 Posts: 32
|
|
Posted: Thu Jul 29, 2004 2:28 pm |
|
|
i've got some news: if i make a hardware mclr, the program sends a naked "received" every second. but as soon as i hit a key, the pic stops to send anything.
that looks like this:
received
received
received
received
received
received
received
received
received
(i added a \r\n)
i thought that looks like the program hangs in the interrupt routine. but as soon as i change the program by omitting the printf("received");, the program works fine!!
the terminal looks like this:
ggggghzufdddddddddddll��hhgggggggtrsyaaa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 30, 2004 1:02 pm |
|
|
I installed PCM vs. 3.110 and compiled your original program.
I got this output:
areceivedareceivedareceivedareceivedareceivedareceived
(It has "a" in front of "received" because I pressed the "a" key).
So there is nothing wrong with vs. 3.110.
I suggest that you do this:
1. Re-install the compiler.
2. Change the fuse setting from XT to HS, since you're using
a 20 MHz crystal.
3. Check your circuit for any floating pins, such as MCLR or Ground pins.
4. Check your settings for Hyperterminal. Do not set it for
"hardware handshaking", or XON/XOFF.
5. Check your settings in Control Panel / System / Device Manager /
Ports / Com1 (or whichever one you're using) / Port Settings /
Advanced. Set the Fifo buffers to the Default mode.
6. Try another PIC chip. |
|
|
kloppy
Joined: 28 Jul 2004 Posts: 32
|
|
Posted: Sat Jul 31, 2004 10:05 am |
|
|
most of your suggestions i alreay tried. if the compiler isn't the problem, i think i will check my hardware again.... |
|
|
Ttelmah Guest
|
|
Posted: Sat Jul 31, 2004 2:42 pm |
|
|
kloppy wrote: | most of your suggestions i alreay tried. if the compiler isn't the problem, i think i will check my hardware again.... |
Try adding the ERRORS directive to your RS232.
If your PC, is implementing 'echo', then when you type one character, this is sent to the PIC, which then sends back the long string 'received', which would then be echoed back by the PC. At this point, since you are inside the interrupt handler (general comment, don't send characters in the receive interrupt handler, do it outside...), the UART will overflow, the error bit will be set, and the system will hang.
Turning off echo would fix this (but having ERRORS would be worthwhile as well).
Best Wishes |
|
|
|