// Nathon Iadimarco
// Project 2
// Problem 2
// Embedded System Programming
// Defining masks required
#define BIT0_MASK 0x01 // 0000 0001 - Bit 0
#define BIT1_MASK 0x02 // 0000 0010 - Bit 1
#define BIT2_MASK 0x04 // 0000 0100 - Bit 2
#define BIT3_MASK 0x08 // 0000 1000 - Bit 3
#define BIT4_MASK 0x10 // 0001 0000 - Bit 4
#define BIT5_MASK 0x20 // 0010 0000 - Bit 5
#define INIT_MASK 0x3F // 0011 1111 - Initialization
// Defining port C
#define PORTC_MASK 0x28
#define DDRC_MASK 0x27
#define PINC_MASK 0x26
// Defining port D
#define PORTD_MASK 0x2B
#define DDRD_MASK 0x2A
#define PIND_MASK 0x29
// Defining external interrupt control
#define EICRA_MASK 0x69
#define EIMSK_MASK 0x3D
// Defining MyDelay function
void MyDelay(unsigned long mSec);
int main(void) {
// Initialization and assignment for DDRD
unsigned char *portD;
portD = (unsigned char *) PORTD_MASK; //0x2B
// Enable the pull-up resistor on PD2 using the Port D
// Data Register (PORTD)
*portD |= BIT2_MASK; //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 *) EICRA_MASK; //0x69
*AT328_EICRA |= BIT0_MASK; //0x01
*AT328_EICRA &= ~BIT1_MASK; //0x02
// Enable external interrupt 0 using the External Interrupt Mask Register (EIMSK)
unsigned char *AT328_EIMSK;
AT328_EIMSK = (unsigned char *) EIMSK_MASK; //0x3D
*AT328_EIMSK |= BIT0_MASK; //0x01
// Initialization and assignment for DDRC
unsigned char *portDDRC;
portDDRC = (unsigned char *) DDRC_MASK; //0x27
*portDDRC |= INIT_MASK; //0x3F Setting pins PC0-PC5 for output
// portPinC and portC intialization
unsigned char *portPinC;
unsigned char *portC;
portPinC = (unsigned char *) PINC_MASK; //0x26
portC = (unsigned char *) PORTC_MASK; //0x28
// Enable interrupts (globals enabled)
unsigned char *ptrSREG;
portDDRC = (unsigned char *) 0x5F;
*portDDRC |= 0x80;
if (*portPinC != 0x00) {
*portC &= *portPinC;
}
while (1) // Loop forever
{
if (*portPinC == 0x20) {
MyDelay(1000);
*portC |= INIT_MASK; //0x3F
*portC &= BIT0_MASK; //0x01
}
if (*portPinC == 0x00) {
MyDelay(1000);
*portC |= INIT_MASK; //0x3F
*portC &= 0x01;
}
else {
MyDelay(1000);
*portC |= INIT_MASK; //0x3F
*portC &= *portPinC * 2;
}
}
}
ISR(INT0_vect) {
unsigned char *portPinD;
portPinD = (unsigned char *) PIND_MASK; //0x29
unsigned char *portPinC;
unsigned char *portC;
portPinC = (unsigned char *) PINC_MASK; //0x26
portC = (unsigned char *) PORTC_MASK; //0x28
if (*portPinD & BIT2_MASK) { //0x04
// PD2 is high, so button is released
// Set PB5 low using the Port B Data Register (PORTB)
}
else {
// PD2 is low, so button is pressed
// Set PB5 high using the Port B Data Register (PORTB)
*portC &= *portPinC;
while (!(*portPinD & BIT2_MASK)) { // 0x04
if (*portPinC == 0x01) {
MyDelay(1000);
*portC |= INIT_MASK; //0x3F
*portC &= BIT5_MASK; //0x20
}
MyDelay(1000);
*portC |= INIT_MASK; //0x3F
*portC &= *portPinC / 2;
}
}
}
// Define MyDelay function
void MyDelay(unsigned long mSec)
{
volatile unsigned long i;
unsigned long endT = 7 * mSec;
for (i = 0; i < endT; i++);
}