1

Тема: Амперметр ADS1110 Atmega8L

http://forum.rcl-radio.ru/uploads/images/2021/07/613c4d5a2be9554f3c7d6ae41bcca9aa.gif


#define ADDR    0x48
#define CPU_F   4000000 // Clock Speed
#define SCL_F   50000 // // I2C Speed

#define CLK 2// PD2
#define DIO 3// PD3

#define KALL_0 18
#define KALL_I 25887//(25600)

long dig_sum,dig,u;


void setup() {
   TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
   TWSR = 0;
}
 
void loop() {
 for(int i=0;i<10;i++){
   dig_sum = dig_sum+read_u(ADDR,0b10001111);delay(20);}
   dig = dig_sum/10;dig_sum=0;
   u = dig*KALL_I/0x7FFF-KALL_0;
   if(u<5){u=0;}
   print_time(u,3,7);
  }
 
int read_u(byte i2c_addr, byte i2c_reg){
   TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_addr << 1;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_reg;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
  
   TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT))); 
   TWDR = (i2c_addr << 1) | 1;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA); 
  while(~TWCR&(1<<TWINT));
   int i2c_data0 = TWDR; 

   TWCR = (1<<TWINT) | (1<<TWEN);
   while(~TWCR&(1<<TWINT));
   int i2c_data1 = TWDR; 

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
   int i2c_data = (i2c_data0<<8)+i2c_data1;
   return i2c_data;
  }  

void tm_dec(byte dig){
       for(int i = 0; i < 8; i++) {
         DDRD |= (1 << CLK);del();
       if (dig & 0x01)
         DDRD &= ~(1 << DIO);
       else
         DDRD |= (1 << DIO);del();
         DDRD &= ~(1 << CLK);del();
         dig = dig >> 1;
  }
         DDRD |= (1 << CLK);
         DDRD &= ~(1 << DIO);del();
         DDRD &= ~(1 << CLK);del();
 
       if (((PIND >> DIO) & 1) == 0)
         DDRD |= (1 << DIO);del();
         DDRD |= (1 << CLK);del();
  }  
 
void tm_stop(){
         DDRD |= (1 << DIO);del();
         DDRD &= ~(1 << CLK);del();
         DDRD &= ~(1 << DIO);del();
  }  
 
void tm_start(){
         DDRD |= (1 << DIO);del();
  }
 
void print_time(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 = t / 1000 % 10;
        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(){delayMicroseconds(200);}  

Как прошить - http://rcl-radio.ru/?p=82486