The kernel of cubieboard1 already supports W1 temperature sensor DS18B20 (dependent kernel options and modules include Dallas's 1-wire support, gpio_sunxi, ww.sunxi, ww.gpio, ww.slave_therm ). DIY is an experiment that displays the current time and temperature at the same time on the 8-bit 8-segment LED display module.
Jarry's post long ago (http://forum.cubietech.com/forum.php? MoD = viewthread & tid = 474) describes the use of DS18B20 on cubieboard1, and does not need to be patched or compiled separately. You only need to modify script. Bin according to the Kernel configuration instructions:
In this example, the script. Fex settings are as follows:
# 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/ww.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 b20_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 b20_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;
}