2

How do I make an LED connected to PORTB.4 alternate between very bright and dark every 2 seconds using ATmega128?

If you press the switch using the external interrupt function using the ATmega128, the LED connected to PORTB.4 changes brightness alternately every two seconds to "very bright" and "dark." As far as I know, I can adjust the brightness change of led according to the value of duration, so I coded as below, but when I pressed the 0 switch, led turns on and no further progress is made. I've been studying coding for about a month or two and it's so hard. Can you help me? This is my code and i'm using CodeVisionAVR

#include <mega128.h>
#include <delay.h>

unsigned char direction = 0;
unsigned int cnt = 0, duration = 500, MAX = 1000;

interrupt [EXT_INT0] void ext_int0_isr(void)
{
    if (PIND.0 == 1)
    {
       direction = 1;
    }
}

interrupt [EXT_INT1] void ext_int1_isr(void)
{
    if (PIND.1 == 1)
    {
       direction = 2;
    }
}

interrupt [TIM0_COMP] void time0_comp_isr(void)
{
   if (direction == 1)
   {
      if(cnt++ >= 62499) // 62500*32usec = 2sec
      {
         cnt = 0;
         if (duration == 999)
         {
           if(cnt < duration)
            PORTB = 0x10, duration = 10;
           else PORTB = 0x00;
           if(cnt++ >= MAX)
            cnt = 0; 
         }
         else
         {
           if(cnt < duration)
            PORTB = 0x10, duration = 999;
           else PORTB = 0x00;  
           if(cnt++ >= MAX)
            cnt = 0; 
         }
      }
   }
   else
   {
     PORTB = 0x00;   
   }
  
}

void main(void)
{   
    DDRB = 0xff;
    DDRD = 0x00; 
    PORTD = 0xff;
    EIMSK |= 0x03;
    EICRA |= 0x0f;
    SREG.7 = 1;
    TIMSK |= 0x02;
    TCCR0 |= 0x03;
    TCCR0 |= 0x08;
    OCR0 = 15;    
    for(;;);
}
halli
  • 21
  • 1

0 Answers0