GG
Joined: 19 Nov 2010 Posts: 1
|
Can't save characters to an array in 16F877A |
Posted: Fri Nov 19, 2010 9:31 pm |
|
|
Pls any one help me...!
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
char msg[10],a,i=0;
void main()
{
lab1:
while(!kbhit())
{
i=0;
do
{
a=getch();
putc(a);
msg[i]=a;
i++;
if(i == 10)
i=0;
}while(a!=0x0D);
puts(msg);
goto lab1;
}
}
|
In this, character is read from hyper-terminal and to be stored in an array.
Problem is, a=getch() is working, putc(a) works perfectly it prints the char what we enter. But char's are not stored in array.... I can't able to find what was the problem.
When puts(msg) is called it prints some garbage values like symbols!
I am new to pic programming! pls suggest a nice book for embedded c programming.
Advance thanks for reply..... |
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Fri Nov 19, 2010 9:52 pm |
|
|
Look up puts() in the manual.
A null-terminated string means that the string has a zero, 0, 0x00 at the end. That's how the function knows when to stop displaying characters.
figure out how to add a zero to the end of your string.
Look at some of the example programs that come with the compiler and figure out how to make an infinite loop without the GOTO.
Indent your code and make it more readable.
Don't give up so easily. You've already gotten the hardest part of your program working. You're receiving and displaying characters. Maybe you can use that skill to display each element of msg as it is acquired.
I've found that a simple bit of terminal IO is better than a debugger for troubleshooting.
Here's a helpful hint: you can print your index counter 'i' by using
That will work for 0<=i<=9. |
|