Using the (M2's) DSM module for a simple logic function (e.g. inversion).


Senior Member

The Data Signal Modulator (DSM) is an optional Logic module contained in all M2 PICaxes. The base PIC data sheets explain how it can be used for complex modulation schemes such as FSK (Frequency Shift Keying) or Amplitude modulation (AM or On-Off Keying, etc.), but the aim here is to show how it can be used to replace simple external hardware such as a latch or inverter (for example to invert the HSERIN signal).

The following diagram, taken from the base Microchip (18M2) data sheet, has been annotated with the Logic functions, Special Function Register flags, External Leg numbers (for 8, 14, 18 and 20 pin chips) and sample internal logic states. This diagram is similar to that created in response to a question in a recent thread (post #19), but I have changed the SFR flag values slightly, to simplify the setup for all three possible input pins.


To implement a simple inverter we need a single "active" path (between the chosen input and output pins), with the remainder of the DSM "passive" (i.e. stable). To minimise the number of flags which need to be set, the "High" path was chosen to be passive (i.e. blocked) and the "Low" path (including the modulation block for its associated input pin) being active.

For the active signal to pass though the OR gate at the output of the DSM module, its upper (unchanging) input must be Low (logic 0). Then, working backwards towards the upper input multiplexer, we see that all the relevant flags and input selector can be set to 0. Conversely, the lower AND gate needs to pass a signal through from either the Modulation Input or the "Low" input (depending on the desired input pin), so we set both of its inputs initially to a 1 (high) state. Hence the choice of the active modulation path via the NOT gate, and setting the flag into the XOR, to invert the fixed Low (Vss) input. Finally, we must set the SFR flags to select the required input pin, and the hardware Enables (DSM, OE, etc.).
symbol MDCON  = $FC             ; ENABLE , OE , Sync , Inv , out , x , x , Mdbit
symbol MDSRC  = $FD            ; 1 for input 0, else 0
symbol MDCARL = $FE            ; Input Selection 0=MDMIN ; 1=MDCIN1 ; 2=MDCIN2
symbol MDCARH = $FF             ; High carrier path (blocked)

symbol MDCL = 64 + 0            ; Invert flag (64) + ** Select the required Input pin (0/1/2) **:
symbol MDMSi = 80 - MDCL    ; Intermediate calculation (SYMBOL accepts only one operator)
symbol MDMS = MDMSi / 80    ; =1 if MDMS=0 , else =0
; symbol MDMS = 10                 ; Can use TX input for "soft" testing 08M2 (output to SerOut pin)

    pokesfr MDCON, %11000000     ; ENABLE , OE(Mode) , Sync , Inv , out , x , x , Mdbit,
    pokesfr MDSRC, MDMS             ; 1 if MDCL=0 else 0
    pokesfr MDCARL, MDCL            ; Input pin: 0=MDMIN, 1=MDCIN1, 2=MDCIN2
    pokesfr MDCARH, %00000000   ; Block the "High" path (all 0)
    peeksfr MDCON,b0                  ; Bit3 can confirm the output level
The above code should configure the DSM to act as an inverter for the selected input of any M2, but has not been comprehensively tested. However, the 08M2 is rather limited because the DSM output employs the same pin as the Serial (Programming) output, and also it is rather greedy of pins. But it is possible to test/demonstrate the DSM using only the normal programming cable by connecting the DSM in series with the HSEROUT signal, which can be linked internally to the Modulation multiplexer (input 10 decimal). However, this is not a very useful facility because the HSEROUT hardware already includes an inversion capability; it is the HSERIN inversion (to the normal PICaxe idle-low) which is not programmable in M2s.

Furthermore, it is possible to implement HSERIN and HSEROUT of both polarities (software-selectable) in an 08M2 by using the Alternate Pin Function (APFCON , 4) SFR to move HSEROUT onto Leg 3, thus releasing Leg 7 to link the (optionally) inverted input signal across to HSERIN on Leg 6 (or optionally swapped over onto Leg 2 after a DISCONNECT and APFCON , $84). The input to the DSM inverter would then be via Leg 5 (c.2); or Leg 6 if both serial pins are swapped (leaving Leg 5 for one of its valuable functions such as PWM and Comparator Outputs, or others such as ADC input, Touch, T0 clock, SR Latch output, CCP1, etc.).
; Demo using the Data Signal Modulator (DSM) to Invert HSERial signals;  AllyCat, January 2020
#picaxe 08m2
symbol PIR1     = $11            ; Interrupt status register (always active)
  symbol RCIF     = bit5        ; A received byte is available to read
  symbol TXIF     = bit4        ; Transmit Buffer is (now) empty
symbol APFCON = $5D        ; 128=RX on c.5 / c.1 ; 8=T1G on c.3 / c.4 ; 4=TX on c.4 / c.0
symbol RCREG     = $79            ; Serial receive register
symbol TXREG     = $7A            ; Transmit byte register
symbol RCSTA     = $7D            ; UART Receive Status register
symbol TXSTA     = $7E            ; 32=TX Enable;4=High speed Baud rate;2=TRMT(RO)=Reg EMPTY

symbol MDCON  = $FC             ; ENABLE , OE , Sync , Inv , out , x , x , Mdbit
symbol MDSRC  = $FD            ; 1 for input 0 else 0
symbol MDCARL = $FE            ; Input Selection 0=MDMIN, 1=MDCIN1, 2=MDCIN2
symbol MDCARH = $FF             ; High carrier path (disabled)

symbol MDCL = 64 + 0            ; Invert (64) + ** Select the required Input pin ** (0 for Leg6)
symbol IR = 0                     ; ** 0 for Idle-Low, 16 for Idle-High HSERIN **
symbol IT = 2                     ; ** 2 for Idle-Low, 0 for Idle-High HSEROUT **
symbol MDMC = %11000000 + IR
symbol MDMS = 0
    sertxd ("Starting  ")                ; Test for reset(s)
    hsersetup b4800_8 , IT        ; MODE: disable RX=16 ; disable TX=8 ; Invert TX=2  
    setfreq m8
    pokesfr MDCON, MDMC          ; ENABLE,OE(Mode),Sync,inv,out,x,x,Mdbit
    pokesfr MDSRC, MDMS             ; 1 if MDCL=0 else 0
    pokesfr MDCARL, MDCL         ; Input pin: 0=MDMIN , 1=MDCIN1 , 2=MDCIN2
    pokesfr MDCARH, %00000000     ; Block the "High" path (all 0)
    pokesfr APFCON, %10000100    ; Swap RXD (128) to c.5 (Leg2) and TXD (4) to c.4 (Leg3)  
;    peeksfr MDCON , b0                ; Bit3 can be used to confirm the output level
    disconnect                 ; To allow the SerIn pin to be used without programming
    pause 50
    pokesfr TXREG , "A"        ; Verify the setup
    hserout 0 , ("B" , "C" , " ")
do                                        ; Receive / Transmit loop
    peeksfr PIR1 , b0         ; Test if a character is available
    if RCIF = 1 then
        peeksfr RCREG , b0        ; Get the character
        pokesfr TXREG , b0       ; Echo the received character
        pause 5                        ; Lazy way to ensure TXREG empties
loop until time = 15                ; 30 seconds delay then restore the programming capability
    pokesfr MDCON , 0
    pokesfr APFCON , 0
    hsersetup off
    setfreq m4
    sertxd(cr , lf , "Ready" , cr , lf)
The above code is not thoroughly tested but, if an external link is connected from Leg7 (c.0) to Leg2 (SerIn), it should echo from Leg3 (c.4) any characters sent at 4800 baud to Leg6 (c.1). Legs 4 (c.3) and 5 (c.2) are then available for other purposes.

Cheers, Alan.

Bill z

Alan ,

This is sort of like the chicken or the egg paradox. You can't program the 08M2 without a cable and if you have a cable, then do you need an inverter? In my case, I do want one because, well, just because I want to learn.

However, I'm not at this level yet regarding all of your detailed and very well done explanation. Can you dumb it up some for us beginners?

Can I use an 08M2 as an inverter using your code? If so, will you show a pin diagram of how to build one (what USB 2.0 wire goes to what 08M2 pin and if any resisters, diodes and or caps are needed)?


Senior Member

Welcome to the forum. The "trick" above was primarily intended to act as an inverter for the HSERIN (Hardware serial input) of the M2 PICaxe chips because Microchip "forgot" to provide a (configurable) inversion option on that particular internal hardware input. Sadly, I don't think it's possible to use it as an inverter for Programming any (single) M2 chip for (at least) two reasons:

The first stage of (re-) programming a PICaxe chip is that the hardware is "Reset", so any setup / configuration of the DSM (or any other hardware) would be cancelled or disabled. Secondly, the programming process also uses the Serial Output (to validate the data that is being written) so two inverters would be required. Therefore, even if you were asking to use a separate 08M2 to act just as an inverter, I doubt if it would be possible; it might be practical with a larger M2, using both the DSM and some other internal module, such as the S-R Latch or a Comparator, but it really doesn't seem worth the effort:

You can make a simple logic inverter with almost any transistor and two resistors (one from the input signal to the base, the other from the collector to the supply rail), or even a 2N7000 (FET) with just one pullup resistor from the Drain to the supply rail. Or the majority of logic gate packages (NAND, NOR, EXOR, etc.) not just Inverters (NOT gate), can be connected to provide an inverting function.

To clarify your last comment, it's not possible to connect ANY PICaxe to "USB" pins because these PICs don't have a USB interface; Programming a PICaxe always needs a "USB to RS232 (serial)" adapter / cable, unless your computer has a "COMx" (RS232) Port (usually with 9 pins). That in itself is not a major issue because many such adapters are available at no more cost than a typical "dumb" cable, but unfortunately PICaxe has two "special" requirements: It requires the "classical" RS232 (COM port) polarity of "Idle Low" signals, but a high proportion of serial interface chips use "Idle High" (commonly called "RS232-TTL"). This is on the assumption that they will be followed by a "high voltage" inverter to give the "official RS232" voltage levels.

Secondly, PICaxe programming uses a special (historical) feature called "Break Signalling" to initiate the programming, which is very difficult to determine if supported from most specifications or listings, particularly on ebay. Hence the numerous threads on this forum about programming adapters, and my recommendation that to not use the "official" Programming lead can be a false economy, unless you have the experience (and perhaps plenty of time to waste). ;)

Cheers, Alan.