
Having figured out how to configure the parameters of the SPI bus, and transmit bytes, I wanted then drive my AD9833 module, which is a frequency generator.

I determined from the datasheet that I would need the SCLK signal to be high on idle, and to sample on the leading edge. Also, fsync needs to go low before the data is transmitted.

Here are the FSYNC, SCK, and data signals on a scope:

I actually had to study these signals for a few minutes because at first the initialization code was not working. I released that there was a bug in my code such that the fsync signal was inverted. That was easy to fix.
The above data translates to this example initialization data from the programmer’s guide:

Zooming in, you can see the first byte, 0x21:

With sampling on leading edge of SCK (green signal) we have 00100001, i.e., 0x21.
Here is the code so far:
\ ad9833.fs
\ Copyright 2021 Christopher Howard
\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at
\ http://www.apache.org/licenses/LICENSE-2.0
\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.
ad9833
marker ad9833
: init-spi ( -- )
\ set data direction bits
DD_OUT DD_MOSI lshift
DD_OUT DD_SCK lshift or
DD_OUT DD_SS lshift or DDR_SPI mset
\ Setup control register
1 SPR0 lshift
0 SPR1 lshift or \ fck/16
CPHA_SMPLED CPHA lshift or
CPOL_HIDLE CPOL lshift or
MSTR_MSTR MSTR lshift or
DORD_MSB DORD lshift or
SPE_ENAB SPE lshift or
SPIE_DISAB SPIE lshift or SPCR c!
;
: init-fsync [ DD_OUT #4 lshift ] literal DDRD mset ;
: fsync-low [ 1 #4 lshift ] literal PORTD mclr ;
: fsync-high [ 1 #4 lshift ] literal PORTD mset ;
: demo-400hz
init-spi
init-fsync
fsync-low
$21 tx-spi $00 tx-spi
$50 tx-spi $c7 tx-spi
$40 tx-spi $00 tx-spi
$c0 tx-spi $00 tx-spi
$20 tx-spi $00 tx-spi
fsync-high
;
\ Only for viewing the SPI signals
: test-demo begin demo-400hz 1 ms again ;
First you have to load this ad9833 code:
\ ff-328p.fs
\ Copyright 2021 Christopher Howard
\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at
\ http://www.apache.org/licenses/LICENSE-2.0
\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.
ff-328p
marker ff-328p
\ General I/O port addresses
$25 constant PORTB
$28 constant PORTC
$2b constant PORTD
\ Data Direction Registers
$24 constant DDRB
1 constant DD_OUT
0 constant DD_IN
$2a constant DDRD
\ DDR for SPI comms
$24 constant DDR_SPI
$2 constant DD_SS
$3 constant DD_MOSI
$5 constant DD_SCK
\ SPI control register
$4c constant SPCR
$0 constant SPR0 \ SPI Clock Rate Selector bits
$1 constant SPR1 \ (see table 18-5 in 328P datasheet)
$2 constant CPHA \ Clock Phase bit
1 constant CPHA_SMPTRL \ sample on trailing edge of SCK
0 constant CPHA_SMPLED \ sample on leading edge of SCK
$3 constant CPOL \ Clock Polarity bit
1 constant CPOL_HIDLE \ SCK high when idle
0 constant CPOL_LIDLE \ SCK low when idle
$4 constant MSTR \ Master/Slave Select bit
1 constant MSTR_MSTR \ master mode
0 constant MSTR_SLAVE \ slave mode
$5 constant DORD \ Data Order bit
1 constant DORD_LSB \ LSB transmitter first
0 constant DORD_MSB \ MSB transmitted first
$6 constant SPE \ SPI Enable bit
1 constant SPE_ENAB \ SPI enabled
0 constant SPE_DISAB \ SPI disabled
$7 constant SPIE \ SPI Interrupt Enable bit
1 constant SPIE_ENAB \ SPI Interrupt Enabled
0 constant SPIE_DISAB \ SPI Interrupt Disabled
\ SPI status register
$4d constant SPSR
$0 constant SPI2X \ Double SPI Speed Bit
$6 constant WCOL \ Write COLlision Flag
$7 constant SPIF \ SPI Interrupt Flag
\ SPI Data Register (i/o port)
$4e constant SPDR
: tx-spi ( c -- )
SPDR c! begin SPSR c@ 1 SPIF lshift and until
;