go away spammer

Author Topic: Basic to C  (Read 3742 times)

0 Members and 1 Guest are viewing this topic.

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
    • DC/MD/VA Robotics and Automation Team
Basic to C
« on: June 03, 2008, 11:02:48 AM »
Hi,

I'm converting BASIC to C and would like to know how to convert this simple BASIC statement to C:

Dim TempBit As Bit

I'm using AVR Studio, AVRlib and WinAVR.

I tried this statement but it caused a compiler error in C:

static BIT TempBit;

-Melanie
« Last Edit: June 03, 2008, 11:13:18 AM by vidam »

Offline SixRingz

  • Full Member
  • ***
  • Posts: 76
  • Helpful? 0
  • Bit's and pc's = Robot.
    • ManMachineSystem
Re: Basic to C
« Reply #1 on: June 03, 2008, 02:37:57 PM »
I've never used (don't know if they exist even) bit variable types in C. What is the use of TempBit? My suggestion is:
char TempBit;

A "char" is 8 bits (one byte) long. You can then use all the bits separately.
Grounding things properly means burying them in the backyard...

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
    • DC/MD/VA Robotics and Automation Team
Re: Basic to C
« Reply #2 on: June 03, 2008, 02:41:38 PM »
I've never used (don't know if they exist even) bit variable types in C. What is the use of TempBit? My suggestion is:
char TempBit;

A "char" is 8 bits (one byte) long. You can then use all the bits separately.

Thanks for the tip. And good question. It is being used as a boolean in the code. So I guess I could just convert it to bool in C and be done with it.

BTW, if I do use a char for the other Bits in the basic code, how do you use the bits separately (i.e. what is the syntax)?


Offline SixRingz

  • Full Member
  • ***
  • Posts: 76
  • Helpful? 0
  • Bit's and pc's = Robot.
    • ManMachineSystem
Re: Basic to C
« Reply #3 on: June 03, 2008, 02:58:54 PM »
Well, if you want to use a certain bit you can use a mask. For example to set the second bit, set the char variable to 2:
char TempBit = 2;

This is how TempBit now looks in binary representation:
00000010

If we want to check if the second bit is set we can use a bit mask variable. Let's assume some of the other bits have been set in TempBit, by for example an input port (binary rep. TempBit):
01001010
(73 if you are looking at the decimal representation)

Ok, now we create:
char BitMask = 2;

in binary, BitMask:
00000010


Finally to check the second bit we use logic "and" between BitMask and TempBit:

  01001010 (TempBit)
&
  00000010 (BitMask)
----------------------------
  00000010

If the result is greater than zero, the bit is set. If the bit is not set, the result will be zero.
Grounding things properly means burying them in the backyard...

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
    • DC/MD/VA Robotics and Automation Team
Re: Basic to C
« Reply #4 on: June 03, 2008, 03:06:07 PM »
Well, if you want to use a certain bit you can use a mask. For example to set the second bit, set the char variable to 2:
char TempBit = 2;

This is how TempBit now looks in binary representation:
00000010

If we want to check if the second bit is set we can use a bit mask variable. Let's assume some of the other bits have been set in TempBit, by for example an input port (binary rep. TempBit):
01001010
(73 if you are looking at the decimal representation)

Ok, now we create:
char BitMask = 2;

in binary, BitMask:
00000010


Finally to check the second bit we use logic "and" between BitMask and TempBit:

  01001010 (TempBit)
&
  00000010 (BitMask)
----------------------------
  00000010

If the result is greater than zero, the bit is set. If the bit is not set, the result will be zero.

Forgive my ignorance, but I thought char in C could only be a character data type such as the alphabet A-Z. You set a char TempBit = '2'. Confused... How does the compiler interpret a char set to '2' as a number or as a character?


Offline benji

  • Supreme Robot
  • *****
  • Posts: 830
  • Helpful? 0
Re: Basic to C
« Reply #5 on: June 03, 2008, 03:25:34 PM »
actually you can define bit variables in C


try this

bit Tempbit;

it works with the compiler i use (codevisionAVR)
good ol' BeNNy

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
Re: Basic to C
« Reply #6 on: June 03, 2008, 03:48:33 PM »
Forgive my ignorance, but I thought char in C could only be a character data type such as the alphabet A-Z. You set a char TempBit = '2'. Confused... How does the compiler interpret a char set to '2' as a number or as a character?
The char datatype is an integer that is a single byte in size.  It is used to represent characters, which are just numbers (look up ASCII code).  It is probably the smallest datatype you can use on an AVR since registers and RAM locations are all 8-bits in size.

- Ben

paulstreats

  • Guest
Re: Basic to C
« Reply #7 on: June 03, 2008, 07:26:00 PM »
Some compilers allow booleans or single bit. But i think it ends up wasting the other 7 bits in the byte.
If you are definately going to use more than 1 boolean it might be worth making a struct of single bit datatypes eg.


struct{

       char boolean1:1; //set 1 bit to boolean1
       char boolean2:1;
       char boolean3:1;
       char boolean4:1;
       char 4bits:4; //set 4 bits to 4bits
}booleans

access using:

booleans.boolean1 = 1;

or

booleans.4bits = 0b0101;

Its just a way to utilise all of the available bits. If you just need 1 boolean, than maybe there is something else that you can get away with using the remaining 7 bits on. (the different allocations also overflow independantly too)

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
    • DC/MD/VA Robotics and Automation Team
Re: Basic to C
« Reply #8 on: June 04, 2008, 07:53:59 AM »
Some compilers allow booleans or single bit. But i think it ends up wasting the other 7 bits in the byte.
If you are definately going to use more than 1 boolean it might be worth making a struct of single bit datatypes eg.


struct{

       char boolean1:1; //set 1 bit to boolean1
       char boolean2:1;
       char boolean3:1;
       char boolean4:1;
       char 4bits:4; //set 4 bits to 4bits
}booleans

access using:

booleans.boolean1 = 1;

or

booleans.4bits = 0b0101;

Its just a way to utilise all of the available bits. If you just need 1 boolean, than maybe there is something else that you can get away with using the remaining 7 bits on. (the different allocations also overflow independantly too)

Thanks this is really neat. It eliminates the need to do bit-masking.


Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: Basic to C
« Reply #9 on: June 04, 2008, 09:21:31 AM »
hey Vidam,
this has been posted before but it's still useful.
if you want a nice rundown on bit-masking check this article: http://andrey.thedotcommune.com/bitmasking.html

dunk.

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
    • DC/MD/VA Robotics and Automation Team
Re: Basic to C
« Reply #10 on: June 04, 2008, 10:43:16 AM »
hey Vidam,
this has been posted before but it's still useful.
if you want a nice rundown on bit-masking check this article: http://andrey.thedotcommune.com/bitmasking.html

dunk.

The article on bit-masking is just what I needed. Thank you dunk. :-)

 


Get Your Ad Here