Timer1 Compare Match  ver.1.0
Nguyen 2016
start.cpp
Go to the documentation of this file.
1 
28 #include <Arduino.h>
29 
30 //#define DEBUG_LCD disable for simulation
31 #include "_debug.h"
32 
42 void setup()
43 {
44 
45  DDRB=0b00001000; // all ports input except PB3 its output
46  PORTB=0xFF; //Pullups for all on ### PINB PB3==1
47  PORTC=0xFF; //Pullups for all Pins
48  T("Input output ok");//Trace
49 
50  TCNT1=0;
51  // CTC Mode Compare Match A
52  // WGM13-12-11-10 == 0100
53  TCCR1A &=~_BV(WGM10); // 8 Bit PWM 255
54  TCCR1A &=~_BV(WGM11);
55  TCCR1B |= _BV(WGM12);
56  TCCR1B &=~_BV(WGM13);
57  //T("8 Bit PWM");//Trace
58 
59  TCCR1B &= ~_BV(CS10); //resetting the prescaler oder TCCR1B &= ~(_BV(CS10)|_BV(CS11)|_BV(CS12));
60  TCCR1B &= ~_BV(CS11);
61  TCCR1B &= ~_BV(CS12);
62  T("resetting prescaler");//Trace
63 
64 
65  // setup prescaler
66  uint8_t prescaler = PINB & 0b111; // read just the lower 3 bytes
67 
68  prescaler = 1; //debug only
69 
70  if (prescaler >5) prescaler=5; // stick at max allowed value
71  TCCR1B &=~(0b111);
72  TCCR1B |= prescaler;
73 
74  /* der folgende Quelltext wurde auskommentiert;
75  nicht so toll, weil schwierig zu debuggen, man muss beim Simulieren immer wieder PINB setzen
76 
77  if((PINB & _BV(PB0))) //Setting prescaler depending on if the pin is used or not
78  //(if used it pulls to ground) (Wenn der eingang auf ground geht)
79  {
80  TCCR1B |= _BV(CS10);
81  }
82 
83  if((PINB & _BV(PB1)))
84  {
85  TCCR1B |= _BV(CS11);
86  }
87 
88  if((PINB & _BV(PB2)))
89  {
90  TCCR1B |= _BV(CS12);
91  }
92  */
93  T("setting prescaler");//Trace
94 
95  OCR1AH=PINC; //Highbyte for compare is PINC
96  /* ATTENTION!!! Beim Simulieren wird das Higher nibble nicht gesetzt ,
97  wenn wir PINC auf den lower nibble setzten wir es normal im Compare gesetzt
98 
99  Solution: der Timer muss zuerst in CTC Modus gestellt werden, dann erst kann man OCR1H setzen
100  */
101  OCR1AH = 0x1; //for debug only
102  OCR1AL =0; //lowbite fix 0
103 
104  //oder: OCR1A = PINC<<8;
105 
106  T("compare and interrupt enable");//Trace
107 
108  TCNT1=0;
109  TIFR1 = _BV(OCF1A); //Interrupt flag löschen
110  TIMSK1|=_BV(OCIE1A); // Interrupt enable
111 
112  L("Setup Successful"); //Trace
113  sei();
114 }
115 
117 ISR(TIMER1_COMPA_vect)
118 {
119  PORTB ^= _BV(PORTB3);
120  L("Timer Compare"); //Trace
121 }
122 
124 void loop()
125 {
126  //sei();
127  L("loop");
128 
129 }
130 
131 
132 
#define T(x)
Definition: _debug.h:85
void loop()
Definition: start.cpp:124
void setup()
Definition: start.cpp:42
#define L(x)
Definition: _debug.h:78
ISR(TIMER1_COMPA_vect)
Definition: start.cpp:117