View previous topic :: View next topic |
Author |
Message |
m4k
Joined: 27 Mar 2018 Posts: 29
|
addressing register |
Posted: Mon Apr 13, 2020 2:49 pm |
|
|
Hi CCS forum,
I have a little problem with write for example 3 bit on 8 bit unsigned register
example:
#define add 0xFA //0b11111010
how to write in bit 6-7 or write in bit 2-3?
tnx |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 13, 2020 3:02 pm |
|
|
That's not a register. That's a defined constant.
But, if you were writing to a register, you could use the CCS functions
of bit_set() and bit_clear(). Look them up in the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf |
|
|
m4k
Joined: 27 Mar 2018 Posts: 29
|
|
Posted: Mon Apr 13, 2020 3:31 pm |
|
|
PCM programmer wrote: | That's not a register. That's a defined constant.
But, if you were writing to a register, you could use the CCS functions
of bit_set() and bit_clear(). Look them up in the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf |
Thank you, but I didn't understand what you meant about bit_set()
i know that how to write in 1 bit..for example:
Code: | #word AD1CON1=0x0320
#bit adon=ad1con1.15
pten=1; //or 0 |
but i dont know how to write for example 3 bit as below
in this picture i want to write in SSRC <2:0>
but i dont know how to?
tnx
m4k |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 13, 2020 5:03 pm |
|
|
You're using the PCD compiler, I believe. Look at how Ttelmah does it in
the following post:
http://www.ccsinfo.com/forum/viewtopic.php?t=56937&start=2
He makes a structure with all the bitfields defined. Each bitfield has a width.
In the case of ssrc, it is 4 bits for his example. In your case, it is 3 bits wide.
Then you can write to it in code as a group. Example:
There should also have a #locate line for AD1CON1 to specify the address
of the register. Place this line below the structure:
Code: | #locate AD1CON1 = getenv("SFR:AD1CON1") |
|
|
|
m4k
Joined: 27 Mar 2018 Posts: 29
|
|
Posted: Tue Apr 14, 2020 1:48 am |
|
|
PCM programmer wrote: | You're using the PCD compiler, I believe. Look at how Ttelmah does it in
the following post:
http://www.ccsinfo.com/forum/viewtopic.php?t=56937&start=2
He makes a structure with all the bitfields defined. Each bitfield has a width.
In the case of ssrc, it is 4 bits for his example. In your case, it is 3 bits wide.
Then you can write to it in code as a group. Example:
There should also have a #locate line for AD1CON1 to specify the address
of the register. Place this line below the structure:
Code: | #locate AD1CON1 = getenv("SFR:AD1CON1") |
|
tnx a lot PCM programmer. I read the Ttelmah post and unfortunately I did not fully understand. It's complex for me. Can you help more ?
tnx
m4k |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Apr 14, 2020 2:16 am |
|
|
Key thing to understand is 'bitfields'. In C, when declaring a structure (and
some other variables), you can declare sub 'parts' that only occupy a
number of bits of the 'container'. Using the ':' syntax. So :
Code: |
struct __attribute__((packed)) {
uint done:1;
uint samp:1;
uint asam:1;
uint na:1;
uint ssrc:4;
uint form:2;
uint mode12:1;
uint dmaen:1;
uint dmabm:1;
uint adsidl:1;
uint na1:1;
uint adon:1;
} AD1CON1;
|
Declares a structure called 'AD1CON1', that contains internal variables
called 'done', 'samp' etc., that are respectively, one bit, one bit, one bit,
one bit, 4 bits, 2 bits etc. etc..
So you can use ADCON1.done as a one bit integer to hold 0/1 TRUE/FALSE.
But ADCON1.SSRC is a 4 bit integer and can hold values from 0 to 15.
Now setting it up as 'packed' makes sure the compiler fits all these
sequentially in memory. All in one 'unsigned int16' (on the PIC24).
This variable you can then 'locate' to be explicitly placed in memory
where the required register 'is'.
Now the declaration would need to change a little for your PIC, since
on yours SSRC is only a 3bit value. So:
Code: |
struct __attribute__((packed)) {
uint done:1;
uint samp:1;
uint asam:1;
uint simsam:1;
uint na:1;
uint ssrc:3;
uint form:2;
uint ad12b:1;
uint na1:1;
uint addmabm:1;
uint adsidl:1;
uint na2:1;
uint adon:1;
} AD1CON1;
#locate AD1CON1 = getenv("SFR:AD1CON1")
|
Gives you a structure called AD1CON1, containing all the parts
laid out exactly as the data sheet.
So ADCON1.SSRC is a 3 bit register, which you can read or write.
The 'unused' bits, I call 'na', 'na1' etc.. (not available). These are
needed to keep the packing 'right'. |
|
|
m4k
Joined: 27 Mar 2018 Posts: 29
|
|
Posted: Tue Apr 14, 2020 3:11 pm |
|
|
tnx alot Ttelmah and pcm programmer. This method is working very well and I'm trying to expand that to other registers.
best regard
m4k |
|
|
|