uint8 Mf500Read( uint8 reg )
{
uint32 BitMask, AddressMask, Data;
uint8 val, i;
Data = 0;
BitMask = 0;
AddressMask = 0;
for( i = 0; i < sizeof( BusAdjust ); i ++ )
{
if( reg & 1 << i )
AddressMask |= 1 << BusAdjust[i];
BitMask |= 1 << BusAdjust[i];
}
IO1DIR = IO1DIR | BitMask;
IO1CLR = Mf500AlePin;
IO0SET = Mf500RdPin | Mf500WrPin;
IO1SET = Mf500CsPin;
BitMask = ~ BitMask;
IO1PIN = ( IO1PIN & BitMask ) | AddressMask;
IO1SET = Mf500AlePin;
BitMask = ~ BitMask;
IO1CLR = Mf500AlePin;
IO1SET = BitMask;
BitMask = ~ BitMask;
IO1DIR = IO1DIR & BitMask;
IO1CLR = Mf500CsPin;
IO0CLR = Mf500RdPin;
i = 25;
while( -- i );
Data = IO1PIN;
IO0SET = Mf500RdPin;
IO1SET = Mf500CsPin;
val = 0;
for( i = 0; i < sizeof( BusAdjust ); i ++ )
{
if( Data & 1 << BusAdjust[i] )
val |= 1 << i;
}
return val;
}
以上程序为ARM7的,程序不是按位操作的,但是与时序图可以吻合。
FM1702的则为SPI口操作的,和我的设置有些不一样,所以就不贴了。
我用的是飞思卡尔8位机9S08,读寄存器的代码如下
unsigned char RC500Read(unsigned char Addr)
{
unsigned char AddrL = 0; //低八位地址
unsigned char AddrH = 0; //高八位地址
unsigned char Date = 0;
AddrL = Addr & 0x0f;
AddrH = (Addr & 0xf0)>>4;
PTBDD = 1; //AD0-AD3,Output
PTDDD = 1; //AD4-AD7,OUTput
RC500_ALE = 0;
RC500_CS = 1;
RC500_RD = 1;
RC500_WR = 1;
RC500_ADL = AddrL; //给地址
RC500_ADH = AddrH;
delay_50us (1); //延时50uS
RC500_ALE = 1; //地址锁存拉高,实则为6位
delay_50us (1); //延时50uS
RC500_ALE = 0; //地址锁存释放,锁存完成
delay_50us (1); //延时50uS,保持地址
PTBDD = 0; //AD0-AD3,input
PTDDD = 0; //AD4-AD7,input
RC500_CS = 0; //片选拉低
RC500_RD = 0; //读信号
delay_50us (1);
Date = (RC500_ADH << 4)|RC500_ADL; //读入
RC500_CS = 1; //片选拉低
RC500_RD = 1; //读信号
return Date;
}
由于IO的原因,我选择了两个IO口,每个IO口都是4位,所以拼起来做了一个8位的口。程序基本参考了imzhangx的。
写寄存器的代码
void RC500Write(unsigned char Addr,unsigned char Date)
{
unsigned char AddrL = 0; //低八位地址
unsigned char AddrH = 0; //高八位地址
unsigned char DateL = 0; //低八位数据
unsigned char DateH = 0; //高八位数据
AddrL = Addr & 0x0f;
AddrH = (Addr & 0xf0)>>4;
DateL = Date & 0x0f;
DateH = (Date & 0xf0)>>4;
PTBDD = 1; //AD0-AD3,Output
PTDDD = 1; //AD4-AD7,OUTput
RC500_ALE = 0;
RC500_CS = 1;
RC500_RD = 1;
RC500_WR = 1;
RC500_ADL = AddrL; //给地址
RC500_ADH = AddrH;
delay_50us (1); //延时50uS
RC500_ALE = 1; //地址锁存拉高,实则为6位
delay_50us (1); //延时50uS
RC500_ALE = 0; //地址锁存释放,锁存完成
delay_50us (1); //延时50uS,保持地址
RC500_CS = 0;
RC500_ADL = DateL; //给数据
RC500_ADH = DateH;
RC500_WR = 0;
delay_50us (1); //等待写入
RC500_CS = 1;
RC500_WR = 1; //写入完成
}
|