eblc1388 Active Member Sep 18, 2005 #1 Hi guys, On a 16F877, I have set up eight flags using a single register called "flags" and I need to change them or toggle them individually. I know I can test the bit and change it, but is there an easy way to just toggle a single bit, for example?
Hi guys, On a 16F877, I have set up eight flags using a single register called "flags" and I need to change them or toggle them individually. I know I can test the bit and change it, but is there an easy way to just toggle a single bit, for example?
Nigel Goodwin Super Moderator Most Helpful Member Sep 18, 2005 #2 eblc1388 said: Hi guys, On a 16F877, I have set up eight flags using a single register called "flags" and I need to change them or toggle them individually. I know I can test the bit and change it, but is there an easy way to just toggle a single bit, for example? Click to expand... It's VERY, VERY simple - you should read the datasheet, there's only 35 instructions!. But see BSF (to set a bit), and BCF (to clear a bit).
eblc1388 said: Hi guys, On a 16F877, I have set up eight flags using a single register called "flags" and I need to change them or toggle them individually. I know I can test the bit and change it, but is there an easy way to just toggle a single bit, for example? Click to expand... It's VERY, VERY simple - you should read the datasheet, there's only 35 instructions!. But see BSF (to set a bit), and BCF (to clear a bit).
eblc1388 Active Member Sep 18, 2005 #3 Thanks Nigel, but I like to "toggle" the bit not set or clear it. Nevermind, I figured it out myself. Code: movlw B'00001000' ;toggle bit3 xorwf flags,F
Thanks Nigel, but I like to "toggle" the bit not set or clear it. Nevermind, I figured it out myself. Code: movlw B'00001000' ;toggle bit3 xorwf flags,F
Joel Rainville New Member Sep 18, 2005 #5 eblc1388 said: Thanks Nigel, but I like to "toggle" the bit not set or clear it. Nevermind, I figured it out myself. Code: movlw B'00001000' ;toggle bit3 xorwf flags,F Click to expand... Exactly 8) And a little optimization trick : say you frequently toggle 2 or multiple flags at the same time, you can OR them together as assembler #defines and use the #defined value to toggle both flags in 1 instruction Code: #define FLAG1 B'00000001' #define FLAG2 B'00000010' #define FLAGS1n2 FLAG1 | FLAG2 [...] movlw FLAGS1n2 xorwf [file] Very useful for some algorithms... Sorry if I'm being Captain Obvious here :lol:
eblc1388 said: Thanks Nigel, but I like to "toggle" the bit not set or clear it. Nevermind, I figured it out myself. Code: movlw B'00001000' ;toggle bit3 xorwf flags,F Click to expand... Exactly 8) And a little optimization trick : say you frequently toggle 2 or multiple flags at the same time, you can OR them together as assembler #defines and use the #defined value to toggle both flags in 1 instruction Code: #define FLAG1 B'00000001' #define FLAG2 B'00000010' #define FLAGS1n2 FLAG1 | FLAG2 [...] movlw FLAGS1n2 xorwf [file] Very useful for some algorithms... Sorry if I'm being Captain Obvious here :lol:
Joel Rainville New Member Sep 18, 2005 #6 Jay.slovak said: on PIC18F, you would just use BTG (Bit togle) instruction Click to expand... Yes, but as you can see from my previous example, xorwf is more "powerful". If I am not mistaken, BTG limits you to 1 bit at a time?...
Jay.slovak said: on PIC18F, you would just use BTG (Bit togle) instruction Click to expand... Yes, but as you can see from my previous example, xorwf is more "powerful". If I am not mistaken, BTG limits you to 1 bit at a time?...
J Jay.slovak Active Member Sep 18, 2005 #7 Joel Rainville said: Jay.slovak said: on PIC18F, you would just use BTG (Bit togle) instruction Click to expand... Yes, but as you can see from my previous example, xorwf is more "powerful". If I am not mistaken, BTG limits you to 1 bit at a time?... Click to expand... Actually yes, good point here (I didn't think about that).
Joel Rainville said: Jay.slovak said: on PIC18F, you would just use BTG (Bit togle) instruction Click to expand... Yes, but as you can see from my previous example, xorwf is more "powerful". If I am not mistaken, BTG limits you to 1 bit at a time?... Click to expand... Actually yes, good point here (I didn't think about that).