NCO square-wave output using Smartpin
in Propeller 2
I wanted a 12.375 MHz square wave to drive the clock on an FTDI EVE3 chip.
Was pretty easy to do using the NCO smartpin mode.
Basically like this in FlexProp C:
in FlexProp C using inline assembly to create NCO on pin #8 (12.375 MHz with 297 MHz P2 clock):
__asm {
wrpin ## 0b00000000000000000000000011001100, #8 //smartpin NCO
WXPIN #12, #8 //base period
WYPIN ##$8000_0000, #8 //add amount per clock
drvh #8
}
I've made some notes on the math and the Spin2 version here.
Was pretty easy to do using the NCO smartpin mode.
Basically like this in FlexProp C:
in FlexProp C using inline assembly to create NCO on pin #8 (12.375 MHz with 297 MHz P2 clock):
__asm {
wrpin ## 0b00000000000000000000000011001100, #8 //smartpin NCO
WXPIN #12, #8 //base period
WYPIN ##$8000_0000, #8 //add amount per clock
drvh #8
}
I've made some notes on the math and the Spin2 version here.

Comments
org wrpin ##(P_OE | P_NCO_FREQ), #NCO_PIN wxpin #12, #NCO_PIN wypin ##$8000_0000, #NCO_PIN drvh #NCO_PIN endeg: PINSTART(NCO_PIN, (P_OE | P_NCO_FREQ), 12, $8000_0000)
Try it yourself interactively via the serial console.
Exactly, and pinstart() works also in flex-C if you write it with a underscore at begin: Andy
Thanks!
You're absolutely right. I have recently been porting P1 laser tag code to the P2, and have setup my IR modulation pins manually so that I can control when the oscillation starts. In this case, it is desired right away, so pinstart() is appropriate.
pub set_freq(port, hz) '' Sets output frequency for selected port(s), 1, 2, or 3 (both) '' -- application should check for not busy before changing frequency '' -- setting frequency to 0 disables port if (tx1p >= 0) ' port 1 pin defined? if ((port == CMD_P1) || (port == CMD_P3)) ' update port 1 frequency? pinclear(tx1p) ' clear settings if (hz > 0) wrpin(tx1p, P_NCO_FREQ | P_OE) ' NCO with output wxpin(tx1p, 1) wypin(tx1p, hz frac clkfreq) ' set frequency if (tx2p >= 0) if ((port == CMD_P2) || (port == CMD_P3)) pinclear(tx2p) if (hz > 0) wrpin(tx2p, P_NCO_FREQ | P_OE) wxpin(tx2p, 1) wypin(tx2p, hz frac clkfreq)