楼主: 驴友花雕
|
[教程] 一块扩展板完成Arduino的10类37项实验(代码+图形+仿真) |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 扩展实验 31 兼容HC-06从机蓝牙模块(通信与存储类,TTL接口) 项目三:Arduino 蓝牙控制 LED 灯 串口发送“1”为开灯,发送“2"为关灯 连接方式 HC-06(兼容) UNO VCC VCC GND GND TXD D0 RXD D1 */ void setup() { Serial.begin(38400); pinMode(13, OUTPUT); } void loop() { while(Serial.available()) { char c=Serial.read(); if(c=='1') { Serial.println("Eagler8"); Serial.write("Serial--13--high"); digitalWrite(13, HIGH); } if(c=='2') { Serial.write("Serial--13--low"); digitalWrite(13, LOW); } } }[/mw_shl_code] |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 扩展实验 31 兼容HC-06从机蓝牙模块(通信与存储类,TTL接口) 项目四:Arduino 蓝牙控制板载LED灯和有源蜂鸣器 串口发送“1”为开灯响,发送“2"为关灯 连接方式 HC-06(兼容) UNO VCC VCC GND GND TXD D0 RXD D1 */ void setup() { Serial.begin(9600); pinMode(5, OUTPUT); pinMode(12, OUTPUT); } void loop() { while(Serial.available()) { char c=Serial.read(); if(c=='1') { Serial.println("Eagler8"); Serial.write("Serial--12--high"); digitalWrite(5, HIGH); digitalWrite(12, HIGH); } if(c=='2') { Serial.write("Serial--12--low"); digitalWrite(5, LOW); digitalWrite(12, LOW); } } }[/mw_shl_code] |
32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) [mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 板载端口扩展实验 32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) 项目一:点亮OLED屏 实验接线:A4---SDA, A5---SCL */ #include "U8glib.h" String i="Time:"; int s=0;//变量秒 用来显示 U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); void draw() { u8g.setFont(u8g_font_unifont); // 设置字体 这句必要 u8g.drawStr( 1, 10, "Hello Eagler8!");//第一个是x第二个是y坐标 u8g.setFont(u8g_font_ncenB14);//还是先设置一个字体 u8g.setPrintPos(0,44);//然后设置一个位置 u8g.print(i);//然后就是数据 u8g.print(s); } void setup(void) { } void loop(void) { if(s!=millis()/10) s=millis()/10; //上面这一句是取系统运行秒数 单位ms除以1000就是秒 u8g.firstPage(); do { draw(); } while( u8g.nextPage() ); //延迟一段时间后重新生成图片 delay(10); }[/mw_shl_code] |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 板载端口扩展实验 32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) 项目二:测试之1-100循环显示数字 实验接线:A4---SDA, A5---SCL */ #include "U8glib.h" //加载显示库文件 U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI 实例化 void setup() { } void loop() { for(int i=1;i<101;i++){ u8g.firstPage(); //一下是显示实现部分 do { u8g.setFont(u8g_font_fub30); //设置字体和自号,目前测试字号有fub14,17,20,30 u8g.setPrintPos(0, 50); //显示的位置 u8g.print(i);//显示变量i的值 u8g.setFont(u8g_font_fub14);//设置字体和自号 u8g.setPrintPos(95, 50); //显示的位置 u8g.print("cm");//显示cm字样 } while( u8g.nextPage() ); delay(100);//显示的时间间隔。 } }[/mw_shl_code] |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 板载端口扩展实验 32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) 实验接线:A4---SDA, A5---SCL 项目三:将U8Glib标志放在显示屏上(Put the U8GLIB logo on the display.) 显示“Copyright (c) 2019, eagler8@126.com” */ #include "U8glib.h" U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0); void drawColorBox(void) { u8g_uint_t w, h; u8g_uint_t r, g, b; w = u8g.getWidth() / 32; h = u8g.getHeight() / 8; for ( b = 0; b < 4; b++ ) for ( g = 0; g < 8; g++ ) for ( r = 0; r < 8; r++ ) { u8g.setColorIndex((r << 5) | (g << 2) | b ); u8g.drawBox(g * w + b * w * 8, r * h, w, h); } } void drawLogo(uint8_t d) { #ifdef MINI_LOGO u8g.setFont(u8g_font_gdr17r); u8g.drawStr(0 + d, 22 + d, "U"); u8g.setFont(u8g_font_gdr20n); u8g.drawStr90(17 + d, 8 + d, "8"); u8g.setFont(u8g_font_gdr17r); u8g.drawStr(39 + d, 22 + d, "g"); u8g.drawHLine(2 + d, 25 + d, 34); u8g.drawVLine(32 + d, 22 + d, 12); #else u8g.setFont(u8g_font_gdr25r); u8g.drawStr(0 + d, 30 + d, "U"); u8g.setFont(u8g_font_gdr30n); u8g.drawStr90(23 + d, 10 + d, "8"); u8g.setFont(u8g_font_gdr25r); u8g.drawStr(53 + d, 30 + d, "g"); u8g.drawHLine(2 + d, 35 + d, 47); u8g.drawVLine(45 + d, 32 + d, 12); #endif } void drawURL(void) { #ifndef MINI_LOGO u8g.setFont(u8g_font_4x6); if ( u8g.getHeight() < 59 ) { u8g.drawStr(53, 9, "Copyright (c) 2019,"); u8g.drawStr(77, 18, "eagler8@126.com"); } else { u8g.drawStr(1, 54, " Copyright (c) 2019, eagler8@126.com"); } #endif } void draw(void) { if ( u8g.getMode() == U8G_MODE_R3G3B2 ) { drawColorBox(); } u8g.setColorIndex(1); if ( U8G_MODE_GET_BITS_PER_PIXEL(u8g.getMode()) > 1 ) { drawLogo(2); u8g.setColorIndex(2); drawLogo(1); u8g.setColorIndex(3); } drawLogo(0); drawURL(); } void setup(void) { } void loop(void) { u8g.firstPage(); do { draw(); u8g.setColorIndex(1); } while ( u8g.nextPage() ); delay(200); }[/mw_shl_code] |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 板载端口扩展实验 32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) 实验接线:A4---SDA, A5---SCL 项目四:动画示例中的雪花飘 */ #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET 4 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define NUMFLAKES 10 #define LOGO_HEIGHT 16 #define LOGO_WIDTH 16 static const unsigned char PROGMEM logo_bmp[] = { B00000000, B11000000, B00000001, B11000000, B00000001, B11000000, B00000011, B11100000, B11110011, B11100000, B11111110, B11111000, B01111110, B11111111, B00110011, B10011111, B00011111, B11111100, B00001101, B01110000, B00011011, B10100000, B00111111, B11100000, B00111111, B11110000, B01111100, B11110000, B01110000, B01110000, B00000000, B00110000 }; void setup() { Serial.begin(9600); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } display.display(); delay(2000); display.clearDisplay(); display.drawPixel(10, 10, WHITE); display.display(); delay(2000); testdrawline(); testdrawrect(); testfillrect(); testdrawcircle(); testfillcircle(); testdrawroundrect(); testfillroundrect(); testdrawtriangle(); testfilltriangle(); testdrawchar(); testdrawstyles(); testscrolltext(); testdrawbitmap(); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); } void loop() { } void testdrawline() { int16_t i; display.clearDisplay(); for(i=0; i<display.width(); i+=4) { display.drawLine(0, 0, i, display.height()-1, WHITE); display.display(); delay(1); } for(i=0; i<display.height(); i+=4) { display.drawLine(0, 0, display.width()-1, i, WHITE); display.display(); delay(1); } delay(250); display.clearDisplay(); for(i=0; i<display.width(); i+=4) { display.drawLine(0, display.height()-1, i, 0, WHITE); display.display(); delay(1); } for(i=display.height()-1; i>=0; i-=4) { display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); display.display(); delay(1); } delay(250); display.clearDisplay(); for(i=display.width()-1; i>=0; i-=4) { display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); display.display(); delay(1); } for(i=display.height()-1; i>=0; i-=4) { display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); display.display(); delay(1); } delay(250); display.clearDisplay(); for(i=0; i<display.height(); i+=4) { display.drawLine(display.width()-1, 0, 0, i, WHITE); display.display(); delay(1); } for(i=0; i<display.width(); i+=4) { display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE); display.display(); delay(1); } delay(2000); } void testdrawrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2; i+=2) { display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE); display.display(); delay(1); } delay(2000); } void testfillrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2; i+=3) { // The INVERSE color is used so rectangles alternate white/black display.fillRect(i, i, display.width()-i*2, display.height()-i*2, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawcircle(void) { display.clearDisplay(); for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) { display.drawCircle(display.width()/2, display.height()/2, i, WHITE); display.display(); delay(1); } delay(2000); } void testfillcircle(void) { display.clearDisplay(); for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) { display.fillCircle(display.width() / 2, display.height() / 2, i, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawroundrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2-2; i+=2) { display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE); display.display(); delay(1); } delay(2000); } void testfillroundrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2-2; i+=2) { display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawtriangle(void) { display.clearDisplay(); for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) { display.drawTriangle( display.width()/2 , display.height()/2-i, display.width()/2-i, display.height()/2+i, display.width()/2+i, display.height()/2+i, WHITE); display.display(); delay(1); } delay(2000); } void testfilltriangle(void) { display.clearDisplay(); for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) { display.fillTriangle( display.width()/2 , display.height()/2-i, display.width()/2-i, display.height()/2+i, display.width()/2+i, display.height()/2+i, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawchar(void) { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); display.cp437(true); for(int16_t i=0; i<256; i++) { if(i == '\n') display.write(' '); else display.write(i); } display.display(); delay(2000); } void testdrawstyles(void) { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println(F("Hello, world!")); display.setTextColor(BLACK, WHITE); display.println(3.141592); display.setTextSize(2); display.setTextColor(WHITE); display.print(F("0x")); display.println(0xDEADBEEF, HEX); display.display(); delay(2000); } void testscrolltext(void) { display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(10, 0); display.println(F("scroll")); display.display(); delay(100); display.startscrollright(0x00, 0x0F); delay(2000); display.stopscroll(); delay(1000); display.startscrollleft(0x00, 0x0F); delay(2000); display.stopscroll(); delay(1000); display.startscrolldiagright(0x00, 0x07); delay(2000); display.startscrolldiagleft(0x00, 0x07); delay(2000); display.stopscroll(); delay(1000); } void testdrawbitmap(void) { display.clearDisplay(); display.drawBitmap( (display.width() - LOGO_WIDTH ) / 2, (display.height() - LOGO_HEIGHT) / 2, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1); display.display(); delay(1000); } #define XPOS 0 #define YPOS 1 #define DELTAY 2 void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) { int8_t f, icons[NUMFLAKES][3]; for(f=0; f< NUMFLAKES; f++) { icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); icons[f][YPOS] = -LOGO_HEIGHT; icons[f][DELTAY] = random(1, 6); Serial.print(F("x: ")); Serial.print(icons[f][XPOS], DEC); Serial.print(F(" y: ")); Serial.print(icons[f][YPOS], DEC); Serial.print(F(" dy: ")); Serial.println(icons[f][DELTAY], DEC); } for(;;) { display.clearDisplay(); for(f=0; f< NUMFLAKES; f++) { display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); } display.display(); delay(200); for(f=0; f< NUMFLAKES; f++) { icons[f][YPOS] += icons[f][DELTAY]; if (icons[f][YPOS] >= display.height()) { icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); icons[f][YPOS] = -LOGO_HEIGHT; icons[f][DELTAY] = random(1, 6); } } } }[/mw_shl_code] |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 板载端口扩展实验 32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) 实验接线:A4---SDA, A5---SCL 项目五:显示汉字“雕爷学编程” */ #include "stdio.h" #include "stdlib.h" #include "U8glib.h" U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI //U8GLIB_SSD1306_128X64 u8g(10, 9, 12, 11); // SW SPI Com: SCL = 10, SDA = 9, CS = 12, DC = 11,RES=RESET static const unsigned char wendu[5][80] PROGMEM={ {0x00,0x14,0xFE,0x24,0x92,0x02,0x92,0x7E,0xBA,0x13,0x92,0x12,0xFE,0x7E,0x82,0x12, 0xBA,0x12,0xAA,0x7E,0xAA,0x12,0xBA,0x12,0x82,0x12,0x82,0x7E,0xA2,0x02,0x41,0x02},/*"雕",0*/ {0x20,0x02,0x10,0x04,0x18,0x0A,0x24,0x11,0xC0,0x00,0x30,0x03,0x0C,0x0C,0x03,0x70, 0xF8,0x0F,0x40,0x08,0x40,0x08,0x40,0x08,0x40,0x0A,0x40,0x04,0x40,0x00,0x40,0x00},/*"爷",1*/ {0x44,0x10,0x88,0x10,0x88,0x08,0x00,0x04,0xFE,0x7F,0x02,0x40,0x01,0x20,0xF8,0x07, 0x00,0x02,0x80,0x01,0xFF,0x7F,0x80,0x00,0x80,0x00,0x80,0x00,0xA0,0x00,0x40,0x00},/*"学",2*/ {0x08,0x01,0x08,0x02,0xC4,0x3F,0x44,0x20,0x52,0x20,0xDF,0x3F,0x48,0x00,0x44,0x00, 0xC2,0x3F,0xDF,0x2A,0xC2,0x2A,0xA0,0x3F,0xB8,0x2A,0xA7,0x2A,0x92,0x22,0x80,0x30},/*"编",3*/ {0x10,0x00,0xB8,0x3F,0x8F,0x20,0x88,0x20,0x88,0x20,0xBF,0x3F,0x08,0x00,0x0C,0x00, 0x9C,0x7F,0x2A,0x04,0x2A,0x04,0x89,0x3F,0x08,0x04,0x08,0x04,0xC8,0x7F,0x08,0x00},/*"程",4*/ }; void draw(void) { //画出16*16汉字 u8g.drawXBMP( 0, 0, 16, 16, wendu[0]); u8g.drawXBMP( 16, 0,16, 16, wendu[1]); u8g.drawXBMP( 32, 0,16, 16, wendu[2]); u8g.drawXBMP( 48, 0,16, 16, wendu[3]); u8g.drawXBMP( 64, 0,16, 16, wendu[4]); u8g.drawXBMP( 0, 16,16, 16, wendu[0]); u8g.drawXBMP( 16, 16,16, 16, wendu[1]); u8g.drawXBMP( 32, 16,16, 16, wendu[2]); u8g.drawXBMP( 48, 16,16, 16, wendu[3]); u8g.drawXBMP( 64, 16,16, 16, wendu[4]); u8g.setFont(u8g_font_osb18);//设置要显示字符的字体 u8g.drawStr(0, 64, "Arduino");//显示字符ABC u8g.setColorIndex(1);//显示对象为不透明 } void setup(void) { } void loop(void) { u8g.firstPage(); do { draw(); } while( u8g.nextPage() ); } [/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed