////////////////////////////////
//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();
bool flag = false;
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 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;
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;
int time = 100;
// Loop forever
while (1)
{
switch (currentLED) {
case 0:
*portC |= BIT0_MASK; // Turn on PC0
MyDelay(time); // Delay for 1 second
*portC &= ~BIT0_MASK; // Turn on PC0
break;
case 1:
*portC |= BIT1_MASK; // Turn on PC0
MyDelay(time); // Delay for 1 second
*portC &= ~BIT1_MASK; // Turn on PC0
break;
case 2:
*portC |= BIT2_MASK; // Turn on PC0
MyDelay(time); // Delay for 1 second
*portC &= ~BIT2_MASK; // Turn on PC0
break;
case 3:
*portC |= BIT3_MASK; // Turn on PC0
MyDelay(time); // Delay for 1 second
*portC &= ~BIT3_MASK; // Turn on PC0
break;
case 4:
*portC |= BIT4_MASK; // Turn on PC0
MyDelay(time); // Delay for 1 second
*portC &= ~BIT4_MASK; // Turn on PC0
break;
case 5:
*portC |= BIT5_MASK; // Turn on PC0
MyDelay(time); // Delay for 1 second
*portC &= ~BIT5_MASK; // Turn on PC0
break;
}
// Increment currentLED and wrap around if needed
if(flag == false){
currentLED++;
if (currentLED >= 6)
currentLED = 0;
}
else if(flag == true) {
*portC &= ~BIT0_MASK;
*portC &= ~BIT1_MASK;
*portC &= ~BIT2_MASK;
*portC &= ~BIT3_MASK;
*portC &= ~BIT4_MASK;
*portC &= ~BIT5_MASK;
}
Serial.println(time);
--time; //looks like anything from 1-9 looks pretty still. 9 gives off a
// candle type effect
}
}
ISR(INT0_vect)
{
unsigned char *portPinD;
portPinD = (unsigned char *) 0x29;
// Read PD2 using the Port D Pin Input Register (PIND)
if (*portPinD & 0x04){
flag = true;
}
else
{
// PD2 is low, so button is toggled off
// Set PC0-5 high using the Port C Data Register (PORTC)
flag = false;
}
}
// 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++);
}