1

Тема: ATtiny45 + TEA5767 + TM1637

#define CLK  PB4 // TM1637
#define DIO  PB3 // TM1637
#define CH   8

#include <EEPROM.h>
#include <TinyWireM.h>    // https://github.com/adafruit/TinyWireM.git
#include <TEA5767Tiny.h>  // https://github.com/andykarpov/TEA5767Tiny.git
 TEA5767Tiny Radio;

 float ch[]={104.4, 101.9, 102.5, 88.0, 101.5, 105.0, 103.9, 105.7};
 unsigned char buf[5];
 double current_freq;
 int ch_n;


void setup() { 
  pinMode(1,INPUT_PULLUP);
  ch_n=EEPROM.read(0);
  Radio.init();
  Radio.set_frequency(ch[ch_n]);  
}

void loop() {
  if(digitalRead(1)==LOW){
    ch_n++;
    if(ch_n>CH-1){ch_n=0;}
    Radio.set_frequency(ch[ch_n]);
    EEPROM.update(0,ch_n);
  }
  if (Radio.read_status(buf) == 1){current_freq =  floor (Radio.frequency_available (buf) / 100000 + .5);}
  tm_print(current_freq,1,5);
  delay(200);
}

void tm_dec(byte dig){
       for(byte i = 0; i < 8; i++) {
         DDRB |= (1 << CLK);del();
       if (dig & 0x01)
         DDRB &= ~(1 << DIO);
       else
         DDRB |= (1 << DIO);del();
         DDRB &= ~(1 << CLK);del();
         dig = dig >> 1;
  }
         DDRB |= (1 << CLK);
         DDRB &= ~(1 << DIO);del();
         DDRB &= ~(1 << CLK);del();
 
       if (((PINB >> DIO) & 1) == 0)
         DDRB |= (1 << DIO);del();
         DDRB |= (1 << CLK);del();
  }  
 
void tm_stop(){
         DDRB |= (1 << DIO);del();
         DDRB &= ~(1 << CLK);del();
         DDRB &= ~(1 << DIO);del();
  }  
 
void tm_start(){
         DDRB |= (1 << DIO);del();
  }
 
void tm_print(int t, byte pd_t, int br){
        tm_start();tm_dec(0b10001000 + br);//tm_stop();tm_start();
        tm_dec(0x40);tm_stop();tm_start();
        int data0;
        if(t<1000){data0=10;}else{ data0 = t/1000;}
        int data1 = t / 100 % 10;
        int data2 = t / 10 % 10;
        int data3 = t % 10;
 
      for(byte n = 0; n < 4; n++){
        int data;
      switch(n){
        case 0: data = data0;break;
        case 1: data = data1;break;
        case 2: data = data2;break;
        case 3: data = data3;break;
        }
 
      switch(data){    // XGFEDCBA
        case 0:  data = 0b00111111;break;     // 0
        case 1:  data = 0b00000110;break;     // 1
        case 2:  data = 0b01011011;break;     // 2
        case 3:  data = 0b01001111;break;     // 3
        case 4:  data = 0b01100110;break;     // 4
        case 5:  data = 0b01101101;break;     // 5
        case 6:  data = 0b01111101;break;     // 6
        case 7:  data = 0b00000111;break;     // 7
        case 8:  data = 0b01111111;break;     // 8
        case 9:  data = 0b01101111;break;     // 9
        case 10: data = 0b00000000;break;     // пусто
        case 11: data = 0b01000000;break;     // -
        }
 
        if(n == 0){data0 = data;}
        if(n == 1){data1 = data;}
        if(n == 2){data2 = data;}
        if(n == 3){data3 = data;}
        }
      switch(pd_t){
        case 1 : data2 = data2+0b10000000;break;
        case 2 : data1 = data1+0b10000000;break;
        case 3 : data0 = data0+0b10000000;break;
        }
      tm_dec(0xC0);tm_dec(data0);tm_dec(data1);tm_dec(data2);tm_dec(data3);tm_stop();
}  
 
void del(){delay(1);}