|
Tài trợ cho PIC Vietnam |
Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC |
|
Ðiều Chỉnh | Xếp Bài |
23-11-2009, 12:01 PM | #5 |
Đệ tử 4 túi
Tham gia ngày: Mar 2009
Bài gửi: 64
: |
code tham khảo của bác falleaf (dùng cho chú dsPIC30F4013).Cụ thể là trong thread này:
http://www.picvietnam.com/forum/showthread.php?t=1663 Code:
#define BLOCK_SIZE 512 unsigned char SD_ReadBlock(unsigned long addr, unsigned char *buf) { unsigned int i; unsigned char status; unsigned char CMD17_READ_SINGLE_BLOCK[] = {17,0x00,0x00,0x0,0x00,0xFF}; CMD17_READ_SINGLE_BLOCK[1] = ((addr & 0xFF000000) >> 24); CMD17_READ_SINGLE_BLOCK[2] = ((addr & 0x00FF0000) >> 16); CMD17_READ_SINGLE_BLOCK[3] = ((addr & 0x0000FF00) >> 8); CMD17_READ_SINGLE_BLOCK[4] = ((addr & 0x000000FF)); SD_Enable(); i=0; do { status = SD_WriteCommand(CMD17_READ_SINGLE_BLOCK); send_hex(status,"CMD17:"); if(i++ > 50) { putsUART1((unsigned int*)"Read sector failed\n\r"); break; } } while(status != 0x00); //Waif Delay_Us(150); // Send the read command if(status != 0) { // ABORT: invalid response for read single command return 1; } // Now wait for the "Start Block" token (0xFE) // (see SanDisk SD Card Product Manual v1.9 section 5.2.4. Data Tokens) do { status = SPIRead(); send_hex(status,"waitf token:"); } while(status != 0xFE); // Read off all the bytes in the block for(i = 0; i < BLOCK_SIZE; ++i) { status = SPIRead(); *buf = status; buf++; } // Read CRC bytes status = SPIRead(); status = SPIRead(); SD_Disable(); // Following a read transaction, the SD Card needs 8 clocks after the end // bit of the last data block to finish up its work. // (from SanDisk SD Card Product Manual v1.9 section 5.1.8) SPIWrite(0xFF); return 0; } status = SD_ReadBlock(0, buffer); Hàm SD_WriteCommand vẫn giữ nguyên. Hàm read đã có write dummy data (write FF) Còn lệnh nào có response R1 mà test đuợc không nhỉ. Tốc độ giao tiếp phụ thuộc thế nào với các loại thẻ khác nhau. Khởi tạo thẻ nhớ với tốc độ 150k, gửi lệnh cmd0 để reset thẻ, R1 = 0x01 (ok..đã in idle state). Sau đó gửi lệnh cmd1 để vào data transfer mode. Chuyển tốc độ lên 1.25MHz. R1 = 0x00 (đã vào mode transfer) Sau đó gửi lệnh CMD17 để đọc single block thì toàn response 0x20 Ở đây em chỉ đọc 512 byte bắt đầu từ địa chỉ 0x00, không biết đó có phải sector 0 hay ko. (em đã quét cả dải địa chỉ mà vẫn báo về như thế...) CRC có chú ý gì ko? |
|
|