setting and clearing bits?
Moderator: Benj
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
- Contact:
setting and clearing bits?
example,i want to change t2con register directly, for instance its set to 1:15 = 1110101 then somewhere along the program i want to change the prescaler to 1:6 = 0101101 but i just want to set bits 3-6, how would i do that with 0101000?
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: setting and clearing bits?
For setting register, you can use c code box...example,i want to change t2con register directly, for instance its set to 1:15 = 1110101 then somewhere along the program i want to change the prescaler to 1:6 = 0101101 but i just want to set bits 3-6, how would i do that with 0101000?
for 1110101
Code: Select all
t2con=0x75;
Code: Select all
t2con=0x2D;
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
- Contact:
Re: setting and clearing bits?
yes but i think i might have figured it outFor setting register, you can use c code box...
for 1110101
Code: Select all
t2con=0x75;
t2con_temp=t2con;//temperary variable
t2con_temp&=0b10000111;//clearing bits 3-6
t2con_temp|=mask<<3;//setting bits 3-6 from mask
t2con=t2con_temp;//variable to register write
example
01111101 is currently set
changing bits 3-6 to 0b1010
clear bits #3-6
01111101
&
10000111
---------------
00000101
set appropriate bits #3-6
00000101
|
01010000
----------------
01010101 (answer)
Last edited by brandonb on Tue Nov 20, 2012 7:38 am, edited 5 times in total.
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: setting and clearing bits?
First of all you have to declare two variable named t2con_temp and mask_value in Flowcode. But in c code these are accessible as FCV_T2CON_TEMP & FCV_MASK_VALUE respectively.yes but what about something like this
CODE: SELECT ALL
t2con_temp=t2con;
t2con_temp|=mask_value<<3;
t2con_temp&=~(mask_value<<3);
t2con=t2con_temp;
Code: Select all
FCV_T2CON_TEMP=t2con;
FCV_T2CON_TEMP = FCV_T2CON_TEMP | FCV_MASK_VALUE << 3;
FCV_T2CON_TEMP = FCV_T2CON_TEMP & (!(FCV_MASK_VALUE << 3));
t2con=FCV_T2CON_TEMP;
I am bit confused with your example..Like if you initialize mask_value by 0b00001010. In that case need to get 01010000 you need 3 left shift. Isn't that right? If that right, the code I have posted will do what you are looking for.(general example)
01111101 current
01010000 masking
setting masking bits 4-7 (1010)
01111101
|
01010000
-------------------
01111101
In the above example you have left shifted and inverted the mask value and then AND the value with current t2con which results in 00101101 (and should be!)....but why you are saying it should be 01001101?clearing masking bits 4-7
01010000
!
10101111
&
01111101
-------------------
00101101 -->should be 01001101
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
- Contact:
Re: setting and clearing bits?
oopsi just changed my post while you were replying
because my math didnt work, i think i have the forumula correct this time at least it works on paper but the code is not really codebut why you are saying it should be 01001101?
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
- Contact:
Re: setting and clearing bits?
Enamul, sorry for the post confusion, i should not be an idiot and change my posts .... i wanted to post my second idea, it works pretty good, i did this on the pickit demo board for simplicity,, cant beleive it took me so long to figure out the bitmath because its really simple, cheers
- Attachments
-
- t2con_postscalerbits.fcf
- (10.5 KiB) Downloaded 255 times
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: setting and clearing bits?
I have had a look in the code and it seems now count variable is used as mask. so you have sorted that. That's great.
-
- Posts: 258
- Joined: Wed Nov 29, 2006 6:32 pm
- Location: Finland
- Has thanked: 7 times
- Been thanked: 36 times
- Contact:
Re: setting and clearing bits?
Hi.
If you want set the postscaler direct at variable, you can try this:
That should set the postscaler value at postscaler_value + 1, eg 0=1 > 15 = 16, and don't write other bits on t2con register..
I don't know does that work, but you can try..
Mika
If you want set the postscaler direct at variable, you can try this:
Code: Select all
t2con.T2OUTPS0 = FCV_POSTSCALER_VALUE;
t2con.T2OUTPS1 = FCV_POSTSCALER_VALUE >> 1;
t2con.T2OUTPS2 = FCV_POSTSCALER_VALUE >> 2;
t2con.T2OUTPS3 = FCV_POSTSCALER_VALUE >> 3;
I don't know does that work, but you can try..
Mika