2003年度  第2期


标题:X25045在智能仪表系统中的应用设计
作者:郑 伟
作者单位:南京师范大学
关键字:智能仪表,单片微处理器,X25045
摘要:

本文介绍了XICOR公司的X25045在基于单片微处理器的智能仪表系统中的应用设计技术,给出了与单片微处理器的硬件连接及系统软件控制流程,其使用非常方便简洁,应用广泛。

 

杂志上略去的程序清单如下:

/**********************

X25045

**********************/

// 定义常量

#define WREN    0x06    //指令码

#define WRDI    0x04

#define RDSR    0x05

#define WRSR    0x01

#define READ    0x03

#define WRITE   0x02

sfr  PORT=P1;             // 单片机P1口

#define  SI       0x02

#define  SCK     0x04    

#define  SO       0x08    

#define  CS       0x01    

//

void  w_byte(Data)       //写字节

     char Data;

{

     char i;

     PORT &= (SCK^0xff);  //复位X25045的串行时钟SCK端

     for(i=0;i<8;i++)

     {

          if(Data & 0x80)  PORT |=SI;  //置位X25045的串行输入端

         else PORT &= (SI^0xff);       //复位X25045的串行输入端

           PORT |=SCK; //产生一个串行时钟,将串行输入的一位移入X25045

          Data=Data<<1;

         PORT &= (SCK^0xff);

     }

}

char r_byte(void)   // 读字节

{

     char i;

     char result;

     result=0;

     for(i=0;i<8;i++)

     {

         PORT |=SCK;       

          result=result<<1;

          if((PORT & SO) != 0)

              result |= 0x01;

         PORT &=(SCK^0xff);

     }

     return(result);

}

void w_status(status)    // 写状态寄存器

     char status;

{

     PORT &=(CS^0xff); // 使X25045片选CS为低电平,准备“写”操作

     w_byte(status);

     PORT |=CS;       // 置X25045片选CS为高电平

     return;

}

void clr_wchdog(void)        // 清看门狗

{

     PORT &= (CS^0xff);    //在X25045片选CS端产生一个脉冲

     PORT |=CS;

}

void w_reg(_code)        //写指令寄存器

     char _code;

{

     w_status(WREN); //置位写使能寄存器(允许E2PROM写)

     PORT &= (CS^0xff);

     w_byte(WRSR);

     w_byte(_code);

     PORT |=CS;

     wait();       //延迟

}

unsigned char read_byte(address)  //读地址单元的数据

     unsigned int address;

{   

     char result;

 

     PORT &=(CS^0xff);       // 片选X25045       

     w_byte((char)(address>255 ? (0x08|READ): READ));

     w_byte((char)(address & 0x00ff));

     result=r_byte();

     PORT |=CS;

     return(result);

}

void wait(void)

{

     unsigned int i;

     i=3000;

     while(--i);

}

void write_byte(address,Data)  //向指定的单元写数据

     unsigned int address;

     char Data;

{

     w_status(WREN);       //置位写使能寄存器(允许E2PROM写)

     PORT &= (CS^0xff);       // 片选X25045 低电平  

     w_byte((unsigned char)(address>255 ? (0x08|WRITE): WRITE));

     w_byte((unsigned char)(address & 0x00ff));

     w_byte(Data);            // 写入数据

     PORT |=CS;

     wait();

     return;

}