////////////////////////////////
//Ean Dodge //
//Computer science //
//Embedded systems programming//
////////////////////////////////
#define BIT0_MASK 0x01 //0000 0001
#define BIT1_MASK 0x02 //0000 0010
#define BIT2_MASK 0x04 //0000 0100
#define BIT3_MASK 0x08 //0000 1000
#define BIT4_MASK 0x10 //0001 0000
#define BIT5_MASK 0x20 //0010 0000
#define DDRC_MASK 0x27 // directrion for C
#define PORTC_MASK 0x28 // for the output
void MyDelay(unsigned long mSec); // delay for miliseconds
void Myloop(); //for light pattern
bool flag = false;
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 *portDDRC;
portDDRC = (unsigned char *) DDRC_MASK;
*portDDRC |= BIT0_MASK; //enable all of them as outputs
*portDDRC |= BIT1_MASK;
*portDDRC |= BIT2_MASK;
*portDDRC |= BIT3_MASK;
*portDDRC |= BIT4_MASK;
*portDDRC |= BIT5_MASK;
// Enable interrupts
unsigned char *ptrSREG;
ptrSREG = (unsigned char *) 0x5F;
*ptrSREG |= 0x80;
// Loop forever
while (1)
{
Myloop(); //light pattern
}
}
void Myloop(){
unsigned char *portC;
portC = (unsigned char *) PORTC_MASK;
*portC &= ~BIT0_MASK; //initialize as off
*portC &= ~BIT1_MASK;
*portC &= ~BIT2_MASK;
*portC &= ~BIT3_MASK;
*portC &= ~BIT4_MASK;
*portC &= ~BIT5_MASK;
// Read PD2 using the Port D Pin Input Register (PIND)
// PD2 is high, so button is toggled on
*portC |= BIT0_MASK; //turn on 1
*portC |= BIT5_MASK; //turn on 6
MyDelay(1000); //stay on for one second
*portC &= ~BIT0_MASK;//turns off 1
*portC &= ~BIT5_MASK;//turns off 6
*portC |= BIT1_MASK; //second light turns on
*portC |= BIT4_MASK; //turns on 5
MyDelay(1000); //stays on for one second
*portC &= ~BIT1_MASK;//turns off 2
*portC &= ~BIT4_MASK;//turns off 5
*portC |= BIT2_MASK; //third light turns on
*portC |= BIT3_MASK; //fouth light turns on
MyDelay(1000); //stays on for one second
*portC &= ~BIT2_MASK;//turns off 3
*portC &= ~BIT3_MASK; //turns off 4
*portC |= BIT2_MASK; //third light turns on
*portC |= BIT3_MASK; //fouth light turns on
MyDelay(1000); //stays on for one second
*portC &= ~BIT2_MASK;//turns off 3
*portC &= ~BIT3_MASK; //turns off 4
*portC |= BIT1_MASK; //second light turns on
*portC |= BIT4_MASK; //turns on 5
MyDelay(1000); //stays on for one second
*portC &= ~BIT1_MASK;//turns off 2
*portC &= ~BIT4_MASK;//turns off 5
}
// Define MyDelay function
void MyDelay(unsigned long mSec)
{
volatile unsigned long i;
unsigned long endT = 1000 * mSec; //run for n seconds
for (i = 0; i < endT; i++);
}