달력

5

« 2026/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2023. 11. 14. 20:47

Timer/counter 0 Test Atmega3282023. 11. 14. 20:47

Timer/counter 0 Test

 

#include <mega328p.h>
#include <delay.h>

unsigned char cnt=0;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
  TCNT0=0x64;
  cnt++;
  if(cnt==100) //TCNT0=0x64, 16Mhz의 경우 약 1초
   {
   PORTB=(0<<PORTB7) | (0<<PORTB6) | (1<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);// PORTB=0x00;
   delay_ms(500);
   PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0); //PORTB=0xFF;
   cnt=0;
   }
 }

void main(void)
{

DDRB=(0<<DDB7) | (0<<DDB6) | (1<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);


// Clock value: 15.625 kHz
// Mode: Normal top=0xFF
// Timer Period: 16.384 ms
TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (0<<WGM01) | (0<<WGM00);
TCCR0B=(0<<WGM02) | (1<<CS02) | (0<<CS01) | (1<<CS00);
TCNT0=0x64;
OCR0A=0x00;
OCR0B=0x00;              

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=(0<<OCIE0B) | (0<<OCIE0A) | (1<<TOIE0);

// Globally enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here    
      }
}//

'Atmega328' 카테고리의 다른 글

LED Arry (Bar LED) WCNLBA-SR12 N2306 Pin map  (0) 2023.11.14
Timer/Count 0 기본  (1) 2023.11.14
1. Builtin LED 제어  (1) 2023.11.14
CP210X Window Universal Drive  (0) 2023.10.20
328-Test  (0) 2023.10.20
:
Posted by gonlab