我们如何通过微控制器和SPI通信读取4个字节

时间:2017-07-07 06:17:51

标签: c++ microcontroller atmel

我想将数据从微控制器传输到我的SPI存储器芯片4字节(256个八位字节),我开发了下面的代码,我想写一些字节然后从Read函数读取它,但我得到不同的值在4个字节中(例如,我发送0x10011111,我得到A100A1F0)

在存储器芯片的数据表中,没有关于页面传输的信息(256位),数据传输总是双字节(1字= 16位)

这是我写的4字节代码:

  void ecrire4OctetsFRAM(uint32_t const addressRegister, uint32_t const fourBytes, uint8_t const slaAddress)
{
    if (fourBytes != lire4OctetsFRAM(addressRegister, slaAddress))
    {
        if(addressRegister%4)
        {
            ecrire1OctetFRAM(addressRegister+3,(uint32_t) (fourBytes>> 24),FRAM);
            ecrire1OctetFRAM(addressRegister+2,(uint32_t) (fourBytes>> 16),FRAM);
            ecrire1OctetFRAM(addressRegister+1,(uint32_t) (fourBytes>> 8),FRAM);
            ecrire1OctetFRAM(addressRegister,  (uint32_t) (fourBytes)    ,FRAM);




        }
        else
        {
            uint8_t tries(0);
            do 
            {
                error = 0;
                positionnerPointeur(addressRegister, slaAddress);
                if (error) continue;
                transfer((uint8_t) (fourBytes));
                if (error) continue;
                transfer((uint8_t) (fourBytes>> 8));
                if (error) continue;
                transfer((uint8_t) (fourBytes>>16));
                if (error) continue;
                transfer((uint8_t) (fourBytes>>24));
                release();
            } while (error && ++tries < MAX_TRIES);
            error = (tries >= MAX_TRIES)? TW_MAX_TRIES: error;
        }
    }
}

,这是读取

uint32_t    lire4OctetsFRAM  (uint32_t const addressregister, uint8_t const slaveadress ){


uint32_t fourBytes(0);
uint8_t tries(0);
//**
do
    {   error = 0;
    positionnerPointeur(addressregister, slaveadress);
    select();
    transfer(READ);

    fourBytes = transfer((uint32_t) ((addressregister >>  24) & 0xFFFF));
    fourBytes += transfer((uint32_t) ((addressregister >> 16) & 0xFF));
    fourBytes += transfer((uint32_t) ((addressregister >>   8) & 0xFF));

    fourBytes +=transfer((uint32_t) ( addressregister & 0xFF));
    transfer(0);
    fourBytes = SPDR; 
    fourBytes += (fourBytes << 24) ;
    fourBytes += (fourBytes << 16);
    fourBytes = (fourBytes <<  8);
transfer(0);
    fourBytes += SPDR; 
    release();
}
while (error && ++tries < MAX_TRIES);
error = (tries >= MAX_TRIES)? TW_MAX_TRIES: error;
return fourBytes ; 
}

Positionnerpointeur是一个指向位地址(指针)的函数。

0 个答案:

没有答案