// //////////////////////////////////////////////
// ESP (CS 33301-001)
// Project 1.1: Input - Polling --> LED ON OFF w/ SW
// Kent State University
// Dept. of Computer Science
// Ean Dodge
// //////////////////////////////////////////////
#define BIT3_MASK 0x08 // 0000 1000 --> Pin 3
#define BIT5_MASK 0x20 // 0010 0000 --> Pin 5
#define PORTB_MASK 0x25 //for input port
#define PORTD_MASK 0x2B //for output port
#define DDRB_MASK 0x24// for the DDRB
#define DDRD_MASK 0x2A //for the DDRD
#define PINB_MASK 0x23 //for input device
unsigned char *portDDRB; //declaring DDRB
unsigned char *portDDRD; //declaring DDRD
unsigned char *portPinB; //decalaaring Pin B to use as input B
unsigned char *portB; //declaring port B to use as output D
unsigned char *portD; //declaing port D to use as output D
// Determine MyDelay function
void MyDelay(unsigned long mSec);
// The setup function runs once when you press reset or power the
//board
void setup() {
// Port B
portDDRB = (unsigned char *) DDRB_MASK; //pointing DDRB to a hardcoded value of 0x24
*portDDRB &= ~(BIT3_MASK); // Configure bit 3 of DDRB as an input
*portDDRB |= BIT5_MASK; //Configure bit 5 of DDRB as an output
portPinB = (unsigned char *) PINB_MASK; //pointing PinB to a hardcoded value of 0x23
portB = (unsigned char *) PORTB_MASK; //pointing PortB to a hardcoded value of 0x25
*portB |= BIT5_MASK; //turns on internal LED to initialize it as on
// Port D
portDDRD = (unsigned char *) DDRD_MASK; //pointing DDRD to a hardcoded value of 0x2A
*portDDRD |= (BIT5_MASK); // Configure bit 5 of DDRD as an output
portD = (unsigned char *) PORTD_MASK; // pointing portD to a hardcoded value of 0x2B
*portD |= BIT5_MASK; //turns on external LED to initialize it as on
}
// the loop function
void loop() {
// Check the Input Status:
if ((*portPinB & BIT3_MASK) == 0x08) //checking to see if both the input value is 1(00001000)
{ //We are looking at only bit3 here
*portD &= ~(BIT5_MASK); // External LED Off:
MyDelay(1000); //delay 1 seconds
*portB &= ~(BIT5_MASK); //internal LED Off:
*portD |= BIT5_MASK; // External LED ON:
MyDelay(1000); //delay 1 seconds
*portB |= (BIT5_MASK);//internal LED on
}
//External Light: on | off | on | off | ...
// ----|-----|-----|-----|-----
//Internal Light: on | on | off | on | ...
}
// 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++);
}