169单片机与片外flash芯片SPI读写数据

[复制链接]
5058|19
 楼主| yiyanzhong 发表于 2012-4-21 16:47 | 显示全部楼层 |阅读模式
我参照书上的实例写的一个程序,如题所述,用示波器测试表明读写功能都没问题,有波形出来,但是用液晶显示出来的数据一直是255,Debug调试显示的也是0xff,检查不出哪儿错了?大伙儿给点建议
 楼主| yiyanzhong 发表于 2012-4-23 13:42 | 显示全部楼层
zhl100 发表于 2012-4-23 16:36 | 显示全部楼层
上ti官网下载个例程,对照着看看吧,你又没有程序给看

八层是你的时序不对:lol
shenmulzb1985 发表于 2012-4-23 20:27 | 显示全部楼层
楼主,很明显的你的片外flash中要写的数据一直就没有写进去的啊,从擦xie后的flash中独处的数据党然都是0xff啦
七叶一枝花 发表于 2012-4-23 21:21 | 显示全部楼层
:victory: 4# shenmulzb1985
foreverly 发表于 2012-4-23 21:37 | 显示全部楼层
:handshake 4# shenmulzb1985
 楼主| yiyanzhong 发表于 2012-4-25 15:53 | 显示全部楼层
有什么好的办法检测数据是否写到flash上么?
 楼主| yiyanzhong 发表于 2012-4-25 15:56 | 显示全部楼层
void write_byte(char byte)      //write byte
{  
  U0TXBUF=byte;
  while((IFG1&UTXIFG0)==0);
  IFG1&=~UTXIFG0;
}

char read_byte()           //read byte
{
  char byte;  
  U0TXBUF=0Xff;            //register clear
  while((IFG1&UTXIFG0)==0);
  IFG1&=~UTXIFG0;
  while((IFG1&URXIFG0)==0);
  byte=U0RXBUF;
  IFG1&=~URXIFG0;
  return(byte);   
}

void busy()                 //check busy
{
     char i;
     Enable;  
     write_byte(0x05);      //Read Status Register
     while(1)
     {
      i=read_byte();   
      i&=0x01;
      if(i==0)
      {
        Disable;
        break;
      }
     }
}

void write_enable()         //WREN    06H
{
  busy();
  Enable;
  write_byte(0x06);         //write  enable
  Disable;
  busy();
}

void write_data(char add1,char add2,char add3,char sample)
{
  write_enable();
  
  //Enable;  
  //write_byte(0x05);      //Read Status Register
  //state = read_byte();
  //Disable;
  
  Enable;
  write_byte(0x02);     //write  02H
  write_byte(add1);
  write_byte(add2);
  write_byte(add3);
  write_byte(sample);
  Disable;  
  busy();
}

char read_data(char add1,char add2,char add3)
{
  busy();
  Enable;
  write_byte(0x03);    //read data
  write_byte(add1);
  write_byte(add2);
  write_byte(add3);   
  data = read_byte();
  Disable;
  busy();
  return(data);  
}

void bulk_erase()       //chip erase
{
  write_enable();
  busy();
  Enable;
  write_byte(0xc7);
  Disable;
  busy();  
}

这是我用到的几个子程序,各位看看时序是否不对,我感觉是严格按照手册来的
shenmu2012 发表于 2012-4-25 23:17 | 显示全部楼层
敢问楼主,你这是用单片机自身的SPI总线么?若是的话,可不可以把你初始化SPI总线的初始化部分的代码贴出来的啊
firstblood 发表于 2012-4-26 16:25 | 显示全部楼层
楼主在线仿真啊,你通过单片机往flash中写一个字节0xaa,然后再通过单片机读出来,一看就知道写没写成功的啊
快乐出发 发表于 2012-4-26 16:40 | 显示全部楼层
molagefei 发表于 2012-4-26 16:50 | 显示全部楼层
 楼主| yiyanzhong 发表于 2012-4-26 21:36 | 显示全部楼层
#include<msp430x16x.h>
#define spi_cs BIT7
#define Enable P2OUT &=~spi_cs
#define Disable P2OUT |=spi_cs

char data,state;

void int_spi()
{  
  
  P2DIR|=spi_cs;            //p2.7  spi_cs
  U0CTL|=CHAR+SYNC+MM+SWRST;//8-bit data,spi mode,USART is master
  U0TCTL|=SSEL1+SSEL0+STC;  //3 pin AND SMCLK AS SPI CLOCK   
  U0TCTL|=CKPH;
  U0TCTL&=~CKPL;            //CKPL CKPH:01 rising Write data
  U0BR0=0X02;
  U0BR1=0X00;
  U0MCTL=0X00;              //Baud Rate set
  ME1 |= USPIE0;              //enable SPI
  U0CTL &= ~SWRST;            //reset disable
  IE1 &= ~UTXIE0;             //RX interrupt enable
  IE1 &= ~URXIE0;             //TX interrupt enable
  P3SEL |= 0x0E;              //p3.1 SIMO,p3.2 SOMI,p3.3 clk
  P3DIR |= BIT1 + BIT3;       //p3.1  p3.3 output
  _EINT();
}

这个是初始化部分,这部分一般不会错吧
 楼主| yiyanzhong 发表于 2012-4-26 21:38 | 显示全部楼层
楼主在线仿真啊,你通过单片机往flash中写一个字节0xaa,然后再通过单片机读出来,一看就知道写没写成功的啊
firstblood 发表于 2012-4-26 16:25

测试的时候不能随便写入一个数么?非要写入0xaa?
 楼主| yiyanzhong 发表于 2012-4-26 21:47 | 显示全部楼层
刚试了下,写入0xaa,还是输出0xff
 楼主| yiyanzhong 发表于 2012-4-26 21:53 | 显示全部楼层
#include<msp430x16x.h>

#include"12864.h"
#include"spi.h"


char addr1 = 0,addr2 = 0,addr3 = 0;
char map[10] = {'0','1','2','3','4','5','6','7','8','9'};
char value;

void test()
{
  char a,b,c,d,e;
  int_spi();
  bulk_erase();
  delaynms(2000);
  
  addr1 = 0;addr2 = 0;addr3 = 0;
  write_data(addr1,addr2,addr3,0xaa);
  delaynms(50);
   
  value = read_data(addr1,addr2,addr3);
   
    a = value/10000;
    b = value%10000/1000;
    c = value%1000/100;
    d = value%100/10;
    e = value%10;
    write(0,1);
    write(0,0x80);
    write(1,map[a]);
    write(0,0x81);
    write(1,map[b]);
    write(0,0x82);
    write(1,map[c]);
    write(0,0x83);
    write(1,map[d]);
    write(0,0x84);
    write(1,map[e]);
}

顺便加上读写的主函数部分,一并感谢大家持续的关注和建议
 楼主| yiyanzhong 发表于 2012-4-27 19:54 | 显示全部楼层
求支援
jxmzzr 发表于 2012-4-29 18:10 | 显示全部楼层
楼主,很明显的你的片外flash中要写的数据一直就没有写进去的啊,从擦xie后的flash中独处的数据党然都是0xff啦

我也这么认为。
 楼主| yiyanzhong 发表于 2012-4-30 08:39 | 显示全部楼层
确实是写操作子函数的问题,现在问题解决了,我在写使能前将寄存器清零,最后再加上不可写就正常了,感谢大家的持续关注与建议,O(∩_∩)O~
dghidt 发表于 2012-5-29 15:49 | 显示全部楼层
这个东西有写入保护功能的。先把这个功能去掉,在状态寄存器里面,好象大容量的有两个这样的状态寄存器
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:谦虚谨慎

1

主题

26

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部