View previous topic :: View next topic |
Author |
Message |
Marc Guest
|
bit manipulation |
Posted: Fri Jun 29, 2007 8:50 am |
|
|
I have 3 registry bits:
ADCONbits.CHS2=1
ADCONbits.CHS1=0
ADCONbits.CHS0=0
How can I define something which contains them so that I can write
CHS=100
and those bits take the values I want
Thank You |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Fri Jun 29, 2007 9:23 am |
|
|
You didn't say what processor you were using, so I wrote this is for the PIC18F65J10.
you should be able to convert it though.
Code: |
struct {
unsigned int1 ADON;
unsigned int1 GOnDONE;
unsigned int8 CHS : 4;
unsigned int1 bit6;
unsigned int1 ADCAL;
} ADCON0;
#locate ADCON0 = getenv( "SFR:ADCON0" )
void main( void )
{
ADCON0.CHS = 0b0100;
}
|
|
|
|
Marc Guest
|
|
Posted: Fri Jun 29, 2007 2:12 pm |
|
|
Thank you but if I have:
unsigned char ADCON2;
#locate ADCON2=0x0FC0
struct {
unsigned ADCS0:1;
unsigned ADCS1:1;
unsigned ADCS2:1;
unsigned ACQT0:1;
unsigned ACQT1:1;
unsigned ACQT2:1;
unsigned :1;
unsigned ADFM:1;
} ADCON2bits;
#locate ADCON2bits=0x0FC0
without touching this structure, how can I use ADCS=110 ? |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat Jun 30, 2007 5:25 am |
|
|
You can set bit by bit.
ADCON2.ADCS0 = 0;
ADCON2.ADCS1 = 1;
ADCON2.ADCS2 = 1;
or
ADCON2 &= 0b11111000;
ADCON2 |= 0b00000110; |
|
|
Marc Guest
|
|
Posted: Sat Jun 30, 2007 9:28 am |
|
|
Perhaps I can explain.
I need something that modify only three bits and have a name ADCS, I don't know but I think a structure or union that contains those bits |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jun 30, 2007 10:13 am |
|
|
Apparently you don't want to use the "dot" syntax to access the bitfield.
Instead, you want to access it with the symbolic name of "ADCS".
You can do this with a #define statement. Use Kevcon's structure
and define a symbolic name for that bitfield. Then access it by that
name in your main program. Example:
Code: |
#define ADCS (ADCON0.CHS)
//==============================
void main()
{
ADCS = 0b1010;
while(1);
} |
|
|
|
Marc Guest
|
|
Posted: Sun Jul 01, 2007 2:49 am |
|
|
So with command
#define ADCS (ADCON0.CHS)
the compiler knows that ADCON0.CHS means ADCON0.CHS0 plus ADCON0.CHS1, ADCON0.CHS2, ADCON0.CHS3 (ADCON0.CHSx) ?
Or it works only in this situation? Because I'm speaking about grouping 3 bits in a structure which contains 8 bits each of them with a name |
|
|
|