|
|
View previous topic :: View next topic |
Author |
Message |
lsteele
Joined: 02 Jan 2007 Posts: 18
|
Trouble getc() ing characters with serial |
Posted: Thu Jan 18, 2007 6:34 pm |
|
|
Hi there,
I'm trying to get a first serial program working between a pic877a clocked at 20mhz and my pc, via a serial cable and an rs232 interface built to this design.
Using the following code I can successfully read the output from the PIC in hyperterminal, but pressing a pc key doesn't cause the pic to echo it back as intended:
Code: |
#include <16f877a.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=pin_c6,rcv=pin_c7)
...
...
do {
set_adc_channel(0);
delay_us(ADCTIME);
aCon = read_adc();
set_adc_channel(5);
delay_us(ADCTIME);
arCon = read_adc();
set_adc_channel(1);
delay_us(ADCTIME);
accelTot = read_adc();
set_adc_channel(4);
delay_us(ADCTIME);
gyroNull = read_adc();
printf("\r\n\naC:%Ld arC:%Ld ", aCon, arCon);
printf("\r\nac:%Ld gy:%f ", accelTot, gyroNull);
if (kbhit()) {
charIn = getc();
printf("\r\n\r\nYou pressed %c", charIn);
delay_ms(500);
}
} while(true);
...
|
If I add ERRORS to the #use rs232 statement (i.e. #use rs232(baud=9600, xmit=pin_c6,rcv=pin_c7, errors) ) it starts saying "you pressed [spade symbol]" over and over - but if I press a key it doesn't seem to pick it up.
I was previously messing around with Sparkfun's boot loader. The test program you have to run echos back input to the terminal, and this worked just fine, so presumably my hardware setup is ok.
I'm sorry to be posting on such a common topic, but I'm at a bit of a loss here. Any suggestions are greatly appreciated!
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 18, 2007 8:09 pm |
|
|
Quote: | but if I press a key it doesn't seem to pick it up. |
Add the NOLVP fuse and check your hardware connections to the
Rx pin on the PIC. Are you using a MAX232-type chip ? |
|
|
lsteele
Joined: 02 Jan 2007 Posts: 18
|
|
Posted: Fri Jan 19, 2007 3:25 pm |
|
|
Thanks,
I've added the NOLVP fuse, however ICprog defaults to this anyway so that's not an issue.
I'm now using a MAX232 with a 16f876a. I've revised the code slightly to make it clearer (below). I've managed to get a picbasic version of this program working absolutely fine on the same hardware, so I know now it's a software problem. But I haven't a clue what!
Here's the test program:
Code: |
#include <16f876a.h>
#device ADC=10
#fuses xt,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=pin_c6,rcv=pin_c7)
#include "lcd.c"
#define GLED pin_c0
void flashLed()
{
char i;
for(i=0; i<10; i++) {
output_high(GLED);
delay_ms(30);
output_low(GLED);
delay_ms(30);
}
}
void main() {
char charIn;
unsigned long counter;
set_tris_b(0x83);
set_tris_c(0x80);
flashLed(); // Tell world processor's running
lcd_init(); // Initialise LCD
setup_uart(9600);
counter = 0;
do {
printf("\r\nProgram running. Counter: %Ld. Input a character: ", counter);
counter++;
charIn = getc();
printf("\r\n\r\nYou pressed %c", charIn);
} while(true);
}
|
I'm completely at a loss! Any suggestions really are appreciated. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Jan 19, 2007 3:49 pm |
|
|
Have you considered using the ISR to detect an incoming character? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 19, 2007 3:51 pm |
|
|
Is the LCD connected to the PIC's UART Tx pin ? You have printf()
statements in your program and you're not using the re-direction feature
to send the data to lcd_putc(). For this reason, I suspect that either
you have a serial LCD or you're not using an LCD. Can you give some
more information on this ?
What device are you using to send data to the PIC's Rx pin ?
Is it a terminal window program running on your PC, or something else ?
There are a few things in your program that are a little strange.
1. You have two NOPROTECT statements in the fuses.
2. You're using the setup_uart() function to set the baud rate to 9600,
but this has already been done in the #use rs232() statement.
It doesn't need to be done two times.
3. You're setting the TRIS but you're not using the #fast_io() mode.
You should have the ERRORS parameter in your #use rs232() statement.
If your program has a problem with ERRORS, then it means you need to
solve the problem. I can't think of a way that ERRORS would ever cause
a problem.
What is your compiler version ? You can find this number at the top
of your .LST file, which is in your project directory. The number will
be similar to this: 3.191, or 3.249, or 4.018, etc. Don't post any
numbers that come after the version. Just post the version. |
|
|
lsteele
Joined: 02 Jan 2007 Posts: 18
|
|
Posted: Fri Jan 19, 2007 7:21 pm |
|
|
Hi,
Thanks for the reply.
The strange things you point out are mostly due to the fact that I re-wrote a much bigger program to create this simple test program and left in a few unnecessary things. To clarify:
I'm not using an LCD at the moment (well, it's there, but I'm not sending anything to it). The PIC is hooked up to a PC serial connection via a MAX232. I'm trying to send data using a terminal program on the PC - I've tried using both hyperterminal and Procomm.
Compiler version is 4.018.
I'll try putting in the ERRORS parameter. I wasn't entirely clear from the documentation what it's purpose is...
I tried using setup_uart(9600) to see if it made any difference (although I now appreciate it's unnecessary).
Thanks again. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 19, 2007 10:43 pm |
|
|
Try a simple program below, that echoes back the characters that
you type in to the terminal window. It should work.
Code: |
#include <16F876A.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
void main()
{
char c;
while(1)
{
c = getc();
putc(c);
}
} |
|
|
|
lsteele
Joined: 02 Jan 2007 Posts: 18
|
|
Posted: Sat Jan 20, 2007 3:24 am |
|
|
Ok, that worked! Thanks.
I'm now going to go step by step adding to your program until I locate the difference that's preventing my version from working. |
|
|
canuck Guest
|
hyperterminal vs. B&B COM test |
Posted: Sat Feb 10, 2007 10:34 am |
|
|
I have also been having problems using getc() with Hyperterminal. When I press a key it seems to bog down, but nothing is echoed back to the screen. Other print statements work fine.
When I use "B&B COM Test" v1.0 everything works fine.
code:
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
void main()
{
char c;
while(1)
{
c = getc();
putc(c);
}
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 10, 2007 5:39 pm |
|
|
I've had many communication problems related to Hyperterminal. Please save yourself a lot of time and get a decent terminal emulator program.
For example use Siow.exe, provided with your CCS compiler. |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Sat Feb 10, 2007 7:05 pm |
|
|
ckielstra wrote: | I've had many communication problems related to Hyperterminal. Please save yourself a lot of time and get a decent terminal emulator program.
For example use Siow.exe, provided with your CCS compiler. |
I really like this one : http://bray.velenje.cx/avr/terminal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Feb 10, 2007 11:44 pm |
|
|
There are improved versions of HyperTerminal.
http://www.hilgraeve.com/htpe/index.html
Readme file:
Scroll down to the versions list.
http://www.hilgraeve.com/support/readmes/htpe.html
I've used version 5.0. It looks like they now have a later version.
I'm not saying it's better than the other terminal programs. It's
probably not. But if you did want to have HyperTerminal on your
system and use it occasionally, it's probably better to use this improved
"Private Edition" version. |
|
|
canuck Guest
|
Thanks |
Posted: Tue Feb 13, 2007 4:59 pm |
|
|
Thanks for your suggestions everyone. |
|
|
Guest
|
Hmmm... |
Posted: Mon Mar 26, 2007 5:15 pm |
|
|
I'm using a PIC18F4550, and neither gets() or getc() work at all. They continue to just hang.
I've tried everything mentioned here.
I have the proper line driver, etc. For some reason, sometimes it works while other times it won't work. Output is no problem, it's just the input.
Even something as simple as this doesn't work:
#include <18F4550.h>
#device adc=8
#FUSES HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
void main(void)
{
char key;
printf("This works.\n\r");
key=getc();
putc(key);
printf("We've finally allowed you to get here.\n\r");
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 26, 2007 5:31 pm |
|
|
If putc() works and getc() doesn't work, check the wiring.
Is this a board that you built yourself, or is it a manufactured demo board ? |
|
|
|
|
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
|