Arduino与Proteus仿真实例-SD卡数据储存驱动仿真

SD卡数据储存驱动仿真

SD 卡模块特别适用于需要数据记录的项目。在Arduino中,通过SD Library 可以在 SD 卡中创建文件以使用 SD 库写入和保存数据。

在前面的文章中,对SD卡的底层驱动做了详细的介绍,请参考:

1、仿真电路原理图

在这里插入图片描述

注意,需要创建SD卡映像文件,并绑定到SD Card模拟组件中。

1)下载并安装:winimage

2)创建SD卡映像文件

在这里插入图片描述

在这里插入图片描述

3)保存映像文件,并将映像文件的后缀更改为.mmc

4)将创建好的SD卡映像文件绑定到SD Card模拟组件中:

在这里插入图片描述

2、仿真代码实现

1)查询SD卡信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/\*
SD card test

This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.

The circuit:
SD card attached to SPI bus as follows:
\*\* MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
\*\* MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
\*\* CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
\*\* CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples


created 28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
\*/
// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD\_SS\_PIN
const int chipSelect = SS;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}


Serial.println("Initializing SD card...");

// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("\* is a card inserted?");
Serial.println("\* is your wiring correct?");
Serial.println("\* did you change the chipSelect pin to match your shield or module?");
while (1);
} else {
Serial.println("Wiring is correct and a card is present.");
}

// print the type of card
Serial.println();
Serial.print("Card type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}

// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\r\nMake sure you've formatted the card");
while (1);
}

Serial.print("Clusters: ");
Serial.println(volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(volume.blocksPerCluster());

Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() \* volume.clusterCount());
Serial.println();

// print the type and size of the first FAT-type volume
uint32\_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(volume.fatType(), DEC);

volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize \*= volume.clusterCount(); // we'll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
Serial.print("Volume size (Kb): ");
Serial.println(volumesize);
Serial.print("Volume size (Mb): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Gb): ");
Serial.println((float)volumesize / 1024.0);

Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);

// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}

void loop(void) {
}

2)文件读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/\*
SD card read/write

This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
\*\* MOSI - pin 11
\*\* MISO - pin 12
\*\* CLK - pin 13
\*\* CS - pin 4 (for MKRZero SD: SDCARD\_SS\_PIN)

created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

\*/

#include <SPI.h>
#include <SD.h>

File myFile;
int readPin = 2;
int writePin = 3;
int counts = 1;
byte readState;
byte writeState;
void read\_interrupt(){
readState = 1;
}

void write\_interrupt(){
writeState = 1;
}

void setup(){
pinMode(readPin,INPUT);
pinMode(writePin,INPUT);
attachInterrupt(digitalPinToInterrupt(readPin),read_interrupt,RISING);
attachInterrupt(digitalPinToInterrupt(writePin),write_interrupt,RISING);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}


Serial.println("Initializing SD card...");

if (!SD.begin(SS)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");

}

void loop() {
if(writeState){
writeState = 0;
Serial.println("write file:");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(counts);
Serial.print(counts);
counts++;
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("write:error opening test.txt");
}
}

if(readState){
readState = 0;
Serial.println("read file:");
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("read:error opening test.txt");
}
}
}



3、仿真结果

1)SD卡信息读取

在这里插入图片描述

2)文件读写

在这里插入图片描述

使用WinImage打开生成的文件如下:

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/121068376