Thông báo

Collapse
No announcement yet.

HELP code giao tiếp pic với thẻ nhớ

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

  • HELP code giao tiếp pic với thẻ nhớ

    em tìm dk cái code này trên mạng nhưng ko hiểu lắm bác nào biết gt hộ em với ạ.


    #include <16F877.H>
    #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
    #use delay(clock = 20000000)
    #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
    #use fast_io(C)
    #byte SSPBUF = 0x13
    #byte SSPCON = 0x14
    #byte SSPSTAT = 0x94
    #bit BF = SSPSTAT.0
    #bit SMP = SSPSTAT.7
    #bit CKE = SSPSTAT.6
    #bit CKP = SSPCON.4
    #bit SSPM1 = SSPCON.1
    #bit SSPEN = SSPCON.5

    #byte PORTC = 7
    #bit CS = PORTC.2

    //****************************************
    // This is the same as the CCS spi_read() function.

    char SPI(char d)
    {
    SSPBUF=d;
    while (!BF);
    return SSPBUF;
    }

    //******************************************

    char Command(char befF, int16 AdrH, int16 AdrL, char befH)
    {
    SPI(0xFF);
    SPI(befF);
    SPI(AdrH >> 8);
    SPI(AdrH);
    SPI(AdrL >> 8);
    SPI(AdrL);
    SPI(befH);
    SPI(0xFF);
    return SPI(0xFF); // Return with the response
    }
    //********************************************

    char MMC_Init()
    {
    char i;

    // Init SPI
    SMP=0;
    CKE=0;
    CKP=1;
    SSPM1=1;
    //SSPM0=1;
    SSPEN=1;

    CS=1; // MMC-Disabled

    // MMC in SPI Mode -- start and Reset.
    for(i=0; i < 10; i++) SPI(0xFF); // 10*8=80 clocks
    CS=0; // MMC-Enabled

    // CMD0
    if (Command(0x40,0,0,0x95) !=1) goto Error; // Reset

    st:
    // CMD1
    if (Command(0x41,0,0,0xFF) !=0) goto st ; // CMD1

    return 1;

    Error:
    return 0;
    }
    //*********************************************

    void main(void)
    {
    int16 i;
    setup_port_a(NO_ANALOGS);
    set_tris_c(0b11010011); // sck rc3-0, sdo rc5-0, CS rc2-0.
    set_tris_b(0b00000010);
    puts("Start\n\r");
    if(MMC_Init())
    puts("MMC ON\n\r"); // MMC Init OK

    //*****************************************

    // Write in 512 Byte-Mode
    if (Command(0x58,0,512,0xFF) !=0) puts("Write error ");
    SPI(0xFF);
    SPI(0xFF);
    SPI(0xFE);

    SPI("Begin\n\r"); // 7 characters

    for(i=0; i < 500; i++) // Was 512, but used 12 for text
    {
    SPI('M');
    }
    SPI("\n\rEnd"); // 5 characters


    SPI(255); // Send two bytes of 0xFF at the end
    SPI(255);
    i=SPI(0xFF);
    i &=0b00011111;
    if (i != 0b00000101) puts("Write Error ");
    while(SPI(0xFF) !=0xFF); // Wait for end of Busy condition

    //*************************************

    // Read in 512 Byte-Mode
    if (Command(0x51,0,512,0xFF) !=0) puts("Read Error ");

    while(SPI(0xFF) != 0xFE);

    for(i=0; i < 512; i++)
    {
    putc(SPI(0xFF)); // Send data
    }
    SPI(0xFF); // Send two bytes of 0xFF at the end
    SPI(0xFF);



    //**********************************************
    while(1); // The program stops here.
    }

  • #2
    bác co sơ đồ nguyên lý không ạ cho em xin với

    Comment


    • #3
      Cái này giao tiếp với thẻ nhớ bằng SPI thui mà

      Comment

      Về tác giả

      Collapse

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

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

      Collapse

      Đang tải...
      X