本帖最后由 维博哥哥 于 2016-10-11 19:33 编辑
之前搞了好久Arduino Due,在驱动液晶ILI9481的时候,一直弄不出来。然后想用usb接口驱动北通游戏手柄还是弄不出来,于是就转战stm32,想学学这底层到底怎么弄。弄stm32估计都快半个月了吧,又在IIC上面驱动BMA180搞不出来。心灰意懒,还是做arduino好玩,没事还能出来写写帖子。
这其实是好久之前做的小实验,读取bmp文件并显示出来。挺有意思的主要这次图片不再是把图片转化成文本文件,而是直接的二进制文件,真正的二进制文件。说到二进制的图片文件,bmp估计是最简单的一种了。除了文件头,后面就是图片数据。其实吧,这些文件都是很简单的,关键是找到很好的教材,我就是找到写得很整齐的小程序是用c写的,直接生成一个bmp图片。程序我挂在后面,因为之前对结构体不是很懂,所以看很多都不是很懂,有了这个程序基本上明白。
说回我们的这次小实验,使用的设备128*128像素的小液晶,一个SD卡读卡器,一个8G的TF卡。如图所示,中间的ov7670是没有用到的。 - #include <SPI.h>
- #include <SD.h>
- #include <UTFT.h>
- UTFT myGLCD(ST7735S,A2,A1,A5,A4,A3);
- File myFile;
- byte a;
- unsigned short pic[0x4000];
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- myGLCD.InitLCD(PORTRAIT);
- Serial.print("Initializing SD card...");
- // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
- // Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega, 14 on the Leonardo) must be left as an output
- // or the SD library functions will not work.
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- return;
- }
- Serial.println("initialization done.");
-
- // re-open the file for reading:
- myFile = SD.open("4.bmp");
- if (myFile) {
- Serial.println("4.bmp:");
- // read from the file until there's nothing else in it:
- while (myFile.available()) {
- //跳过前0x36个字节的头
- for(int i=0;i<0x36;i++)
- {
- Serial.print(myFile.read(),HEX);
- Serial.print(" ");
- }
- //把rgb24转换成rgb16,128*128像素转换成hex是0x4000
- for(int i=0;i<0x4000;i++)
- {
- a = myFile.read();
- pic[i] = (a*32/256)<<11;
- a = myFile.read();
- pic[i] |= (a*64/256)<<5;
- a = myFile.read();
- pic[i] |= (a*32/256);
- }
- Serial.println("OK");
- }
- // close the file:
- myFile.close();
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening test.txt");
- }
- }
- void loop()
- {
- // nothing happens after setup
- myGLCD.fillScr(255, 255, 255);
- myGLCD.drawBitmap (0, 0, 128, 128, pic, 1);
- delay(5000);
- }
复制代码 |