機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
c51寫的X25045的讀寫程序
c51寫的X25045的讀寫程序
 更新時(shí)間:2010-1-30 16:35:10  點(diǎn)擊數(shù):0
【字體: 字體顏色

/***************************************************************
   *  X25043/45 application  Procedures
   *  absolute one address access
  ***************************************************************
 WARNING: The function with '_' ahead ,user may not use it.as it
 used internal
*/
//使用函數(shù):write_status(status) 寫狀態(tài),一般寫0
//clr_wchdog(void)   清看門狗
//unsigned char read_byte(address) //讀一個(gè)字節(jié)
//void write_byte(address,Data)   //寫一個(gè)字節(jié)

#define ALONE_COMPILE
#ifdef ALONE_COMPILE
 #include "INTRINS.H"
#endif

#ifndef nop2()
#define nop2()  _nop_();_nop_();_nop_()
#endif

#define WREN    0x06
#define WRDI    0x04
#define RDSR    0x05
#define WRSR    0x01
#define READ    0x03
#define WRITE   0x02

//#define PORT P1
sfr PORT=0x90; //25045的4根io腳接在同一端口,本例為p1
   //請(qǐng)根據(jù)實(shí)際電路更改引腳定義
#define _SI 0x80  //si接在p1.3,0x80=00001000b
#define _SCK 0x40  //sck接在p1.2,0x80=00000100b
#define _SO 0x20  //so接在p1.1   
#define _CS 0x10  //cs接在p1.0

//----------------------------------------------------------------
#ifndef dword
 #define dword unsigned long
 #define word unsigned int
 #define byte unsigned char
 typedef union{
  word w;
  byte bh;
  byte bl;
 }WordType;

 typedef union{
  dword dw;
  word w[2];
  byte b[4];
 }DwordType;

#endif

//----------------------------------------------------------------
void _w_byte(Data)
 char Data;
{
 char i;
  
 PORT &= (_SCK^0xff);
 for(i=0;i<8;i++)
 {
  nop2();nop2();/////////////
  if(Data & 0x80)  PORT |= _SI;
  else PORT &= (_SI^0xff);
               nop2();nop2();/////////////
  PORT |=_SCK;
  nop2();nop2();/////////////
  Data=Data<<1;
  nop2();nop2();/////////////
  PORT &= (_SCK^0xff);
  nop2();nop2();/////////////
 }
}
//----------------------------------------------------------------
char _r_byte(void)
{
 char i;
 char result;
  
 result=0;
 for(i=0;i<8;i++)
 {
  nop2();nop2();/////////////
  PORT |=_SCK;  
  result=result<<1;
  nop2();nop2();/////////////
  if((PORT & _SO) != 0)
   result |= 0x01;
  nop2();nop2();/////////////
  PORT &=(_SCK^0xff);
  nop2();nop2();/////////////
 }
 return(result);
}
//----------------------------------------------------------------
void write_status(status)
 char status;
{
 PORT &=(_CS^0xff);
 nop2();nop2();/////////////
 _w_byte(status);
 PORT |=_CS;
 nop2();nop2();/////////////
 return;
}
//----------------------------------------------------------------
void clr_wchdog(void)
{
 PORT &= (_CS^0xff);
 PORT |=_CS;
}
//----------------------------------------------------------------
void wait_free(void)
{
 unsigned int  t;

 t=3000;
 while(--t);
}
//----------------------------------------------------------------
void write_reg(_code)
 char _code;
{
 write_status(WREN);
 PORT &= (_CS^0xff);
 nop2();nop2();/////////////
 _w_byte(WRSR);
 _w_byte(_code);
 nop2();nop2();/////////////
 PORT |=_CS;
 wait_free();
}
//----------------------------------------------------------------

unsigned char read_byte(address)
 unsigned int address;

 char result;

 PORT &=(_CS^0xff);  // Chip select   
 nop2();nop2();/////////////
 _w_byte((char)(address>255 ? (0x08|READ): READ));
 _w_byte((char)(address & 0x00ff));
 result=_r_byte();
 nop2();nop2();/////////////
 PORT |=_CS;
     // Chip unselect  
 return(result);
}
//----------------------------------------------------------------
void write_byte(address,Data)
 unsigned int address;
 char Data;
{
 write_status(WREN);
 nop2();nop2();/////////////
 PORT &= (_CS^0xff);
 nop2();nop2();/////////////
 _w_byte((unsigned char)(address>255 ? (0x08|WRITE): WRITE));
 _w_byte((unsigned char)(address & 0x00ff));
 _w_byte(Data);
 nop2();nop2();/////////////
 PORT |=_CS;
 wait_free();
 return;
}
/*
//----------------------------------------------------------------
unsigned long read_data(format,address)
 unsigned char format;
 unsigned int address;

 DwordType result;

 switch(format&0xdf)
 {
 case 'L':
  result.b[0]=read_byte(address);
  result.b[1]=read_byte(address+1);
  result.b[2]=read_byte(address+2);
  result.b[3]=read_byte(address+3);
  break;
 case 'D':
  result.b[2]=read_byte(address);
  result.b[3]=read_byte(address+1);
  break;
 case 'C':
  result.b[3]=read_byte(address);
  break;
 }
 return(result.dw);
}
//----------------------------------------------------------------
void write_data(format,address,Data)
 unsigned char format;
 unsigned int address;
 DwordType data* Data;
{
 switch(format&0xdf)
 {
 case 'L':
  write_byte(address  , Data->b[0]);
  write_byte(address+1, Data->b[1]);
  write_byte(address+2, Data->b[2]);
  write_byte(address+3, Data->b[3]);
  break;
 case 'D':
  write_byte(address  , Data->b[2]);
  write_byte(address+1, Data->b[3]);
  break;
 case 'C':
  write_byte(address  , Data->b[3]);
  break;
 }

}
//----------------------------------------------------------------
*/

  • 上一篇: 時(shí)鐘芯片ds12c887的驅(qū)動(dòng)程序
  • 下一篇: 沒有了
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1