|
|
View previous topic :: View next topic |
Author |
Message |
th3gr8
Joined: 16 Jul 2013 Posts: 12 Location: Pakistan
|
Serial Interfacing problem |
Posted: Sun Oct 13, 2013 10:00 pm |
|
|
Hello All ..
I wrote a simple program which takes value of "r" from user via serial port and calculates Area (pi*r*r). Program is fine but it has an issue that whenever I take input from user, i.e. value of "r", a constant "48" automatically gets added with the value of "r". Then I have to manually subtract that value of constant i.e "48".
I want to ask why does it happen? Did I miss something in the program??
This is code:
Code: |
// To Calculate Radius of Circle using RS232 Protocol
#include <16F877A.h>
#fuses HS, NOLVP, NOWDT
#use delay(clock = 12Mhz)
#use rs232(baud=9600, xmit = pin_A0, rcv = pin_A1)
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B4
#define LCD_DATA5 PIN_B5
#define LCD_DATA6 PIN_B6
#define LCD_DATA7 PIN_B7
#include <lcd.c>
void main()
{
unsigned int r=0,c;
float a=0;
lcd_init();
delay_ms(30);
loop:
printf("\n\rInsert value of r (from 0-9): ");
r = getc();
putc(r);
delay_ms(1000);
r = r-48;
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"Radius = %d", r);
a = (3.14*r*r);
printf("\n\rArea of circle = %f", a);
lcd_gotoxy(1,2);
printf(lcd_putc,"Area = %f", a);
printf("\n\rRepeat? (Y/N): ");
c = getc();
putc(c);
if(c=='y')
{
goto loop;
}
else
{
break;
}
} |
_________________ A learner |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Sun Oct 13, 2013 11:00 pm |
|
|
All characters you see, 0-9, a-z, A-Z, special characters and even some others you probably aren't familiar with are all grouped together in one table called an ASCII table. Each of those gets a special number to represent it. The printed character 0 is actually 48 in the table, which is why you have to subtract 48 from any number 0-9.
http://www.asciitable.com/
http://en.wikipedia.org/wiki/ASCII |
|
|
th3gr8
Joined: 16 Jul 2013 Posts: 12 Location: Pakistan
|
Woah !! |
Posted: Mon Oct 14, 2013 1:10 am |
|
|
Thanx jeremiah ..
I forgot that ASCII table :D ..
Is there any way so that i get my desired value without subtracting "48" ??? _________________ A learner |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Oct 14, 2013 1:25 am |
|
|
No.....
Why worry. Subtracting 48, from an integer, is a single machine cycle. There are compiler functions that will do this for multi character strings (atoi, atol etc..), but all they are doing is subtracting 48 from each digit.
Make it more plain what you are doing though:
Code: |
r = r-'0'; //convert from ASCII to numeric.
|
Your float multiplication by PI, takes about 1000 cycles, so the overhead of one cycle is rather insignificant....
Best Wishes |
|
|
|
|
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
|