Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #define DETECT 2 //zero cross detect
  5. #define GATE 9 //TRIAC gate
  6. #define PULSE 4 //trigger pulse width (counts)
  7. int i=483;
  8.  
  9. void setup(){
  10.  
  11. // set up pins
  12. pinMode(DETECT, INPUT); //zero cross detect
  13. digitalWrite(DETECT, HIGH); //enable pull-up resistor
  14. pinMode(GATE, OUTPUT); //TRIAC gate control
  15.  
  16. // set up Timer1
  17. //(see ATMEGA 328 data sheet pg 134 for more details)
  18. OCR1A = 100; //initialize the comparator
  19. TIMSK1 = 0x03; //enable comparator A and overflow interrupts
  20. TCCR1A = 0x00; //timer control registers set for
  21. TCCR1B = 0x00; //normal operation, timer disabled
  22.  
  23.  
  24. // set up zero crossing interrupt
  25. attachInterrupt(0,zeroCrossingInterrupt, RISING);
  26. //IRQ0 is pin 2. Call zeroCrossingInterrupt
  27. //on rising signal
  28.  
  29. }
  30.  
  31. //Interrupt Service Routines
  32.  
  33. void zeroCrossingInterrupt(){ //zero cross detect
  34. TCCR1B=0x04; //start timer with divide by 256 input
  35. TCNT1 = 0; //reset timer - count from zero
  36. }
  37.  
  38. ISR(TIMER1_COMPA_vect){ //comparator match
  39. digitalWrite(GATE,HIGH); //set TRIAC gate to high
  40. TCNT1 = 65536-PULSE; //trigger pulse width
  41. }
  42.  
  43. ISR(TIMER1_OVF_vect){ //timer1 overflow
  44. digitalWrite(GATE,LOW); //turn off TRIAC gate
  45. TCCR1B = 0x00; //disable timer stopd unintended triggers
  46. }
  47.  
  48. void loop(){ // sample code to exercise the circuit
  49.  
  50. i--;
  51. OCR1A = i; //set the compare register brightness desired.
  52. if (i<65){i=483;}
  53. delay(15);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement