View previous topic :: View next topic |
Author |
Message |
cile
Joined: 10 May 2009 Posts: 17
|
convert int to char |
Posted: Sun May 24, 2009 2:25 pm |
|
|
Hello....
Problem: I use keypad 4x4, which is connected to 18F4550.
I will through keypad give the speed to PIC. For example I want to give
120km/h. Problem is that kbd_getc() returns typ 'char'. I just want to take the this '120' and convert to int 120, so I can than this number use for PWM control !
there is code:
Code: |
int temp=0;
char=k;
while(1){
k=kbd_getc();
if(k=='1'){
lcd_putc('\f');
lcd_putc("Put speed km/h:\n");
while (1){
k=kbd_getc(); // returns typ 'char'
if(k!=0)
lcd_putc(k);
temp=10*temp+(int)k;
if(temp>180) { lcd_putc("wrong speed!\n"); // can not be //speed higher than 180km/h.
lcd_putc('\f');
lcd_putc("put new speed:\n");
temp=0; }
if(k=='D') break;
}
} |
I compile, no errors, but doesn't work...
I have tried with atoi function... but doesn't work... maybe I make mistakes..
so, any advices or explain ??
please help! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 24, 2009 2:38 pm |
|
|
Quote: | k=kbd_getc(); // returns typ 'char'
if(k!=0)
lcd_putc(k);
temp=10*temp+(int)k; |
The character is in ASCII format. You have to remove the ASCII bias.
Using '(int)' will not do this.
0x31 is '1' in ASCII. You just want 0x01 as the result. You have to
remove the 0x30 from it. Same thing for digits 0 to 9. They are 0x30
to 0x39.
Here is a simple macro that will do the conversion (for ascii values 0-9).
Add this line above main():
Code: | #define toint(c) ((int)((c)-'0')) |
|
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Sun May 24, 2009 4:58 pm |
|
|
OK, I did not change my code, I have only put this macro above main
Code: | #define toint(c) ((int)((c)-'0')) |
but still doesn't work.
Maybe I have wrong understand your explain... or...
I have also make another C code for this and also doesn't work.
There is code:
Code: |
void main() {
char k;
int temp=0;
int temp=0;
lcd_init();
kbd_init();
while (1) {
k=kbd_getc();
if(k!=0)
if(k=='*')
lcd_putc('\f');
switch(k) {
case 48: lcd_putc("Number 0"); temp=0; break; // case 48: ( ascii code for char '0' (zero)
case 49: lcd_putc("Number 1"); temp=1; break;//for char '1'
case 50: lcd_putc("Nr 2"); temp=2; break;
case 51: lcd_putc("Nr 3"); temp=3; break;
case 52: lcd_putc("Nr 4"); temp=4; break;
case 53: lcd_putc("Nr 5"); temp=5; break;
case 54: lcd_putc("Nr 6"); temp=6; break;
case 55: lcd_putc("Nr 7"); temp=7; break;
case 56: lcd_putc("Nr 8"); temp=8; break;
case 57: lcd_putc("Nr 9"); temp=9; break;
}
temp1=(10*temp1)+ temp;
if(temp1>180){
lcd_putc('\f');
lcd_putc("WRONG SPEED!\n");
lcd_putc("PUT AGAIN:\n");
temp1=temp=0;
}
}
} |
I really have no idea why not work this code.. I have put in if loop
if(temp1==9) // or temp1==8=7=6=5=4=3=2=1;
than put this message on the LCD.
But when I put again 180 km/h in if lloop... I just press on keyboard
'1' or '2' or '3' ... and LCD goes crazy just putting message ...
?!?!?!?!?!?HELP!?!?!??!?!
THANK'S |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 24, 2009 5:07 pm |
|
|
You have got to actually call it.
Give it an ascii character as the parameter and it will return an integer.
For example, if you give it '1' (which is 0x31), it will return 0x01.
Edit your code, and any place where you have an ASCII char and you
want an integer, then use that function to convert it. |
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Sun May 24, 2009 5:17 pm |
|
|
I did on this way
Code: | char k; int temp=0;
while (1){
k=kbd_getc(); // returns typ 'char'
if(k!=0)
lcd_putc(k);
temp=10*temp+toint(k);
if(temp>180) { .....}
........ |
But still doesn't work... I have put also ('k') and again no results. |
|
|
apcaye
Joined: 22 May 2009 Posts: 29 Location: Brazil
|
|
Posted: Mon May 25, 2009 7:45 am |
|
|
You have to ensure that 'temp' is not going to be greater than 255. Besides, have you made simple tests to guarantee that the keyboard and LCD functions are working correctly? It seems that you get only one digit from the keyboard, what about the other two, as in '120'?
Regards,
Adriano. |
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Mon May 25, 2009 7:57 am |
|
|
I have also make int16 temp and temp1; but still same thing...
LCD and keyboard work OK.. For example
while(1){
k=kbf_getc();
if(k!=0);
lcd_putc(k);
}
This code works great, I press on keyboard button ex '1' and this put on LCD...
I'm testing my code in PROTEUS |
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Mon May 25, 2009 8:23 am |
|
|
Is there any ideas?? have someone explain or advice what is actually wrong?? thank you !!! |
|
|
apcaye
Joined: 22 May 2009 Posts: 29 Location: Brazil
|
|
Posted: Mon May 25, 2009 8:31 am |
|
|
I don't know what Proteus is, but can you simulate your code, step by step? As the keyboard and LCD functions are Ok, skip them and attribute an ASCII value to the 'k' variable, for example, 0x31. Check then what you get in the 'temp' variable, that could give you a direction.
Adriano. |
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Mon May 25, 2009 8:53 am |
|
|
There is code again... Can someone look it and find (if there is any mistake)..or
Code: |
void main() {
char k;
int temp=0;
int temp=0;
lcd_init();
kbd_init();
while (1) {
k=kbd_getc();
if(k!=0)
if(k=='*')
lcd_putc('\f');
switch(k) {
case 48: lcd_putc("Number 0"); temp=0; break; // case 48: ( ascii code for char '0' (zero)
case 49: lcd_putc("Number 1"); temp=1; break;//for char '1'
case 50: lcd_putc("Nr 2"); temp=2; break;
case 51: lcd_putc("Nr 3"); temp=3; break;
case 52: lcd_putc("Nr 4"); temp=4; break;
case 53: lcd_putc("Nr 5"); temp=5; break;
case 54: lcd_putc("Nr 6"); temp=6; break;
case 55: lcd_putc("Nr 7"); temp=7; break;
case 56: lcd_putc("Nr 8"); temp=8; break;
case 57: lcd_putc("Nr 9"); temp=9; break;
}
temp1=(10*temp1)+ temp;
if(temp1>180){
lcd_putc('\f');
lcd_putc("WRONG SPEED!\n");
lcd_putc("PUT AGAIN:\n");
temp1=temp=0;
}
}
} |
I have put also at the end of program
k=0 and k='0' I have all try ... but doesn't work
Could be mistake in program PROTEUS ?? |
|
|
apcaye
Joined: 22 May 2009 Posts: 29 Location: Brazil
|
|
Posted: Mon May 25, 2009 10:21 am |
|
|
Try to put opening and closing brackets ('{' and '}') in your 'if' statements, it's good programming practice to avoid bugs:
Code: |
if(k!=0)
{
if(k=='*')
{
lcd_putc('\f');
}
(...)
}
|
Regards,
Adriano. |
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Mon May 25, 2009 10:28 am |
|
|
Code: | #define toint(c) ((int)((c)-'0'))
.
.
.
int temp=0;
char=k;
while(1){
k=kbd_getc();
if(k=='1'){
lcd_putc('\f');
lcd_putc("Put speed km/h:\n");
while (1){
k=kbd_getc(); // returns typ 'char'
if(k!=0) { // I put this '{' and now it is working
if(k>=48&&k<=57){
lcd_putc(k);
temp=10*temp+toint(k);
if(temp>180) { lcd_putc("wrong speed!\n"); // can not be //speed higher than 180km/h.
lcd_putc('\f');
lcd_putc("put new speed:\n");
temp=0; }
} // end of loop if(k!=0)
if(k=='D') break;
}
} |
Now it is working.... I have just put: if (k!=0){...//thanks ADRIANO} and macro(for convert char to int) which is preposition from PCM programmer (thanks)!
Thanks for help !!
Last edited by cile on Mon May 25, 2009 11:08 am; edited 1 time in total |
|
|
apcaye
Joined: 22 May 2009 Posts: 29 Location: Brazil
|
|
Posted: Mon May 25, 2009 10:47 am |
|
|
You're welcome Cile, glad to hear now it's working. Good luck in your projects!
Regards,
Adriano. |
|
|
cile
Joined: 10 May 2009 Posts: 17
|
|
Posted: Mon May 25, 2009 11:09 am |
|
|
THANK YOU on your wishes.... |
|
|
Simbo
Joined: 02 Mar 2008 Posts: 7 Location: UK
|
|
Posted: Thu Mar 25, 2010 3:36 pm |
|
|
How could I modify the macro to support up to 255 for pwm?
Thanks for any help _________________ <~Simbo~> |
|
|
|