x5045 c程序
//X5045.H
#ifndef X5045_h
#define X5045_h
#define WDT1_4? 0x00;
#define WDT600? 0x10;
#define WDT200? 0x20;
#define WDTDIS? 0x30;
//BIT
sbit B_CS5045=P2^0;
sbit B_SO5045=P2^1;
sbit B_SCLK5045=P2^3;
sbit B_SI5045=P2^2;
extern unsigned char data tcX5045;
void RstWDT(void);
void WrB5045(unsigned char,unsigned char) reentrant;????? //寫入單字節(jié)數(shù)據(jù)
unsigned char RdB5045(unsigned char);?????????? //讀出單字節(jié)數(shù)據(jù)
void byteout(unsigned char);??????????????????? //向5045輸出單字節(jié)
void wip_chk(void);???????????????????????????? //等待5045結(jié)束內(nèi)部寫周期
unsigned char rdsr(void);?????????????????????? //5045狀態(tài)寄存器讀出程序
unsigned char bytein(void);???????????????????? //從5045讀出一字節(jié)數(shù)據(jù)
unsigned char readx(void);????????????????????? //讀地址,地址位于100h
void wrsr(unsigned char);?????????????????????? //5045狀態(tài)寄存器寫入程序
void wren(void);??????????????????????????????? //寫允許
void wrdi(void);??????????????????????????????? //寫禁止
#endif
/*
X5045/3 讀寫驅(qū)動
*/
#include
#include "X5045.h"
//復位看門狗
void RstWDT()
{
??? B_CS5045=0;
??? B_CS5045=1;
}
//向X5045寫入單字節(jié)數(shù)據(jù)
//輸入:x,待寫數(shù)據(jù),address,寫入數(shù)據(jù)的地址
void WrB5045(unsigned char x,unsigned char address) reentrant //數(shù)據(jù),地址
{
??????? wren();
??????? B_SCLK5045=0;
??????? B_CS5045=0;
??????? byteout(0x2); //write
??????? byteout(address);
??????? byteout(x);
??????? B_SCLK5045=0;
??????? B_CS5045=1;
??????? wip_chk();
??????? wrdi();
}
//從X5045讀入單字節(jié)數(shù)據(jù)
//輸入:address,待讀數(shù)據(jù)地址
//返回:讀出的數(shù)據(jù)
unsigned char RdB5045(unsigned char address)
{
??????? unsigned char data x;
??????? B_SCLK5045=0;
??????? B_CS5045=0;
??????? byteout(0x3);? //read 000h
??????? byteout(address);
??????? x=bytein();
??????? B_SCLK5045=0;
??????? B_CS5045=1;
??????? return (x);
}
//向X5045輸出單字節(jié)
void byteout(unsigned char x)
{
??? unsigned char data i,j;
??? j=128;
??? for(i=0;i<8;i++)
??? {
??????? B_SCLK5045=0;
??????? B_SI5045=x/j;
??????? B_SCLK5045=1;
??????? x%=j;
??????? j/=2;
??? }
??? B_SI5045=0;
}
//等待X5045結(jié)束內(nèi)部寫周期
void wip_chk()
{
??? unsigned char data dat;
??? tcX5045=0;
?do
??? {
??????? dat=rdsr();
??????? dat&=0x1;
??????? if(tcX5045>=5) break;
??? }
??? while(dat);
}
//5045狀態(tài)寄存器讀出程序
unsigned char rdsr()
{
??? unsigned char data dat;
?B_SCLK5045=0;
??? B_CS5045=0;
??? byteout(5);
?dat=bytein();
?B_SCLK5045=0;
?B_CS5045=1;
?return (dat);
}
//從X5045讀出一字節(jié)數(shù)據(jù)
unsigned char bytein()
{
??? unsigned char data i,x;
??? x=0;
??? for (i=0;i<8;i++)
??? {
??????? B_SO5045=1;
??????? B_SCLK5045=1;
??????? B_SCLK5045=0;
??????? if (B_SO5045) x=x+x+1;
??????? else x<<=1;
??? }
??? return(x);
}
//5045狀態(tài)寄存器寫入程序
void wrsr(unsigned char x)
{
??? wren();
?B_SCLK5045=0;
?B_CS5045=0;
?byteout(1);?? //wrsr
?byteout(x);
?B_SCLK5045=0;
?B_CS5045=1;
?wip_chk();
??? wrdi();
}
//寫允許
void wren()
{
?B_SCLK5045=0;
?B_CS5045=0;
?byteout(6);
?B_SCLK5045=0;
?B_CS5045=1;
}
//寫禁止
void wrdi()
{
?B_SCLK5045=0;
?B_CS5045=0;
??? byteout(4);
??? B_SCLK5045=0;
?B_CS5045=1;
}
x5045c驅(qū)動程序
//*******************X5045 DRIVE**************************
?//X5045.H
?//#include?
?//#include?
?#include?
?#define??byte? ?unsigned char
?#define??N_NOP?_nop_();//_nop_();_nop_();_nop_();_nop_();
?//////////////////////////////////////////////////////////////////
?//x5045
??//////////////////////////////////////////////////////////////////
?//DEFINE
?#define??WRSR_INIT?0X01?//寫狀態(tài)寄存器指令;
?#define??WRITE_INIT?0X02?//寫數(shù)據(jù)指令;
?#define??READ_INIT?0X03?//讀數(shù)據(jù)指令;
?#define??WRDI_INIT?0X04?//禁止寫操作指令;
?#define??RDSR_INIT?0X05?//讀狀態(tài)寄存器指令;
?#define??WREN_INIT?0X06?//允許寫操作指令;
?#define??WAIT_TIMES?200??//等待時間常數(shù);
?sbit??SO? ? ?=P2^4;
?sbit??SI? ? ?=P2^5;
?sbit??SCK? ? ?=P2^6;
?sbit??CS? ? ?=P2^7;
?/////////////////////////////////////////////////////////////////
?//函數(shù)聲明;PROPERTY
?void ?wren_cmd(void);???//允許寫操作指令函數(shù);
?void ?write(byte dt);???//寫一個字節(jié)數(shù)據(jù)函數(shù);
?byte ?read(void);????//讀一個字節(jié)數(shù)據(jù)函數(shù);
?byte ?read_state(void);??//讀狀態(tài)控制字函數(shù);
?void ?write_state(byte dt);?//寫狀態(tài)控制字函數(shù);
?void ?read_nbyte(?bit bA8,?//讀n個字節(jié)數(shù)據(jù)函數(shù);
??????byte adr_L,
??????byte n,
??????byte *adr);
?void ?write_nbyte(bit bA8,?//寫n個字節(jié)數(shù)據(jù)函數(shù);
??????byte adr_L,
??????byte n,
??????byte *adr);
?void ?wait_end(byte run_times);//等待函數(shù);
?/////////////////////////////////////////////////////////////////
?//函數(shù)定義;
?//FOUNCTION
?//enable before write data to x5045;
?void wren_cmd(void)
?{//向X5045寫入允許寫操作指令字;
?
??CS=1;
??SCK=0;
??CS=0;
??write(WREN_INIT);
??CS=1;
??SCK=0;
?}
?//make clk for send data
?void write(byte dt)
?{//向X5045寫入一個字節(jié)數(shù)據(jù)
??register?byte?i;
??for(i=0;i<8;i++)
??{??
???SCK=0;
???????? ?//N_NOP;?????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????
?????????????????????
???SI=(bit)(dt & 0x80);//_|-
???SCK=1;
???????? ?//N_NOP;?????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????
?????????????????????
???? dt=dt<<1;
??}
??SI=0;
?}
?//make clk for receive data
?byte read(void)
?{//從X5045讀一個字節(jié)數(shù)據(jù);
??register?byte?i;
??register byte dt=0;
??SO=1;
??for(i=0;i<8;i++)
??{
???SCK=1;
???????? ?//N_NOP;?????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????
?????????????????????
???SCK=0;
???????? ?//N_NOP;?????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????
?????????????????????
???dt=dt<<1;
???if(SO)dt++;
??}
??return dt;
?}
?//---------------------------------------
?//X5045狀態(tài)控制字:??????|
?//BIT:?7?6?5?4?3?2?1?0?
|
?//????? x?? x?? WD1 WD0 BL1 BL0 WEL WIP |
?//WIP:0---正在寫操作;?????|
?//?? 1---寫操作完成;?????|
?//WEL:0---寫使能鎖存器復位;????|
?//??? 1---寫使能鎖存器置位;????|
?//BL1 BL0:????????
|
?//?? 00--不保護;??????|
?//?? 01--保護:01H--180H--1FFH;???|
?//??? 10--保護:100H--1FFH;????|
?//??? 11--保護:000H--1FFH;????|
?//WD1 WD0:????????
|
?//?? 00--1.4秒;??????|
?//?? 01--600毫秒;??????|
?//??? 10--200毫秒;??????|
?//??? 11--禁止看門狗;?????|?
?//---------------------------------------
?//read state word from x5045;
?byte read_state(void)
?{//從X5045讀狀態(tài)控制字;
??register?byte dt;
??wren_cmd();
??CS=1;
??SCK=0;
??CS=0;
??write(RDSR_INIT);
??dt=read();
??SCK=0;
??CS=1;
??return dt;
?}
?//write state word to x5045 before do somthing;
?void write_state(byte dt)
?{//向X5045寫狀態(tài)控制字;
??wren_cmd();
??CS=1;
??SCK=0;
??CS=0;
??write(WRSR_INIT);
??write(dt);
??SCK=0;
??CS=1;
??wait_end(WAIT_TIMES);
?}
?//read n bytes from x5045;
?void read_nbyte(bit bA8,//選擇EPROM上半部分/下半部分
??????????????????????? byte adr_L,//從EPROM中什么地址開始讀;
??????????????????????? byte n,?//要讀的字節(jié)數(shù);
??????????????????????? byte *adr)//所讀的數(shù)據(jù)從什么地址開始存放;
?{//從X5045讀n個字節(jié)數(shù)據(jù);
??register byte? i ;
??
??CS=1;
??SCK=0;
??CS=0;
??write(bA8?READ_INIT | 0x08 : READ_INIT);
??write(adr_L);
??for(i=0;i
???*adr=read();
???adr++;
??}
??SCK=0;
??CS=1;
?}
?//write n bytes to x5045;
?void write_nbyte(bit bA8,//選擇EPROM上半部分/下半部分;
???????????????????????? byte adr_L,//從EPROM中什么地址開始寫;
???????????????????????? byte n,//要寫的字節(jié)數(shù);
???????????????????????? byte *adr)//從什么地址開始取數(shù)據(jù)寫;
?{//向X5045寫n個字節(jié)數(shù)據(jù);
??register byte? i ;
??EA=0;
??wren_cmd();
??CS=1;
??SCK=0;
??CS=0;
??write(bA8?WRITE_INIT | 0x08 : WRITE_INIT);?
??write(adr_L);
??for(i=0;i
???write(*adr);
???adr++;
??}
??SCK=0;
??CS=1;
??wait_end(WAIT_TIMES);
??EA=1;
?}
?// wait for be sure to complete
?void wait_end(byte run_times)
?{////等待向X5045寫操作完成;
??register byte dt=0;
??while(run_times--)
??{
???dt=read_state();
???if(dt&0x01==0)break;
??}
?}
?//////////////////////////////////////////////////////////////////
評論
查看更多