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 CCS Technical Support

Really Really Need Help. C confused me!!

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







Really Really Need Help. C confused me!!
PostPosted: Sat Jan 31, 2009 5:09 am     Reply with quote

Hello everyone,

I got a question.

I am trying to transmit a number which value is 10 and a character which is 'b'. My first question is if I want to use a code like following.
Code:

a=getc();

switch (a);

how can I declare 'a' ===> unsigned char a;
or int a;

Second question:
I want transmit this value which command I should use?
putc(10) or putc('10') ==> which one is correct? If I want to the following thing.
Code:
a=getc();

switch(a);
      case '10'
               ...........;
               break;

      case '11'
               ...........;
               break;

putc(10) or putc('10') ===> which one can go into the "case"?

I am new on CCS so any suggestion or help will be really appreciated.

Thank you very much.
Ttelmah
Guest







PostPosted: Sat Jan 31, 2009 6:13 am     Reply with quote

Start by understanding that a single character, is really at all times a binary value. A 'char' data type, simply holds this value. A char, is actually a single byte integer at all times. So if (for instance), you take:
Code:

char val;
val='a'; //This puts the binary value 0110 0001 into 'val' - 97 in decimal
val=val-32; //subtracts 32 from this number

putc(val); //will output the single character ASCII 65 - 'A'.

The point is that you are dealing with numbers all the time. It is only when you send it, and hve a program that displays a particular number as a 'text' value, that it is displayed as a character.
Note also, that C contains a conversion for you, if you put a text character inside single inverted commas, it automatically uses the ASCII value of this character. You can use this at any time. So:
Code:

int val;
val=32; //number '32' in the byte
val=val+'a'; //add 97 to this.....

putc(val); //outputs the single character 'A' again.

Note the use of the int, rather than the char.
int, is by default 'unsigned'.

So for the first part of your question, however you want. char, and int, are the same in CCS. In most C's, an 'int' by default is a larger type (commonly int16, or int32), but in fact the laguage will generally convert automatically, even in this case.

Now, on your second part, is the number going to be the value '10', sent as a _single_ binary character, or is it going to be the test string, of two characters "10"?. If the former, then getc will work as you show, _but_ your tests will need to be as "case 10:", just the number ten.
In the latter, you are going to have to convert the two characters into a number first. For this, you need to have a multi character storage space (something like "char text[3]"), then read the two characters into 'text[0]', and 'text[1]'. Then add a terminator character "text[2]='\0'". Now, in most languages, a 'string', is a sequence of bytes terminated with a binary 0 - this code puts the terminator in place. Then use 'atoi', to convert this string, into the number.
You can then compare this number.

On the output, putc(10); sends the binary character 00001010. putc('10'), is not legitimate. However putc("10"); will send the two characters binary 00110001 and 00110000 which correspond to the ASCII characters '1', and '0'.

Now, most of this, is standard C. The exception, is in CCS allowing you to use 'putc("10"); in most C's, to send more than one character, you have to send them separately (putc('1'); putc('0');) or use printf, which supports sending strings.

You really need to read something on basic C.

Best Wishes
GregEigsti



Joined: 12 Jan 2008
Posts: 12

View user's profile Send private message

Re: Really Really Need Help. C confused me!!
PostPosted: Sat Jan 31, 2009 6:40 am     Reply with quote

Brian wrote:

Code:
a=getc();
switch (a);   

how can I declare 'a' ===> unsigned char a;
or int a;

You are not switching on the literal character a; rather the variable a's value. In the case of switching on a variable, a for instance, it could be many different values. If a is being set to the return of getc() it is a char (unsigned int8 in CCS parlance). Take a look at the CCS help for getc() and for "data types" to learn more.
Brian wrote:

I want transmit this value which command I should use?
putc(10) or putc('10') ==> which one is correct? If I want to the following thing.

Code:
a=getc();
switch(a);
      case '10'
               ...........;
               break;
      case '11'
               ...........;
               break;

putc(10) or putc('10') ===> which one can go into the "case"?

Again, if you look at CCS's help for putc() you will see that it takes a char. 10 is a valid value for a char but '10' is not. Take a look at an ASCII chart to see what characters map to what values. Also, if you are wanting to switch on the value of 10 your case statement should not include the single quotes - and you will need to place a : (colon) at the end of the case statement - and you will need to remove the ; (semi colon) after the switch statement (as well as add some curly braces). e.g.
Code:
switch(a) {
      case 10:
               ...........;
               break;
}

Its late and I am tired and not really understanding what it is you are trying to achieve (other than receiving a character and send it back out). Are you wanting to send back out the same character that you received? A better way to do this would be to get rid of the switch alltogether (unless you are going to do some other processing there).

My best advice for you is to step away from the PIC and play with a little C coding on your computer. There are many free C compilers/linkers out there to play with. This will allow you to experiment and learn while removing the overhead of having to load your code onto the PIC. Once you get your code building and debugged (working) on your computer then you can build it in CCS and put it on the PIC. Debugging on the computer is going to be much much much easier than on the PIC.

Greg
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