04-18-2021, 04:42 PM
Pozdrav svima.Da pitam i ja nesto.Dosta toga sam nasao i na internetu ali nisam siguran da sam u pravu kako sam to shvatio.Evo kako su neki bitovi definisani ovako i to mi je jasno
TCCR1B |= (1 << CS12) | (1 << CS11) | (0 << CS10);
A u isto programu ima i ovo:
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
_BV je vec definisan kao bit i daje vrednost bajta.Koliko sam ja uspeo da shvatim ali kako to ustvari radi?
evo ceo neki kod koji sam pronasao
const int led = LED_BUILTIN; // the pin with a LED
void setup(void)
{
pinMode(led, OUTPUT);
TIMSK1 |= (1 << TOIE1); // interrupt TIMER1 on
TCCR1A = 0; //normal OC1A OC1B off
TCCR1B = 0; //clear register
TCCR1B = 0; //clear register
TCCR1B |= (1 << CS12) | (1 << CS11) | (0 << CS10);
TCNT1 = 0; //clear register
Serial.begin(9600);
pinMode(5, INPUT_PULLUP);
TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
pinMode(3, OUTPUT); // output pin for OCR2B, this is Arduino pin number
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = 63;
OCR2B = 0;
}
unsigned long blinkCount = 0; // use volatile for shared variables
ISR(TIMER1_OVF_vect) //interrupção do TIMER1
{
blinkCount++; // increase when overflows
digitalWrite(led, !digitalRead(led));
}
uint32_t millis1;
uint32_t calcMed = 0;
void loop(void)
{
calcMed = (blinkCount * 65536) + TCNT1;
blinkCount = 0;
TCNT1 = 0;
Serial.print("Freq = ");
Serial.println(calcMed);
while ((millis1 + 1000) > millis()) {
_delay_us(5);
if ( OCR2B < 63 )
OCR2B += 5;
else
OCR2B = 0;
}
millis1 = millis();
}
TCCR1B |= (1 << CS12) | (1 << CS11) | (0 << CS10);
A u isto programu ima i ovo:
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
_BV je vec definisan kao bit i daje vrednost bajta.Koliko sam ja uspeo da shvatim ali kako to ustvari radi?
evo ceo neki kod koji sam pronasao
const int led = LED_BUILTIN; // the pin with a LED
void setup(void)
{
pinMode(led, OUTPUT);
TIMSK1 |= (1 << TOIE1); // interrupt TIMER1 on
TCCR1A = 0; //normal OC1A OC1B off
TCCR1B = 0; //clear register
TCCR1B = 0; //clear register
TCCR1B |= (1 << CS12) | (1 << CS11) | (0 << CS10);
TCNT1 = 0; //clear register
Serial.begin(9600);
pinMode(5, INPUT_PULLUP);
TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
pinMode(3, OUTPUT); // output pin for OCR2B, this is Arduino pin number
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = 63;
OCR2B = 0;
}
unsigned long blinkCount = 0; // use volatile for shared variables
ISR(TIMER1_OVF_vect) //interrupção do TIMER1
{
blinkCount++; // increase when overflows
digitalWrite(led, !digitalRead(led));
}
uint32_t millis1;
uint32_t calcMed = 0;
void loop(void)
{
calcMed = (blinkCount * 65536) + TCNT1;
blinkCount = 0;
TCNT1 = 0;
Serial.print("Freq = ");
Serial.println(calcMed);
while ((millis1 + 1000) > millis()) {
_delay_us(5);
if ( OCR2B < 63 )
OCR2B += 5;
else
OCR2B = 0;
}
millis1 = millis();
}