|
|
View previous topic :: View next topic |
Author |
Message |
Abdulla M.A.
Joined: 28 Mar 2010 Posts: 30 Location: Baghdad, Iraq
|
Getting char from computer and type it on GLCD (problem) |
Posted: Fri Oct 22, 2010 12:29 am |
|
|
Hi guys,
when I execute the program and start typing letters, nothing appear
on the GLCD, why???
Code: | #include <16f877a.h>
#use delay(clock=4000000)
#include <HDM64GS12.c>
#include <graphics.c>
#use rs232(baud=9600, xmit=pin_c2, rcv=pin_c3)
#fuses xt,nowdt,nocpd,nolvp,noprotect
unsigned char i,text;
unsigned char text45[6]={"Hello"};
void main()
{
glcd_init(on); // Initialize the GLCD
delay_ms(100);
glcd_text57(0,0,text45,1,on); // type test message on GLCD
delay_ms(1000);
printf("Hello world"); // type test to hyperterminal
delay_ms(1000);
while(1)
{
printf("\n\r Start typing your letter???");
xt=1;
yt=20;
for(i=0;i<=20;i++)
{
text=getc();
glcd_text57(xt,yt,text,1,on);
xt+=10;
if(xt>120)
{
xt=0;
yt+=10;
}
}
}
}
|
_________________ "A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engineer is twice bigger than necessary." |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Oct 22, 2010 1:42 am |
|
|
I have not used that driver but looking at your code :-
glcd_text57(xt,yt,text,1,on);
it looks like glcd_text57 requires a string, text is a single char. This probably means the next location in memory does not contain a null.
Your program will output garbage.
Either use a put routine to output a single char or convert the char to a string. |
|
|
Abdulla M.A.
Joined: 28 Mar 2010 Posts: 30 Location: Baghdad, Iraq
|
|
Posted: Fri Oct 22, 2010 5:22 am |
|
|
I get the input as a string, I stored it in text array, nothing changed!!
gets(text);
glcd_text57(xt,yt,text[i],1,on);
I think glcd_text57 deal with char. because "Hello" message appeared on
the glcd. but the char. sent from computer did not appear on glcd.
Abdulla _________________ "A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engineer is twice bigger than necessary." |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Oct 22, 2010 5:38 am |
|
|
I just checked and glcd_text57 does require a string.
unsigned char text45[6]={"Hello"};
"Hello" is a null terminated string.
unsigned char i,text;
text is a single char.
gets(text); will wait for a STRING to be entered, you must press enter key. If you havn't change the definition of text your program will most likely fail.
You have 2 options.
option 1 if you want to enter a string by typing the full string out and then press return
Code: |
char text[80]; // Maimum size string can be, if it is bigger program will fail.
void main()
{
glcd_init(on); // Initialize the GLCD
delay_ms(100);
glcd_text57(0,0,text45,1,on); // type test message on GLCD
delay_ms(1000);
printf("Hello world"); // type test to hyperterminal
delay_ms(1000);
while(true)
{
gets(text);
glcd_text57(xt,yt,text,1,on);
}
}
|
Or by entering 1 char at a time:-
Code: |
unsigned char i, text[2];
unsigned char text45[6]={"Hello"};
void main()
{
glcd_init(on); // Initialize the GLCD
delay_ms(100);
glcd_text57(0,0,text45,1,on); // type test message on GLCD
delay_ms(1000);
printf("Hello world"); // type test to hyperterminal
delay_ms(1000);
text[1] = '/0'; // null terminate my single char making it a string
while(1)
{
printf("\n\r Start typing your letter???");
xt=1;
yt=20;
for(i=0;i<=20;i++)
{
text[0]=getc(); // Get a single char and put it as the first char in my string
glcd_text57(xt,yt,text,1,on);
xt+=10;
if(xt>120)
{
xt=0;
yt+=10;
}
}
}
}
|
|
|
|
Abdulla M.A.
Joined: 28 Mar 2010 Posts: 30 Location: Baghdad, Iraq
|
|
Posted: Fri Oct 22, 2010 7:43 am |
|
|
Correct this.
text[1] = '\0'; // null terminate my single char making it a string
now, the program work fine.
if I just want to get the xt and yt from computer, I assumed I should
add the following
unsigned int32 xt, yt; //
gets(xt);
gets(yt);
I added it but, the program worked fine if xt, or yt contain just one
number like (4,6), if xt=16, and yt=30 the program did not work
correctly, why?? int32 is 32 bit, so I think I can fill it with four numbers!
I'm really grateful for your help Wayne_ , thank you.
Abdulla _________________ "A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engineer is twice bigger than necessary." |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Oct 22, 2010 8:20 am |
|
|
Several problems.
First:- gets expects a string of characters and expects to store the data into an array of 8 bit values (chars). Your code should fail as you are trying to read a string in to an int32.
Second problem is how you are sending the data from the PC. If you are using a program like hyperterm then the PC will be sending ASCII characters. The value 1234 will be characters '1', '2', '3' and '4' not the decimal values. if you are sending bytes, then you could send the value 1 followed by the value 2 etc but you still have a problem as shown next.
Third problem is that a 32 bit int can hold a value up to 4294967295. The computer can at most send bytes, even a 16 bit value would be split in to 2 bytes. You need to receive these in to a buffer and then convert it to an int32 when all numbers have been entered.
If you have a program to send the data then you may be sending 4 bytes for a 32bit value, again you would need to receive these one at a time and convert, transpose in to your 32bit var.
Try searching the forum on how you can do this. |
|
|
|
|
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
|