2013-5-25 10:50:58 [显示全部楼层]
6686浏览
查看: 6686|回复: 4

关于12864显示分级菜单求助

[复制链接]
我用12864做一个有多级菜单,能实时显示温度值的东西,我用按键翻页,当按下按键时显示的温度值只是按按键那一时刻的,不会随时变化,我该怎么办才能让它变化,还能按其他键回到下一级菜单,真的很急,请路过的各位能指点一下,不胜感激。

Ricky  NPC

发表于 2013-5-25 12:19:56

有代码会比较容易看。
回复

使用道具 举报

任伊心  学徒
 楼主|

发表于 2013-5-25 12:23:24

本帖最后由 Ricky 于 2013-5-25 12:27 编辑
  1. #include "dht11.h"
  2. #include "LCD12864RSPI.h"
  3. #define DHT11_PIN1 1
  4. #define DHT11_PIN2 2
  5. #define DHT11_PIN3 3
  6. #define DHT11_PIN4 4
  7. #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
  8. unsigned char wendu1[]=" T1:";
  9. unsigned char wendu2[]=" T2:";
  10. unsigned char wendu3[]=" T3:";
  11. unsigned char wendu4[]=" T4:";
  12. unsigned char shidu1[]=" H1:";
  13. unsigned char shidu2[]=" H2:";
  14. unsigned char shidu3[]=" H3:";
  15. unsigned char shidu4[]=" H4:";
  16. unsigned char du[]={0xA1, 0xE6};
  17. unsigned char h[]=" %";
  18. byte dht11_dat[5];
  19. int key=-1,oldkey=-1, NUM_KEYS = 5,adc_key_in,now;
  20. int adc_key_val[5] ={50, 200, 400, 600, 800 };
  21. char dha1[4],dha2[4],dhb1[4],dhb2[4],dhc1[4],dhc2[4],dhd1[4],dhd2[4];
  22. double ma1=0,ma2=0,mb1=0,mb2=0,mc1=0,mc2=0,md1=0,md2=0;
  23. void setup()
  24. {
  25.   Serial.begin(9600);
  26.   LCDA.Initialise(); //INIT SCREEN
  27.   delay(100);
  28.   initDHT( DHT11_PIN1 );
  29.   initDHT( DHT11_PIN2 );
  30.   initDHT( DHT11_PIN3 );
  31.   initDHT( DHT11_PIN4 );
  32. }
  33. void initDHT( int nPinID )
  34. {
  35.    DDRC |= _BV(nPinID);
  36.    PORTC |= _BV(nPinID);  
  37. }
  38. byte reading_dht11_dat( int nPinID )
  39. {
  40.     byte i = 0;
  41.     byte result=0;
  42.     for(i=0; i< 8; i++){
  43.        while(!(PINC & _BV(nPinID)));  // wait for 50us
  44.        delayMicroseconds(30);
  45.                   
  46.        if(PINC & _BV(nPinID))
  47.                result |=(1<<(7-i));
  48.   
  49.        while((PINC & _BV(nPinID)));  // wait '1' finish
  50.      }
  51.      return result;
  52. }
  53. bool Read_Data_From_PIN(int nPinID, byte dht11_dat[] )
  54. {
  55.         byte dht11_in;
  56.         byte i;
  57.         // start condition
  58.         // 1. pull-down i/o pin from 18ms
  59.         PORTC &= ~_BV(nPinID);
  60.         delay(18);
  61.         PORTC |= _BV(nPinID);
  62.         delayMicroseconds(40);
  63.         
  64.         DDRC &= ~_BV(nPinID);
  65.         delayMicroseconds(40);
  66.         
  67.         dht11_in = PINC & _BV(nPinID);
  68.         
  69.         if(dht11_in){
  70.                 Serial.println("dht11 start condition 1 not met");
  71.                 return false;
  72.         }
  73.         delayMicroseconds(80);
  74.         
  75.         dht11_in = PINC & _BV(nPinID);
  76.         
  77.         if(!dht11_in){
  78.                 Serial.println("dht11 start condition 2 not met");
  79.                 return false;
  80.         }
  81.         delayMicroseconds(80);
  82.         // now ready for data reception
  83.         for (i=0; i<5; i++)
  84.                 dht11_dat = reading_dht11_dat( nPinID );
  85.                
  86.         DDRC |= _BV(nPinID);
  87.         PORTC |= _BV(nPinID);
  88.         
  89.         byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
  90.         // check check_sum
  91.         if(dht11_dat[4]!= dht11_check_sum)
  92.         {
  93.                 Serial.println("dht11 checksum error");
  94.                 return false;
  95.         }
  96.         return true;
  97. }
  98. void print_Data(int nPinID, byte dht11_dat[] )
  99. {switch(nPinID)
  100.   {
  101.     case DHT11_PIN1:
  102.                     ma1=dht11_dat[0]+dht11_dat[1];
  103.                     ma2=dht11_dat[2]+dht11_dat[3];
  104.                     dtostrf(ma1,3,1,dha1);
  105.                     dtostrf(ma2,3,1,dha2);
  106.                     LCDA.DisplayString(0,1,shidu1,AR_SIZE(shidu1));
  107.                     LCDA.DisplayString(0,3,(unsigned char *)dha1,AR_SIZE(dha1));
  108.                     LCDA.DisplayString(0,5,h,AR_SIZE(h));
  109.                     LCDA.DisplayString(1,1,wendu1,AR_SIZE(wendu1));
  110.                     LCDA.DisplayString(1,3,(unsigned char *)dha2,AR_SIZE(dha2));
  111.                     LCDA.DisplayString(1,5,du,AR_SIZE(du));
  112.                     break;
  113.      case DHT11_PIN2:              
  114.                     mb1=dht11_dat[0]+dht11_dat[1];
  115.                     mb2=dht11_dat[2]+dht11_dat[3];
  116.                     dtostrf(mb1,3,1,dhb1);
  117.                     dtostrf(mb2,3,1,dhb2);
  118.                     LCDA.DisplayString(2,1,shidu2,AR_SIZE(shidu2));
  119.                     LCDA.DisplayString(2,3,(unsigned char *)dhb1,AR_SIZE(dhb1));
  120.                     LCDA.DisplayString(2,5,h,AR_SIZE(h));
  121.                     LCDA.DisplayString(3,1,wendu2,AR_SIZE(wendu2));
  122.                     LCDA.DisplayString(3,3,(unsigned char *)dhb2,AR_SIZE(dhb2));
  123.                     LCDA.DisplayString(3,5,du,AR_SIZE(du));
  124.                     break;
  125.       case DHT11_PIN3:
  126.                     mc1=dht11_dat[0]+dht11_dat[1];
  127.                     mc2=dht11_dat[2]+dht11_dat[3];
  128.                     dtostrf(mc1,3,1,dhc1);
  129.                     dtostrf(mc2,3,1,dhc2);
  130.                     LCDA.DisplayString(0,1,shidu3,AR_SIZE(shidu3));
  131.                     LCDA.DisplayString(0,3,(unsigned char *)dhc1,AR_SIZE(dhc1));
  132.                     LCDA.DisplayString(0,5,h,AR_SIZE(h));
  133.                     LCDA.DisplayString(1,1,wendu3,AR_SIZE(wendu3));
  134.                     LCDA.DisplayString(1,3,(unsigned char *)dhc2,AR_SIZE(dhc2));
  135.                     LCDA.DisplayString(1,5,du,AR_SIZE(du));
  136.                     break;
  137.      case DHT11_PIN4:              
  138.                     md1=dht11_dat[0]+dht11_dat[1];
  139.                     md2=dht11_dat[2]+dht11_dat[3];
  140.                     dtostrf(md1,3,1,dhd1);
  141.                     dtostrf(md2,3,1,dhd2);
  142.                     LCDA.DisplayString(2,1,shidu4,AR_SIZE(shidu4));
  143.                     LCDA.DisplayString(2,3,(unsigned char *)dhd1,AR_SIZE(dhd1));
  144.                     LCDA.DisplayString(2,5,h,AR_SIZE(h));
  145.                     LCDA.DisplayString(3,1,wendu4,AR_SIZE(wendu4));
  146.                     LCDA.DisplayString(3,3,(unsigned char *)dhd2,AR_SIZE(dhd2));
  147.                     LCDA.DisplayString(3,5,du,AR_SIZE(du));
  148.                     break;
  149.   }
  150.   delay(1000);
  151. }
  152. void  xianshi1()
  153. {
  154. if( Read_Data_From_PIN( DHT11_PIN1, dht11_dat ) )
  155.    {
  156.     print_Data( DHT11_PIN1, dht11_dat );
  157.    }
  158. if( Read_Data_From_PIN( DHT11_PIN2, dht11_dat ) )
  159.   {
  160.    print_Data( DHT11_PIN2, dht11_dat );
  161.    }
  162. }
  163. void xianshi2()
  164. {
  165.   if( Read_Data_From_PIN( DHT11_PIN3, dht11_dat ) )
  166.     {
  167.      print_Data( DHT11_PIN3, dht11_dat );
  168.     }
  169.   if( Read_Data_From_PIN( DHT11_PIN4, dht11_dat ) )
  170.     {
  171.      print_Data( DHT11_PIN4, dht11_dat );
  172.     }
  173. }
  174. int get_key(unsigned int input)
  175. {
  176.     int k;
  177.     for (k = 0; k < NUM_KEYS; k++)
  178.     {
  179.       if (input < adc_key_val[k])
  180.      {
  181.             return k;
  182.         }
  183.    }
  184.        if (k >= NUM_KEYS)k = -1;  // No valid key pressed
  185.        return k;
  186. }
  187. void loop()
  188. {
  189. adc_key_in = analogRead(5);
  190. key = get_key(adc_key_in);
  191.   if(key!=oldkey)
  192.    {
  193.      delay(50);
  194.      adc_key_in = analogRead(5);
  195.      key = get_key(adc_key_in);
  196.      if(key!=oldkey)
  197.      {
  198.        oldkey=key;
  199.        if(key>=0)
  200.        {
  201.          switch(key)
  202.         {
  203.           case 0:
  204.           {
  205.             LCDA.CLEAR();
  206.             xianshi1();
  207.             break;
  208.           }
  209.           case 1:
  210.              {
  211.               LCDA.CLEAR();
  212.               xianshi2();
  213.               break;
  214.              }
  215.          }
  216.        }
  217.      }
  218.    }
  219. }  
复制代码
回复

使用道具 举报

20060606  高级技匠

发表于 2020-8-25 06:22:44

同问同问
回复

使用道具 举报

xiang12138  学徒

发表于 2023-9-3 12:20:49

版主,显示屏和arduino的接线该怎么接啊,能不能发一下接线图。
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail