• Pic® Basic


  • Read Synchronizes to Write

    Reducing crosstalk between tracks on the PCB is a matter only of hardware and making the schematic design and the PCB.

    Reduce the current of the output ports can improve this issue. For example it may be desirable to reduce the current to 0.5 mA per pin. To activate the power outputs it is better to use the NPN transistors or FETs (to drive LEDs and also relays).

    To avoid high currents in the inputs due to high ESD use a 100k resistor (and capacitor) in series with the inputs. To reduce EMI use a RF filter without forgetting to configure the SLRCON register of the PIC.

    When everything has been tried then you could get a little help with the small code that I propose.

    From the "Debounce library" I had written to filter the input rebounds I added this new system to write ports in a different time period. This new concept gives the decision to a timer the moment to read or to write ports. The different of phase could be 180º from the inputs to outputs.

    A new trick allows writing to an output port in a 180º phase and also to write to another port using a variable phase. This variable phase allows to correct the activation delay of the relays (signal or power relays). The PDS user could move the phase to have the lower noise as possible. Perhaps you could use this technique for reading from the ADC to dodge some specific noise. Let the user find the possible uses of this system.

    This changes the concept of read and write ports. Now the ports are invisible and can not access them only with some buffers. Then input and output PORTs are completely virtuals.

    I propose 2 differents timing for the PDS user (4mS or 1 mS) and 2 techniques.

    First technique:
    All outputs (PORTs) are written between reading at 180º phase.
    See the Test1_ReadSyncWrite.bas and the ReadSyncWrite1.inc (for timing 4mS) and ReadSyncWrite3.inc (for timing 1mS).

    Second technique:
    One output PORT (or more) is written between reading at 180º phase.
    Another one output PORT (or more) is written between reading at variable phase defined by the user. See the Test2_ReadSyncWrite.bas and the ReadSyncWrite2.inc (for timing 4mS) and ReadSyncWrite4.inc (for timing 1mS).

    But all is not as BGC, read: B = Beautiful, G = Good, C = Cheap. ( in Spanish it is BBB, the 3 B's).
    See this code:
    Code:
            If SW0_Fedge = 1 Then                   
                SW0_Fedge = 0                       ' Clear the bit when it is already read.
                HRSOut "Falling Detect SW0", CR
                
                Toggle LED0                         ' Toggle the LED0 (Virtual pin) every time the SW0 is pressed.
                
                If LED0_LAT = 1 Then                ' Read the real state of the PORT pin.
                    HRSOut "LED0 ON", CR
                Else
                    HRSOut "LED0 OFF", CR
                EndIf
                HRSOut CR
            EndIf
    In a standard code, the PORT is immediately written by the command "Toggle" and there is no issue to read the LED.
    But with the Virtual Output PORTS the LED is not written because the buffer must wait the timing, then the state of the LED is false.

    A small code must be added to wait the timing, using a flag.
    Code:
            If SW0_Fedge = 1 Then                   
                SW0_Fedge = 0                       ' Clear the bit when it is already read.
                HRSOut "Falling Detect SW0", CR
                
                ClearWriteReadyFlag()
                Toggle LED0                         ' Toggle the LED0 (Virtual pin) every time the SW0 is pressed.
                WaitWriteReady()                    ' Waiting until the pin will be written.
               
                If LED0_LAT = 1 Then                ' Read the real state of the PORT pin.
                    HRSOut "LED0 ON", CR
                Else
                    HRSOut "LED0 OFF", CR
                EndIf
                HRSOut CR
            EndIf
    A new command can be written to simplify the code: (see the library)
    Code:
            If SW0_Fedge = 1 Then                   
                SW0_Fedge = 0                       ' Clear the bit when it is already read.
                HRSOut "Falling Detect SW0", CR
                
                Toggle_LED(LED0)            
                
                If LED0_LAT = 1 Then                ' Read the real state of the PORT pin.
                    HRSOut "LED0 ON", CR
                Else
                    HRSOut "LED0 OFF", CR
                EndIf
                HRSOut CR
            EndIf
    Please see all commands available in the library.

    But if you want to write several pins at once, then it would be written:
    Code:
           ClearWriteReadyFlag()
           Toggle LED0
           Set LED1
           Clear LED2
           Set LED3
           WaitWriteReady()
    Note:
    To read the output PORTs, the correct way is reading the LATx registers.

    Figure 1: (First technique)
    Write all PORTs at 180º phase.

    Figure 2: (First technique)
    Write all PORTs at variable phase.

    Figure 3: (Second technique)
    Write a PORT at 180º phase. (PORT 1)
    Write a PORT variable phase. (PORT 2)


    How to init the library:
    InitDebounceRate(32,2,1)
    InitDebounceRate(Debounce every 32 x 4 = 128mS), Shift value(2 x 4mS= 8Ms), 1 = Shift Left)

    With 1mS Timing then:
    InitDebounceRate(32,2,1)
    InitDebounceRate(Debounce every 32 x 4 = 128mS), Shift value(2 x 1mS= 2Ms), 1 = Shift Left)

    I hope this code will be useful for PDS users.
    Download the CODE.
    Alberto