It works well with the Attiny13. Here is the temperature of the DS18B20 displayed on a TM1637 4 digit display.
I did not install the libraries but took the functions and copied it into one single tab, so I could post it here.
/**
Attiny13 temperature meter
Temp sensor Dallas DS18B20
TM1637 4 digit display
*/
#include <util/delay.h>
#define DS18B20_PIN PB1
#define LED_PIN PB0
#define TEMP_MIN (19)
#define TEMP_MAX (22)
#define ONEWIRE_SEARCH_ROM 0xF0
#define ONEWIRE_READ_ROM 0x33
#define ONEWIRE_MATCH_ROM 0x55
#define ONEWIRE_SKIP_ROM 0xCC
#define ONEWIRE_ALARM_SEARCH 0xEC
#define ONEWIRE_PIN_INPUT() (DDRB &= ~_BV(pin))
#define ONEWIRE_PIN_OUTPUT() (DDRB |= _BV(pin))
#define ONEWIRE_PIN_LOW() (PORTB &= ~_BV(pin))
#define ONEWIRE_PIN_HIGH() (PORTB |= _BV(pin))
#define ONEWIRE_PIN_READ() (PINB & _BV(pin))
#define ONEWIRE_RESET_RETRIES_MAX (128)
// Main Settings
#define TM1637_DIO_PIN PB3
#define TM1637_CLK_PIN PB4
#define TM1637_DELAY_US (5)
#define TM1637_BRIGHTNESS_MAX (7)
#define TM1637_POSITION_MAX (4)
// TM1637 commands
#define TM1637_CMD_SET_DATA 0x40
#define TM1637_CMD_SET_ADDR 0xC0
#define TM1637_CMD_SET_DSIPLAY 0x80
// TM1637 data settings (use bitwise OR to contruct complete command)
#define TM1637_SET_DATA_WRITE 0x00 // write data to the display register
#define TM1637_SET_DATA_READ 0x02 // read the key scan data
#define TM1637_SET_DATA_A_ADDR 0x00 // automatic address increment
#define TM1637_SET_DATA_F_ADDR 0x04 // fixed address
#define TM1637_SET_DATA_M_NORM 0x00 // normal mode
#define TM1637_SET_DATA_M_TEST 0x10 // test mode
// TM1637 display control command set (use bitwise OR to consruct complete command)
#define TM1637_SET_DISPLAY_OFF 0x00 // off
#define TM1637_SET_DISPLAY_ON 0x08 // on
#define TM1637_DIO_HIGH() (PORTB |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_LOW() (PORTB &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_OUTPUT() (DDRB |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_INPUT() (DDRB &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_READ() (((PINB & _BV(TM1637_DIO_PIN)) > 0) ? 1 : 0)
#define TM1637_CLK_HIGH() (PORTB |= _BV(TM1637_CLK_PIN))
#define TM1637_CLK_LOW() (PORTB &= ~_BV(TM1637_CLK_PIN))
static void TM1637_send_config(const uint8_t enable, const uint8_t brightness);
static void TM1637_send_command(const uint8_t value);
static void TM1637_start(void);
static void TM1637_stop(void);
static uint8_t TM1637_write_byte(uint8_t value);
static uint8_t _config = TM1637_SET_DISPLAY_ON | TM1637_BRIGHTNESS_MAX;
static uint8_t _segments = 0xff;
PROGMEM const uint8_t _digit2segments[] =
{
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
/**
* Initialize TM1637 display driver.
* Clock pin (TM1637_CLK_PIN) and data pin (TM1637_DIO_PIN)
* are defined at the top of this file.
*/
void TM1637_init(const uint8_t enable, const uint8_t brightness);
/**
* Turn display on/off.
* value: 1 - on, 0 - off
*/
void TM1637_enable(const uint8_t value);
/**
* Set display brightness.
* Min value: 0
* Max value: 7
*/
void TM1637_set_brightness(const uint8_t value);
/**
* Display raw segments at position (0x00..0x03)
*
* bits:
* -- 0 --
* | |
* 5 1
* | |
* -- 6 --
* | |
* 4 2
* | |
* -- 3 --
*
* Example segment configurations:
* - for character 'H', segments=0b01110110
* - for character '-', segments=0b01000000
* - etc.
*/
void TM1637_display_segments(const uint8_t position, const uint8_t segments);
/**
* Display digit ('0'..'9') at position (0x00..0x03)
*/
void TM1637_display_digit(const uint8_t position, const uint8_t digit);
/**
* Display colon on/off.
* value: 1 - on, 0 - off
*/
void TM1637_display_colon(const uint8_t value);
/**
* Clear all segments (including colon).
*/
void TM1637_clear(void);
int main(void) {
uint8_t t;
DDRB |= _BV(LED_PIN); // set LED pin as OUTPUT
/* setup */
TM1637_init(1/*enable*/, 5/*brightness*/);
TM1637_display_digit(0, 10); // blank it
// TM1637_display_digit(1,1);
// TM1637_display_digit(2,10);// above 9 blanks position
/* loop */
while (1) {
t = read_temperature(DS18B20_PIN);
if (t < TEMP_MIN || t > TEMP_MAX) {
PORTB |= _BV(LED_PIN); // light up the alarm LED
} else {
PORTB &= ~_BV(LED_PIN); // turn the alarm LED off
}
// TM1637_display_digit(0, ((t / 1000) % 10));
TM1637_display_digit(1, ((t / 100) % 10));
TM1637_display_digit(2, ((t / 10) % 10));
TM1637_display_digit(3, (t % 10));
TM1637_display_colon(1);
_delay_ms(200);
circle();
TM1637_display_colon(0);
_delay_ms(200);
circle();
}
}
uint8_t read_temperature(uint8_t pin) {
uint16_t t;
onewire_reset(pin); // 1-Wire reset
onewire_write(pin, ONEWIRE_SKIP_ROM); // to all devices on the bus
onewire_write(pin, 0x44); // send DS18B20 command, "CONVERT T"
onewire_reset(pin); // 1-Wire reset
onewire_write(pin, ONEWIRE_SKIP_ROM); // to all devices on the bus
onewire_write(pin, 0xBE); // send DS18B20 command, "READ SCRATCHPAD"
t = onewire_read(pin); // read temperature low byte
t |= (uint16_t)onewire_read(pin) << 8; // and high byte
t = ((t >> 4) * 100 + ((t << 12) / 6553) * 10) / 100; // decode temp
return (uint8_t)t;
}
void circle(void) {
static uint8_t k = 1;
TM1637_display_segments(0, k);
k = k << 1;
if (k > B00100000) k = 1;
}
uint8_t
onewire_reset(uint8_t pin)
{
uint8_t retval, retries;
ONEWIRE_PIN_LOW();
ONEWIRE_PIN_INPUT();
retries = ONEWIRE_RESET_RETRIES_MAX;
while (!ONEWIRE_PIN_READ()) {
if (--retries == 0) {
return (2);
}
_delay_us(1);
}
ONEWIRE_PIN_OUTPUT();
_delay_us(480);
ONEWIRE_PIN_INPUT();
_delay_us(66);
retval = ONEWIRE_PIN_READ();
_delay_us(414);
return (retval);
}
static uint8_t onewire_bit(uint8_t pin, uint8_t value) {
uint8_t sreg;
sreg = SREG;
cli();
ONEWIRE_PIN_OUTPUT();
_delay_us(1);
if (value) {
ONEWIRE_PIN_INPUT();
}
_delay_us(14);
value = !(ONEWIRE_PIN_READ() == 0);
_delay_us(45);
ONEWIRE_PIN_INPUT();
SREG = sreg;
return value;
}
uint8_t onewire_write(uint8_t pin, uint8_t value) {
uint8_t i, r;
for (i = 0; i < 8; ++i) {
r = onewire_bit(pin, value & 0x01);
value >>= 1;
if (r) {
value |= 0x80;
}
}
return value;
}
uint8_t
onewire_read(uint8_t pin)
{
return onewire_write(pin, 0xff);
}
void TM1637_init(const uint8_t enable, const uint8_t brightness) {
DDRB |= (_BV(TM1637_DIO_PIN) | _BV(TM1637_CLK_PIN));
PORTB &= ~(_BV(TM1637_DIO_PIN) | _BV(TM1637_CLK_PIN));
TM1637_send_config(enable, brightness);
}
void TM1637_enable(const uint8_t value) {
TM1637_send_config(value, _config & TM1637_BRIGHTNESS_MAX);
}
void TM1637_set_brightness(const uint8_t value) {
TM1637_send_config(_config & TM1637_SET_DISPLAY_ON, value & TM1637_BRIGHTNESS_MAX);
}
void TM1637_display_segments(const uint8_t position, const uint8_t segments) {
TM1637_send_command(TM1637_CMD_SET_DATA | TM1637_SET_DATA_F_ADDR);
TM1637_start();
TM1637_write_byte(TM1637_CMD_SET_ADDR | (position & (TM1637_POSITION_MAX - 1)));
TM1637_write_byte(segments);
TM1637_stop();
}
void TM1637_display_digit(const uint8_t position, const uint8_t digit) {
uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);
if (position == 0x01) {
segments = segments | (_segments & 0x80);
_segments = segments;
}
TM1637_display_segments(position, segments);
}
void TM1637_display_colon(const uint8_t value) {
if (value) {
_segments |= 0x80;
} else {
_segments &= ~0x80;
}
TM1637_display_segments(0x01, _segments);
}
void TM1637_clear(void) {
uint8_t i;
for (i = 0; i < TM1637_POSITION_MAX; ++i) {
TM1637_display_segments(i, 0x00);
}
}
void TM1637_send_config(const uint8_t enable, const uint8_t brightness) {
_config = (enable ? TM1637_SET_DISPLAY_ON : TM1637_SET_DISPLAY_OFF) |
(brightness > TM1637_BRIGHTNESS_MAX ? TM1637_BRIGHTNESS_MAX : brightness);
TM1637_send_command(TM1637_CMD_SET_DSIPLAY | _config);
}
void TM1637_send_command(const uint8_t value) {
TM1637_start();
TM1637_write_byte(value);
TM1637_stop();
}
void TM1637_start(void) {
TM1637_DIO_HIGH();
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_LOW();
}
void TM1637_stop(void) {
TM1637_CLK_LOW();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_LOW();
_delay_us(TM1637_DELAY_US);
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_HIGH();
}
uint8_t TM1637_write_byte(uint8_t value) {
uint8_t i, ack;
for (i = 0; i < 8; ++i, value >>= 1) {
TM1637_CLK_LOW();
_delay_us(TM1637_DELAY_US);
if (value & 0x01) {
TM1637_DIO_HIGH();
} else {
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
_delay_us(TM1637_DELAY_US);
ack = TM1637_DIO_READ();
if (ack) {
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
_delay_us(TM1637_DELAY_US);
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
TM1637_CLK_LOW();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_OUTPUT();
return ack;
}
Sketch uses 754 bytes (73%) of program storage space. Maximum is 1024 bytes.
Global variables use 2 bytes (3%) of dynamic memory, leaving 62 bytes for local variables. Maximum is 64 bytes.