Visti 0 Posted February 28, 2006 Report Share Posted February 28, 2006 V. 6.31 Target PIC16F877A (must apply to others aswell). bit1=bit2; gives following ... bcf bit1 btfsc bit2 bsf bit1 ... ... meaning that an output (in my case) is toggled when it should stay 1 (took me a couple of hours to figure that one out). Workaround: if(bit2) bit1=1;else bit1=0; Quote Link to post Share on other sites
Picxie 0 Posted February 28, 2006 Report Share Posted February 28, 2006 V. 6.31 Target PIC16F877A (must apply to others aswell). bit1=bit2; gives following ... bcf bit1 btfsc bit2 bsf bit1 ... ... meaning that an output (in my case) is toggled when it should stay 1 (took me a couple of hours to figure that one out). Workaround: if(bit2) bit1=1;else bit1=0; <{POST_SNAPBACK}> Manipulating output port bits is a bit hairy on PICs due to the read modify write problem (its documented in every PIC spec sheet.) You would be better mirroring the port bits, manipulating the mirror then writing the mirror out to the port. Quote Link to post Share on other sites
Pavel 0 Posted February 28, 2006 Report Share Posted February 28, 2006 V. 6.31 Target PIC16F877A (must apply to others aswell). bit1=bit2; gives following ... bcf bit1 btfsc bit2 bsf bit1 ... ... meaning that an output (in my case) is toggled when it should stay 1 (took me a couple of hours to figure that one out). Workaround: if(bit2) bit1=1;else bit1=0; Declare the bit as volatile. This will prevent unnecessary toggle but will generate slightly longer code. Regards, Pavel PS: We also recommend to upgrade to the latest release 6.32 Quote Link to post Share on other sites
Visti 0 Posted March 3, 2006 Author Report Share Posted March 3, 2006 Declare the bit as volatile. This will prevent unnecessary toggle but will generate slightly longer code. if(bit1) bit2=1;else bit2=0; is shorter. Mirroring a port is overkill if e.g. only 1 bit is output. The bit2=bit1; could give problems in interrupts as well. PS: We also recommend to upgrade to the latest release 6.32 <{POST_SNAPBACK}> I accidently hit the 1 instead of 2. It is 6.32. Quote Link to post Share on other sites
Pavel 0 Posted March 3, 2006 Report Share Posted March 3, 2006 Declare the bit as volatile. This will prevent unnecessary toggle but will generate slightly longer code. if(bit1) bit2=1;else bit2=0; is shorter. Not really. "if(bit1) bit2=1;else bit2=0;" compiles into 5 instructions while "bit2 = bit1;" into 4 instructions when bit2 is declared as volatile. Regards, Pavel Quote Link to post Share on other sites
Visti 0 Posted March 3, 2006 Author Report Share Posted March 3, 2006 Declare the bit as volatile. This will prevent unnecessary toggle but will generate slightly longer code. if(bit1) bit2=1;else bit2=0; is shorter. Not really. "if(bit1) bit2=1;else bit2=0;" compiles into 5 instructions while "bit2 = bit1;" into 4 instructions when bit2 is declared as volatile. Regards, Pavel <{POST_SNAPBACK}> This is what I got CLRF CompTempVar164 BTFSC gbl_bit1,0 INCF CompTempVar164, F BTFSC CompTempVar164,0 BSF gbl_bit2,6 BTFSS CompTempVar164,0 BCF gbl_bit2,6 The transfer itself is 4 but the setup is another 3. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.