So I managed to set up Timer0. I can see that the timer counts. What I would need/expected is that I could see the square sign on pin 4 (since pin for is attached to timer0). But it doesn't seem to happen. Do I have to enable that timer0 is connected to pin 4?
int timer=0;
void setup() {
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);
TCCR0B=(1<<WGM00); //Set the CTC mode
OCR0A=624; //Value for ORC0A for 1ms
TIMSK0|=(1<<OCIE0A); //Set the interrupt request
sei(); //Enable interrupt
//TCCR0B|=(1<<CS01); //Set the prescale 1/64 clock
TCCR0B|=(1<<CS02);
Serial.begin(9600);
}
void loop() {
Serial.println(timer);
}
ISR(TIMER0_COMPA_vect){ //This is the interrupt request
timer++;
}
@aqari, your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem
Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.
Why would you expect an output signal on an input pin?
If you want to use timer in the main code,
it has to be volatile and accessed with disabled interrupts.
No need to enable something that is enabled by default and was never disabled.
BTW.
Timer0:
Timer0 is a 8bit timer.
In the Arduino world Timer0 is been used for the timer functions,
like delay(), millis() and micros(). If you change Timer0 registers,
this may influence the Arduino timer function.
So you should know what you are doing.
I was using pin 8 for something else, I have a stepper motor connected to the arduino, pin 8 tells the direction, either 0/1, and pin 4 supposed to be an enable square sign, every time it goes HIGH the motor should make a step.
Even if I define pinMode(4,OUTput); i can't see the square sign on it
I know timer0 and timer1 might be used for other funcitons, i am just getting started, I will use timer2-6 later on.
So quaestion is the same, if pin 4 is conencted to tiemr0, why can't I see square sign on the pin?
Okay, so I have the timer0, it works fine, I can monitor it on th serial port.
According to the datasheet pin 4 is attached to timer0 What does this mean?
I thougth the tiemr's sign appears on pin 4 like a square sign or a pwm. But I can't see anything on It.
That makes me wonder if i have to enable the timers output on the pin 4 in order to see it
You have to enable the OUTPUT in some register, look in the data sheet.
I only found an example for a different timer/pin
void startTransducer()
{
TCCR2A = _BV(COM2A0) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = B11000111; // 199, so timer2 counts from 0 to 199 (200 cycles at 16 MHz)
}
Setting the COM2Ax bits (Compare Output Mode bits) to 01 sets timer/counter2
to toggle OC2A (i.e., Arduino digital pin 11) from high to low when the counter matches
the 8-bit value stored in OCR2A (Output Compare Register A on Counter/Timer 2).
Thanks for the help I finnaly found it: in TCCRnA setting (COMnB0) and (COMnB1) where n is the number of timer you use, enables a toggle on a match:
int timer=0;
void setup() {
pinMode(2, OUTPUT);
TCCR3A=0;
TCCR3B=0;
TCCR3B=(1<<WGM32);
OCR3A=23;
TIMSK3|=(1<<OCIE3A);
sei(); //Enable interrupt
TCCR3B|=(1<<CS30);
TCCR3B|=(1<<CS32); TCCR3A|=(1<<COM3B0); //setting pin 2 (timer3 is conencted to it) to toggle upon match TCCR3A|=(0<<COM3B1); //setting pin 2 (timer3 is conencted to it) to toggle upon match
}
void loop() {
}
ISR(TIMER3_COMPA_vect){ //This is the interrupt request
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
}
However this seems to be worse than toggeling a pin as i did on pin 4:
ISR(TIMER3_COMPA_vect){ //This is the interrupt request
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
}
For some reason this way the stepper motor misses some steps, maybe the timer's sign is not long enough, I guess it's only a pulse. While digitalWrite()-ing a pin high and low takes more time, since the controller has to excecute the command?