使用ATmega16A进行MAX5402数字电位器的SPI配置

时间:2016-12-03 17:16:37

标签: assembly avr atmel atmelstudio

    //*----------------------------------
    //*
    //* PROGRAM NAME:
    //* MAX5402_test.asm
    //*
    //* AUTHOR(S):
    //* Wilmer
    //*
    //* Version:
    //* 1.0
    //*
    //* Last Updated:
    //* 12/01/2016
    //*
    //* Target:
    //* ATmega16A
    //*
    //* DESCRIPTION:
    //* This program tests the
    //* MAX5402 Digital POT. using
    //* subroutines MAX5402_SPI_init,
    //* to initialize the ATmega16A's
    //* hardware for SPI, and
    //* subroutine MAX5402_SPI_write
    //* to write an 8-bit value to the
    //* Digital POT to set the position
    //* of the wiper.
    //*
    //* PORTS:
    //*
    //* PORTA:
    //* Not Used.
    //*
    //* PORTB:
    //* PIN5 = MOSI OUPUT
    //* PIN7 = SCK OUTPUT
    //*
    //* PORTC:
    //* J-Tag Pins
    //* PIN0 = CHIP SELECT OUTPUT
    //*
    //* PORTD:
    //* Not Used.
    //*
    //* Version History:
    //* 1.0 Original Version
    //*
    //*----------------------------------

    start:
    //--------Port Configuration--------//
        sbi DDRC, 0 ; Port C, Pin0 as
                     ; output for chip
                     ; select

        ldi r16, $BF ; PORTB as output
        out DDRB, r16; and MISO as Input 

    //----------Initilize Stack----------//
    ldi r16, LOW(RAMEND)
    out SPL, r16
    ldi r16, HIGH(RAMEND)
    out SPH, r16

    //Initialize SPI//
    call MAX5402_SPI_init

    main_loop:
    //Write to the MAX5402//
        ldi r16, $AB
        call MAX5402_SPI_write

        rjmp main_loop

    //*-------------------------------------
    //*
    //* SUBROUTINE NAME:
    //* MAX5402_SPI_init
    //*
    //* AUTHOR(S):
    //* Wilmer
    //*
    //* MODIFIES:
    //* SPCR (SPI Control Register)
    //*
    //* DESCRITION:
    //* Initialize SPI hardware to
    //* communicate with the MAX5402
    //*
    //*-------------------------------------

    MAX5402_SPI_init:
        push r16 ; conserve

    //---Configuring SPI Control Register---//
        ;SPI Interrupt disabled
        ;SPI enabled
        ;MSB transmitted first
        ;Master Selected
        ;Clock Phase & Polarity = 0
        ;SCK Frequency = fosc/128

        ldi r16, (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | 
        (0 << CPOL) | (0 << CPHA) | (1 << SPR1) | (1 << SPR0)
        out SPCR,r16 

    //-------Clear SPI Interrupt Flag-------//
        in r16, SPSR
        in r16, SPDR

        pop r16 ; restore
        ret

    //*--------------------------------
    //*
    //* SUBROUTINE NAME:
    //* MAX5402_SPI_write
    //*
    //* AUTHOR(S):
    //* Wilmer
    //*
    //* MODIFIES:
    //* R16
    //*
    //* DESCRITION:
    //* Writes 8-bit value in r16.
    //* This value is used to set the
    //* wiper position of the MAX5402.
    //*
    //*--------------------------------

    MAX5402_SPI_write:
        push r17 ; conserve
        push r18
        push r19

        ldi r19, $FF ; Max Value

        ldi r17, $00    ; select MAX5402
        out PINC, r17

    //----------Send Data to Slave----------//
    send_data:
        out SPDR, r16 ; send data to the
                      ; SPI Data Register

    //-----Wait for Data to be Transfered-----//
    wait:
        sbis SPSR, SPIF
        rjmp wait

        inc r16     ; cpmare r16 to max value
        cpse r16, r19 ; if equal, stop sending data
        rjmp send_data  ; and repeat

        ldi r17, 1    ; de-select MAX5402

        out PINC, R16

        pop r19
        pop r18
        pop r17 ; restore
        ret

您好,我正在使用ATmega16A为MAX5402数字电位器编写该程序(测试仪)。对于该程序,MAX5402的H端子设置为+ 5V,端子L接地,以创建一个简单的分压器,其输出端子为W(参考数据表为https://datasheets.maximintegrated.com/en/ds/MAX5402.pdf)。

我需要做的是编写一个子程序,用于初始化MAX5402的MCU硬件和另一个使用r16(0x00 - > 0xFF)向其发送8位值的子程序。该8位值用于设置MAX5402的抽头位置。

我的问题是,当使用ATMEL STUDIO 7.0进行仿真时,游标仅输出2.5V。无论我发送给它什么值,输出都不会改变。

我已经检查了所有物理连接,因此问题必须出在代码中;但我无法弄清楚出了什么问题。 非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

MAX5402_SPI_write例程看起来很奇怪。你为什么要通过SPI发送这么多位?

从数据表中看,它看起来像设备所期望的:notCS低,SPI上的一个字节,然后再次高CS。您在中间步骤通过SPI发送许多字节。这将破坏数据,并且设备将默认为中间值,如您所见。