音影先锋亚洲天堂网|电影世界尽头的爱完整版播放|国产 熟女 91|高清无码免费观看欧美日韩|韩国一区二区三区黄色录像|美女亚洲加勒比在线|亚洲综合网 开心五月|7x成人在线入口|成人网站免费日韩毛片区|国产黄片?一级?二级?三级

登錄 免費注冊 首頁 | 行業(yè)黑名單 | 幫助
維庫電子市場網(wǎng)
技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng)
驅(qū)動編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機 | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計 | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe

急求AT45D類芯片的SPI讀寫程序!(c語言)

作者:gciessence 欄目:單片機
急求AT45D類芯片的SPI讀寫程序!(c語言)
 
2樓: >>參與討論
gciessence
大蝦們幫幫小弟啊~
大蝦們幫幫小弟啊~

3樓: >>參與討論
蘭天白云
我明天給你
 
4樓: >>參與討論
蘭天白云
試一試,PIC單片機程序
SPI的使用:
void spi_initialize(void)
{   CSAT=1;               //不選通
    INTCONbits.PEIE=0;    //禁止中斷
    PIE1bits.SSPIE=0;     //禁止SPI中斷
    TRISCbits.TRISC5=0;   //SDO輸出
    TRISCbits.TRISC4=1;   //SDI輸入
    TRISCbits.TRISC3=0;   //SCK輸出
    PIR1bits.SSPIF=0;     //標(biāo)志位清零
    SSPCON1=0x31;         //使能SPI,CKP=1,F(xiàn)OSC/16
    SSPSTAT=0x80;         //CKE=0上升沿發(fā)送數(shù)據(jù),模式3
}  
//----------------------------------  
unsigned CHAR spi_out_in(unsigned CHAR x)
{   SSPBUF=x;
    while(PIR1bits.SSPIF==0)
    {;}
    PIR1bits.SSPIF=0;
    x=SSPBUF;
    return(x);        
}

5樓: >>參與討論
gciessence
多謝了
我主要是搞不明白AT芯片寫和讀時候發(fā)送指令以及頁地址字節(jié)地址的
方式,想找個示例程序看一下。
另外想找高手問一下,AT455DB081B供電是2.7-3.6伏,
但是如果供電5V的話會怎么樣,能否正常讀寫數(shù)據(jù)?

6樓: >>參與討論
蘭天白云
用5V供電,肯定燒。
AT芯片寫和讀發(fā)送指令以及頁地址字節(jié)地址,這些你要看數(shù)據(jù)手冊。

7樓: >>參與討論
wjc3k
可以.
AT45DB041B   5V供電沒咩問題。

void AT45Init()
{
    FLASH_CS=1;        FLASH_SI=1;
    FLASH_SO=1;        FLASH_SCK=0;
}
//----------------------------------
void  AT45WriteByte(uchar d)
{
  uchar i;
  FLASH_SCK=0;      FLASH_CS=0;
  for (i=0;i<8;i++)
  {
      FLASH_SI=d&0x80;    FLASH_SCK=1;     FLASH_SCK=0;
      d<<=1;
    }
}

//---------------------------------------------------
uchar AT45ReadByte()
{
  uchar i,d=0;
    FLASH_CS=0;
  FLASH_SCK=0;
  for(i=8;i>0;i--)
  {
      FLASH_SCK=1;      FLASH_SCK=0;     d=(d<<1)|FLASH_SO;
  }
  return(d);
}
uchar AT45Status()
{
  uchar d;
    AT45Init();
    AT45WriteByte(0x57);
    d=AT45ReadByte();
  FLASH_CS=1;
  return(d);
}
//---------------------------------------------------
void  AT45WaitReady()
{uint errtime=0;
    AT45D_OK=1;
    while (!(AT45Status()&0x80))
    {    if(errtime++>=2345) {     AT45D_OK=0;    AT45Init();    break;    }    }    //時間溢出.
}

