![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
omarsito12
Joined: 02 Oct 2009 Posts: 6
|
Problem capturing keys of showing in PRINTF |
Posted: Thu Feb 25, 2010 1:50 pm |
|
|
I need to capture 11 keypad digits and store them in a matrix of type char array of 11 positions (char n [11]) and then print this char [] by "printf" to talk with the GSM modem. Because I am making a communication PIC - ModemGSM but got many wrong and ugly symbols. I am seeing them by hyperterminal, which actually I use it only to see the conversation between the modem and the pic. my code is:
Code: |
#include<18f97j60.h>
#device adc=10
#fuses HS, NODEBUG, NOWDT, NOPROTECT, NOIESO, NOETHLED
#FUSES
#use delay(CLOCK=25000000)
#use rs232(baud=115200,parity=N,bits=8,stop=1,xmit=PIN_C6,rcv=PIN_C7,enable=PIN_C5,errors)
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<flexy_lcd.c>
#include<flexy_kbd.c>
//////////*************MENSAJE GSM******************////////////
void msj_gsm()
{
char k,num;
char n[11];
int i;
int state=1;
lcd_init();
kbd_init();
while(true){
switch(state){
case 1:
i=0;
lcd_putc("\fPress numbers:\n#: ");
while(i<=10){ //captures 11 number or digits
k=kbd_getc(); //waits for the keyboard been pressed
if(k!=0){ ///if press the keyboard
if(k=='#'){
lcd_putc("\fExit\n");
}else{ //when I press a number... get in here
n[i]=k; //save the key pressed on keyboard, saving it in "n", but in the position of 'i'...of course!!!
lcd_putc(k);//prints by the display
++i; ///move 'i' position
}
}
}
default 2:
printf("AT+CMGS=\"%s\"",n);//prints n
delay_ms(2000);
putc (0x0D);//enter
state=1;
break;
}
}
}
}
|
I dont get it nothing well in hyperterminal...
Quote: |
ERROR
04445015555555555
ERROR
5
ý}}5555555555
2 5"AT+CMGS="88888888888
2 8"
|
I even tried to change the baud rate but I still do not have anything ...besides the modem works fine, I tried sending text messages, calls and even from a little program in java that I created, I can control it all. I work more on java, which is the reason why this language seems to me a little difficult ... in java STRINGS handling is very common, but here... I'm lost ....
So I dont know how to capture all keys pressed and show them by printf for the modem GSM....
Please help ... I gonna defend thesis in a few days and this is what is missing ... Thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 25, 2010 2:31 pm |
|
|
Quote: |
#include<flexy_kbd.c>
k=kbd_getc(); //waits for the keyboard been pressed
|
Does your keypad driver come from here, in the Code Library ?
http://www.ccsinfo.com/forum/viewtopic.php?t=26333
If so, the comment is wrong. It does not wait for a key.
To fix it, add this routine above main():
Code: |
// Wait for a key to be pressed. Then return it.
char keypad_getc(void)
{
char k;
while(1)
{
k = kbd_getc();
if(k != 0)
break;
delay_ms(10);
}
return(k);
}
|
Then edit your program. Change the kbd_getc() line to call
keypad_getc() instead, as shown in bold below:
Quote: |
while(i<=10){ //captures 11 number or digits
k=keypad_getc(); //waits for the keyboard been pressed
if(k!=0){ ///if press the keyboard
if(k=='#'){
lcd_putc("\fExit\n");
}else{ //when I press a number... get in here
n[i]=k; //save the key pressed on keyboard, saving it in "n", but in the position of 'i'...of course!!!
lcd_putc(k);//prints by the display
++i; ///move 'i' position
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Feb 26, 2010 2:56 am |
|
|
Another problem is that a string in c needs to be a null terminated aray of chars. You have the array and you fill it but you don't null terminate it. This is very easy to do.
First make sure you allocate 1 extra char in your array for the null '\0' or 0 char.
Then just set this when you have recieved your data:-
Code: |
char n[12];
case 1:
i=0;
lcd_putc("\fPress numbers:\n#: ");
while(i<=10){ //captures 11 number or digits
k=kbd_getc(); //waits for the keyboard been pressed
if(k!=0){ ///if press the keyboard
if(k=='#'){
lcd_putc("\fExit\n");
}else{ //when I press a number... get in here
n[i]=k; //save the key pressed on keyboard, saving it in "n", but in the position of 'i'...of course!!!
lcd_putc(k);//prints by the display
++i; ///move 'i' position
}
}
}
n[i] = 0; // Null terminate the string.
default 2:
|
Your output is also a little confusing.
What is outputting the "ERROR" ?
How can you enter more than 11 chars "04445015555555555" ? |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|