极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7858|回复: 1

Cubieboard1显示DS18B20温度信息到LED

[复制链接]
发表于 2014-8-12 16:29:25 | 显示全部楼层 |阅读模式
本帖最后由 cubieboard 于 2014-8-12 16:36 编辑

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



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

本例中的script.fex相关设置如下:
  • [gpio_para]
  • gpio_used = 1
  • gpio_num = 31
  • ...
  • gpio_pin_28 = port:PB10<0><default><default><0>
  • gpio_pin_29 = port:PB11<1><default><default><default>
  • gpio_pin_30 = port:PB13<1><default><default><default>
  • gpio_pin_31 = port:PH07<1><default><default><default>
  • [w1_para]
  • gpio = 28


[color=rgb(46, 166, 255) !important]复制代码

最后是本例的C代码
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <string.h>
  • #include <time.h>
  • #include <signal.h>
  • #include <pthread.h>
  • #include "gpio_lib.h"
  • #define LSBFIRST 0
  • #define MSBFIRST 1
  • #define  DISPBUF_LEN 8
  • typedef unsigned char byte;
  • // w1-thermal(DS18B20) device file, change it in your case.
  • const char* DS18B20_DEVICE="/sys/bus/w1/devices/28-000004f0230d/w1_slave";
  • /*
  • * 74HC595 relative stuff
  • */
  • // Cubieboard pin connected to ST_CP of 74HC595 (RCK)
  • unsigned int latchPin = SUNXI_GPB(11);
  • // Cubieboard pin connected to SH_CP of 74HC595 (SCK)
  • unsigned int clockPin = SUNXI_GPB(13);
  • // Cubieboard pin connected to DS of 74HC595 (DIN)
  • unsigned int dataPin = SUNXI_GPH(7);
  • /*
  • * Display relative stuff
  • */
  • // Digits: "0,1,2,....,9" for common anode 8-segment-LED
  • unsigned int digit_tab[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
  •                         0x82, 0xf8, 0x80, 0x90};
  • // Digits with a 'dot' at the right-bottom corner
  • unsigned int digitdot_tab[] = { 0xc0&0x7f, 0xf9&0x7f, 0xa4&0x7f,
  •                         0xb0&0x7f, 0x99&0x7f, 0x92&0x7f, 0x82&0x7f,
  •                         0xf8&0x7f, 0x80&0x7f, 0x90&0x7f};
  • // Symbols: 'clear', 'dot', '-'
  • unsigned int symbol_tab[]={ 0xff, 0x7f, 0xbf};
  • // LED display buffer
  • static char dispbuf[DISPBUF_LEN];
  • /**
  • * Set Cubieboard's GPIO port-D pin I/O mode: INPUT/OUTPUT
  • */
  • void pinMode(unsigned int pin, unsigned int io_mode)
  • {
  •     if (SETUP_OK != sunxi_gpio_set_cfgpin(pin, io_mode))
  •     {
  •         printf("Failed to config GPIO pin\n");
  •     }
  • }
  • /**
  • * Set Cubieboard's GPIO port-D pin value(LOW/HIGH)
  • */
  • void digitalWrite(int pin, int hl)
  • {
  •     if (sunxi_gpio_output(pin, hl))
  •     {
  •         printf("Failed to set GPIO pin value\n");
  •     }
  • }
  • /**
  • * Arduino shiftOut:
  • * https://github.com/arduino/Ardui ... uino/wiring_shift.c
  • */
  • void shiftOut(unsigned int dataPin, unsigned int clockPin, int bitOrder, byte val)
  • {
  •     byte i;
  •     for (i = 0; i < 8; i++)
  •     {
  •         if (bitOrder == LSBFIRST)
  •             digitalWrite(dataPin, ! !(val & (1 << i)));
  •         else
  •             digitalWrite(dataPin, ! !(val & (1 << (7 - i))));
  •         digitalWrite(clockPin, HIGH);
  •         digitalWrite(clockPin, LOW);
  •     }
  • }
  • /**
  • * Initialize the GPIO & relative pins
  • */
  • void init_gpio()
  • {
  •     if (SETUP_OK != sunxi_gpio_init())
  •     {
  •         printf("Failed to initialize GPIO\n");
  •     }
  •     pinMode(latchPin, OUTPUT);
  •     pinMode(clockPin, OUTPUT);
  •     pinMode(dataPin, OUTPUT);
  • }
  • /**
  • * Get current temperature from the w1-thermal device
  • */
  • void get_temperature(char* tempbuf, int len)
  • {
  •     FILE* fp=fopen(DS18B20_DEVICE,"r");
  •     char* line=NULL;
  •     char* temperature_tok=NULL;
  •     int temperature=0;
  •     int n;
  •     if(!fp){
  •         fprintf(stderr,"Failed to open device(%s) file!\n", DS18B20_DEVICE);
  •         return;
  •     }
  •     // skip the first line
  •     getline(&line, &n, fp);
  •     free(line);
  •     line=NULL;
  •     // get the temperature line
  •     getline(&line, &n, fp);
  •     strtok(line,"=");
  •     temperature_tok=strtok(NULL,"\n");
  •     strncpy(tempbuf, temperature_tok, len);
  •     free(line);
  •     fclose(fp);
  • }
  • /**
  • * Thread of filling the time infomation into display buffer
  • */
  • void* time_to_dispbuf()
  • {
  •     time_t timep;
  •     struct tm *p;
  •     char timebuf[4];
  •     int interval=1; // in seconds
  •     while(1){
  •         // get localtime
  •         time(&timep);
  •         p = localtime(&timep);
  •         sprintf(timebuf, "%02d%02d", p->tm_hour, p->tm_min);
  •         dispbuf[0]=digit_tab[timebuf[0] - '0'];
  •         dispbuf[1]=digitdot_tab[timebuf[1] - '0'];
  •         dispbuf[2]=digit_tab[timebuf[2] - '0'];
  •         dispbuf[3]=digit_tab[timebuf[3] - '0'];
  •         dispbuf[4]=symbol_tab[2]; // '-'
  •         sleep(interval);
  •     }
  • }
  • /**
  • * Thread of filling the temperature into display buffer
  • */
  • void* temp_to_dispbuf()
  • {
  •     char tempbuf[3];
  •     int interval=5; // in seconds;
  •     while(1){
  •         get_temperature(tempbuf, sizeof tempbuf);
  •         dispbuf[5]=digit_tab[tempbuf[0]-'0'];
  •         dispbuf[6]=digitdot_tab[tempbuf[1]-'0'];
  •         dispbuf[7]=digit_tab[tempbuf[2]-'0'];
  •         sleep(interval);
  •     }
  • }
  • int main(int argc, char **argv)
  • {
  •     pthread_t get_time_thread, get_temp_thread;
  •     void * thread_ret;
  •     init_gpio();
  •     pthread_create( &get_time_thread, NULL, time_to_dispbuf, NULL);
  •     pthread_create( &get_temp_thread, NULL, temp_to_dispbuf, NULL);
  •     while (1)
  •     {
  •         int i;
  •         for(i=0;i<DISPBUF_LEN;i++){
  •             digitalWrite(latchPin, 0);
  •             shiftOut(dataPin, clockPin, MSBFIRST, 1<<i);
  •             shiftOut(dataPin, clockPin, MSBFIRST, dispbuf);
  •             digitalWrite(latchPin, 1);
  •             usleep(2000);
  •         }
  •     }
  •     pthread_join(get_time_thread,&thread_ret);
  •     pthread_join(get_temp_thread,&thread_ret);
  •     return 0;
  • }


[color=rgb(46, 166, 255) !important]复制代码








原文作者:soloforce
原文链接:http://forum.cubietech.com/forum ... 1545&extra=page%3D1

回复

使用道具 举报

发表于 2014-8-13 17:17:51 | 显示全部楼层
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 21:30 , Processed in 0.042172 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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