: các bạn ơi giúp tôi với liên quan về SDcard với 89S52, tôi cần kết nối dữ liệu gửi từ máy tính tới 89S52 rồi vi điều khiển này ghi dữ liệu đó vào thẻ nhớ 1G . cứu tôi mau với , mất nhiều thời gian mà chưa xong, có gì gửi vào mail saobui@gmail.com
Thông báo
Collapse
No announcement yet.
SDcard va MMCard voi 8051
Collapse
X
-
file Fat32.h
//-----------------------------------------------------------------------
// File: fat32.h
// Description: MCS51 C module for FAT32 layer on Memory Card, v 1.0408
// Author: PHAM ANH SAO <saobui@gmail.com>
// Compiler: Keil uVision
// Date: March 30, 2008
//-----------------------------------------------------------------------
#ifndef _FAT32_H_
#define _FAT32_H_
#define FAT32_OPTIMIZE_CODE
#ifdef _WRITE_SUPPORT
#define FAT32_WRITE_SUPPORT
#endif
// Errors
#define FAT32_ERROR_INIT -1
#define FAT32_NO_ERROR 0
#define FAT32_ERROR_IO 1
#define FAT32_ERROR_FORMAT 2
// File Type Difinition
typedef char fat32File;
// Max number of concurrent files
#define HANDLE_MAX 10
// File Attributes
#define FAT32_FILE_ATTR_READ_ONLY 0x01 // Read Only
#define FAT32_FILE_ATTR_HIDDEN 0x02 // Hidden
#define FAT32_FILE_ATTR_SYSTEM 0x04 // System
#define FAT32_FILE_ATTR_VOLUME_ID 0x08 // Volume ID
#define FAT32_FILE_ATTR_DIRECTORY 0x10 // Directory
#define FAT32_FILE_ATTR_ARCHIVE 0x20 // Archive
#define FAT32_FILE_ATTR_LONG_NAME 0x0F // Long Name
// Function Prototypes
extern char fat32Init(void);
#ifdef FAT32_WRITE_SUPPORT
extern fat32File fat32FileOpen(const char *Path, char Mode);
extern int fat32FileWrite(fat32File Handle, char *BufferOut, unsigned int NoBytes);
extern char fat32FileUnlink(const char *Path);
#else
extern fat32File fat32FileOpen(const char *Path);
#endif // FAT32_WRITE_SUPPORT
extern char fat32FileClose(fat32File Handle);
extern long fat32FileLSeek(fat32File Handle, unsigned long Offset, char FromWhere);
extern unsigned long fat32FileSize(fat32File Handle);
extern int fat32FileRead(fat32File Handle, char *BufferIn, unsigned int NoBytes);
extern char fat32FileEnd(fat32File Handle);
#ifndef FAT32_OPTIMIZE_CODE
extern char fat32FileSetPos(fat32File Handle, unsigned long Pos);
#else
#define fat32FileSetPos(Handle, Pos) fat32FileLSeek(Handle, Pos, SEEK_SET)
#endif // FAT32_OPTIMIZE_CODE
// Definitions and Macros to use functions as ANSI C
// Modes used in Accessing files
#define O_RONLY 0x01
#define O_WONLY 0x02
#define O_RDWR 0x03
#define O_APPEND 0x04
#define O_CREATE 0x08
#define O_EXCL 0x10
#define O_TRUNC 0x20
#define O_TEXT 0x40
#define O_BINARY 0x80
// Attributes
#define FA_RONLY FAT32_FILE_ATTR_READ_ONLY
#define FA_HIDDEN FAT32_FILE_ATTR_HIDDEN
#define FA_SYSTEM FAT32_FILE_ATTR_SYSTEM
#define FA_LABEL FAT32_FILE_ATTR_VOLUME_ID
#define FA_DIREC FAT32_FILE_ATTR_DIRECTORY
#define FA_ARCH FAT32_FILE_ATTR_ARCHIVE
#define FA_LNAME FAT32_FILE_ATTR_LONG_NAME
// SEEKs
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
// Macros
#ifdef FAT32_WRITE_SUPPORT
#define open(path, mode) fat32FileOpen(path, mode)
#define write(handle, buff, len) fat32FileWrite(handle, buff, len)
#define unlink(path) fat32FileUnlink(path)
#define setattr(path, attr) fat32FileSetAttribute(path, attr) // Not ANSI C
#else
#define open(path, mode) fat32FileOpen(path)
#endif // FAT32_WRITE_SUPPORT
#define close(handle) fat32FileClose(handle)
#define read(handle, buff, len) fat32FileRead(handle, buff, len)
#define lseek(handle, offset, fromwhere) fat32FileLSeek(handle, offset, fromwhere)
#define eof(handle) fat32FileEnd(handle) // Not ANSI C
#define sizef(handle) fat32FileSize(handle) // Not ANSI C
#endif // _FAT32_H_
Comment
-
File mc.h
//-----------------------------------------------------------------------
// File: mc.h
// Description: C module for Memory Card (MMC/SD) physical operations
// Author: PHAM ANH SAO <saobui@gmail.com>
// Compiler: Keil uVision
// Date: March 15, 2008
//-----------------------------------------------------------------------
#ifndef _MC_H_
#define _MC_H_
#define MC_OPTIMIZE_CODE
#ifdef _WRITE_SUPPORT
#define MC_WRITE_SUPPORT
#endif
// Errors
#define MC_TRUE 1
#define MC_FALSE 0
#define MC_NO_ERROR 0x00
#define MC_ERROR_IO 0x02
#define MC_ERROR_TIME_OUT 0x54
// MC Types
#define MC_TYPE_MMC 1
#define MC_TYPE_SD 0
// Constants
#define MC_BLOCK_LEN 512
// Global Function Prototypes for Memory Card Layer
// If Code is Prefered some functions may be macros, or not available
extern char mcInitCard(void);
extern char mcSetBlockSize(unsigned int NoBytes);
extern char mcReadBlockPart(unsigned long Address, char *BufferIn,
unsigned int Start, unsigned int Size);
#ifdef MC_WRITE_SUPPORT
extern char mcWriteBlockPart(unsigned long Address, char *BufferIn,
unsigned int Start, unsigned int Size);
#ifndef MC_OPTIMIZE_CODE
extern char mcWriteBlock(unsigned long Address, const char *BufferOut);
#else // MC_OPTIMIZE_CODE
#define mcWriteBlock(Blk, Bufo) mcWriteBlockPart(Blk, Bufo, 0, MC_BLOCK_LEN)
#endif // MC_OPTIMIZE_CODE
#endif // MC_WRITE_SUPPORT
#ifndef MC_OPTIMIZE_CODE
extern char xdata mcCSD[18]; // Card Specific Data Register (16 bytes+ CRC16)
extern char xdata mcOCR[4]; // Operating Control Register
extern char xdata mcCID[18]; // Card Identification Register (16 bytes+ CRC16)
extern char mcReadCSD(void);
extern char mcWriteCSD(void);
extern char mcReadCID(void);
extern char mcReadOCR(void);
extern char mcReadBlock(unsigned long Address, char *BufferIn);
#else // MC_OPTIMIZE_CODE
#define mcReadBlock(Blk, Bufi) mcReadBlockPart(Blk, Bufi, 0, MC_BLOCK_LEN)
#endif // MC_OPTIMIZE_CODE
#endif // _MC_H_
File rs232.h
//-----------------------------------------------------------------------
// File: rs232.h
// Description: C module for RS-232 communication on MCS-51
// Author: PHAM ANH SAO <saobui@gmail.com>
// Compiler: Keil uVision
// Date: March 23, 2008
//-----------------------------------------------------------------------
#ifndef _RS232_H_
#define _RS232_H_
#include <reg52.h>
extern void rs232Init(char asVoid); // Data: 1-8-1, No Parity, 9600 @ 24M
extern void rs232SendByte(char Sent);
extern char rs232ReceiveByte(unsigned char TimeOut);
extern void rs232SendString(const char *String);
extern void rs232SendBytes(unsigned int NoBytes, const char *Bytes);
extern void rs232ReceiveLine(char *LineBuf);
#ifndef __STDIO_H__
#define puts(s) rs232SendString(s)
#define gets(s) rs232ReceiveLine(s)
#define getch() rs232ReceiveByte(0xFF)
#define putch(c) rs232SendByte(c)
#endif // __STDIO_H__
#endif // _RS232_H_
File mcTest.c
//#define _WRITE_SUPPORT
#include "rs232.h"
#include "fat32.h"
#include <reg52.h>
sbit LED_MCIN= P1^7; // Memory Card Insert LED
sbit MCOUT= P3^2; // Memory Card Out
#define MCIN !MCOUT
void Timer0(void);
char xdata mcInserted;
void main()
{
char xdata mcBuffer[2050];
char xdata Path[256];
unsigned int len;
fat32File file;
// Init Serial Port
rs232Init(0);
rs232SendString(" MEMORY CARD TEST by PHAM ANH SAO\n");
// Init Timer 0
TMOD|= 0x02; // Timer0, 8 bit auto load
TH0= 0x00; // auto load value
IE|= 0x82;
TR0= 1; // Start Timer0
// Init FAT32 for Memory Card
while(fat32Init()!= 0)
{
rs232SendString("> FAT32 Initialization Error!\n");
}
// Open a sample file and display its contents
while(1)
{
rs232SendString("> Type the File Path to Open...\n> ");
rs232ReceiveLine(Path);
// Open a file for Read and Write (O_RDWR)
// and set file pointer to end of file (O_APPEND)
// If file does not exist, we create it (O_CREATE)
if((file= open(Path, O_CREATE| O_RDWR| O_APPEND))!= -1)
{
rs232SendString("> File Opened!\n");
// Write a sample string to file
#ifdef _WRITE_SUPPORT
if(write(file, "<saobui@gmail.com>", 21)== -1)
rs232SendString("\n> Write Fail!\n");
else
rs232SendString("\n> Data Successfully Written to File!\n");
#endif
// Rewind the file
lseek(file, 0, SEEK_SET);
// Display contents
while(!eof(file))
{
len= read(file, mcBuffer, 2048);
rs232SendBytes(len, mcBuffer);
}
// Close file
if(close(file)== 0)
rs232SendString("\n> File Now Closed!\n");
}
else
{
rs232SendString("> File Could Not be Opened!\n");
}
}
}
void Timer0() interrupt 1 using 1
{
TR0= 0;
if(MCIN)
{
LED_MCIN= 1;
if(!mcInserted)
{
rs232SendString("\n> Card Inserted!\n");
}
mcInserted= 1;
}
else
{
LED_MCIN= 0;
if(mcInserted)
{
rs232SendString("\n> Card Removed! \n");
}
mcInserted= 0;
}
TR0= 1;
}
tôi gửi cả 4 fi le đó, các bạn xem. tôi đã chuyển File mcTest.c sang File.Hex mà cứ báo lỗi là sao nhỉ, có phai do tôi thiếu thư viện File hỗ trợ không, ai có hi cho tôi , hoặc chỉ ra thất bại của tôi ở đâu nhé
Comment
-
Giao tiếp thẻ nhớ MMC & SD Card - Quang bao, led, ty gia vang
tôi đã xem qua link này nhưng không biết ghép chô nào với nhau nữa, giúp tôi với cả DIỄN ĐÀN ơi
Comment
-
tốt nhất là mỗi khi post code không do mình viết ra thì cứ ghi là sưu tầm/tham khảoNguyên văn bởi bvhoang Xem bài viếtLại một đồng chí lấy thư viện FAT32 của mình và sửa tên tác giả thành của họ. Rất may mà mình chưa post source thư viện lên...
Comment
Bài viết mới nhất
Collapse
-
bởi mèomướpDạ chú rủ cô ấy đi ăn uống, cà phê, xem phim...nhớ nắm tay, rồi thì ôm hôn,...trong vòng 1 tuần mà ko nắm tay được, 1 tháng mà ko ôm đc thì dẹp ngay và luôn ạ. Nhắn tin, gọi điện ít thôi ạ, trả lời quá lâu hoặc ko trả lời cũng dẹp ngay ạ. Nên thể hiện chú là người có điều kiện nữa ạ...
-
Channel: Tâm tình dân kỹ thuật
Hôm qua, 18:50 -
-
bởi songchodep76Cảm ơn bác đã nhiệt thành chia sẻ kinh nghiệm bản thân mình cho mọi người.
Tuy nhiên, theo tiếp xúc hạn hẹp của em với mosfet thì vấn đề bác đo vôn ở cực D và thấy đỉnh nó 70V/55V Vds max, như thí nghiệm và trong hình của bác...-
Channel: Hướng dẫn sử dụng diễn đàn
07-02-2026, 09:46 -
-
Trả lời cho Yêu thơ mê nhạc, mời các bác vào đây!bởi dinhthuong92Cho tới thời điểm này, quả thật Đình Thường đây quá thất vọng, không hào hứng với Suno-AI lắm bởi ra lệnh Creat mấy chục lần với các thay đổi thì mới chọn được 2 bản hát đúng giai điệu tầm 80% để cắt ghép tạo thành bài hát...
-
Channel: Tâm tình dân kỹ thuật
06-02-2026, 17:01 -
-
Trả lời cho Yêu thơ mê nhạc, mời các bác vào đây!bởi dinhthuong92Kính chào cả nhà, nhân dịp Tết đang về, sắp 23 tháng chạp rồi, xin gởi lời chúc xuân qua bài hát sau ạ:
Bao nhiêu hân hoan
Chúc Mừng Năm Mới, xuân sang!
Nơi nơi hát vang
nâng chén vui chúc câu An Lành.
Vạn Sự đều Hanh Thông,
Rạng...-
Channel: Tâm tình dân kỹ thuật
06-02-2026, 16:46 -
-
bởi ittcChán quá các bác, em nhạt nhẽo quá nên tán em nào cũng tạch, tuyệt vọng vô cùng, nay lại được mấy anh đồng nghiệp cty đối tác mách cho em gái kia sinh năm 2K đầu, em chả biết nhóm đối tượng này phải tán ra sao bây giờ ?
Tính ra em...-
Channel: Tâm tình dân kỹ thuật
06-02-2026, 00:18 -
-
bởi bqvietCó thể, ví dụ phần phản hồi gồm vi mạch cách ly quang, zener thứ cấp, transistor và điện trở phản hồi dòng ... Bất kỳ linh kiện nào nhóm đó hỏng dẫn tới mất đường phản hồi. TNY chính hãng phát hiện được chuyện đó nhưng linh kiện...
-
Channel: Điện tử công suất
05-02-2026, 18:36 -
-
bởi Nexus 6Pcho e hỏi, khi mạch có linh kiện nào đó hư thì có làm hỏng led đắt tiền (osram) không?
-
Channel: Điện tử công suất
05-02-2026, 10:42 -
-
bởi chinhnguyen9· Thí nghiệm 1 (Mạch boost, Vcc=12V, kích bằng dao động PƯM, duty 10%):
* Không có snubber + không tải: Xuất hiện hiện tượng dao động tắt dần tại cực D Mosfet (ringing). Hình 1 cho thấy trong chu kỳ đầu, điện áp spike lên tới hàng trăm V, điện...-
Channel: Hướng dẫn sử dụng diễn đàn
04-02-2026, 09:16 -
-
bởi Nexus 6Pe dùng KiCad 9.0 và đã xuất được file PDF mạch in gòi bác...
-
Channel: Điện tử công suất
03-02-2026, 16:25 -
-
bởi bqvietBấm chuột vào các tệp sẽ bật ra chương trình tương ứng. Nên dùng bản KiCAD sau
https://kicad-downloads.s3.cern.ch/a...ll_version.exe-
Channel: Điện tử công suất
03-02-2026, 14:57 -

Comment