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 support@ccsinfo.com

getch question

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



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

getch question
PostPosted: Tue Mar 12, 2013 5:05 pm     Reply with quote

I have a dc motor and I want to add a pot as a feedback. The problem is the pot has set values from 0-255. How can I type them, it seems when I press 1, 49 shows up and when I press 2, 50 appears and so on.

Code:
#include <16F88.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP, INTRC_IO
//#fuses HS, NOWDT, NOPROTECT, BROWNOUT, NOLVP
#use delay (clock=4000000)
#use rs232(baud=9600,rcv=PIN_B2, xmit=PIN_B5)

void main()
{
   int16 data;
   char value;
   setup_adc_ports(sAN0);
   setup_adc(ADC_CLOCK_DIV_8);
   printf("Please Set Value\r\n");

   while(true)
   {   
 
   set_adc_channel(0);
   delay_us(10);
   data=read_adc();
   if(kbhit())
   {
       value=getch();
   printf("it would stop at %u\r\n", value);
   }
   if(data>=value){
     printf("the data is %lu\r\n", data);
     delay_ms(200);
   }
     }     
   }
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Mar 12, 2013 5:21 pm     Reply with quote

The ASCII value for '0' is 0x30 or 48 in decimal.
The ASCII value for '1' is 0x31 or 49 in decimal.
The ASCII value for '2' is 0x32 or 50 in decimal etc.

Does this answer your question?

Mike

EDIT

I think you may need to collect several characters followed by <ENTER>, then do a conversion.
OR convert on the fly, till you get <ENTER>.
IceMetal



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Mar 12, 2013 5:24 pm     Reply with quote

Mike Walne wrote:
The ASCII value for '0' is 0x30 or 48 in decimal.
The ASCII value for '1' is 0x31 or 49 in decimal.
The ASCII value for '2' is 0x32 or 50 in decimal etc.

Does this answer your question?

Mike

EDIT

I think you may need to collect several characters followed by <ENTER>, then do a conversion.
OR convert on the fly, till you get <ENTER>.


thanks but that was not the question the question is how can I type 1 and 1 would show up.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Mar 12, 2013 5:41 pm     Reply with quote

I answered your question.

Subtract 48 from the ASCII value and you convert from ASCII to decimal!!

Mike
IceMetal



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Mar 12, 2013 5:46 pm     Reply with quote

still I can not type 100 or 50, that does not work :( is there a way to type those numbers
if you are already angry you can ignore this post you dont have to answer
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Mar 12, 2013 6:00 pm     Reply with quote

No. I'm not angry, yet.

I also gave you 2 ways of how to deal with multiple digits.

This is NOT the ONLY way:-

1) Declare a variable say New_input which you set to zero.
2) When 1st character arrives convert to decimal.
3) Make New_input equal to decimal equivalent of 1st character.

4) Wait for next_character.
5) If next character is <ENTER> you exit.
6) Multiply New_input by 10.
7) Add decimal value of next_character to New_input.
8) Loop back to 4.

Mike

Edit you may have to filter out non-decimal characters etc.
CCS examples also show you ways to do same task.
There are built in 'C' functions to convert ASCII strings.

It's gone mid-night here, so I'm done for now.
temtronic



Joined: 01 Jul 2010
Posts: 9165
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 12, 2013 7:17 pm     Reply with quote

getch() only 'gets' one character at a time....

so like Mike says you have to loop to 'get' 3 characters to get 100.
but...
need to trap out if <100.
You can set a trap for the <cr>....

Yup..kinda complicated but....
always a but...
if you look over the examples in the EXAMPLES folder, you'll find an example that CCS supplies !!!

as well
learn to use the search engine here(or google using site:) and you'll find a few code snippets that others have created,located either in this forum or the code library.

All this is based that you're sending data to the PIC from a PC terminal program and that you're pressing the number keys.....
IceMetal



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Mar 12, 2013 11:14 pm     Reply with quote

I will try to figure out since no one is helping, people seems they love to rant here.
Ttelmah



Joined: 11 Mar 2010
Posts: 19350

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 1:33 am     Reply with quote

We are suffering from a lot of posts where people want "answer's on a plate".
This is not a "teach you really basic programming" forum, it is meant to be to answer specific CCS problems.

Your question has nothing specifically CCS about it. It really is "nursery school" programming....

Two answers have already been posted. Last line of Mike's original post, then in detail in his later post.

He _has_ posted how to do it. Read, understand, try.

As a comment, you could actually learn this part much faster with a simple C on a PC. Type numbers, watch what their values are, understand how to handle multiple digits etc.. With an environment like this you in most cases would have the ability to 'watch' numbers as they come in, and see how the value needs to be converted, and built up to form the result. A much quicker learning tool than using the PIC to start. Once you are confident in some basic programming skills, then try the PIC.
Big problem, is that programming at a certain 'level' is a bit like trying to ride a bicycle you have to actually do it.

Best Wishes
twidget



Joined: 21 Feb 2013
Posts: 32
Location: Orange County, California

View user's profile Send private message Visit poster's website

PostPosted: Wed Mar 13, 2013 1:46 am     Reply with quote

And you thought I was rude.

What they are telling you is that you can’t just return the “number” you enter via RS232. RS232 returns what’s called an ASCII value. ASCII (American Standard Code) was developed from telegraphic codes. When sending messages, data (numbers, letters, etc) is transmitted in bit format (1’s and 0’s).
You would think that a ‘1’ entered via RS232 would return the number ‘1’ but in fact it returns the number “49”.
Why? Well, ASCII handles everything from numbers, letters, and “non-printing” control. It does this by way of a 7 bit look-up table (128 possible values). Each address from 0000000 – 1111111 is either a number, letter, or “non-printing” control. For example:
The number 6 is located at the 110110 address of the look-up table, values for the number 6 can be in any of the below form.
Code:

Binary      Decimal      Hexadecimal      Octal
0110110      54              36               66


An easy way to ‘decode’ ASCII, when you’re only dealing with single digit numbers, is to subtract 48. That is because the number 48 in ASCII is 0.
If your trying to decode more than one digit, you have to decode one digit at a time.
I honestly don’t think anyone is going to help you after your last post, but fortunately you have a somewhat easy problem to solve. I spotted three examples fairly quickly just searching ASCII Decoding.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 1:47 am     Reply with quote

If you're willing to type the value into the terminal program using hex
notation (00 to FF, instead of to 255), then here's a quick demo program:
Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, UART1, ERRORS)

#include <input.c>

//================================

void main()
{
int8 value;

while(1)
  {
   value = gethex();
   printf("\n\r");
   printf("value = %x \n\r", value);
  }   
   
}   


Here is a typical output of this program. If I type "12" into the terminal
window, the gethex() function echoes the numbers back to the terminal
so they are displayed on the first line. Then my program displays the
value that was received from gethex() with printf in the 2nd line.
Then I also typed in "77" and "FF" and got those back correctly as well.
Code:
12
value = 12
77
value = 77
ff
value = ff
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 4:37 am     Reply with quote

IceMetal wrote:
I will try to figure out since no one is helping, people seems they love to rant here.

A forum search shows you've been playing with PICs over four years, and asked questions on different topics.
You should, by now, know how it works here.

I was expecting the level of answers I gave to be more than adequate.
As another poster says "its kindergarten stuff".
My intention IS that you figure it out for yourself, with guidance.
I'm trying to help you learn, like I did a long time ago, the hard way.

I don't get angry, I just give up.

Bye.

Mike
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