找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4028|回复: 0
收起左侧

几根线做的Arduino液晶屏旋转编码器带你进入数字化编程之旅

[复制链接]
ID:358930 发表于 2018-11-24 21:47 | 显示全部楼层 |阅读模式
这个板块没有51热闹,但是编程简单,可以有更多的精力去创意,其实没有创意和灵魂的东西永远不会成为经典。

制作出来的实物图如下:
20181124_213554.jpg 20181124_213814.jpg 20181124_213840.jpg 20181124_213625.jpg 20181124_213857.jpg

Arduino程序源码:
  1. #define ENCODER_A_PIN 2
  2. #define ENCODER_B_PIN 3
  3. #define SWITCH_PIN    4
  4. long position;//
  5.       int latchPin = 8;//RS
  6.       int dataPin = 9;//RW
  7.       int clockPin =10;//EN
  8.       unsigned char tabe[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};//查表法//0-9数字
  9. void setup()
  10. {
  11.   //setup our pins 初始化我们的需要的引脚
  12.   pinMode(ENCODER_A_PIN, INPUT);
  13.   pinMode(ENCODER_B_PIN, INPUT);
  14.   pinMode(SWITCH_PIN, INPUT);

  15.   attachInterrupt(0, read_quadrature, CHANGE);

  16.         pinMode(latchPin, OUTPUT); //设置引脚为输出
  17.         pinMode(clockPin, OUTPUT);
  18.         pinMode(dataPin, OUTPUT);  
  19.        Lcdint( );//lcd  

  20. }
  21. ////////////////////////////写SPI时序,具体参考shiftout 命令使用/////////////////////////////
  22.       void WriteByte(int dat)
  23.       {

  24.           digitalWrite(latchPin, HIGH);         
  25.           shiftOut(dataPin, clockPin, MSBFIRST, dat);
  26.           digitalWrite(latchPin, LOW);
  27.       }
  28.       ///////////////////写命令/////////////////////////////////////
  29.       void LcdCommandWrite(int value) {  
  30.         int H_data,L_data;
  31.          H_data = value;
  32.          H_data &= 0xf0;           //屏蔽低4位的数据
  33.          L_data = value;             //xxxx0000格式
  34.          L_data &= 0x0f;           //屏蔽高4位的数据
  35.          L_data <<= 4;             //xxxx0000格式
  36.          WriteByte(0xf8);          //RS=0,写入的是指令;
  37.          WriteByte(H_data);
  38.          WriteByte(L_data);
  39.       }   
  40.       //////////////////////写数据/////////////////////////////////////
  41.       void LcdDataWrite(int value) {  
  42.         int H_data,L_data;
  43.          H_data = value;
  44.          H_data &= 0xf0;           //屏蔽低4位的数据
  45.          L_data = value;             //xxxx0000格式
  46.          L_data &= 0x0f;           //屏蔽高4位的数据
  47.          L_data <<= 4;             //xxxx0000格式
  48.          WriteByte(0xfa);          //RS=1,写入的是数据
  49.          WriteByte(H_data);
  50.          WriteByte(L_data);
  51.       }   
  52.     ////////////////////// /////////////////////////////////////
  53.       void Lcdint(void)
  54.       {  
  55.         LcdCommandWrite(0x30);  //  设定为基本指令         
  56.         delay(5);     
  57.         LcdCommandWrite(0x03);  // //允许输入卷动位址
  58.         delay(5);
  59.         LcdCommandWrite(0x0c);  // //脱离随眠状态,显示打开,关光标,反白关.
  60.         delay(5);
  61.          LcdCommandWrite(0x01);  // 清屏指令.         
  62.         delay(5);     
  63.         LcdCommandWrite(0x06);  // AC自动加一,光标右移,整体显示不移动  
  64.         delay(5);   
  65.       }
  66.    void LCD_zfc(char *p)//定义一个带指针的函数?(字符串)
  67. {
  68.         while(*p!=0)//不能用";"
  69.         LcdDataWrite(*p++);
  70. }   
  71. <font style="font-size: 36.9444px"> /************************显示1 *********************************/                                                           </font>
  72. void display1(void)
  73. {
  74.     int dt1,dt2,dt3,dt4,dt5,dt6,dt7,dt8;

  75.           long temp ;                        //存放温度值的10倍=12345678;  

  76.         temp=position;//旋转
  77.         //temp=12345678;   
  78.         dt1 = temp%10;//1位
  79.         dt2 = temp%100/10;//2位
  80.         dt3 = temp%1000/100;//3位
  81.         dt4 = temp%10000/1000;//4位

  82.         dt5 = temp%100000/10000;//5位
  83.         dt6 = temp%1000000/100000;//6位
  84.         dt7 = temp%10000000/1000000;//7位
  85.         dt8 = temp/10000000;//8位             最高位  
  86.         
  87.     LcdCommandWrite(0x98);//LCD12864_W ((0或1),****)  0写指令  1写数据
  88.    // 0xCE,0xC2,0xB6,0xC8

  89.          LcdDataWrite(0xd0 );
  90.          LcdDataWrite(0xfd );
  91.          LcdDataWrite(0xd7 );
  92.          LcdDataWrite(0xaa );
  93.          LcdDataWrite(0x3a);// .

  94.     // LcdDataWrite(tabe[dt8]);//8位
  95.      //LcdDataWrite(tabe[dt7]);//7位
  96.     // LcdDataWrite(tabe[dt6]);//6位
  97.      //LcdDataWrite(tabe[dt5]);//5位   
  98.      LcdDataWrite(tabe[dt4]);//4位
  99.      LcdDataWrite(tabe[dt3]);//3位
  100.      //LcdDataWrite(0x2e);// .....
  101.      LcdDataWrite(tabe[dt2]);//2位
  102.      LcdDataWrite(tabe[dt1]);//1位   
  103.      LCD_zfc(" R");  
  104. }
  105. void read_quadrature()
  106. {  
  107.   if (digitalRead(ENCODER_A_PIN) == LOW)
  108.   {        
  109.       if (digitalRead(ENCODER_B_PIN) == LOW)
  110.       position++;
  111.       if(position>1000)position=1000;
  112.   }  
  113.   else
  114.   {   
  115.      if (digitalRead(ENCODER_B_PIN) == LOW)
  116.       position--;
  117.       if(position<0)position=0;
  118.   }  
  119.    if (digitalRead( SWITCH_PIN ) == LOW)
  120.   {
  121.   position=0;
  122.   }
  123. }
  124. void loop()
  125. {
  126.         display1();//旋转
  127.         read_quadrature();

  128. }
复制代码

全部资料51hei下载地址:
128显旋转编码器OK2.zip (2.07 KB, 下载次数: 28)

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表