导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→C51编程:急切求助93C46的读写!!!![tiger420]

 *第5396篇: C51编程:急切求助93C46的读写!!!!

  
楼 主:tiger420 2002年10月10日17:40
 C51编程:急切求助93C46的读写!!!!
有谁用过AT93C46啊,我的程序为什么总不能正确读写。请各位大虾指点小弟。万分感激。
我的邮件tiger420@163.com
(我的读写函数如下)
// Write enable must precede all programming modes.
void Ewen(void) {
    unsigned char temp,InData;
    CS=0;    
    SK=0;
    CS=1;
    InData=0x98;    // 10011XXXX
    for(temp=9;temp!=0;temp--) {    // 9
        DI=InData&0x80;
        SK=1;    SK=0;
        InData<<=1;
    }
    CS=0;
}

// Disables all programming instructions.
void Ewds(void) {
    unsigned char temp,InData;
    CS=0;
    SK=0;
    CS=1;
    InData=0x80;    // 10000XXXX
    for(temp=9;temp!=0;temp--) {    // 9
        DI=InData&0x80;
        SK=1;    SK=0;
        InData<<=1;
    }
    CS=0;
}

// Reads data stored in memory, at specified address.
unsigned int Read_data(unsigned char address) {
    unsigned char temp;
    unsigned int result;
    Ewen();
    SK=0;    DI=1;    // 110 A5-A0
    CS=0;    CS=1;
    SK=1;    SK=0;            // 
    address=address&0x3f|0x80;
    for(temp=8;temp!=0;temp--) {    // 8
        DI=address&0x80;
        SK=1;    SK=0;
        address<<=1;
    }
    DO=1;
    for(temp=16;temp!=0;temp--) {    // 16
        SK=1;
        result=(result<<1)|DO;
        SK=0;
    }
    CS=0;
    Ewds();
    return(result);
}

// Write_datas memory location An - A0.
void Write_data(unsigned char address,unsigned int InData) {
    unsigned char temp;
    Ewen();
    SK=0;    DI=1;    // 101 A5-A0
    CS=0;    CS=1;
    SK=1;    SK=0;            // 1
    address=address&0x3f|0x40;
    for(temp=8;temp!=0;temp--) {    // 8
        DI=address&0x80;
        SK=1;    SK=0;
        address<<=1;
    }
    for(temp=16;temp!=0;temp--) {    // 16
        DI=InData&0x8000;
        SK=1;    SK=0;
        InData<<=1;
    }
    CS=0;    DO=1;
    CS=1;    SK=1;
    while(DO==0) {            // busy test
        SK=0;    SK=1;
    }
    SK=0;    CS=0;
    Ewds();
}

unsigned char ReadChar(unsigned char address) {
    unsigned char temp=address>>1;
    if(address&0x01) return((unsigned char)(Read_data(temp)>>8));
    else return((unsigned char)(Read_data(temp)));
}

void WriteChar(unsigned char address,unsigned char InData) {
    unsigned char temp=address>>1;
    if(address&0x01) Write_data(temp,(unsigned int)(Read_data(temp)
&0x00ff|(InData<<8)));
    else Write_data(temp,(unsigned int)(Read_data(temp)&0xff00|InData));
}

  
2楼:pan-x 2002年10月10日20:33
 建议
DI=address&0x80;
result=(result<<1)|DO;
是否定义有问题?DI、DO是位变量,表达式为unsigned char,改为:
if(address&0x80==0x80)
    {di=1;}
else  di=0;
然后,进行循环,试一下,如何?  
  
3楼:lingl2 2002年10月11日09:58
 你的程序没问题,我已试过,查查你的硬件 
  
4楼:tiger420 2002年10月13日12:30
 我的操作
我原本的程序:
        XBYTE[0x304c]=0x25;     //port 1 drop time slot
        XBYTE[0x307c]=0x45;     //port 2 drop time slot
        XBYTE[0x30ac]=0x65;     //port 3 drop time slot
        XBYTE[0x30dc]=0x29;     //port 4 drop time slot

        XBYTE[0x304d]=0x25;     //port 1 add time slot
        XBYTE[0x307d]=0x45;     //port 2 add time slot
        XBYTE[0x30ad]=0x65;     //port 3 add time slot
        XBYTE[0x30dd]=0x29;     //port 4 add time slot

这样能看到我外围应用片子正常工作,但该为:
       WriteChar(0x00,0x25) ;
       WriteChar(0x01,0x45) ;
       WriteChar(0x02,0x65) ;
       WriteChar(0x03,0x29) ;
       WriteChar(0x04,0x25) ;
       WriteChar(0x05,0x45) ;
       WriteChar(0x06,0x65) ;
       WriteChar(0x07,0x29) ;
        XBYTE[0x304c]=ReadChar(0x00) ;     //port 1 drop time slot
        XBYTE[0x307c]=ReadChar(0x01) ;     //port 2 drop time slot
        XBYTE[0x30ac]=ReadChar(0x02) ;     //port 3 drop time slot
        XBYTE[0x30dc]=ReadChar(0x03) ;     //port 4 drop time slot

        XBYTE[0x304d]=ReadChar(0x04) ;     //port 1 add time slot
        XBYTE[0x307d]=ReadChar(0x05) ;     //port 2 add time slot
        XBYTE[0x30ad]=ReadChar(0x06) ;     //port 3 add time slot
        XBYTE[0x30dd]=ReadChar(0x07) ;     //port 4 add time slot
就不行了,硬件连接不会有问题吧,就那么几个脚。

  
5楼:lingl2 2002年10月13日15:17
 这个程序我正在用
要注意93c46 6脚的接法,6脚接地为8位数据格式,6脚接+5V为16位
数据格式。
  我有中文的资料,发给你,你看看。

  
6楼:billzyf 2002年11月7日17:37
 我修改了一下
// Write enable must precede all programming modes.
void Ewen(void) {
    unsigned char temp,InData;
    CS=0;    
    SK=0;
    CS=1;
    InData=0x98;    // 10011XXXX
    for(temp=9;temp!=0;temp--) {    // 9
        //DI=InData&0x80;
        if((InData&0x80) == 0x80)
        {
          DI=1;
        }else{
          DI=0;
        }
        InData<<=1;
        SK=1; //必要时加延时   SK=0; //必要时加延时
        //InData<<=1;
    }
    CS=0;
    
   //必要时加延时   
}
//参造Ewen
// Disables all programming instructions.
void Ewds(void) {
    unsigned char temp,InData;
    CS=0;
    SK=0;
    CS=1;
    InData=0x80;    // 10000XXXX
    for(temp=9;temp!=0;temp--) {    // 9
        DI=InData&0x80;
        SK=1;    SK=0;
        InData<<=1;
    }
    CS=0;
}

// Reads data stored in memory, at specified address.
unsigned int Read_data(unsigned char address) {
    unsigned char temp;
    unsigned int result;
    //Ewen();去掉
    SK=0;    DI=1;    // 110 A5-A0
    CS=0;    CS=1;
    SK=1;    SK=0;            // 
    address=address&0x3f|0x80;
    for(temp=8;temp!=0;temp--) {    // 8
        DI=address&0x80;
        SK=1;    SK=0;
        address<<=1;
    }
    DO=1;
    for(temp=16;temp!=0;temp--) {    // 16
        SK=1;
        result=(result<<1)|DO;
        SK=0;
    }
    CS=0;
    //去掉 Ewds();
    return(result);
}

//参造Ewen
// Write_datas memory location An - A0.
void Write_data(unsigned char address,unsigned int InData) {
    unsigned char temp;
    Ewen();
    SK=0;    DI=1;    // 101 A5-A0
    CS=0;    CS=1;
    SK=1;    SK=0;            // 1
    address=address&0x3f|0x40;
    for(temp=8;temp!=0;temp--) {    // 8
        DI=address&0x80;
        SK=1;    SK=0;
        address<<=1;
    }
    for(temp=16;temp!=0;temp--) {    // 16
        DI=InData&0x8000;
        SK=1;    SK=0;
        InData<<=1;
    }
    CS=0;    DO=1;
    CS=1;    SK=1;
    while(DO==0) {            // busy test
        SK=0;    SK=1;
    }
    SK=0;    CS=0;
    Ewds();
}

  
7楼:error 2002年11月7日17:43
 程序我没有仔细看,但提示一下你系统工作频率不同会产生不同效果!还有ATMEL的比CSI的写的速度快.如你使用CSI的要调一下等待时间!

>>>>>>对该主题发表你的看法

本主题贴数7,分页: [第1页]


[上一篇主题]:C51编程:ht1621

[下一篇主题]:C51编程:函数调用