0
\$\begingroup\$

I was wondering if someone could help me solve one problem, because I am very new to this and I was given the code of a former programmer to work with.
I'm trying to figure out the value of 0x8008 & ~(1<<15).
Maybe someone could also tell me what's the purpose of writing a value in this manner?

Code:

 // STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX; 
 U1MODE = (0x8008 & ~(1<<15)); // disabling UARTEN bit 

From what I understand:

0x8008 = 1000000000001000

1<<15 = 1000000000000000

~(1<<15) = 0111111111111111

so: 1000000000001000 & 0111111111111111 = 00000000000001000

This answer bugs me, because judging by it - UART is disabled, which is definitely not true.

datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/70000582e.pdf


I did some digging in the code and I found this line:

 U1MODEbits.UARTEN = 1;

Still don't get what's the purpose of disabling the UART first and then enabling it.

W5VO
19.6k7 gold badges65 silver badges97 bronze badges
asked Jun 14, 2018 at 14:29
\$\endgroup\$
4
  • 1
    \$\begingroup\$ It's a bit of a round-about way of doing things, but it certainly will disable the UART as claimed. The UARTEN bit in U1MODE is the 0x8000 (0b1000000000000000) bit and this is set to 0 (disabled) by that instruction. \$\endgroup\$ Commented Jun 14, 2018 at 14:34
  • 3
    \$\begingroup\$ Just curious, as I haven't written code for 16-bit PICs, but in the 8-bit XC8 compiler case it comes with header files which specifies the parts of registers via bitfields, so typically you would write something like U1MODEbits.UARTEN = 0, and it would work (in the 8-bit case translating to a bcf or bsf instruction). Isn't that the case with the 16-bit PICs? \$\endgroup\$ Commented Jun 14, 2018 at 15:23
  • \$\begingroup\$ @anrieff Yes - and it seems from the code examples in the datasheet that this should be the case here too. \$\endgroup\$ Commented Jun 14, 2018 at 15:29
  • 1
    \$\begingroup\$ "This answer bugs me, because judging by it - UART is disabled, which is definitely not true." Bit 15=0 afterward, Are you saying the UART isn't disabled by that operation? \$\endgroup\$ Commented Jun 14, 2018 at 15:50

1 Answer 1

4
\$\begingroup\$

This is an obtuse bit of code, and doesn't really have any virtue. It combines a shift with a magic number, so it's even less clear than just directly writing the value...

You would usually define somewhere the meaningful bit name(s), in this case UARTEN, with its index in the register. It would be more usual to do a read-modify-write sequence (unless you're initialising registers). So, copy the original, modify the bit, and then re-write to the register. This would look something like:

#define UARTEN 15
U1MODE = U1MODE & ~(1<<UARTEN); // clear UARTEN
U1MODE = U1MODE | (1<<UARTEN); // set UARTEN

As the mask is a constant, the compiler should optimise out the shift and negation and then do a simple bitwise mask (although check the disassembled output).

answered Jun 14, 2018 at 14:44
\$\endgroup\$
2
  • \$\begingroup\$ Depending upon the hardware design, read-modify-write sequences may not behave as intended. In a good design, registers that are readable and writable should generally behave sanely when given a read-modify-write sequence, but not all designs are good. Writing an exact combination of bits will generally avoid trouble even when using badly designed devices. \$\endgroup\$ Commented Jun 14, 2018 at 17:23
  • \$\begingroup\$ @supercat - indeed, the code above does hide the internals of what is happening, but should suffice to explain this situation. \$\endgroup\$ Commented Jun 14, 2018 at 18:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.