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;
//Configrue PC0 - PC5 as outputs using DDRC
unsigned char *portDDRC;
portDDRC = (unsigned char *) 0x27;
*portDDRC |= 0x3F;
// Enable interrupts
unsigned char *ptrSREG;
unsigned char *portDDRB;
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 *AT328_EIMSK;
AT328_EIMSK = (unsigned char *) 0x3D;
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
// Table 4 for logical high
*AT328_EIMSK &= ~0x01; //turn external input off
*portC = 0x00; *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; *AT328_EIMSK |= 0x01; //turn external input on
MyDelay(1000);
*portC &= ~0x10; *portC |= 0x20;
MyDelay(1000);
*portC &= ~0x20; *portC |= 0x10;
MyDelay(1000);
*portC &= ~0x10; *portC |= 0x08; *AT328_EIMSK &= ~0x01; //turn external input off
MyDelay(1000);
*portC &= ~0x08; *portC |= 0x10;
MyDelay(1000);
*AT328_EIMSK |= 0x01;
}
else
{
// PD2 is low, so button is pressed
// 1.1 for logical low
*portC = 0x00; *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; *portC |= 0x10;
MyDelay(1000);
// 1.2 for logical low
*portC = 0x00; *portC |= 0x21;
MyDelay(1000);
*portC &= ~0x21; *portC |= 0x12;
MyDelay(1000);
*portC &= ~0x12; *portC |= 0xC;
MyDelay(1000);
*portC &= ~0xC; *portC |= 0x12;
MyDelay(1000);
*portC &= ~0x12; *portC |= 0x21;
MyDelay(1000);
*portC &= ~0x21; *portC |= 0x12;
MyDelay(1000);
*portC &= ~0x12; *portC |= 0xC;
MyDelay(1000);
//1.3 - same as 1.1 for logical low
*portC = 0x00; *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; *portC |= 0x10;
MyDelay(1000);
}
}
void MyDelay(unsigned long mSec) {
volatile unsigned long i;
unsigned long endT = 1000 * mSec;
for(i = 0; i < endT; i++);
}