////////////////////////////////
//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
bool flag = false;// flag to tell which direction
int main(void)
{
Serial.begin(9600);
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 PC0-5 as an output using the Port C Data Direction Register (DDRC)
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;
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;
int currentLED = 0; //start it at light 0
// Loop forever
while (1)
{
switch (currentLED) {
case 0: //light 0
*portC |= BIT0_MASK; // Turn on PC0
MyDelay(1000); // Delay for 1 second
*portC &= ~BIT0_MASK; // Turn on PC0
break;
case 1: //light 1
*portC |= BIT1_MASK; // Turn on PC1
MyDelay(1000); // Delay for 1 second
*portC &= ~BIT1_MASK; // Turn on PC1
break;
case 2: // light 2
*portC |= BIT2_MASK; // Turn on PC2
MyDelay(1000); // Delay for 1 second
*portC &= ~BIT2_MASK; // Turn on PC2
break;
case 3: //light 3
*portC |= BIT3_MASK; // Turn on PC3
MyDelay(1000); // Delay for 1 second
*portC &= ~BIT3_MASK; // Turn on PC3
break;
case 4: //light 4
*portC |= BIT4_MASK; // Turn on PC4
MyDelay(1000); // Delay for 1 second
*portC &= ~BIT4_MASK; // Turn on PC4
break;
case 5: //light 5
*portC |= BIT5_MASK; // Turn on PC5
MyDelay(1000); // Delay for 1 second
*portC &= ~BIT5_MASK; // Turn on PC5
break;
}
// Increment currentLED and wrap around if needed
if(flag == false){ //PD2 is high
Serial.println("false"); //print false
currentLED++; //go up a light
if (currentLED >= 6)
currentLED = 0; //if you go past 6th light, go back to 1st
}
else if(flag == true) { //Pd2 is low
Serial.println("true"); // print true
currentLED--;
if (currentLED <0) //if you go past 1st light, go to 6th
currentLED = 5;
}
}
}
ISR(INT0_vect)
{
unsigned char *portPinD; //Pin D
portPinD = (unsigned char *) 0x29;
// Read PD2 using the Port D Pin Input Register (PIND)
if (*portPinD & 0x04){ //if PD2 high
flag = false; //lights go forward
}
else
{
// PD2 is low, so button is toggled off
flag = true; //lights go backwards
}
}
// 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++);
}