So for a university project I need to essentially create an analog voltmeter by measuring the ADC and outputting a PWM wave to a servo. That part is easy and I have done it already. What I am struggling with is implementing interrupts. The assignment is to have one interrupt that begins the ADC Conversion (i.e. sets the ADCTRL bit 6 HIGH), and one that is the ADC Conversion Complete interrupt. I have tried so many things inside the ISR’s and nothing works.
#include <stdint.h>
#include "myLibrary.h"
#include "avr/interrupt.h"
int main(void){
init();
Serial.begin(9600);
// Set PB1 to be the output PWM from TCNT1
ioDDRB = 0x02;
// Set up ADC but do not begin conversion yet
ADMUX_reg = 0x40;
ADCTRL_reg = 0x87;
// Set up TCNT1 to output PWM wave which will control the servo
// We begin by setting OCR to the value which will represent 0V input
TC1A_reg = 0x82;
TC1B_reg = 0x1A;
IC1H_reg = 0x9C;
IC1L_reg = 0x40;
OC1H_reg = 0x12;
OC1L_reg = 0xC0;
// Set up TCNT2 to give an overflow interrupt through CTC mode every 10-15 ms
TC2A_reg = 0xA2;
TC2B_reg = 0x05;
OCR2A_reg = 0xAA;
OCR2B_reg = 0xAA;
TIMSK2_reg = 0x01;
// Enable global interrupts
SREG_reg = 0x80;
while(1){
ADCTRL_reg = (ADCTRL_reg) | 0x40;
while ((ADCTRL_reg & 0x40) == 0x40 ){}
volatile uint8_t theLow;
volatile uint16_t result;
volatile uint16_t temp;
theLow = ADCL_reg;
result = (((ADCH_reg) << 8 | (theLow)));
float volt;
volt = result;
volt = volt * 5 / 1024;
// Since we only want one decimal place, we must round volt
volt = volt * 10;
volt = round(volt);
volt = volt / 10;
temp = 0x12C0;
temp = temp - (volt*730);
OC1H_reg = (temp >> 8);
OC1L_reg = (temp & 0x00FF);
}
return(0);
}
ISR(TIMER2_OVF_vect){
//ADCTRL_reg = (ADCTRL_reg) | 0x40; // start conversion
}
ISR(ADC_vect){
}