Transfer PWM output to a different pin?

cpedw

Senior Member
I would like to obtain PWM out from a Port C pin of an 18M2 instead of from B3 or B6. I want to use PWM along with an OLED display and using the display's 18M2 for PWM is attractive - the 18M2 has plenty of code space and 3 unused pins, C0, C1 and C2.

I suppose using 4 bit mode of the display would get around it but I seem to remember that's quite hard - I tinkered with 4 bit a few years ago.

The background is a Signal Generator using PWM. I have written it for an 8M2 with an AXE133 display but it's appealing to put it all into the 18M2.

Derek
 

AllyCat

Senior Member
Hi,

"Background" PWM outputs require dedicated on-chip hardware, so they can be available only on certain specific pins. It may be possible to move some PWM outputs by using the "Alternate Pin Function" Special Function Register (SFR), but a brief look at the Microchip 18-pin data sheet indicates that pins C.0 to C.2 cannot be used for (PWM) Alternate Pin Functions. :(

However, pins C.6 and C.7 do have an APF capability, so if these would be satisfactory then we can look at the possibilities. But driving a LCD/OLED in 4-bit mode shouldn't be too difficult: InglewoodPete has written a "Universal" program to do exactly that, (which was discussed on the forum quite recently).

Cheers, Alan.
 

cpedw

Senior Member
Thanks for the heads up. Although C.7 is used for the OLED display, it will require only minor surgery to divert that to C.0 and use C.7 for PWM output.
 

AllyCat

Senior Member
Hi,

The (base PIC) equivalent pins to PICaxe C.7 (and C.6) are referred to as the "default" outputs for the H-bridge PWM hardware (silicon), but the PICaxe HPWM{DUTY} command doesn't appear to specify an 18 pin chip. So you might need to adopt POKESFR commands to use PWM on those particular pins. :(

Cheers, Alan.
 

cpedw

Senior Member
I took a deep breatth and dived into the datasheet. I came up with this
Code:
' Test Moving PWM output to an alternate pin.
#PICAXE 18M2
' 18M2 has PWMOUT on B.3 and B.6. These may be redirected to B.3 and C.7 respectively.

SYMBOL APFCON0    = $5D           ; $11D - Alternate Pin Function register 0
SYMBOL CCP2SEL    = bit3            ; This bit=1 moves PWMOUT from B.6 (leg 12) to C.7 (leg 16)
SYMBOL LogPWMpin= B.6

PWMOUT pwmdiv4, LogPWMpin, 249, 499        ' 1kHz, 50% duty on pin B.6
OUTPUT C.7

DO
    PAUSE 30000                    ' Run for 30 seconds
    PEEKSFR APFCON0, b0
    CCP2SEL = 1
    POKESFR APFCON0, b0            ' Move PWMoutput to pin C.7

    PAUSE 30000                    ' Run for 30 seconds
    PEEKSFR APFCON0, b0
    CCP2SEL = 0
    POKESFR APFCON0, b0            ' Move PWMoutput back to pin B.6

LOOP
and it works!
The PWM output switches between pins B.6 and C.7 every 30 seconds. So I can move the PWM output to a pin I can use.

Thanks guys.

Derek
 

inglewoodpete

Senior Member
Thanks for trying it out and reporting back. While the 18M2 is not my favourite chip (too close in size to big-brother 20X2), it gives us hope that the same technique could be used on other PICAXEs.

Perhaps the next generation of PICAXEs could be based on the K42 family of PICs, with superior peripheral-to-pin control!
 
Top