// ESP project 4 Part 2d
// Evan McDermott
#define BIT5_MASK 0x20 // 0010 0000 --> Pin 5
#define BIT4_MASK 0x10 // 0001 0000 --> Pin 4
#define PORTB_MASK 0x25 // input port
#define DDRB_MASK 0x24//
#define PINB_MASK 0x23 //input
unsigned char *ADMUX_ESP;
unsigned char *ADCSRA_ESP;
unsigned char *ADCSRB_ESP;
unsigned char *ADCL_ESP;
unsigned char *ADCH_ESP;
#define ADC_PIN 14
uint16_t adc_read(uint8_t adcx);
unsigned char *TCCR1A_ESP; //0x80
unsigned char *TCCR1B_ESP; //0x81
unsigned char *TCNT1H_ESP; //0x85
unsigned char *TCNT1L_ESP; //0x84
unsigned char *OCR1AH_ESP; //0x89
unsigned char *OCR1AL_ESP; //0x88
unsigned char *TIMSK1_ESP; //0x6F
unsigned char *portDDRB; //declaring DDRB
unsigned char *portB; // port B output
unsigned char *portPinB; // input
void MyDelay(unsigned long mSec);
void setup() {
Serial.begin(9600); // open serial
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;
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;
portDDRB = (unsigned char *) DDRB_MASK;
*portDDRB |= BIT5_MASK;
*portDDRB &= ~BIT4_MASK;
portB = (unsigned char *) PORTB_MASK;
*portB &= ~BIT5_MASK;
portPinB = (unsigned char *) PINB_MASK;
*TCCR1A_ESP = 0x00;
*TCCR1B_ESP = 0x0C; // 256 prescaler
*TCNT1H_ESP = 0x00;
*TCNT1L_ESP = 0x00;
*OCR1AH_ESP = 0xF2;
*OCR1AL_ESP = 0x24;
*TIMSK1_ESP = 0x02; // enable interrupt
}
int flag = 0;
bool done = false;
bool eval = false;
int potpin = 0;
int num;
void loop() {
/* Enable the ADC */
*ADCSRA_ESP |= 0b10000000;
num = adc_read(ADC_PIN);
if (num <= 270) {
done = true;
}else{
done = false;
*portB &= ~BIT5_MASK; //turns off internal LED to initialize it as off
}
//converts temp to celsius, code from wokwi thermometer page
int BETA = 3950;
float celcius = 1 / (log(1 / (1023. / num - 1)) / BETA + 1.0 / 298.15) - 273.15;
if (!eval){
if (*portPinB == 0x10){
Serial.print ("Temp: ");
Serial.print(celcius);
eval = true;
}
}
}
ISR(TIMER1_COMPA_vect)
{
if(done == true){
if (flag == 0)
{
*portB &= ~0x20; // Internal LED off
flag = 1;
} else {
*portB |= 0x20; // Internal LED on
flag = 0;
}
}
if ((*portPinB) <= 3) {
eval = 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));
}