Hi,
finally I want to generate a variable delay and then a pulse of about 1 ms to be used in a phase control for motor control. Timer1 and the output compare interrupts A and B of an ATtiny84 shall be used for that purpose.
To understand timer1 I wanted to test it without interrupts in following sketch:
#include <SoftwareSerial.h>
#define CLCK_SEL_BITS (5) // ./. 1024, 64 usec steps
SoftwareSerial mySerial(5, 6); // RX, TX
uint32_t baudrt, t0, t1;
uint16_t lcnt = 0;
void setup() {
// put your setup code here, to run once:
baudrt = 38400;
mySerial.begin(baudrt);
delay(100);
mySerial.println();
mySerial.print("Baudrate=");
mySerial.println(baudrt);
// Timer1 konfigurieren
TCCR1A = 0; // OC1A/OC1B disconnected, normal mode
TCCR1B = 0; // normal mode, no clock
TCCR1C = 0; // no force output compare
TCNT1 = 0; // clear counter
TIFR1 = 0; // clear interrupt flags
TIMSK1 = 0; // OCIE1A und OCIE1B Interrupts disable
while (1) {
OCR1B = 0x6000; // output compare B match is early
OCR1A = 0xC300; // output compare A match is later
TCNT1 = 0; // clear counter register
TIFR1 = 0; // clear all interrupt flags
TCCR1B = (1<<CS12) | (1<<CS10); // Timer start, ./. 1024
while (TIFR1 & 4 == 0); // wait for output compare B match flag
TCCR1B = 0; // timer stop, time for print()
mySerial.print(TIFR1);
mySerial.print(" ");
mySerial.print(TCNT1H,HEX); mySerial.print(TCNT1L,HEX);
mySerial.print(" ");
TCCR1B = (1<<CS12) | (1<<CS10); // Timer continue
while (TIFR1 & 2 == 0); // wait for output compare A match flag
TCCR1B = 0; // timer stop
mySerial.print(TIFR1);
mySerial.print(" ");
mySerial.print(TCNT1H,HEX); mySerial.println(TCNT1L,HEX);
delay(5000);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
The code is edited according to first answer!
After configuring timer1 the while loop is the intended test.
My understanding of timer1 is that timer1 provides two output compare registers A and B. When the counter equals register B and A the corresponding flags are set and if interrupts are enabled, corresponding interrupt routines will be fired.
With this understanding my intention is to generate a delay corresponding to the value of OCR1B. When the counter matches OCR1B an output bit is set (to trigger a triac) and when the counter matches OCR1A (OCR1A > OCR1B) the bit is reset and the counter resets automatically (Clear Timer on Compare with OCR1A). The time between OCR1B and OCR1A is the pulse length.
The while loop in the sketch simulates the sequence of processing and the prints shall show the states of flags and content of counter.
What I expected on the Serial Monitor is:
4 600 6 C30 (a hex value of 0 is printed as a single "0")
meaning OCF1B=1 and counter value 0x6000 then OCF1B=1,OCF1A=1 and counter 0xC300.
After compare match with OCR1A the counter is stopped. Therefore an overflow never should occur.
What I get is:
7 00 7 00
7 means that OCF1B=OCF1A=TOV1=1, i.e. both output compare flags and overflow flag are set and the counter always is 0.
I don't understand how timer1 really works. Can somebody explain it to me, please.