|
|
| 第1行: |
第1行: |
| − | {{Language|Lesson_42--Microduino_Motor_Speed_Measurement}}
| |
| − | {| style="width: 800px;"
| |
| − | |-
| |
| − | |
| |
| − | ==目的==
| |
| | | | |
| − | 本教程将教大家如何用Microduino OLED来显示电机的转速
| |
| − |
| |
| − | ==设备==
| |
| − | *'''[[Microduino-Core]]'''
| |
| − | *'''[[Microduino-FT232R]]'''
| |
| − | *其他硬件设备
| |
| − | **面包板跳线 一盒
| |
| − | **面包板 一块
| |
| − | **Microduino OLED 一块
| |
| − | **电位器 一个
| |
| − | **直流电机 一个
| |
| − | **USB数据连接线 一根
| |
| − |
| |
| − |
| |
| − |
| |
| − | ==原理图==
| |
| − | [[File:第四十二课-Microduino电机转速测量原理图.jpg|600px|center|thumb]]
| |
| − |
| |
| − | ==程序==
| |
| − | <source lang="cpp">
| |
| − |
| |
| − | #include "U8glib.h"
| |
| − |
| |
| − |
| |
| − | int potPin = A0;//电位器引脚定义
| |
| − | int motorPin = 9;//电机引脚定义
| |
| − | int potValue = 0;//电位器的数值
| |
| − | int motorValue = 0;//电机的数值
| |
| − |
| |
| − | U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);//定义OLED连接方式
| |
| − |
| |
| − | //显示速度
| |
| − | void draw(int speedNum) {
| |
| − |
| |
| − | u8g.setFont(u8g_font_unifont);//字体1
| |
| − | u8g.drawStr( 0, 16, "Speed:");
| |
| − | u8g.setFont(u8g_font_7x13);//字体2
| |
| − | u8g.setPrintPos(0, 32);
| |
| − | u8g.print(speedNum);
| |
| − | u8g.print(" (rpm)");
| |
| − | }
| |
| − |
| |
| − |
| |
| − | void setup()
| |
| − | {
| |
| − | }
| |
| − |
| |
| − |
| |
| − | void loop()
| |
| − | {
| |
| − | //读取电位器的值
| |
| − | potValue = analogRead(potPin);
| |
| − | //映射为电机的速率值
| |
| − | motorValue = map(potValue, 0, 1023, 0, 200);
| |
| − | analogWrite(motorPin, motorValue);
| |
| − |
| |
| − | u8g.firstPage();
| |
| − | do {
| |
| − | draw(motorValue);
| |
| − | }
| |
| − | while( u8g.nextPage() );
| |
| − | delay(2);
| |
| − | }
| |
| − |
| |
| − |
| |
| − |
| |
| − | </source>
| |
| − |
| |
| − | ==调试==
| |
| − | 步骤一:把代码复制到IDE中,编译
| |
| − |
| |
| − | 步骤二:电路连接如下图:
| |
| − | [[File:第四十二课-Microduino电机转速测量连接图.jpg|600px|center|thumb]]
| |
| − | 步骤三:运行代码
| |
| − |
| |
| − | 步骤四:转动电位器,查看OLED显示
| |
| − |
| |
| − |
| |
| − | ==结果==
| |
| − |
| |
| − | 转动电位器,可以调整电机的转速,在本例中最大转速为200
| |
| − |
| |
| − | ==视频==
| |