yelvAVR 发表于 2012-5-23 15:52:30

c8051模拟spi 调试at45db321d

本帖最后由 yelvAVR 于 2012-5-23 15:53 编辑

用的是c8051f040单片机! 和spi 总线的dataflash
读不到id和寄存器状态!全部都是FF!谢谢大家帮忙,等调试通过了奉献出来!~
下面是硬件接口!
P11->SO
P12->SI
P13->SCK
P14->CS
P15->WP
P16->RST
P17->RDY/BUSY

初始化io程序
void InitPortIO( )
{
                  
    SFRPAGE = 0x0F;
//   P0MDOUT = 0x01; // Output configuration for P0
    P1MDOUT = 0xff; // Output configuration for P1
//   P2MDOUT = 0x15; // Output configuration for P2
//   P3MDOUT = 0x00; // Output configuration for P3,P3.6 and P3.7 used for RTC communication
//   P4MDOUT = 0x15; // Output configuration for P4
//   P5MDOUT = 0xAA; // Output configuration for P5
    P6MDOUT = 0x0f; // Output configuration for P6
//    P7MDOUT = 0x2A; // Output configuration for P7

    P1MDIN |= 0x82;// Input configuration for P1
   // P2MDIN = 0xFF;// Input configuration for P2
//P3MDIN = 0xff;// Input configuration for P3

    P0 = 0xff;
    P1 = 0xff;
    P2 = 0xff;
    P3 = 0xff;
    P4 = 0xff;
    P5 = 0xff;
    P6 = 0x00;
    P7 = 0xff;

    SFRPAGE = 0x0F;
//        XBR0 = 0x34;        // XBAR0: Initial Reset Value
//        XBR0 = 0x0e;
        XBR1 = 0x00;        // XBAR1: Initial Reset Value
//        XBR2 = 0x42;        // XBAR2: Initial Reset Value
//    XBR3 = 0x80;    // XBAR3: Configure CAN TX pin (CTX) as push-pull digital output

    SFRPAGE = 0x0F;
    P0MDOUT = 0x01; // Output configuration for P0
    P1MDOUT = 0x7d; // Output configuration for P1
    P2MDOUT = 0x00; // Output configuration for P2
    P3MDOUT = 0x00; // Output configuration for P3
    P4MDOUT = 0x00; // Output configuration for P4
    P5MDOUT = 0xA8; // Output configuration for P5
    P6MDOUT |= 0x0F; // Output configuration for P6
    P7MDOUT = 0x2A; // Output configuration for P7

    SFRPAGE = 0x00;
    EMI0CF = 0x03;    // External Memory Configuration Register
}

下面是初始化内部振荡器

void InitOSC( )
{
        xdata unsigned int n = 0;
        EA = 0;
    SFRPAGE = CONFIG_PAGE;
                  // 0x0f
//        OSCXCN = 0x67;        // EXTERNAL Oscillator Control Register       
                                        // Use external Crystal Oscillator
                                        // Use OSC as system clock
                  // 11.092MHz
    for (n = 0; n < 2550; n++);
                  // wait for osc to start
   // while ( (OSCXCN & 0x80) == 0 );      // wait for xtal to stabilize

        OSCICN=0x82;
    CLKSEL = 0x00;// 内部晶振

//   CLKSEL = 0x01;// select external crystal as sysclk
                  // sysclk = 18.432MHz

    n = CLKSEL;
//        OSCICN = 0x80;        // Internal Oscillator Control Register
                                        // internal clk freqent = 24.5MHz/8 = 3.1MHz;
//        EA = 1;
}

下面是驱动SPI的几个开始的函数

