void MyDelay(unsigned long mSec);
int main(void)
{
unsigned char *portD;
portD = (unsigned char *) 0x2B;
// Enable the pull-up resistor on PD2 using the Port D
// Data Register (PORTD)
*portD |= 0x04;
// Configure external interrupt 0 to generate an interrupt request on any
// logical change using External Interrupt Control Register A (EICRA)
unsigned char *AT328_EICRA;
AT328_EICRA = (unsigned char *) 0x69;
*AT328_EICRA |= 0x01;
*AT328_EICRA &= ~(0x02);
// Enable external interrupt 0 using the External Interrupt Mask Register (EIMSK)
unsigned char *AT328_EIMSK;
AT328_EIMSK = (unsigned char *) 0x3D;
*AT328_EIMSK |= 0x01;
// Configure PB5 as an output using the Port B Data Direction Register (DDRB)
unsigned char *portDDRB;
portDDRB = (unsigned char *) 0x24;
*portDDRB |= 0x20;
// Enable interrupts
unsigned char *ptrSREG;
portDDRB = (unsigned char *) 0x5F;
*portDDRB |= 0x80;
// Loop forever
while (1)
{
// Nothing to do here. All work is done in the ISR
}
}
ISR(INT0_vect)
{
unsigned char *portPinD;
portPinD = (unsigned char *) 0x29;
unsigned char *portC;
portC = (unsigned char *) 0x28;
// Read PD2 using the Port D Pin Input Register (PIND)
if (*portPinD & 0x04)
{
// PD2 is high, so button is released
// Set PB5 low using the Port B Data Register (PORTB)
*portC |= 0x01;
*portC |= 0x20;
MyDelay(1000);
*portC &= ~0x01;
*portC &= ~0x20;
*portC |= 0x02;
*portC |= 0x10;
MyDelay(1000);
*portC &= ~0x02;
*portC &= ~0x10;
*portC |= 0x04;
*portC |= 0x08;
MyDelay(2000);
*portC &= ~0x04;
*portC &= ~0x08;
*portC |= 0x02;
*portC |= 0x10;
MyDelay(1000);
*portC &= ~0x02;
*portC &= ~0x10;
}
else
{
// PD2 is low, so button is pressed
// Set PB5 high using the Port B Data Register (PORTB)
*portC |= 0x01;
MyDelay(1000);
*portC &= ~0x01;
*portC |= 0x02;
MyDelay(1000);
*portC &= ~0x02;
*portC |= 0x04;
MyDelay(1000);
*portC &= ~0x04;
*portC |= 0x08;
MyDelay(1000);
*portC &= ~0x08;
*portC |= 0x10;
MyDelay(1000);
*portC &= ~0x10;
*portC |= 0x20;
MyDelay(1000);
*portC &= ~0x20;
}
}
void MyDelay(unsigned long mSec)
{
volatile unsigned long i;
unsigned long endT = 500 * mSec;
for (i = 0; i < endT; i++);
}