• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Wyświetlacz 7-segmentowy z TM1637, wyświetlanie kropek
#5
(28-06-2018, 12:50)Scislov napisał(a): Jestem tak zwanym samoukiem w arduino i najlepiej bym zrozumiał działanie kropek na jakimś przykładzie. Gdybyś mógł napisać najprostszy program wyświetlający kropkę samemu bym sobie to rozszyfrował Wink
Akurat miałem nieprzyjemność pisać soft sterujący tym beznadziejnym kontrolerem. Funkcje skopiowałem z jakieś beznadziejnej biblioteki Arduino i zmodyfikowałem. Niestety żenująca amatorska obsługa została ale co złego to nie na mnie.
Kod:
//----------------------------------------------------------------------//
//                    Jak wyzej ale wyswietla przeciki
//    miejsca przecinków:
//        128 - pierwszy od lewej
//         64 - drugi
//         32 - trzeci
//----------------------------------------------------------------------//
void TM1637Display_showNumberDecEx(int num, uint8_t dots, byte leading_zero, uint8_t length, uint8_t pos)
{
 uint8_t digits[4];
    const static int divisors[] = { 1, 10, 100, 1000 };
    byte leading = true;

    for(int8_t k = 0; k < 4; k++) {
        int divisor = divisors[4 - 1 - k];
        int d = num / divisor;
   uint8_t digit = 0;

        if (d == 0) {
          if (leading_zero || !leading || (k == 3))
              digit = TM1637Display_encodeDigit(d);
          else
              digit = 0;
        }
        else {
            digit = TM1637Display_encodeDigit(d);
            num -= d * divisor;
            leading = false;
        }
   
   // Add the decimal point/colon to the digit
   digit |= (dots & 0x80);
   dots <<= 1;
   
   digits[k] = digit;
    }

    TM1637Display_setSegments(digits + (4 - length), length, pos);
}

A tu jak tego użyć:
Kod:
        TM1637Display_showNumberDecEx(VER, 2<<4, false, 3, 1);

Reszta kodu:
Kod:
void TM1637Display_init()
{
    // Set the pin direction and default value.
    // Both pins are set as inputs, allowing the pull-up resistors to pull them up
 PORT_USI &= ~(1<<PIN_USI_SDA);            // SDA = 0
 PORT_USI &= ~(1<<PIN_USI_SCL);

 DDR_USI  &= ~(1<<PIN_USI_SCL);           // Enable SCL as input (pullup)
 DDR_USI  &= ~(1<<PIN_USI_SDA);           // Enable SDA as input
}


//----------------------------------------------------------------------//
void TM1637Display_bitDelay()
{
    _delay_us(50);
}


//----------------------------------------------------------------------//
void TM1637Display_start()
{
 DDR_USI |= (1<<PIN_USI_SDA);        // SDA = L
 TM1637Display_bitDelay();
}


//----------------------------------------------------------------------//
void TM1637Display_stop()
{
 DDR_USI |= (1<<PIN_USI_SDA);        // SDA = L
    TM1637Display_bitDelay();

 DDR_USI &= ~(1<<PIN_USI_SCL);        // SCK = H
    TM1637Display_bitDelay();

 DDR_USI &= ~(1<<PIN_USI_SDA);        // SDA = H
    TM1637Display_bitDelay();
}


//----------------------------------------------------------------------//
byte TM1637Display_writeByte(uint8_t b)
{
 uint8_t data = b;

 // 8 Data Bits
 for(uint8_t i = 0; i < 8; i++) {
   // CLK low
 DDR_USI |= (1<<PIN_USI_SCL);        // SCK = L
    TM1637Display_bitDelay();


    // Set data bit
   if (data & 0x01)
      DDR_USI &= ~(1<<PIN_USI_SDA);        // SDA = H
   else
      DDR_USI |= (1<<PIN_USI_SDA);        // SDA = L


    TM1637Display_bitDelay();


    // CLK high
 DDR_USI &= ~(1<<PIN_USI_SCL);        // SCK = H
    TM1637Display_bitDelay();

   data = data >> 1;
 }

 // Wait for acknowledge
 // CLK to zero
 DDR_USI |= (1<<PIN_USI_SCL);        // SCK = L
      DDR_USI &= ~(1<<PIN_USI_SDA);        // SDA = H
    TM1637Display_bitDelay();


 // CLK to high
DDR_USI &= ~(1<<PIN_USI_SCL);        // SCK = H
    TM1637Display_bitDelay();
 uint8_t ack = PORT_USI & (1<<PIN_USI_SDA);
 if (ack == 0) DDR_USI |= (1<<PIN_USI_SDA);        // SDA = L



    TM1637Display_bitDelay();
 DDR_USI |= (1<<PIN_USI_SCL);        // SCK = L
    TM1637Display_bitDelay();


 return ack;
}


//----------------------------------------------------------------------//
void TM1637Display_setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
   // Write COMM1
    TM1637Display_start();
    TM1637Display_writeByte(TM1637_I2C_COMM1);
    TM1637Display_stop();

    // Write COMM2 + first digit address
    TM1637Display_start();
    TM1637Display_writeByte(TM1637_I2C_COMM2 + (pos & 0x03));

    // Write the data bytes
    for (uint8_t k=0; k < length; k++)
      TM1637Display_writeByte(segments[k]);

    TM1637Display_stop();

    // Write COMM3 + brightness
    TM1637Display_start();
    TM1637Display_writeByte(TM1637_I2C_COMM3 + (TM1637brightness & 0x0f));
    TM1637Display_stop();
}


//----------------------------------------------------------------------//
void TM1637Display_setBrightness(uint8_t brightness, byte on)
{
    TM1637brightness = (brightness & 0x7) | (on? 0x08 : 0x00);
}


//----------------------------------------------------------------------//
//                    Wyświetla INT na LED
//     wartość
//    wygaszanie zera
//    liczba znaków na LCD
//    pozycja od lewej
//----------------------------------------------------------------------//
void TM1637Display_showNumberDec(int num, byte leading_zero, uint8_t length, uint8_t pos)
{
 TM1637Display_showNumberDecEx(num, 0, leading_zero, length, pos);
}
pewnie nie będzie potrzebna ale daję na wszelki wypadek.
 
Odpowiedź
  


Wiadomości w tym wątku
RE: Wyświetlacz 7-segmentowy z TM1637, wyświetlanie kropek - przez es2 - 28-06-2018, 13:08

Skocz do:


Przeglądający: 1 gości