bit Flash_PageToBuf(uchar idx,uint page)    //將數(shù)據(jù)從主存頁讀到idx指定的緩存中. 成功返回1,失敗返回0.
{
    AT45WriteByte(0x53+idx*2);
    AT45WriteByte((uchar)(page>>7));
    AT45WriteByte((uchar)(page<<1));
    AT45WriteByte((uchar)(page));
    FLASH_CS=1;
    AT45WaitReady();    
    curpage=page;
    return(AT45D_OK);
}
bit Flash_BufToPage(uchar idx,uint page)    //
{
    AT45WriteByte(0x83+idx*2);
    AT45WriteByte((uchar)(page>>7));
    AT45WriteByte((uchar)(page<<1));
    AT45WriteByte((uchar)(page));
    FLASH_CS=1;
    AT45WaitReady();
    return(AT45D_OK);
}
void Flash_WriteBuf(uchar idx,uint off,uint len,uchar *buf)
{
    AT45Init();
    AT45WriteByte(0x84+idx*3);
    AT45WriteByte(0x00);
    AT45WriteByte((uchar)(off>>8));
    AT45WriteByte((uchar)off);
    while(len--) AT45WriteByte(*buf++);
    FLASH_CS=1;
}
bit Flash_Comp(uchar idx,uint page)
{
    AT45Init();
    AT45WriteByte(0x60+idx);
    AT45WriteByte((uchar)(page>>7));
    AT45WriteByte((uchar)(page<<1));
    AT45WriteByte(0x00);
    FLASH_CS=1;
    return(!(AT45Status()&0x40));
}
bit Flash_Write(uint page,uint off,uchar len,uchar *buf)
{
    if(curpage !=page)
    {    Flash_BufToPage(0x00,curpage);     
        Flash_PageToBuf(0x00,page);    
    }
    Flash_WriteBuf(0x00,off,len,(uchar*)buf);
    Flash_BufToPage(0x00,page);
    return(Flash_Comp(0x00,page));
}
void Flash_ReadBuf(uchar idx,uint off,uint len,uchar *buf)
{
    AT45Init();
    AT45WriteByte(0x54+idx*2);
    AT45WriteByte(0x00);
    AT45WriteByte((uchar)(off>>8));
    AT45WriteByte((uchar)off);
    AT45WriteByte(0x00);
    while(len--) *buf++=AT45ReadByte();
    FLASH_CS=1;
}

bit Flash_Read(uint page,uint off,uchar len, uchar *buf)
{
     if(curpage != page)
     {
         Flash_BufToPage(0x00,curpage);
        Flash_PageToBuf(0x00,page);
    }
    Flash_ReadBuf(0x00,off,len,(uchar*)buf);
    return(AT45D_OK);
}

8樓: >>參與討論
gciessence
有幾個地方不明白
idx是做什么用的?不是直接先送命令字嗎?
還有送命令字后送的地址
AT45WriteByte((uchar)(page>>7));
AT45WriteByte((uchar)(page<<1));
AT45WriteByte((uchar)(page));
是什么意思?

9樓: >>參與討論
winloop
我的正在用,效果非常好
/******************************************************************************/
/*LOGIC transfer protocol:                                                    */
/*    A valid instruction starts with the falling edge of CS followed by the  */
/*    appropriate 8-bit opcode and the desired buffer or main MEMORY address  */
/*    location.*/
/*Physical transfer protocol:                                                 */
/*    All instructions,addresses and data are transferred with the most signi-*/
/*    ficant bit(MSB) first.                                                  */
/******************************************************************************/
//Data is always clocked out of the DEVICE on the falling edge of SCK.
unsigned CHAR SPI_HostReadByte(void){
    unsigned CHAR i,rByte=0;
    
    for(i=0;i<8;i++){
        SPI_SCK=0;
        SPI_SCK=1;
        
        rByte<<=1;
        if(SPI_SO){rByte+=1;}
    }
    return rByte;    
}

