Thông báo

Collapse
No announcement yet.

Cách giao tiếp, ghi, đọc, xóa với SPI EEPROM

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Cách giao tiếp, ghi, đọc, xóa với SPI EEPROM

    Chảo 4rum!
    Em đang loay hoay giao tiếp giữa con MSP430FE425 và con SPI EEPROM 93LC86, đọc mãi datasheet--> viết code mà vẫn chưa đựoc, không tìm thấy điểm sai đâu cả. Con 93LC86 mạch cứng cấu hình chế độ x16.
    Đây là code của e, xin mọi ng chỉ giúp:
    //Chuong trinh giao tiep USART_SPI giua msp430fe425 vs 93LC86
    //19.12.2011

    #include <msp430xe42x.h>

    unsigned char LCD_num[11]={0x5F,0x50,0x3D,0x79,0x72,0x6B,0x6F,0x51,0x7F,0x7 B,0x80};//ma LCD SEQ0280
    // 0 1 2 3 4 5 6 7 8 9 DOT'

    //ma code cua 93LC86 mode x16

    //_______funtion____SB+Opcode____A9A8_______
    #define READ_code 0x18 // 00
    #define WRITE_code 0x14 // 00
    #define ERASE_code 0x1C // 00
    #define EWEN_code 0x13 // 11
    #define EWDS_code 0x10 // 00

    unsigned char get_data[2]={0,0};
    unsigned int get_data1;
    unsigned char get_data1_high, get_data1_low;

    void CLK_init(void)
    {
    // Xtal=32768Hz
    SCFQCTL=SCFQ_4M;//0x7F--> N=1127,fDCO=(127+1)*32768=4.19Mhz
    SCFI0= FN_4;// 0x10;range 3.2-25Mhz,/1
    SCFI1=0x00;//tu dong chon va sua doi bang FLL+
    FLL_CTL0=XCAP10PF;//0x10;DCO out devided, LFXT1=low,XCAP=10PF,cac dao dong ko loi
    FLL_CTL1=FLL_DIV_1 ;//0x00;crystal input,SMCLK on,XT2 off,MCLK tu DCOCLK,SMCLK tu DCO,ACLK/1
    }

    void LCD_init(void)
    {
    LCDCTL=LCDSG0_3 + LCD4MUX+LCDON;//chon S0-->S23,4-MUX,ON
    BTCTL=BT_fCLK2_ACLK_DIV256+BT_fLCD_512+BT_fCLK2_DI V128 ;
    }

    void LCD_clear(void)
    {
    LCDM1=LCDM2=LCDM3=LCDM4=LCDM5=LCDM6=LCDM7=LCDM8=LC DM9=LCDM10=LCDM11=LCDM12=0x00;
    }
    void delay(void)
    {
    int i;
    for(i=0;i<=13000;i++);
    }

    //SPI
    //chuong trinh cai dat SPI0

    void SPI_init(void)
    {
    U0CTL=CHAR+SYNC+MM+SWRST;//8bit,SPI mode,master,reset
    U0TCTL=SSEL1+SSEL0+STC+TXEPT;//canh len cua CLK, SMCLK4M,3pin mode,TX epmty

    U0BR0=0x04;//baudrate=UCLK/U0BR=SMCLK/U0BR=4Mhz/4=1Mhz
    U0BR1=0x00;
    U0MCTL=0x00;

    ME1 |=USPIE0;
    U0CTL &=~SWRST;//disable softwear reset

    P1SEL |=BIT6+BIT7;//SIMO0, SOMI0 selected
    P1DIR |=BIT1+BIT3+BIT6;//P1.1 CS2,P1.3 SS(rf) P1.6 Master out
    P1DIR &=~BIT7;//Master in

    P2SEL |=BIT1;//P2.1 UCLK master out
    P2DIR |=BIT1;
    }
    //SPI read/write

    unsigned char SPI_rw(unsigned char data)
    {
    IFG1 &=~URXIFG0;//xoa flag RX
    U0TXBUF=data; //write
    while(!(IFG1 & URXIFG0));// && (--limit_time > 0));//doi RX flag hoac limit_time=0
    return U0RXBUF;//read
    }

    void EEPROM_ewen(void)//erase & write enable
    {
    P1OUT |=BIT1; //CS2=1
    SPI_rw(EWEN_code);//0x13
    SPI_rw(0x00);//
    P1OUT &=~BIT1;//CS2=0
    }
    void EEPROM_ewds(void)//erase & write disable
    {
    P1OUT|=BIT1;
    SPI_rw(EWDS_code);//0x10
    SPI_rw(0x00);//low
    P1OUT &=~BIT1;
    }
    /*
    void EEPROM_read(unsigned char add, unsigned char buff[2])
    {
    int i=0;
    P1OUT |=BIT1;

    SPI_rw(READ_code);//0x18
    SPI_rw(add);


    while((P1IN & 0x80)!=0);//wait bit7 SOMI=0

    for(i=0;i<2;i++)//2 byte
    {
    buff[i]=SPI_rw(0);
    }

    P1OUT &=~BIT1;
    }
    */
    void EEPROM_read(unsigned char add, unsigned int get_dt)
    {
    unsigned char byte1,byte2,byte3;
    get_dt=0;
    P1OUT |=BIT1;

    SPI_rw(READ_code);//0x18
    SPI_rw(add);

    byte1=SPI_rw(0);
    byte2=SPI_rw(0);
    byte3=SPI_rw(0);

    P1OUT &=~BIT1;
    get_dt |unsigned int)byte1)<<9 | ((unsigned int)byte2)<<1 | ((unsigned int)byte3)>>7;
    }

    void EEPROM_write(unsigned char add,unsigned int data)//2 byte data
    {

    unsigned char data_high, data_low;

    data_high=(unsigned char)(data >>8);
    data_low =(unsigned char)(data & 0x00FF);

    P1OUT |=BIT1;

    SPI_rw(WRITE_code);//0x14
    SPI_rw(add);
    SPI_rw(data_high);
    SPI_rw(data_low);
    SPI_rw(0x0);
    //delay();
    while((P1IN & 0x80)==0);//ready
    delay();
    P1OUT &=~BIT1;

    LCDM9=LCD_num[data_high];
    LCDM10=LCD_num[data_low];
    }

    void EEPROM_erase(int add)
    {

    P1OUT |=BIT1;

    SPI_rw(ERASE_code);//0x1C
    SPI_rw(add);
    SPI_rw(0x0);

    while((P1IN & 0x80)==0);
    delay();
    P1OUT &=~BIT1;
    }
    void main(void)
    {
    WDTCTL=WDTPW+WDTHOLD;

    CLK_init();
    LCD_init();
    SPI_init();
    P1OUT |=BIT3;//unselect RF modul

    EEPROM_ewen();

    EEPROM_erase(0xA2);
    LCD_clear();
    LCDM5=LCD_num[5];
    delay();

    EEPROM_write(0xA2,0x05);
    LCDM6=LCD_num[6];
    delay();



    //test result on LCD

    EEPROM_read(0xA2,get_data1);

    get_data1_high=(unsigned char)(get_data1>>8);
    get_data1_low=(unsigned char)(get_data1 & 0x00FF);
    LCDM7=LCD_num[get_data1_high];
    LCDM8=LCD_num[get_data1_low];
    /*
    EEPROM_read(0xAA, get_data);

    LCDM7=LCD_num[get_data[0]];
    LCDM8=LCD_num[get_data[1]];
    */
    while(1);
    }
    E cảm ơn!
    Gian Khó Cuộc Đời Nuôi Ta Lớn!
    Giang Hồ Đẫm Máu Dạy Ta Khôn!

    (@_@)______________(@_@)

Về tác giả

Collapse

toan88_tnt Tìm hiểu thêm về toan88_tnt

Bài viết mới nhất

Collapse

Đang tải...
X