/*********************************************************************************
* 函数原型:unsigned char SPI_MCUReadByte(void);                                       
* 名   称:SPI_MCUReadByte
* 功   能:从AT45DB161B通过AT_SO口读入1BYTE数据
* 入口参数:无
* 出口参数:返回读入的1BYTE数据
**********************************************************************************/
unsigned char SPI_MCUReadByte(void)        //读
{
      unsigned char n;
                unsigned int rByte=0;
        //        unsigned char SFRPAGE_SAVE = SFRPAGE;
          //SFRPAGE = CONFIG_PAGE;
                AT_CS=0;
      for(n=0;n<8;n++)
      {       AT_SCK=0;
                        delayus(5);
                AT_SCK=1;
                                delayus(5);
                rByte<<=1;
                                rByte|=AT_SO;
                               
      }
//SFRPAGE = SFRPAGE_SAVE;
      AT_CS=1;
      return rByte;
}
/*********************************************************************************
* 函数原型:void SPI_MCUWriteByte(unsigned char wByte);                                       
* 名   称:SPI_MCUWriteByte
* 功   能:将1字节数据wByte由AT_SI口写入AT45DB161B
* 入口参数:要写入的1BYTE数据
* 出口参数:无
**********************************************************************************/
void SPI_MCUWriteByte(unsigned char rByte)        //写
{
    unsigned char m;
   // unsigned char SFRPAGE_SAVE = SFRPAGE;
        //SFRPAGE = CONFIG_PAGE;
                AT_CS=0;
                AT_SCK=0;
      for(m=0;m<8;m++)
      {    if(rByte&0x80)
                     {
                           AT_SI=1;
                                }
                else{AT_SI=0;}
                AT_SCK=0;
                                delayus(5);
                AT_SCK=1;
                                delayus(5);
                          rByte<<=1;
       }
           AT_CS=1;
//        SFRPAGE = SFRPAGE_SAVE;
}
unsigned char SPI_FLASH_ReadID()          //读取id
{
unsigned char ID;
unsigned char SFRPAGE_SAVE = SFRPAGE;
        SFRPAGE = CONFIG_PAGE;
AT_CS = 0;//片选打开
delayus(5);
   
SPI_MCUWriteByte(0X9F); //9FH是读取状态的命令字

ID = SPI_MCUReadByte(); //读取状态字节

AT_CS = 1; //片选关闭
delayms(5);

return ID;
SFRPAGE = SFRPAGE_SAVE;   
}
/*********************************************************************************
* 函数原型:unsigned char DF_status(void);                                       
* 名   称:DF_status
* 功   能:由AT_SI口读AT45DB161B的状态字
* 入口参数:无
* 出口参数:返回状态字
**********************************************************************************/
/*Status Register Format:                                 */
/*   ----------------------------------------------------------------------- */
/* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
/* |--------|--------|--------|--------|--------|--------|--------|--------| */
/* |RDY/BUSY| COMP |   0   |   1   |   1   |   1   |   X   |   X   | */
/*   ----------------------------------------------------------------------- */
/* bit7 - 忙标记,0为忙1为不忙。                               */
/*       当DF_status的位0移出之后,接下来的时钟脉冲序列将使SPI器件继续*/
/*       将最新的状态字节送出。                               */
/* bit6 - 标记最近一次Main Memory Page和Buffer的比较结果,0相同,1不同。   */
/* bit5                                             */
/* bit4                                             */
/* bit3                                             */
/* bit2 - 这4位用来标记器件密度,对于AT45DB161B,这4位应该是0111,一共能标记 */
/*       16种不同密度的器件。                               */
/* bit1                                             */
/* bit0 - 这2位暂时无效                                     */
/******************************************************************************/
unsigned char DF_status()          //读取状态字
{
unsigned char status;
unsigned char SFRPAGE_SAVE = SFRPAGE;
        SFRPAGE = CONFIG_PAGE;
AT_CS = 0;//片选打开
delayus(5);
   
SPI_MCUWriteByte(READ_STATE_REGISTER); //D7H是读取状态的命令字

status = SPI_MCUReadByte(); //读取状态字节

AT_CS = 1; //片选关闭
delayms(5);

return status;
SFRPAGE = SFRPAGE_SAVE;   
}

yelvAVR 发表于 2012-5-23 16:00:43

请教论坛内的高手!~谢谢帮忙!
页: [1]
查看完整版本: c8051模拟spi 调试at45db321d