Variable types in c and avr

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi dears.
I have a basic question;
1-what are variable types in c?? Int char float? Is bit a type of variable?or not?
2- if bit is a type of variable, which values can take?
3-What is diffrence between bolean and bit? Are both variable type in c? Or use in avr only?
Thabk you!

Aasshh

Last Edited: Mon. Sep 2, 2019 - 02:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) You need to get a good book about C. The one most people choose is "Understanding C" (2nd, ANSI edition) by Brian Kerninghan and Dennis Ritchie. This explains the base types in C, (char, short, int long, long long, float, double) and the signed/unsigned variations that are available. As standard C does not have a "bit" type but there is <stdbool.h> if you want to hold true/false states. However in a micro the mapping of "bool" may not be that efficient so one way to "create" bit variables is to use single bit "bitfields" in a struct. As well as the base types modern C has <stdint.h>, these types are "better" than the base types as they have a guaranteed bit width no matter when architecture the C code is built for. In general you should never use any of "char, short, int long, long long, float, double" directly with the notable exception that you can use char (but not unsigned char nor signed char) for the storage of characters.

 

2) there isn't a bit type but a convention says:

#define false 0
#define true !false

which usually means that 0 represents false and 1 represents true.

Aashkanpasha wrote:
What is duffrence between bolean and bit?
There is no such thing as "bit" so the question is irrelevant.

Aashkanpasha wrote:
Are both variable type in c?
No, like I say <stdbool.h> adds "bool" (and true/false) but there's no such thing as bit.
Aashkanpasha wrote:
Or use un avr inky?
No idea what that question means.

 

BTW as I say you can "manufacture" a "bit variable" using:

typedef struct {
    int b0:1;
    int b1:1;
    int b2:1;
    int b3:1;
    int b4:1;
    int b5:1;
    int b6:1;
    int b7:1;
} bit_t;

bit_t foo;

foo.b3 = 1;

That would set just bit 3 of the storage to 1. But this is not "standard" - I just created bit_t using other, standard features of C.

 

(PS, BTW a C compiler does not have to assign bits from bit 0 upwards so there may a poartability issue using such bitfields (on the whole they are assigned from 0 up though))

Last Edited: Mon. Sep 2, 2019 - 08:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your complete answer.
See i get right?

Bit direct;
.
.
.
PORTA=(Direct) ? (0x01<<i) :(0x80>>i)

Shoulde change to:
#definf false 0
#define true !false
.
.
.
PortA=(true) ? ...

Aasshh

Last Edited: Mon. Sep 2, 2019 - 08:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

AH HA! I just read your other posts and specicfically:

 

https://www.avrfreaks.net/forum/pin-and-port-rejister-define-inatmel

 

so now I understand what you are asking. I was even going to mention Codevision in my answer above. It is a C compiler for AVR with some special features added to C to help program micros. In Codevision for example you can use:

PORTB.3 = 1;

to set just bit 3 of PORTB but in "standard C" the thing that comes after a '.' in this type of syntax must be a valid C symbol name and the rules for those say that it can't start with a digit. So while this is a brilliant feature for programming micros it is (technically) a violation of the rules. As I say, the closest you can get in C to something like this is shown by my example above.

PORTB.b3 = 1;

could be achieved in avr-gcc and other (in fact any) AVR C compiler as it adheres to the rules of C. But it is a bit more cumbersome as I had to include a 'b' at the start of each bit. Here is an actual example in AS7:

 

 

This code:

#include <avr/io.h>

typedef struct {
    int b0:1;
    int b1:1;
    int b2:1;
    int b3:1;
    int b4:1;
    int b5:1;
    int b6:1;
    int b7:1;
} bit_t;

#define PortB (*(volatile bit_t *)&PORTB)

int main() {
    while(1) {
        PortB.b3 = 1;
    }
}

almost replicates the "bit" type you find in Codevision. The code it generates is:

00000080 <main>:

#define PortB (*(volatile bit_t *)&PORTB)

int main() {
	while(1) {
		PortB.b3 = 1;
  80:	2b 9a       	sbi	0x05, 3	; 5
  82:	fe cf       	rjmp	.-4      	; 0x80 <main>

so it has generated the very efficient SBI opcode to perform the action of setting bit 3 in PortB.

 

A few things to note:

 

* PORTB already exists so I had to call my new (bit accessible) version something else - I chose to make it "PortB" instead

* this works by saying "take the existing location for the entire PORTB byte, apply a bit_t type to what  it points to - then dereference that pointer

* the definition MUST include "volatile" or the accesses to "PortB" will be seen as pointless and will not generate code

 

PS I gotta ask - Codevision is a most excellent compiler for AVR - if you liked features such as bit variables and bit access to PORTs then why stop using Codevision? If you are familiar with it surely the best answer is to continue using it?

Last Edited: Mon. Sep 2, 2019 - 08:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My question is not related to my previous question.
I write codes. That i want to say this:
If direction is 1 do function1 if is 0 do function 2.
I have a external interupt that is a botton is a prees it direction should chande from 1to 0. And if press again chane 0to1. I wanna know how write direction data type and define it.

Aasshh

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you need "bit" for that? You can hold a 0/1 state in pretty much any variable type in C. Personally I think I might just use a 0/1 in a uint8_t. A possibly better idea is actually an enum{} so you can give the states sensible names.

Last Edited: Mon. Sep 2, 2019 - 09:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aashkanpasha wrote:
3-What is diffrence between bolean and bit? Are both variable type in c? Or use in avr only?

 

Maybe you are confused because C has boolean and bitwise operators?

In C, if a variable has the value zero, it's considered false, and if it has a value different from zero it's considered true. Logic operators act on logic values. Regarding bitwise operators, they act upon each bit of a variable.

 

So:

uint8_t val = 0x00;
uint8_t bool_not = !val; // will contain some positive number to indicate a TRUE logic value (the ! operator is logic NOT)
uint8_t bin_not = ~val;  // will contain 0xFF (each bit is inverted - the ~ operator is bitwise NOT)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


I think the point maybe (despite protestations above) that OP has been spoiled by the availability of:

 

 

also:

 

 

So there are some C compilers with a "bit" type that is perhaps the obvious choice when you truly have just a "bit" to be stored. As this table even shows - if you use "bool" (the 'standard' for such things?) you use 8 bits to hold a 1 bit state.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aashkanpasha wrote:
I have a basic question;
1-what are variable types in c??

 

Well, if your question is about C language, then the answer is: there's no such thing as a "variable" in C.

 

"Variable" is a colloquial informal term that we use in everyday conversations. It's meaning is usually clear from the context and it is generally synonymous with a more formal term object. But the language terminology itself does not use term "variable".

 

Dessine-moi un mouton