setting and clearing bits?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
brandonb
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?

Post by brandonb »

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?

User avatar
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?

Post by Enamul »

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 setting register, you can use c code box...
for 1110101

Code: Select all

t2con=0x75;
and for next one..

Code: Select all

t2con=0x2D;
Enamul
University of Nottingham
enamul4mm@gmail.com

brandonb
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?

Post by brandonb »

For setting register, you can use c code box...
for 1110101

Code: Select all
t2con=0x75;
yes but i think i might have figured it out

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.

User avatar
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?

Post by Enamul »

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;
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.

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;
(general example)
01111101 current
01010000 masking
setting masking bits 4-7 (1010)
01111101
|
01010000
-------------------
01111101
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.
clearing masking bits 4-7
01010000
!
10101111
&
01111101
-------------------
00101101 -->should be 01001101
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?
Enamul
University of Nottingham
enamul4mm@gmail.com

brandonb
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?

Post by brandonb »

oopsi just changed my post while you were replying
but why you are saying it should be 01001101?
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 code

brandonb
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?

Post by brandonb »

Enamul, sorry for the post confusion, i should not be an idiot and change my posts :oops: .... 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

User avatar
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?

Post by Enamul »

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.
Enamul
University of Nottingham
enamul4mm@gmail.com

Mikat
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?

Post by Mikat »

Hi.

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;
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

Post Reply