Electoday 2025

ไมโครคอนโทรลเลอร์ => AVR and Arduino => Topic started by: nai102 on May 26, 2018, 11:57:58 PM

Title: มีปัญหากับการเปิดใช้งาน timer พร้อมกัน หลาย timer
Post by: nai102 on May 26, 2018, 11:57:58 PM
ผมใช้ Atmega328p มีปัญหาคือ ถ้าผมเปิดใช้ timer 2 ตัวพร้อมกันจะทำให้ timer ไม่ทำงาน
ผมพลาดตรงไหน รบกวนช่วยดูให้หน่อยครับ
Quote
// Set the Timer Mode to CTC
   TCCR0A |= (1 << WGM01);

   // Set the value that you want to count to
   OCR0A = 125;

   TIMSK0 |= (1 << OCIE0A);    //Set the ISR COMPA vect

   sei();         //enable interrupts


   TCCR0B |= (1 << CS01);
   // set prescaler to 256 and start the timer
ถ้าอย่างนี้ ใช้งานได้ปกติ

แต่ถ้าเพิ่มตรงนี้ Timer จะไม่ทำงานเลย
Quote
TIMSK2 |= (1 << OCIE2A);
   //Set interrupt on compare match

   TCCR2B |= (1 << CS21);
   // set prescaler to 64 and starts PWM

ผมพลาดตรงไหน รบกวนหน่อยครับ
ขอบคุณครับ
Title: Re: มีปัญหากับการเปิดใช้งาน timer พร้อมกัน หลาย timer
Post by: az on May 30, 2018, 01:21:17 PM


__disable_interrupt();  // disable interrupt


TCCR0A = 0x02;         // CTC, normal operation
TCCR0B = 0x02;         // prescaler8

OCR0A  = 125;          // set value
TIMSK0 = 0x02;         // Compare Match A Interrupt Enable


TCCR2A = 0x02;         // CTC, normal operation
TCCR2B = 0x02;         // prescaler8

OCR2A  = 125;          // set value
TIMSK2 = 0x02;         // compare match A interrupt enable


//~ TCCR2A = 0x00;         // timer, normal operation
//~ TCCR2B = 0x02;         // prescaler8

//~ TCNT2 = 125;           // set value
//~ TIMSK2  = 0x01;        // Timer/Counter2 Overflow Interrupt Enable


__enable_interrupt();  // enable interrupt