0

I2C経由で送信されたコマンドを受け入れるattiny85を介して電球を暗くしようとしています。私の問題は、電球がほとんど減光せず、かなりちらつくことです。

私の回路はここで見ることができます: I2C で制御される Attiny85 調光回路

Attiny85 では、光アイソレータ (およびトライアック) はピン 2 (AKA PB3、AKA PCINT3) を介して制御され、AC ゼロクロス検出はピン 3 (AKA PB4、AKA PCINT4) で行われます。

外部割り込みピンは、I2C 通信の SCL として使用されるピン 7 (別名 PB2) であるため、コードは単一の外部割り込みイネーブルではなく、ピン変更割り込みイネーブルを使用します。

8MHz の内部クロックが使用され、タイマー/カウンター プリスケーラーは 1024 です。これは、有効な入力範囲 (levelコード内の変数を参照) が約 0 ~ 65 であることを意味します。私の AC ソースは USA (60Hz) です。

#include <avr/interrupt.h>
#include <avr/io.h>
#include <TinyWireS.h>

#define PULSE 4       //trigger pulse width (counts)
#define I2C_SLAVE_ADDR 0x4 // the 7-bit address (remember to change this when adapting this example)

byte trigger = 3;
byte detector = 4;

byte level = 50;
byte maxLevel = 65;
byte minLevel = 0;


void setup() {
    TinyWireS.begin(I2C_SLAVE_ADDR); // join i2c network
    TinyWireS.onRequest(requestEvent); //setup i2c requester

    digitalWrite(detector, HIGH);  //enable pull-up resistor
    pinMode(trigger, OUTPUT);// Set AC Load pin as output

    TCCR1 = 0;     //stop timer
    OCR1A = level;    //initialize the comparator
    TIMSK = _BV(OCIE1A) | _BV(TOIE1); //interrupt on Compare Match A and  enable timer overflow interrupt

    GIMSK = 0b00100000; //Enable pin change interrupt
    PCMSK = 0b00010000; //PB4, physical pin 3 PCINT4
    TCCR1 = B00001011; //Prescale the timer
    sei();  // Turn on interrupts
}


ISR(PCINT0_vect){ //interrupt looking for zero crossing
        TCNT1 = 0;   //reset timer - count from zero
        OCR1A = level;
        TCCR1 = B00001011;// prescaler on 1024, see table 12-5 of the tiny85 datasheet
}

ISR(TIMER1_COMPA_vect){    //comparator match
        digitalWrite(trigger,HIGH); //set triac gate to high
        TCNT1 = 255-PULSE;       //trigger pulse width for a few cycles for triac to latch on. 255 bc the counter can only count up to 255
}

ISR(TIMER1_OVF_vect){  //timer1 overflow
        digitalWrite(trigger,LOW);   //turn off triac gate
        TCCR1 = 0;  //disable timer stop unintended triggers
}


void loop() {}

void requestEvent(){
    if (TinyWireS.available()) {
        level = TinyWireS.receive();
        if (level > maxLevel) {
            level = maxLevel;
        }
        else if (level < minLevel){
            level = minLevel;
        }
    }
    TinyWireS.send(OCR1A);
}

私はさまざまな電球を試しましたが、他の電球よりもうまく機能するものはありませんでした.

これは、さまざまな調光レベルをオシロスコープに送信したときに、ゼロクロス検出器 (黄色) とトリガー (青色) を示すビデオです。悪い電話のビデオ

4

1 に答える 1