Menu

SPI for DAC MCP4802 on ATmega328p

jabcam
2017-03-14
2017-03-14
  • jabcam

    jabcam - 2017-03-14

    Hi all!

    For reference here are two words I use to communicate with an external DAC on an Arduino R3. This is my first forage into inlining conditional AVR-assembly in Forth. PORTB is set all output elsewhere.

    Cheers
    /Micke

    (Update: sorry about the lack of indentation. The editor apparently does not acknowledge spaces...)

    \ 8-bit DAC routines
    -mcp48x2
    marker -mcp48x2

    $4c constant SPCR
    $4d constant SPSR
    $4e constant SPDR

    \ SPCR = 01010000 and double speed
    spi-init
    $50 SPCR c!
    $01 SPSR c! \ double speed fosc/2

    ;

    \ Prepare data and send with 8-bit DAC MCP4802
    DAC ( u --- )
    [ R24 swap, ] \ 4 lshift
    [ R25 R24 mov, ]
    [ R25 $0f andi, ]
    [ R25 $10 ori, ] \ $1000 or
    [ PORTB 2 cbi, ] \ SS on device low
    [ SPDR R25 sts, ] \ Send 1st byte
    [ begin, ] \ wait for SPIF, SPDR bit7
    [ R25 SPSR lds, ]
    [ R25 $80 andi, ]
    [ mi, until, ]
    [ SPDR R24 sts, ] \ Send 2nd byte
    [ begin, ] \ wait for SPIF
    [ R24 SPSR lds, ]
    [ R24 $80 andi, ]
    [ mi, until, ]
    [ R24 Y+ ld, ] \ drop
    [ R25 Y+ ld, ]
    [ PORTB 2 sbi, ] \ SS on device high

    ;

     

    Last edit: jabcam 2017-03-15
  • Mikael Nordman

    Mikael Nordman - 2017-03-15

    You can format the code in a code block with \</> or ctrl-alt-C /Micke

    \ 8-bit DAC routines
    -mcp48x2
    marker -mcp48x2
    
    $4c constant SPCR
    $4d constant SPSR
    $4e constant SPDR
    
    \ SPCR = 01010000 and double speed
    : spi-init
      %0101.0000 SPCR c!
      %0000.0001 SPSR c! \ double speed fosc/2
    ;
    
    \ Prepare data and send with 8-bit DAC MCP4802
    : DAC ( u --- )
      [ R24 swap,       ] \ 4 lshift
      [ R25 R24 mov,    ]
      [ R25 $0f andi,   ]
      [ R25 $10 ori,    ] \ $1000 or
      [ PORTB 2 cbi,    ] \ SS on device low
      [ SPDR R25 sts,   ] \ Send 1st byte
      [ begin,          ] \ wait for SPIF, SPDR bit7
      [   R25 SPSR lds, ]
      [   R25 $80 andi, ]
      [ mi, until,      ]
      [ SPDR R24 sts,   ] \ Send 2nd byte
      [ begin,          ] \ wait for SPIF
      [   R24 SPSR lds, ]
      [   R24 $80 andi, ]
      [ mi, until,      ]
      [ R24 Y+ ld,      ] \ drop
      [ R25 Y+ ld,      ]
      [ PORTB 2 sbi,    ] \ SS on device high
    ;
    
     

    Last edit: Mikael Nordman 2017-03-16
  • jabcam

    jabcam - 2017-03-16

    Thanks for the tip!

    And an ever bigger thanks for the Flashforth! It really is a clever environment. I'm having a ball with it. Currently I am investigating if signal processing can be done with the ATmega's meagre speed. I use ff for logical structure and ease of programming and code critical words in assembler. If I get any results I will post them here too.

    /Micke (SM5JAB...)

     
  • Mikael Nordman

    Mikael Nordman - 2017-03-16

    Thanks for the code.
    There has been requests for Atmega assembler examples, and your code is really good.

    Keep them coming !

    fe value isy
    ff to isy
    :-)

     

    Last edit: Mikael Nordman 2017-03-16

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.