Archive for December 2009
SD卡是我们经常使用到的一种存储设备,像数码相机,MP3,阅读器,GPS导航仪等都会使用到它。在使用Arduino的时候,如何我们要将一些数据记录下来,就可以使用到SD卡。不过要让SD卡能够同Arduino配合使用可不是一件简单的事情,首先在硬件上我们要解决其与Arduino连接的问题,其次软件上我们还要解决文件的存储问题,以便使用读卡器之样的设备能够很方便地将保存在SD卡中的数据读出。
针对硬件上的需求,我们开发了SD卡模块,它通过SPI接口与Arduino进行通信:
同时,为了方便同Arduino的SPI接口进行连接,提供了相应的IDC扩展板,该扩展板上有一个专门为SPI设计的六芯插座:
电路的连接上则依然沿用了电子积木所一向强调的即插即用方式,我们只需要用6芯的IDC连接线,将IDC扩展板上的SPI插座与SD卡模块上的相应插座连接起来,就完成了电路部分的连接:
回到软件部分,我们的目标是要让SD卡上记录的数据能够在电脑上顺利地读出,因此在向SD卡上记录相应数据的时候需要遵循一定的规定,即文件系统的类型。对于像Arduino这样的小系统来讲,相对简单的FAT16是一个比较不错的选择。
在这里我们要用到读卡器来对SD卡进行相应的处理,将其格式化成FAT文件系统:
Arduino上使用的软件则是基于SparkFun提供的FAT库,但根据硬件差异做了一定的修改,你可以从这里下载到修改过的FAT库。该库需要被解压缩到Arduino的hardware\libraries子目录中。
测试时使用的时FAT库里自带的示例程序:
//Include all the libraries necessary for FAT32 #include <byteordering.h> #include <fat.h> #include <FAT16.h> #include <fat_config.h> #include <partition.h> #include <partition_config.h> #include <sd-reader_config.h> #include <sd_raw.h> #include <sd_raw_config.h> FAT TestFile; //This will be the file we manipulate in the sketch char buffer[512]; //Data will be temporarily stored to this buffer before being written to the file int read_size=0; //Used as an indicator for how many characters are read from the file int count=0; //Miscellaneous variable void setup() { Serial.begin(9600); //Initiate serial communication at 9600 bps TestFile.initialize(); //Initialize the SD card and the FAT file system. Serial.println("Starting..."); TestFile.create_file("Sample.txt"); //Create a file on the SD card named "Read_File_Test.txt" //NOTE: This function will return a 0 value if it was unable to create the file. TestFile.open(); //Now that the file has been created, open it so we can write to it. } void loop() { TestFile.write("This is test data."); //using the write function will always write to the beginning of the file. // Here we add some text to the file. TestFile.close(); //We are done writing to the file for now. Close it for later use. while(1){ TestFile.open(); //Open the file. When the file is opened we will be looking at the beginning of the file. read_size=TestFile.read(buffer); //Read the contents of the file. This will only read the amount of data specified // by the size of 'buffer.' Serial.println(read_size, DEC); //Print the number of characters read by the read function. for(int i=0; iSerial.print(buffer[i], BYTE); //Print out the contents of the buffer. } Serial.println(); sprintf(buffer, "%d", count++); //Now we'll use the buffer to write data back to the file. // Here's we'll only add one value to buffer, the 'count' variable. TestFile.write(buffer); //Write the new buffer to the end of the file TestFile.close(); //Close the file for later use. delay(1000); //Wait one second before repeating the loop. } }
这样Arduino在运行的时候,会不断地向SD卡模块上的Sample.txt文件中写入相应的数据,随后我们同样可以通过读卡器读出该文件中记录的内容。
需要注意的是,由于用来操作SD卡和文件系统的代码相对较多,目前该模块只能运行在带有ATmega328P的Arduino上面。另外,目前市场上可选的SD卡种类繁多,可能不是每种SD卡都能够被很好的兼容,目前我测试过通过的SD卡有:
- SanDisk 2GB SD2 Card
- Apacer 60X 1GB SD Card
测试没有通过的SD卡有:
- SanDisk 16MB SD Card
触摸开关去年曾经做过一个版本,当时使用的是一个胶封的芯片,体积比较大,而且一致性也不是很好,并且触摸部分的金属片是附加在按钮上的,制作起来比较复杂。这次做的触摸按钮解决了这两个问题,采用集成了了触摸芯片,并且直接在按钮上设置了一块触摸的区域,中间的焊盘可以很方便地连接其他的金属块,以方便实际中的使用:
连接方法仍然是通过传感器连接线,与传感器扩展板连接起来即可使用Arduino来进行处理了。
触摸按钮属于数字模块,在处理方式上同普通的按钮并没有什么区别,下面是实验时用到的代码:
int ledPin = 13; int switchPin = 7; int value = 0; void setup() { pinMode(switchPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { value = digitalRead(switchPin); if (HIGH == value) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
这个触摸按钮上的O1和O2两个跳线用来设置触摸传感器的工作模式,该传感器一共有4个工作模式,其中如下两种是最常使用的:
滑动电位器在类似于音量调节和参数调整这样的场所经常被使用到,其滑块能够滑动的距离称为行程,60行程即代表能够滑动60mm,是最常使用的一种行程大小。这个电子积木选用的就是60行程的滑动电位器:
使用上与其它基于电位器的电子积木一样,只需要用通用传感器连接线或者模拟传感器连接线,将其连接到某个模拟输入接口,如A5上:
然后再借助Arduino读出其值就可以,如下面的示例代码所示:
int sensorPin = 5; int value = 0; void setup() { Serial.begin(9600); } void loop() { value = analogRead(sensorPin); Serial.println(value, DEC); }
这样当滑块位于滑动电位器上的不同位置时,读出来的值会在0到1023间变连续变化。
No tags
电子设备或者模块在需要同外界进行通信时,经常采用串行通信的方式,即逐位传输一系列二进制编码数据。基于单片机的电子设备的串口有很大一部分使用的是 TTL电平标准,它的逻辑1电平是5V,逻辑0电平是0V;而我们常用的PC机串行口所使用的却是RS-232C电平标准,它的逻辑1电平是 -3V~-12V,逻辑0电平是+3V~+12V。由于两者的电平范围相差很远,因此必须在RS-232C和TTL电路之间进行电平和逻辑关系的变换之 后,才能在PC机和这类电子设备间实现串行通信。一般来讲,实现这种变换的电路可以用分立元件搭建,也可以使用MAX232这样的集成电路芯片。
我们开发的电子积木串口适配器,就是采用了MAX232芯片的电平转换模块,可以很方便地在Arduino和PC间建立起串口通信。由于Arduino自带的是USB转串口的方案,实际使用时如果觉得复杂或是稳定性不够,可以考虑这一串品适配器,由于缺少了USB转串口这一中间环节,通信方式更加直接。不过目前该模块还只能用在Arduino MEGA上,并且只能用在串口COM1, COM2, COM3上,原因可能是COM0是直接同USB转串口芯片连接的,它与MAX232之间似乎不是很兼容。
基于串口的电子积木使用的都是4芯的IIC/COM连接线,利用它可以将串口适配器非常方便地连接到Arduino MEGA专用传感器扩展板上,从而完成同Arduino的通信:
最后再用一根串口线连接PC和串口适配器,硬件部分的连接就完成了:
至于软件部分,在PC端我们可以借用SSCOM这样的串口调试工具,而Arduino这端则使用如下的测试代码:
int val = 0; void setup() { Serial1.begin(9600); } void loop() { val = Serial1.read(); if (-1 != val) { if ('A' == val || 'a' == val) { Serial1.println("Hello from Arduino!"); } } }
将上述代码下载到Arduino中执行后,Arduino会一直等待从PC端过来的命令,如果按受到字符A,则向PC端发送”Hello from Arduino!”这一字符串:

No tags
