cubieboard 发表于 2014-8-12 16:29:25

Cubieboard1显示DS18B20温度信息到LED

本帖最后由 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显示模块上同时显示当前时间和气温的实验。

http://forum.cubietech.com/data/attachment/forum/201311/19/155726ks4md4r6ird7y6xx.jpghttp://forum.cubietech.com/data/attachment/forum/201311/19/155732pzfqqzd4aqcmp7dq.jpg

jarry大侠在很久前的帖子(http://forum.cubietech.com/forum.php?mod=viewthread&tid=474)中介绍了DS18B20在cubieboard1上的使用,现在已经不需要打补丁或者另外编译内核模块了。只要按内核配置的说明改好script.bin即可:
http://forum.cubietech.com/data/attachment/forum/201311/19/171051gt77rzsamagr1agv.png
本例中的script.fex相关设置如下:
[*]
[*]
[*]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>
[*]
[*]
[*]gpio = 28
[*]
[*]

复制代码
最后是本例的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
[*]#defineDISPBUF_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;
[*]
[*]
[*]/**
[*]* 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;
[*]    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=digit_tab - '0'];
[*]      dispbuf=digitdot_tab - '0'];
[*]      dispbuf=digit_tab - '0'];
[*]      dispbuf=digit_tab - '0'];
[*]      dispbuf=symbol_tab; // '-'
[*]
[*]      sleep(interval);
[*]    }
[*]}
[*]
[*]/**
[*]* Thread of filling the temperature into display buffer
[*]*/
[*]void* temp_to_dispbuf()
[*]{
[*]    char tempbuf;
[*]    int interval=5; // in seconds;
[*]
[*]    while(1){
[*]      get_temperature(tempbuf, sizeof tempbuf);
[*]
[*]      dispbuf=digit_tab-'0'];
[*]      dispbuf=digitdot_tab-'0'];
[*]      dispbuf=digit_tab-'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;
[*]}
[*]
[*]

复制代码










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

cB品牌直销店 发表于 2014-8-13 17:17:51

:):):):):):)
页: [1]
查看完整版本: Cubieboard1显示DS18B20温度信息到LED