Advertisement
babyyoda_

Slave_Git_Pot

Jun 27th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // See also AvrDimmerCoprocessor_V2A.
  2. // This code is a slightly altered version of the code from https://forum.arduino.cc/index.php?topic=314773.0
  3. // The original version used pin 7 (interrupt 0) for zero cross detection.
  4. // I do need that pin 7 for I2C serial communication purposes, so I did have to
  5. // alter the code to use a different pin for the detector.
  6. // To do that, I switched to use the general "Pin change interrupt", instead of the
  7. // dedicated interrupt 0 pin.
  8. // As the pin change interrupt triggers on both rising and falling edges, I did add
  9. // a read of the logic level in the interrupt routine, to find the falling edge case.
  10. //
  11. // This version still uses the potmeter on ATTiny45 pin 3 to control the dimmer.
  12. // And this one worked fine also!
  13. //
  14. // Thijs Kaper, October 2019.
  15.  
  16.  
  17. // Voltage controlled dimmer with ATtiny85
  18. //
  19. // This arduino sketch includes a zero
  20. // crossing detect function and an opto-isolated triac.
  21. //
  22. // AC Phase control is accomplished using the internal
  23. // hardware timer1 in the ATtiny85
  24. //
  25. // Timing Sequence
  26. // * timer is set up but disabled
  27. // * zero crossing detected
  28. // * timer starts counting from zero
  29. // * comparator set to "delay to on" value
  30. // * counter reaches comparator value
  31. // * comparator ISR turns on triac gate
  32. // * counter set to overflow - pulse width
  33. // * counter reaches overflow
  34. // * overflow ISR turns off triac gate
  35. // * triac stops conducting at next zero cross
  36.  
  37. // The hardware timer runs at 8MHz.
  38. // A half period of a 50Hz AC signal takes 10 ms is 80000 counts.
  39. // Prescaler set to 1024 gives 78 counts per half period
  40.  
  41.  
  42. #include <avr/io.h>
  43. #include <avr/interrupt.h>
  44.  
  45. //#define DETECT 2      //zero cross detect, interrupt 0, is physical pin 7
  46. #define DETECT PB1 // 1      //zero cross detect, pcint1, is physical pin 6
  47. #define GATE 3        //triac gate is physical pin 2
  48. #define PULSE 2       //trigger pulse width (counts)
  49. #define INSTELPIN 2   // =A2 (digital pin4) is physical pin 3
  50.  
  51. void setup(){
  52.   // set up pins
  53.   pinMode(DETECT, INPUT);      //zero cross detect
  54.   digitalWrite(DETECT, HIGH);  //enable pull-up resistor
  55.   pinMode(GATE, OUTPUT);       //triac gate control
  56.  
  57.   // set up Timer1
  58.   TCCR1 = 0;     // stop timer
  59.   OCR1A = 50;    //initialize the comparator
  60.   TIMSK = _BV(OCIE1A) | _BV(TOIE1);  //interrupt on Compare Match A | enable timer overflow interrupt
  61.  
  62.   // https://thewanderingengineer.com/2014/08/11/pin-change-interrupts-on-attiny85/
  63.   GIMSK = 0b00100000;    // turns on pin change interrupts
  64.   PCMSK = 0b00000010;    // turn on interrupts on PCINT1 = pin 6 = PB1
  65.  
  66.   sei();  // enable interrupts
  67.   // set up zero crossing interrupt
  68.   //attachInterrupt(0,zeroCrossingInterrupt, FALLING);  
  69. }
  70.  
  71. // Pin change interrupt
  72. ISR(PCINT0_vect) {
  73.   // pin change is both rising and falling, so check for LOW as that would be the wanted falling edge interrupt
  74.   if (digitalRead(DETECT) == LOW) {
  75.     TCNT1 = 0;   //reset timer - count from zero
  76.     TCCR1 = B00001011;        // prescaler on 1024, see table 12.5 of the tiny85 datasheet
  77.   }
  78. }
  79.  
  80. //Interrupt Service Routines
  81. //void zeroCrossingInterrupt(){  
  82. //  TCNT1 = 0;   //reset timer - count from zero
  83. //  TCCR1 = B00001011;        // prescaler on 1024, see table 12.5 of the tiny85 datasheet
  84. //}
  85.  
  86. ISR(TIMER1_COMPA_vect){    //comparator match
  87.   digitalWrite(GATE,HIGH); //set triac gate to high
  88.   TCNT1 = 255-PULSE;       //trigger pulse width, when TCNT1=255 timer1 overflows
  89. }
  90.  
  91. ISR(TIMER1_OVF_vect){       //timer1 overflow
  92.   digitalWrite(GATE,LOW);   //turn off triac gate
  93.   TCCR1 = 0;                //disable timer stop unintended triggers
  94. }
  95.  
  96. void loop(){     // use analog input to set the dimmer
  97. int instelwaarde = analogRead(INSTELPIN);
  98. //OCR1A = map(instelwaarde, 0, 1023, 65, 2);    //5-LOW 1-HIGH 0-OFF
  99. //OCR1A = map(instelwaarde, 0, 1023, 5, 0);
  100. OCR1A = 1;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement