Pages: [1]   Go Down
Author Topic: 8bit variable and struct to represent a Register & its bits  (Read 457 times)
0 Members and 1 Guest are viewing this topic.
West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically what i want is something along these lines
pseudo code

uint8_T  =  8_bits_in_register    <-- this would be the variable that would hold the 8 bits for the register

struct  ADMUX               <-- i know  i cannot call it ADMUX but for the sake of making this easier to explain lets say its called admux
{
     some_variable_type       left_Adjust ;
   
    some_variable_type       ref_Voltage_Avcc ;

    some_variable_type       ref_Voltage_1v ;

};


then i  would use it :   
ADMUX.left_Adjust = true ;
ADMUX.ref_Voltage_1v = false ;
ADMUX.ref_Voltage_AVCC = true ;

and as i set these up they get appended to the 8 bit variable (which would be a binary number)
in the proper order so that when that variable is passed to the register all my setting match

so it would look like

8_bit_variable  = 0b00101101  and i would pass that on to the real admux ,


is this feasible?  or is there a more efficient way to write a generic way of setting up something such as ADC or PWM for that matter
basically use a structure to set all the bits in  a port
Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure if it is supported in the avr_gcc compiler but:

http://www.tutorialspoint.com/cprogramming/c_bit_fields.htm
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i just tried it and seems to work
the names bitZero - bitSeven would be changed to match the register bits such as MUX1 , MUX2, obviously i would have to change it since it cant literally match
and the structure name would be changed to something like ADCsetup ..
 
now it would be ideal if i could initialize the fields to zero so i do not have to set each field to zero , but since this will be a generic reusable file for my self
ill have a function to initialize them all to zero
that way i only set the fields i am interested in


Code:
struct testReg
{
  int bitZero ;
  int bitOne ;
  int bitTwo ;
  int bitThree;
  int bitFour;
  int bitFive;
  int bitSix;
  int bitSeven;
 
 
 
};

struct  testReg  regEdit ; //structure that will represent a register and its settings

uint8_t myVar = 0;
void setup()
{
   Serial.begin(9600);
  DDRB |= 1<<PINB5 ;
   
 
}

int doTheMath()
// check each field of the structure and weather its true or false add that bit value to the uint8_t
{
    regEdit.bitZero < 1 ? myVar = 0 : myVar = 1 ;
   regEdit.bitOne < 1 ? myVar = myVar : myVar +=2 ;
   regEdit.bitTwo < 1 ? myVar = myVar : myVar +=4 ;
   regEdit.bitThree < 1 ? myVar = myVar : myVar +=8 ;
   regEdit.bitFour < 1 ? myVar = myVar : myVar +=16 ;
   regEdit.bitFive < 1 ? myVar = myVar : myVar +=32 ;
   regEdit.bitSix < 1 ? myVar = myVar : myVar +=64 ;
   regEdit.bitSeven < 1 ? myVar = myVar : myVar +=128 ;
  return myVar;
}

void loop ()
{
 
   regEdit.bitZero = false ;
   regEdit.bitOne = false ;
   regEdit.bitTwo =false  ;
   regEdit.bitThree = false;
   regEdit.bitFour = false;
   //ive set only the 5th bit to true  , which i will pass to port B where bit 5 is the led on digital pin 13
   regEdit.bitFive = true ;
   regEdit.bitSix = false ;
   regEdit.bitSeven = false;
   
   PORTB ^= doTheMath();
   Serial.println(doTheMath());
   delay(50);
   
}
Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

interesting i totally forgot about this
a quick sizeof showed me  i went from 16 bytes to one bite on the size of the structure , how nice


« Last Edit: August 09, 2013, 10:22:32 pm by eddiea6987 » Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1881
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at "union"s .

Mark
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 72
Posts: 1642
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
struct testReg
{
  int bitZero ;
  int bitOne ;
  int bitTwo ;
  int bitThree;
  int bitFour;
  int bitFive;
  int bitSix;
  int bitSeven;
};

Eeerm, no. That's not how a bit field is made. You do it like this:

Code:
typedef struct {
  byte bitZero:1 ;
  byte bitOne:1 ;
  byte bitTwo:1 ;
  byte bitThree:1;
  byte bitFour:1;
  byte bitFive:1;
  byte bitSix:1;
  byte bitSeven:1;
} BitField;

Notice how there is a ':1' at the end which specifies that each takes up only a single bit.

You can then do:
Code:
 volatile BitField* admux = (volatile BitField*)&ADMUX; //declare a BitField variable which is located at the address of ADMUX.
  admux->bitZero = 1; //equivalent to ADMUX |= _BV(0);

You could also define this type:
Code:
typedef union
  byte allBits;
  struct {
    byte bitZero:1 ;
    byte bitOne:1 ;
    byte bitTwo:1 ;
    byte bitThree:1;
    byte bitFour:1;
    byte bitFive:1;
    byte bitSix:1;
    byte bitSeven:1;
  };
} BitField;
...
//Allowing you to do this:
...
  volatile BitField* admux = (volatile BitField*)&ADMUX; //declare a BitField variable which is located at the address of ADMUX.
  admux->allBits = 0; //set all bits to 0.
  admux->bitZero = 1; //equivalent to ADMUX |= _BV(0);
« Last Edit: August 10, 2013, 09:06:44 am by Tom Carpenter » Logged

~Tom~

Pages: [1]   Go Up
Jump to: