CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Can't save characters to an array in 16F877A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GG



Joined: 19 Nov 2010
Posts: 1

View user's profile Send private message Yahoo Messenger

Can't save characters to an array in 16F877A
PostPosted: Fri Nov 19, 2010 9:31 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 19, 2010 9:52 pm     Reply with quote

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
Code:
putc('0'+i)

That will work for 0<=i<=9.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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