我试图通过attiny85来调暗灯泡,它接受通过I2C发送的命令。我的问题是灯泡几乎没有变暗,并且闪烁很多。
我的电路可以在这里看到: Attiny85 dimming circuit controlled over I2C
光电隔离器(以及三端双向可控硅)通过引脚2(AKA PB3,AKA PCINT3)控制,并且在Attiny85上的引脚3(AKA PB4,AKA PCINT4)上进行交流过零检测
该代码使用引脚更改中断使能而不是单个外部中断使能,因为外部中断引脚是引脚7(也称为PB2),用作I2C通信的SCL。
使用8MHz内部时钟,定时器/计数器预分频器为1024.这意味着我有一个大约0-65的有效输入范围(参见代码中的level
变量)。我的AC源是美国(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);
}
我尝试过各种各样的灯泡,没有一种比另一种更好。
这是一个视频,显示示波器上的零交叉检测器(黄色)和触发器(蓝色),因为我发送了不同的调光级别Bad phone video
我感谢所有人和任何帮助。感谢。