// CS-33301 Project #4 Question 1
// by Chase Osborne
// With code from Prof.JungYoon
// GPIO Variable Initialization
unsigned char *portDDRC;
unsigned char *portPinC;
unsigned char *portC;
unsigned char *portDDRD;
unsigned char *portPinD;
unsigned char *portD;
// ADC Variable Initialization
#define ADC_PIN 0b1110
unsigned char *ADMUX_ESP;
unsigned char *ADCSRA_ESP;
unsigned char *ADCSRB_ESP;
unsigned char *ADCL_ESP;
unsigned char *ADCH_ESP;
// Timer Variable Initializaiton
unsigned char *TCCR1A_ESP;
unsigned char *TCCR1B_ESP;
unsigned char *TCNT1H_ESP;
unsigned char *TCNT1L_ESP;
unsigned char *OCR1AH_ESP;
unsigned char *OCR1AL_ESP;
unsigned char *TIMSK1_ESP;
bool ledON = true; // For LED blink
/*
// External Interrupt Control Initialization
#define EICRA_MASK 0x69
#define EIMSK_MASK 0x3D
unsigned char *AT328_EICRA;
unsigned char *AT328_EIMSK;
unsigned char *ptrSREG;
*/
bool beenRead = false; // for temperature reading
// Dr.JungYoon's adc_read function
uint16_t adc_read(uint8_t adcx);
void setup(){
Serial.begin(9600); // open the serial port at 9600 bps:
// ADC assignment
ADMUX_ESP = (unsigned char*) 0x7c;
ADCSRA_ESP = (unsigned char*) 0x7a;
ADCSRB_ESP = (unsigned char*) 0x7b;
ADCL_ESP = (unsigned char*) 0x78;
ADCH_ESP = (unsigned char*) 0x79;
*ADCSRA_ESP |= 0b10000000; // Enabling ADC
// GPIO Assignment
portDDRC = (unsigned char*) 0x27;
portPinC = (unsigned char*) 0x26;
portC = (unsigned char*) 0x28;
portDDRD = (unsigned char*) 0x2A;
portPinD = (unsigned char*) 0x29;
portD = (unsigned char*) 0x2B;
*portDDRD &= 0b11111011; // Configure bit 0 as input
*portDDRC |= 0b00000010; // Configure bit 5 as an output
*portC &= 0b11111101; // Default to off
// Timer Assignment
TCCR1A_ESP = (unsigned char *) 0x80;
TCCR1B_ESP = (unsigned char *) 0x81;
TCNT1H_ESP = (unsigned char *) 0x85;
TCNT1L_ESP = (unsigned char *) 0x84;
OCR1AH_ESP = (unsigned char *) 0x89;
OCR1AL_ESP = (unsigned char *) 0x88;
TIMSK1_ESP = (unsigned char *) 0x6F;
*TCCR1A_ESP = 0x00;
*TCCR1B_ESP = 0x0C; // CTC mode - BIT 3; 256 prescaler BIT2
*TCNT1H_ESP = 0x00;
*TCNT1L_ESP = 0x00;
*OCR1AH_ESP = 0xF4; // OCRA value of 62499 for 1hz updates
*OCR1AL_ESP = 0x23;
*TIMSK1_ESP = 0x02; // enable timer compare interrupt - BIT1
/*
// Interrupt Assignment
AT328_EICRA = (unsigned char *) EICRA_MASK;
AT328_EIMSK = (unsigned char *) EIMSK_MASK;
// Enabling Interrupt on any logical change of INT0/PB0/D8
*AT328_EICRA |= 0b00000011;
*AT328_EICRA &= 0b11111100;
*AT328_EIMSK |= 0b00000001;
// SREG Assignment
portDDRD = (unsigned char *) 0x5F;
*portDDRD |= 0x80;
*/
}
void loop() {
if (beenRead == false) {
if ((*portPinD) &= 0b00000100) {
Serial.print("[email protected]:");
Serial.println(adc_read(ADC_PIN));
beenRead = true;
}
}
if (adc_read(ADC_PIN) >= 270) { ledON = false; }
}
/*
ISR(INT0_vect) {
Serial.println(BIN, (*portD));
if ((*portD) &= 0b00000100) {
Serial.print("[email protected]:");
Serial.println(adc_read(ADC_PIN));
}
}
*/
ISR(TIMER1_COMPA_vect){
if (ledON) {
ledON = false;
(*portC) |= 0b00000010; // Turning LED on
}
else {
ledON = true;
(*portC) &= 0b11111101; // Turning LED off
}
// Cooldown for button press
// Pressing button interrupts LED
if ((*portPinD) <= 3) {
beenRead = false;
}
}
uint16_t adc_read(uint8_t adcx) {
/* adcx is the analog pin we want to use. ADMUX's first few bits are
* the binary representations of the numbers of the pins so we can
* just 'OR' the pin's number with ADMUX to select that pin.
* We first zero the four bits by setting ADMUX equal to its higher
* four bits. */
*ADMUX_ESP &= 0xf0;
*ADMUX_ESP |= adcx;
/* This starts the conversion. */
*ADCSRA_ESP |= 0b01000000;
/* Finally, we return the converted value to the calling function. */
unsigned char cl = *ADCL_ESP;
unsigned char ch = *ADCH_ESP;
return (cl | (ch << 8));
}