View previous topic :: View next topic |
Author |
Message |
Guest
|
Newbie problem about hyper terminal putc getc |
Posted: Mon Dec 29, 2008 12:29 pm |
|
|
I would like to send and receive integer numbers via hyper terminal by using pic16f877a. But i couldnt do it. where am i wrong? could anyone help me please?
Here x[4] is a string and i should send integer numbers of each members of x[i] string via hyper terminal. And I should see the value of the y0 on the screen.
Code: |
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay (clock=4000000)
#use rs232(baud=300, xmit=PIN_C6, rcv=PIN_C7,parity=N,stop=1)
char x[4];
void main()
{
char y0;
int i;
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
for(i=0,i<=4,i++)
{
x[i]=getc();
}
y0=(x[0]-6*x[1]+x[2]+4*x[3]-5*x[4]);
putc(y0);
getch()
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Dec 29, 2008 12:50 pm |
|
|
You don't receive integer numbers in your example, you receive (and send) character codes. That's obviously not the right way to achieve number calculation.
Why do you setup the SPI interface, b.t.w.? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Dec 29, 2008 12:57 pm |
|
|
Your program may be working in binary, but Hyperterm doesn't display the binary of the serial port. It interprets the bits as ASCII characters.
Try printing known numbers to your PC with printf() and with putc() and examine the difference. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 29, 2008 1:05 pm |
|
|
Quote: | Why do you setup the SPI interface, b.t.w.? |
This block of code is created by the "Wizard" feature of the CCS IDE.
The Wizard puts in code to disable modules, even though they are already
disabled upon power-on reset. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Dec 29, 2008 2:20 pm |
|
|
Quote: | This block of code is created by the "Wizard" feature of the CCS IDE. The Wizard puts in code to disable modules, even though they are already disabled upon power-on reset. |
I see. Unfortunately it's not disabling SPI but enabling it, creating serious problems with some PICs, when it overwrites UART pin settings, see this today's thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=37196
One more erroneous PCW V4.0 feature needing correction. |
|
|
Guest
|
|
Posted: Mon Dec 29, 2008 2:31 pm |
|
|
So what should I do now? what would you do if you were me? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Dec 29, 2008 3:18 pm |
|
|
If you want to receive numbers rather than digits, you have to define an input formatting first. A possible format would be to use <CR> as a delimiter, you can use gets() function to retrieve strings terminated with <CR> and atoi() to convert them to numbers, each. printf() can be used to show the result. |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Tue Dec 30, 2008 7:37 am |
|
|
First, I would try to get the code to compile, you are missing a while loop and the last closing bracket.
When I work on something new, and I bet most other people do to, I try to work in small steps. Why don't you try to just receive one byte, and echo it back to see what the PIC has received. As mentioned before, it will not be what you type into the PC.
Next, see the CCS manual and check out what ATOI() does.
Also, I would check the #USE RS232 statement. It is a good idea to put errors at the end of it eg.
Code: |
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,errors)
|
See how you go.
Mark |
|
|
|