Mask needed for UART use?

2007-08-09
2013-03-12
  • franciscojar
    franciscojar
    2007-08-09

    Hi all!

    I'm use a PIC16F874A and need to setup from RC0 to RC5 as general output, RC6 UART TX (output) and RC7 UART RX (input)

    I know if try to write an input pin, the pin state does not modify:

    PORTC = 0;
    trisc = 0x80;  // RC7 as input, RC6:RC0 as output
    PORTC = 0xFF;  // try to write 1 on RC7

    The real output on PORTC is 0x7F

    My doubts are:
    1) If the UART is transmitting, ie. RC6 is changing owing to internal HW, If I write 0xFF to PORTC, will RC6 change and would I loss the sync??

    2) If the UART is receiving, ie. RC7 is changing owing to external HW, If I write 0xFF to PORTC, RC7 data will change and the data would be corrupted??

    If yes, I would write something like:

    PORTC |= 0x3F; // write 1 on RC5:RC0
    PORTC &= 0xC0; // write 0 on RC5:RC0

    Thanks in advance.

    franciscojar.
    Is mandatory use

     
    • AP2k
      AP2k
      2007-08-09

      =================
      PORTC = 0;
      trisc = 0x80; // RC7 as input, RC6:RC0 as output
      PORTC = 0xFF; // try to write 1 on RC7

      The real output on PORTC is 0x7F

      Measuring the voltages on the device will yeild 0x7F because all of the bits except #7 were set as outputs. Since PORTC7 is an input, the voltage driver is not used, so you see 0V.
      According to the datasheet, RC6 and RC7 must be set as inputs in TRISC. The hardware will control their input and output states automatically.

      I cannot definitively say what will happen if you write to PORTC while data is transmitting, but I would mask the write like this:

      PORTC = 0x3F & char_to_write_to_PORTC;

      Since you have pins that are dedicated to serial I/O, I would not recommend writing to them directly but instead masking ALL writes to PORTC using the code above. This is especially true if you are using synchronous transmission.

       
    • franciscojar
      franciscojar
      2007-08-09

      Thanks a lot AP2K, I will use 0x3F mask.

      franciscojar.