#define F_CPU 16000000UL // Assuming 16 MHz clock frequency #include #include #define CLK_PIN PB0 // CLK pin connected to PB0 #define DIO_PIN PB1 // DIO pin connected to PB1 // Function to send a byte to TM1637 void sendByte(uint8_t byte) { for (uint8_t i = 0; i < 8; i++) { PORTB &= ~(1 << CLK_PIN); // CLK low if (byte & 0x01) PORTB &= ~(1 << DIO_PIN); // Set DIO low if the least significant bit of byte is 1 else PORTB |= (1 << DIO_PIN); // Set DIO high if the least significant bit of byte is 0 _delay_us(50); // Adjust this delay if necessary PORTB |= (1 << CLK_PIN); // CLK high _delay_us(50); // Adjust this delay if necessary byte >>= 1; // Shift to the next bit } } // Function to start signal void startSignal() { PORTB &= ~(1 << CLK_PIN); // CLK low PORTB &= ~(1 << DIO_PIN); // DIO low _delay_us(50); // Adjust this delay if necessary PORTB |= (1 << CLK_PIN); // CLK high _delay_us(50); // Adjust this delay if necessary PORTB |= (1 << DIO_PIN); // DIO high _delay_us(50); // Adjust this delay if necessary } // Function to stop signal void stopSignal() { PORTB &= ~(1 << CLK_PIN); // CLK low PORTB &= ~(1 << DIO_PIN); // DIO low _delay_us(50); // Adjust this delay if necessary PORTB |= (1 << CLK_PIN); // CLK high _delay_us(50); // Adjust this delay if necessary PORTB |= (1 << DIO_PIN); // DIO high _delay_us(50); // Adjust this delay if necessary } // Function to display digits on TM1637 void displayDigits(uint8_t digit0, uint8_t digit1, uint8_t digit2, uint8_t digit3) { startSignal(); sendByte(0x40); // Data command: auto-increment address stopSignal(); startSignal(); sendByte(0xC0); // Address command: start from address 0xC0 sendByte(digit0); // Send digit0 sendByte(digit1); // Send digit1 sendByte(digit2); // Send digit2 sendByte(digit3); // Send digit3 stopSignal(); } int main(void) { DDRB |= (1 << CLK_PIN) | (1 << DIO_PIN); // Set CLK and DIO pins as output while (1) { // Display '1234' continuously displayDigits(0x3F, 0x06, 0x5B, 0x4F); // Corresponding codes for digits '1', '2', '3', '4' _delay_ms(1000); } return 0; }