Arduino Note
Arduino Note
it toggle every
320 mS. (1/(16000000 / 1024) * 5000).
CCR1A
Bit 7 6 5 4 3 2 1 0 TCCR1A
080 COM1A1 COM1A0 COM1B1 COM1B0 - - WGM11 WGM10
ReadWrite RW RW RW RW R R RW RW
Initial Value 0 0 0 0 0 0 0 0
TCCR1B
Bit 7 6 5 4 3 2 1 0 TCCR1B
081 ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10
ReadWrite R/W R/W R R/W R/W R/W R/W R/w
Initial Value 0 0 0 0 0 0 0 0
TCNT1
The Timer Counter Something Timer is a register that is automatically increased or decreased. In other
words, it is the current value of the timer. Depending on the mode, it may count up until it reaches the
special value TOP and then roll over to 0; or it may count up until TOP and then back down to 0.
OCR1A
The Output Compare Register (Channel A) is the register whose value is compared to the current value of
TCNT1. Depending on the wave generation mode and the compare output mode, this may be used as the
threshold after which one of the physical output pins is set to HIGH. See COM and WGM.
TCCR1
The Timer Counter/Control Register has two parts: TCCR1A and TCCR1B. Depending on which bits are
set on this register, Timer 1 will behave differently. You can set the compare output mode (COM), the
wave generation mode (WGM), and select a clock (CS).
// Here's some sample code showing how to set various bits on the TCCR1
registers.
TCCR1A |= (1 << COM1A1) | (1 << COM1A0); // Set COM to 3
TCCR1A |= (1 << WGM11) | (0 << WGM10); // Set lower two bits of WGM to 2.
TCCR1B |= (1 << WGM13) | (1 << WGM12); // Set higher two bits of WGM to 8 +
4. Total WGM = 14.
TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10); // CS = 2.
COM
I mentioned compare output mode. You can use these two bits to determine for what portions of TCNT's
cycle the OC1A pin is on. For example, when COM = 3, the OC1A pin is set to HIGH when TCNT1 is
greater than OCR1A.
WGM
Wave Generation Mode. This lets you choose between Non-PWM, PWM, and Fast-PWM modes; what
the value of TOP is; and more. This four bit number is made up of two bits in the TCCR1A register and
two bits in the TCCR1B register.
TOP
This is the MAX value of TCNT1. This can be a value between 0 and 0xFFFF because Timer 1 is a 16-bit
timer. An 8-bit timer would have a maximum value of TOP equal to 0xFF. Lower values of TOP mean a
higher frequency.
CS
The Clock Select lets you choose a "pre-scaler" for the timer. For example, CS = 2 means that the timer
operates at the speed of the CPU; CS = 3 means that the timer operates at FCPU/4; etc. This is another
way to vary the frequency of the pulse.
Below is a plot of TCNT1 values over time, as a function of the TCCR1 registers and the values of the
OCR1A and ICR1 registers. It will only display visualizations for Fast PWM modes. You can toggle the
values of various bits in the registers to see what they affect in the graph. The diagram will also generate
code for you.
COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10
COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10
TCCR1A 1 1 0 1
ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10
TCCR1B 0 1 0 1 0
Clock Select 2
Wave Generation Mode 5
OCR1A 0x7f
Timer1 are multe moduri de lucrui: pn la o tranziie MAX0, pn la o valoare prestabilit, intrare
analogic, PWM, generator de funcii.
Modul normal
Wgm13_0 = 0. Numr de la valoarea curent la maim, apoi cnd cade la 0, Timer/Counter Owerflow
Flag TOV1 devine 1.
Timer1, ncrcare cu o valoare, numr nainte pn la maxim 0 = ntr
void setup() {
TIMSK1 = 0x01; // activare intreruperi globale si timer overflow;
TCCR1A = 0x00; // mod normal de operare (mode0);
TCNT1 = 0x0BDC; // setarea valorii initiale pentru a elimina eroarea
(16bit counter register) Poate fi rescris oricnd
TCCR1B = 0x04; // start timer / set clock 1 0 0
}
La ntrerupere se apeleaz funcia
ISR(TIMER1_OVF_vect) {...
Timer1, ClearTimerOnCompareMatch CTC. Se numr pn la valoarea de comp din OCR
http://maxembedded.com/2011/07/14/avr-timers-ctc-mode/
Program care merge
// Intreruperi cu CTC, bun
#define myOutputPin 13
int toggle = 0;
void setup()
{
pinMode(myOutputPin, OUTPUT);
cli();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 5000;
TCCR1B |= (1 << WGM12); // Mod CTC
TCCR1B |= (1 << CS10); // prescalar = 1024
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
sei();
}
ISR(TIMER1_COMPA_vect)
{
digitalWrite(myOutputPin, toggle == 0 ? HIGH : LOW);
toggle = ~toggle;
}
void loop(){}
This bit is set to 1 whenever the timer overflows. It is cleared (to zero) automatically as soon as the
corresponding Interrupt Service Routine (ISR) is executed. Alternatively, if there is no ISR to execute, we
can clear it by writing 1 to it.
#include <avr/io.h>
02.#define F_CPU 1600000UL
03.#include <util/delay.h>
04.#include <avr/interrupt.h>
05./* method to initialise the timer*/
06.void timer_init()
07.{
08./*Set Timer/counter register with prescale value 1024 */
09.TCCR1B |= (1 << WGM12)|(1 << CS12)|(1 << CS10);
10./* Set Timer interrupt mask register*/
11.TIMSK |= (1 << OCIE1A);
12./* initialise timer counter value to 0*/
13.TCNT1 = 0;
14.}
15./* method to set time*/
16.void timer_set(int time)
17.{
18.int max;
19.max = (time/1000)*(16000000/1024)-1;
20./* assign the max value to the output compare register*/
21.OCR1A = max;
22./* Enable global interrupts*/
23.sei();
24.
25.}
26.
27./* method to flash an LED for every 1 minute*/
28.ISR(TIMER1_COMPA_vect )
29.{
30.counter++;
31.if(counter == 60){
32.DDRB = 0x01;
33.PORTB |= 0x01;
34._delay_ms(600);
35.PORTB &= ~0x01;
36._delay_ms(600);
37.counter = 0;
38.}
39.
40.}
41./*main method*/
42.int main()
43.{
44.
45.timer_init(); //initialise timer
46.timer_set(1000); // time in milliseconds
47.while(1);
48.}
- See more at: http://www.bredboard.com/blog/?p=641#sthash.iwASXzqL.dpuf
mySerial.print("?f");
mySerial.print("LOW");
}
else {
mySerial.print("?f");
mySerial.print("HIGH");
}
}
ISR(PCINT2_vect) {
bump = 1;
}
ISR(PCINT0_vect) {
bump = 0;
}
#include "TimerOne.h"
#define LED 13
int valLed=1;
void setup()
{
pinMode(LED, OUTPUT);
Timer1.initialize(50000);
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{
if (valLed==1)
{
digitalWrite(LED,LOW);
valLed=0;
}
else
{
digitalWrite(LED,HIGH);
valLed=1;
}
}
void loop()
{
Probabil adaug
#include <avr/interrupt.h>
Jordan McConnell
int ledPin = 13; // LED is attached to digital pin 13
int x = 0; // variable to be updated by the interrupt
void setup() {
//enable interrupt 0 (pin 2) which is connected to a button
//jump to the increment function on falling edge
attachInterrupt(0, increment, FALLING);
Serial.begin(9600); //turn on serial communication
}
void loop() {
digitalWrite(ledPin, LOW);
delay(3000); //pretend to be doing something useful
Serial.println(x, DEC); //print x to serial monitor
}