CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 22392|回复: 16

Cubieboard1显示DS18B20温度信息到LED

[复制链接]
发表于 2013-11-19 16:09:26 | 显示全部楼层 |阅读模式
本帖最后由 soloforce 于 2013-11-19 17:10 编辑

Cubieboard1的内核已经支持w1温度传感器DS18B20 (依赖的内核选项和模块包括dallas's 1-wire support, gpio_sunxi, w1_sunxi, w1_gpio, w1_slave_therm )。 下图的DIY是在8位8段LED显示模块上同时显示当前时间和气温的实验。

P31119-153333_2.jpg P31119-153637.jpg

jarry大侠在很久前的帖子(http://forum.cubietech.com/forum.php?mod=viewthread&tid=474)中介绍了DS18B20在cubieboard1上的使用,现在已经不需要打补丁或者另外编译内核模块了。只要按内核配置的说明改好script.bin即可:
2013-11-19-171003_878x658_scrot.png
本例中的script.fex相关设置如下:

  1. [gpio_para]
  2. gpio_used = 1
  3. gpio_num = 31
  4. ...
  5. gpio_pin_28 = port:PB10<0><default><default><0>
  6. gpio_pin_29 = port:PB11<1><default><default><default>
  7. gpio_pin_30 = port:PB13<1><default><default><default>
  8. gpio_pin_31 = port:PH07<1><default><default><default>

  9. [w1_para]
  10. gpio = 28

复制代码
最后是本例的C代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <signal.h>
  6. #include <pthread.h>
  7. #include "gpio_lib.h"

  8. #define LSBFIRST 0
  9. #define MSBFIRST 1
  10. #define  DISPBUF_LEN 8

  11. typedef unsigned char byte;

  12. // w1-thermal(DS18B20) device file, change it in your case.
  13. const char* DS18B20_DEVICE="/sys/bus/w1/devices/28-000004f0230d/w1_slave";

  14. /*
  15. * 74HC595 relative stuff
  16. */
  17. // Cubieboard pin connected to ST_CP of 74HC595 (RCK)
  18. unsigned int latchPin = SUNXI_GPB(11);
  19. // Cubieboard pin connected to SH_CP of 74HC595 (SCK)
  20. unsigned int clockPin = SUNXI_GPB(13);
  21. // Cubieboard pin connected to DS of 74HC595 (DIN)
  22. unsigned int dataPin = SUNXI_GPH(7);

  23. /*
  24. * Display relative stuff
  25. */
  26. // Digits: "0,1,2,....,9" for common anode 8-segment-LED
  27. unsigned int digit_tab[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
  28.                         0x82, 0xf8, 0x80, 0x90};
  29. // Digits with a 'dot' at the right-bottom corner
  30. unsigned int digitdot_tab[] = { 0xc0&0x7f, 0xf9&0x7f, 0xa4&0x7f,
  31.                         0xb0&0x7f, 0x99&0x7f, 0x92&0x7f, 0x82&0x7f,
  32.                         0xf8&0x7f, 0x80&0x7f, 0x90&0x7f};
  33. // Symbols: 'clear', 'dot', '-'
  34. unsigned int symbol_tab[]={ 0xff, 0x7f, 0xbf};
  35. // LED display buffer
  36. static char dispbuf[DISPBUF_LEN];


  37. /**
  38. * Set Cubieboard's GPIO port-D pin I/O mode: INPUT/OUTPUT
  39. */
  40. void pinMode(unsigned int pin, unsigned int io_mode)
  41. {
  42.     if (SETUP_OK != sunxi_gpio_set_cfgpin(pin, io_mode))
  43.     {
  44.         printf("Failed to config GPIO pin\n");
  45.     }
  46. }

  47. /**
  48. * Set Cubieboard's GPIO port-D pin value(LOW/HIGH)
  49. */
  50. void digitalWrite(int pin, int hl)
  51. {
  52.     if (sunxi_gpio_output(pin, hl))
  53.     {
  54.         printf("Failed to set GPIO pin value\n");
  55.     }
  56. }

  57. /**
  58. * Arduino shiftOut:
  59. * https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/wiring_shift.c
  60. */
  61. void shiftOut(unsigned int dataPin, unsigned int clockPin, int bitOrder, byte val)
  62. {
  63.     byte i;
  64.     for (i = 0; i < 8; i++)
  65.     {
  66.         if (bitOrder == LSBFIRST)
  67.             digitalWrite(dataPin, ! !(val & (1 << i)));
  68.         else
  69.             digitalWrite(dataPin, ! !(val & (1 << (7 - i))));

  70.         digitalWrite(clockPin, HIGH);
  71.         digitalWrite(clockPin, LOW);
  72.     }
  73. }

  74. /**
  75. * Initialize the GPIO & relative pins
  76. */
  77. void init_gpio()
  78. {
  79.     if (SETUP_OK != sunxi_gpio_init())
  80.     {
  81.         printf("Failed to initialize GPIO\n");
  82.     }
  83.     pinMode(latchPin, OUTPUT);
  84.     pinMode(clockPin, OUTPUT);
  85.     pinMode(dataPin, OUTPUT);
  86. }

  87. /**
  88. * Get current temperature from the w1-thermal device
  89. */
  90. void get_temperature(char* tempbuf, int len)
  91. {
  92.     FILE* fp=fopen(DS18B20_DEVICE,"r");
  93.     char* line=NULL;
  94.     char* temperature_tok=NULL;
  95.     int temperature=0;
  96.    
  97.     int n;
  98.     if(!fp){
  99.         fprintf(stderr,"Failed to open device(%s) file!\n", DS18B20_DEVICE);
  100.         return;
  101.     }

  102.     // skip the first line
  103.     getline(&line, &n, fp);
  104.     free(line);
  105.     line=NULL;

  106.     // get the temperature line
  107.     getline(&line, &n, fp);
  108.     strtok(line,"=");
  109.     temperature_tok=strtok(NULL,"\n");
  110.    
  111.     strncpy(tempbuf, temperature_tok, len);

  112.     free(line);
  113.     fclose(fp);
  114. }

  115. /**
  116. * Thread of filling the time infomation into display buffer
  117. */
  118. void* time_to_dispbuf()
  119. {
  120.     time_t timep;
  121.     struct tm *p;
  122.     char timebuf[4];
  123.     int interval=1; // in seconds

  124.     while(1){
  125.         // get localtime
  126.         time(&timep);
  127.         p = localtime(&timep);
  128.         sprintf(timebuf, "%02d%02d", p->tm_hour, p->tm_min);

  129.         dispbuf[0]=digit_tab[timebuf[0] - '0'];
  130.         dispbuf[1]=digitdot_tab[timebuf[1] - '0'];
  131.         dispbuf[2]=digit_tab[timebuf[2] - '0'];
  132.         dispbuf[3]=digit_tab[timebuf[3] - '0'];
  133.         dispbuf[4]=symbol_tab[2]; // '-'

  134.         sleep(interval);
  135.     }
  136. }

  137. /**
  138. * Thread of filling the temperature into display buffer
  139. */
  140. void* temp_to_dispbuf()
  141. {
  142.     char tempbuf[3];
  143.     int interval=5; // in seconds;
  144.    
  145.     while(1){
  146.         get_temperature(tempbuf, sizeof tempbuf);

  147.         dispbuf[5]=digit_tab[tempbuf[0]-'0'];
  148.         dispbuf[6]=digitdot_tab[tempbuf[1]-'0'];
  149.         dispbuf[7]=digit_tab[tempbuf[2]-'0'];
  150.         
  151.         sleep(interval);
  152.     }
  153.    
  154. }

  155. int main(int argc, char **argv)
  156. {
  157.     pthread_t get_time_thread, get_temp_thread;
  158.     void * thread_ret;

  159.     init_gpio();

  160.     pthread_create( &get_time_thread, NULL, time_to_dispbuf, NULL);
  161.     pthread_create( &get_temp_thread, NULL, temp_to_dispbuf, NULL);
  162.   
  163.     while (1)
  164.     {

  165.         int i;
  166.         for(i=0;i<DISPBUF_LEN;i++){
  167.             digitalWrite(latchPin, 0);
  168.             shiftOut(dataPin, clockPin, MSBFIRST, 1<<i);
  169.             shiftOut(dataPin, clockPin, MSBFIRST, dispbuf[i]);
  170.             digitalWrite(latchPin, 1);
  171.             usleep(2000);
  172.         }
  173.         
  174.     }

  175.     pthread_join(get_time_thread,&thread_ret);
  176.     pthread_join(get_temp_thread,&thread_ret);
  177.     return 0;
  178. }

复制代码
回复

使用道具 举报

发表于 2014-3-10 08:33:27 | 显示全部楼层
这个可以有
回复 支持 反对

使用道具 举报

发表于 2014-3-20 12:59:16 | 显示全部楼层
能不能直接发一下w1_sun4i.ko这个模块,我的也是3.4.61. 编译不过
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-21 09:25:12 | 显示全部楼层
4207317 发表于 2014-3-20 12:59
能不能直接发一下w1_sun4i.ko这个模块,我的也是3.4.61. 编译不过

这是我自己编译的内核模块,for CB1.
cb1-w1-ds18b20.tar.gz (77.56 KB, 下载次数: 11)
回复 支持 反对

使用道具 举报

发表于 2014-3-22 10:25:31 | 显示全部楼层
非常感谢,这个目前我已经搞定了。 是我操作过程中的一个失误。

http://www.fdyu.com/monitor.html

这是我ds18b20的实时温度
回复 支持 反对

使用道具 举报

发表于 2014-3-27 03:22:10 | 显示全部楼层
楼主,赞一个
回复 支持 反对

使用道具 举报

发表于 2014-4-1 09:26:11 | 显示全部楼层
是一定要让gpio_pin_28对应PB10这个管脚吗?能够随意使用gpio_pin_XX,我一直对这个问题很困扰。

非常感谢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-1 11:42:41 | 显示全部楼层
Darwin 发表于 2014-4-1 09:26
是一定要让gpio_pin_28对应PB10这个管脚吗?能够随意使用gpio_pin_XX,我一直对这个问题很困扰。

非常感谢 ...

gpio_pin_xxx 中的xxx 要和你自己定义的gpio_num = yyy 有关系,必须满足xxx<=yyy;

xxx 不一定非得指定PB10,可以自己指定为在gpio_para段中分配的管脚。
回复 支持 反对

使用道具 举报

发表于 2014-4-1 16:09:14 | 显示全部楼层
soloforce 发表于 2014-4-1 11:42
gpio_pin_xxx 中的xxx 要和你自己定义的gpio_num = yyy 有关系,必须满足xxx

gpio_pin_28 = portB10<0><default><default><0>

[w1_para]
gpio = 28

另外这要这两个参数保持一致就可以了是吗,如果我把这两个参数的28都换成29,one-wire驱动也应该能够识别吧?

非常感谢...

回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-1 21:04:45 | 显示全部楼层
应该是的。
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-3-29 04:33 , Processed in 0.027624 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部