Тема: Attiny10 + TM1637
Плата Attiny10Core Arduino 1МГц
#include <avr/io.h>
// TM1637 Macros
#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))
// TM1637 Main Settings
#define TM1637_DIO_PIN PB0
#define TM1637_CLK_PIN PB1
#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
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;
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);
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
};
// DELAY library CODE
inline void dly() { //пустая команда
__asm__("NOP");
}
void delay1sec(void)
{
asm volatile (
" ldi r18, 6" "\n"
" ldi r19, 19" "\n"
" ldi r20, 174" "\n"
"1: dec r20" "\n"
" brne 1b" "\n"
" dec r19" "\n"
" brne 1b" "\n"
" dec r18" "\n"
" brne 1b" "\n"
" rjmp 1f" "\n"
"1:" "\n"
);
};
// TM1637 library
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 ? _digit2segments[(digit)] : 0x00);
// uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);
if (position == 0x01) {
segments = segments | (_segments & 0x00);//tochka 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();
dly();
TM1637_DIO_LOW();
}
void
TM1637_stop(void)
{
TM1637_CLK_LOW();
dly();
TM1637_DIO_LOW();
dly();
TM1637_CLK_HIGH();
dly();
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();
dly();
if (value & 0x01) {
TM1637_DIO_HIGH();
} else {
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
dly();
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
dly();
ack = TM1637_DIO_READ();
if (ack) {
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
dly();
TM1637_CLK_HIGH();
dly();
TM1637_CLK_LOW();
dly();
TM1637_DIO_OUTPUT();
return ack;
}
int main(void)
{
/* setup */
TM1637_init(1/*enable*/, 1/*brightness*/);
for(byte i=0;i<5;i++){TM1637_display_segments(i,0b01000000);}
// delay 1 for diagnostic
delay1sec();
while (1) {
byte c;
c++;
TM1637_display_digit(2, c/10%10);
TM1637_display_digit(3, c%10);
TM1637_display_digit(1, c/100%10);
TM1637_display_segments(0, 118);
delay1sec();
// for(byte i=0;i<5;i++){ delay1sec();}//delay 5s.
};
}
Вольтметр 5в. PB2 input.
#include <avr/io.h>
// TM1637 Macros
#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))
// TM1637 Main Settings
#define TM1637_DIO_PIN PB0
#define TM1637_CLK_PIN PB1
#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
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;
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);
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
};
// DELAY library CODE
inline void dly() { //пустая команда
__asm__("NOP");
}
void delay1sec(void)
{
asm volatile (
" ldi r18, 6" "\n"
" ldi r19, 19" "\n"
" ldi r20, 174" "\n"
"1: dec r20" "\n"
" brne 1b" "\n"
" dec r19" "\n"
" brne 1b" "\n"
" dec r18" "\n"
" brne 1b" "\n"
" rjmp 1f" "\n"
"1:" "\n"
);
};
// TM1637 library
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 ? _digit2segments[(digit)] : 0x00);
// uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);
if (position == 0x01) {
segments = segments | (_segments & 0x00);//tochka 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();
dly();
TM1637_DIO_LOW();
}
void
TM1637_stop(void)
{
TM1637_CLK_LOW();
dly();
TM1637_DIO_LOW();
dly();
TM1637_CLK_HIGH();
dly();
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();
dly();
if (value & 0x01) {
TM1637_DIO_HIGH();
} else {
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
dly();
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
dly();
ack = TM1637_DIO_READ();
if (ack) {
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
dly();
TM1637_CLK_HIGH();
dly();
TM1637_CLK_LOW();
dly();
TM1637_DIO_OUTPUT();
return ack;
}
uint16_t VOLT;
int main(void)
{
DDRB = 0<<DDB3;
TM1637_init(1/*enable*/, 1/*brightness*/);
for(byte i=0;i<5;i++){TM1637_display_segments(i,0b01000000);}
// delay 1 for diagnostic
delay1sec();
while (1) {
PrepareADC(ADC2);
ADCSRA |= (1<<ADSC); // start reading
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
VOLT =(ADCL*50)/256;
TM1637_display_digit(2,VOLT %10);
TM1637_display_digit(3, VOLT%1);
TM1637_display_digit(1, VOLT/10%10);
TM1637_display_segments(0, 62);
delay1sec();
// for(byte i=0;i<5;i++){ delay1sec();}//delay 5s.
};
}
void PrepareADC( uint8_t ADCF )
{
PRR &= ~(1<<PRADC); // ADC power enable
ADMUX = ADCF;
ADCSRB = 0;
ADCSRA = (1<<ADEN) | (0<<ADATE) | (0<<ADIE) | (0<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); // ADC on, AutoTrigger off, Interrupt off, divider = 2
dly();
}
+ds18b20 . Работает не стабильно.
#include <avr/io.h>
// TM1637 Macros
#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))
// TM1637 Main Settings
#define TM1637_DIO_PIN PB0
#define TM1637_CLK_PIN PB1
#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
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;
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);
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
};
// DELAY library CODE
inline void dly() { //пустая команда
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
}
void delay1sec(void)
{
asm volatile (
" ldi r18, 6" "\n"
" ldi r19, 19" "\n"
" ldi r20, 174" "\n"
"1: dec r20" "\n"
" brne 1b" "\n"
" dec r19" "\n"
" brne 1b" "\n"
" dec r18" "\n"
" brne 1b" "\n"
" rjmp 1f" "\n"
"1:" "\n"
);
};
// TM1637 library
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 ? _digit2segments[(digit)] : 0x00);
// uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);
if (position == 0x01) {
segments = segments | (_segments & 0x00);//tochka 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();
dly();
TM1637_DIO_LOW();
}
void
TM1637_stop(void)
{
TM1637_CLK_LOW();
dly();
TM1637_DIO_LOW();
dly();
TM1637_CLK_HIGH();
dly();
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();
dly();
if (value & 0x01) {
TM1637_DIO_HIGH();
} else {
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
dly();
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
dly();
ack = TM1637_DIO_READ();
if (ack) {
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
dly();
TM1637_CLK_HIGH();
dly();
TM1637_CLK_LOW();
dly();
TM1637_DIO_OUTPUT();
return ack;
}
int temp;
#define OUT 2 // PB2
int main(void)
{
ACSR ^= ~_BV(ACIE);//comparator off
ACSR |= ACD;
PRR |= (1<<PRADC);// ADC off
TM1637_init(1/*enable*/, 1/*brightness*/);
for(byte i=0;i<5;i++){TM1637_display_segments(i,0b01000000);}
// delay 1 for diagnostic
delay1sec();
while (1) {
temp=read_temp();
TM1637_display_digit(1,temp /10%10);
TM1637_display_digit(2, temp%10);
TM1637_display_digit(0, temp/100%10);
TM1637_display_segments(3, 99);
delay1sec();
// for(byte i=0;i<5;i++){ delay1sec();}//delay 5s.
};
}
/////////////////////
// reset
uint8_t therm_reset(){
uint8_t i;
PORTB &= ~(1 << OUT);
DDRB |= (1 << OUT);
asm volatile (
" ldi r18, 160 \n"
"1: dec r18 \n"
" brne 1b \n"
);
//delayMicroseconds(480);
DDRB &= ~(1 << OUT);
asm volatile (
" ldi r18, 20 \n"
"1: dec r18 \n"
" brne 1b \n"
);
// delayMicroseconds(60);
i=((PINB >> OUT) & 1);
asm volatile (
" ldi r18, 140 \n"
"1: dec r18 \n"
" brne 1b \n"
);
// delayMicroseconds(420);
return i;
}
// write bit
void therm_write_bit(uint8_t bit){
PORTB &= ~(1 << OUT);
DDRB |= (1 << OUT);
asm volatile (
" nop \n"
);
// delayMicroseconds(1);
if(bit) DDRB &= ~(1 << OUT);
asm volatile (
" ldi r18, 20 \n"
"1: dec r18 \n"
" brne 1b \n"
);
// delayMicroseconds(60);
DDRB &= ~(1 << OUT);
}
// read bit
uint8_t therm_read_bit(void){
uint8_t bit=0;
PORTB &= ~(1 << OUT);
DDRB |= (1 << OUT);
asm volatile (
" nop \n"
);
// delayMicroseconds(1);
DDRB &= ~(1 << OUT);
asm volatile (
" ldi r18, 4 \n"
"1: dec r18 \n"
" brne 1b \n"
" rjmp 1f \n"
"1: \n"
);
// delayMicroseconds(14);
if(PINB & (1 << OUT)) bit=1;
asm volatile (
" ldi r18, 15 \n"
"1: dec r18 \n"
" brne 1b \n"
);
// delayMicroseconds(45);
return bit;
}
// read byte
uint8_t therm_read_byte(void){
uint8_t i=8, n=0;
while(i--){n>>=1;n|=(therm_read_bit()<<7);}
return n;
}
// write byte
void therm_write_byte(uint8_t byte){
uint8_t i=8;
while(i--){therm_write_bit(byte&1);byte >>= 1;
}
}
// read temp
int read_temp(){
uint8_t temperature[2];
float temper;
therm_reset();
therm_write_byte(0xCC);
therm_write_byte(0x44);
while(!therm_read_bit());
therm_reset();
therm_write_byte(0xCC);
therm_write_byte(0xBE);
temperature[0]=therm_read_byte();
temperature[1]=therm_read_byte();
therm_reset();
temper = (temperature[1] << 8 | temperature[0])*10/16;
return (int)temper;
}
+DHT11
#define DHT_PIN PB2
byte data_dht[5];
// TM1637 Macros
#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))
// TM1637 Main Settings
#define TM1637_DIO_PIN PB0
#define TM1637_CLK_PIN PB1
#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
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;
/**
* 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);
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
};
void delay1sec(void)
{
// Generated by delay loop calculator
// at http://www.bretmulvey.com/avrdelay.html
//
// Delay 1 000 000 cycles
// 1s at 1 MHz
asm volatile (
" ldi r18, 6" "\n"
" ldi r19, 19" "\n"
" ldi r20, 174" "\n"
"1: dec r20" "\n"
" brne 1b" "\n"
" dec r19" "\n"
" brne 1b" "\n"
" dec r18" "\n"
" brne 1b" "\n"
" rjmp 1f" "\n"
"1:" "\n"
);
};
// -------------------------------------------------------------------------------------------------------
// ---------------------------------------- TM1637 library CODE ------------------------------------------
// -------------------------------------------------------------------------------------------------------
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 ? _digit2segments[(digit)] : 0x00);
if (position == 0x01) {
segments = segments | (_segments & 0x00);//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();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
TM1637_DIO_LOW();
}
void
TM1637_stop(void)
{
TM1637_CLK_LOW();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
TM1637_DIO_LOW();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
TM1637_CLK_HIGH();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
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();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
if (value & 0x01) {
TM1637_DIO_HIGH();
} else {
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
ack = TM1637_DIO_READ();
if (ack) {
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
TM1637_CLK_HIGH();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
TM1637_CLK_LOW();
asm volatile (
" rjmp 1f \n"
"1: rjmp 2f \n"
"2: \n"
);
TM1637_DIO_OUTPUT();
return ack;
}
int main(void)
{
uint8_t firstdig, seconddig;
ACSR ^= ~_BV(ACIE);//comparator off
ACSR |= ACD;
PRR |= (1<<PRADC);// ADC off
// delay 1 for startup
delay1sec();
/* setup */
TM1637_init(1/*enable*/, 1/*brightness*/);
/// display zeros at start - self check
TM1637_display_digit(0, 0);
TM1637_display_digit(1, 0);
TM1637_display_digit(2, 0);
TM1637_display_digit(3, 0);
// delay 1 for diagnostic
delay1sec();
/* setup */
/* Beginning of neverending loop */
while (1) {
dht_read();
byte h = data_dht[0];
byte t = data_dht[2];
TM1637_display_segments(2, 99);
TM1637_display_segments(3, 57);
firstdig = t / 10;
seconddig = t % 10;
TM1637_display_digit(0, firstdig);
TM1637_display_digit(1, seconddig);
delay1sec();
TM1637_display_segments(2, 92);
TM1637_display_segments(3, 118);
firstdig = h / 10;
seconddig = h % 10;
TM1637_display_digit(0, firstdig);
TM1637_display_digit(1, seconddig);
delay1sec();
};
}
int dht_read(){
byte ii = 0,i1 = 0;
for(ii = 0;ii < 5;ii++){data_dht[ii] = 0;}
DDRB |=(1 << DHT_PIN);
PORTB &= ~(1 << DHT_PIN);
asm volatile (
" ldi r18, 24 \n"
" ldi r19, 95 \n"
"1: dec r19 \n"
" brne 1b \n"
" dec r18 \n"
" brne 1b \n"
" rjmp 1f \n"
"1: \n"
);
//delay(18);
PORTB |= (1 << DHT_PIN);
asm volatile (
" ldi r18, 13 \n"
"1: dec r18 \n"
" brne 1b \n"
" nop \n"
);
// delayMicroseconds(40);
DDRB &= ~(1 << DHT_PIN);
asm volatile (
" ldi r18, 26 \n"
"1: dec r18 \n"
" brne 1b \n"
" rjmp 1f \n"
"1: \n"
);
// delayMicroseconds(80);
while(PINB & (1 << DHT_PIN));
for (ii = 0; ii < 5; ii++){
data_dht[ii]=0;
for (i1=0; i1<8; i1++){
while(!(PINB & (1 << DHT_PIN)));
asm volatile (
" ldi r18, 10 \n"
"1: dec r18 \n"
" brne 1b \n"
);
//delayMicroseconds(30);
if (PINB & (1 << DHT_PIN)){data_dht[ii] |= 1 << (7-i1);}
while(PINB & (1 << DHT_PIN));
}}return 1;}