//Data is always clocked into the DEVICE on the rising edge of SCK.
void SPI_HostWriteByte(unsigned CHAR wByte){
    unsigned CHAR i;
    
    for(i=0;i<8;i++){
        if((wByte<<i)&0x80){CPLD_SI=1;}
        else{CPLD_SI=0;}
        
        SPI_SCK=0;
        SPI_SCK=1;
    }    
}
//Polling until the DEVICE is ready or reached the threshold of busy time counter.
//Ready return 1 and
unsigned CHAR AT45DB041B_StatusRegisterRead(void){
    unsigned CHAR i;
    
    CPLD_RST_N=0;
    CPLD_RST_N=1;
            
    CPLD_CS_N=0;    
    SPI_HostWriteByte(0xd7);
    i=SPI_HostReadByte();
    CPLD_CS_N=1;
    
    return i;    
}
/******************************************************************************/
/*Allows to read data directly from any one of the 2048 pages in the main mem-*/
/*ory.Bypassing both of the data buffers and leaving the contents of the buff-*/
/*ers unchanged.The opcode is 52H or D2H.When the end of a page in main memor-*/
/*y is reached during a Main MEMORY Page Read,the DEVICE will continue readin-*/
/*g at the beginning of the same page.                                        */
/*                                                                            */
/*Parameter escribe:                                                          */
/*    page:page address,range of 0 to 2047,11 bits valid                      */
/*    addr:starting byte address within the page,9 bits valid                 */
/*     len:indicates how many bytes to be read,range of 1~264                 */
/*    pHeader:starting address within the mcu MEMORY to store data            */
/******************************************************************************/
void AT45DB041B_MainMEMORYPageRead(UINT PA,UINT BA,UINT len,unsigned CHAR xdata *pHeader){
    unsigned int i=0;      
    

    while(++i<1000){    
        if(AT45DB041B_StatusRegisterRead()&0x80){break;}
    }
        
    CPLD_CS_N=0;
    SPI_HostWriteByte(0xd2);    
    SPI_HostWriteByte((unsigned CHAR)(PA>>7));    
    SPI_HostWriteByte((unsigned CHAR)((PA<<1)|(BA>>8)));
    SPI_HostWriteByte((unsigned CHAR)BA);
    for(i=0;i<4;i++){SPI_HostWriteByte(0x00);}
    
    for(i=0;i<len;i++){pHeader[i]=SPI_HostReadByte();}
    CPLD_CS_N=1;
}
//When the last bit in the main MEMORY array has been read,the DEVICE will con-
//tinue reading BACK at the beginning of the first page of MEMORY.As with cros-
//sing over page boundaries,no delays will be incurred when wrapping around fr-
//om the end of the array to the beginning of the array
void AT45DB041B_ContinuousArrayRead(UINT PA,UINT BA,UINT len,unsigned CHAR *pHeader){
    unsigned int i=0;      
    
    while(++i<1000){    
        if(AT45DB041B_StatusRegisterRead()&0x80){break;}
    }
    
    CPLD_CS_N=0;    
    SPI_HostWriteByte(0xe8);    
    SPI_HostWriteByte((unsigned CHAR)(PA>>7));  &nb
10樓: >>參與討論
gciessence
可以自動翻頁嗎?
我沒有用指針來存儲數(shù)據(jù),而是定義了兩個緩沖器
extern unsigned CHAR xdata flash_rd_buff[264];
extern unsigned CHAR xdata flash_wr_buff[264];
但是寫緩存的話如果不是從零字節(jié)開始寫,而且寫入不止264字節(jié),
要不要再加上翻頁函數(shù)?

11樓: >>參與討論
wjc3k
idx=0第一個內(nèi)部BUF,=1第二個內(nèi)部BUF
內(nèi)部有兩個BUF,idx表示是哪個buf.
page表示頁號.

12樓: >>參與討論
wjc3k
定義

sbit FLASH_CS=P1^7;
sbit FLASH_SCK=P1^6;
sbit FLASH_SI=P2^3;
sbit FLASH_SO=P2^4;

bit AT45D_OK=0;


13樓: >>參與討論
caosongyz
我也問一個問題
是不是它是能是頁寫呀


14樓: >>參與討論
wjc3k
yeah
可以寫B(tài)UF,可以直接寫FLASH,也可以從BUF到FLASH,
寫的時候我一般是先寫B(tài)UF,然后從BUF寫到FLASH。改寫的時候從FLASH讀到BUF,修改BUF,再寫回FLASH。相當(dāng)于E2PROM了。這個DATAFLASH真是好東西。

因為用了一個字節(jié)的len,所以只能讀256個字節(jié),浪費了(264-256)個字節(jié),不過我不在乎這幾個字節(jié)。hoho.

參與討論
昵稱:
討論內(nèi)容:
 
 
相關(guān)帖子
為什么我的LCD可點亮,但就是不能顯示字符?
請教:打印機墨盒永久芯片
ISP下載電路圖
問:單片機軟件防干擾除了用狗外,還有其他簡單好用的方法嗎?
C51可不可以直接操作一個字的高字節(jié)或低字節(jié)
免費注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點此進入


Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號