Created
April 26, 2016 15:09
-
-
Save DepthDeluxe/999bfa84f49582a6b3b29317140403df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for the paper, chose charge time T=0.56ms, used Timer0 | |
// voltage sample times | |
// k = 70 usec | |
// l = 280 usec | |
// m = 490 usec | |
const unsigned int totalPeriodTicks = 140u; // 560 usec | |
const unsigned int kTimeTicks = 17u; // 70 usec | |
const unsigned int lTimeTicks = 70u; // 280 usec | |
const unsigned int mTimeTicks = 120u; // 480 usec | |
// pins | |
int adcPin = A0; | |
int outPin = 11; | |
void setupTimer(); | |
void startTimer(); | |
void stopTimer(); | |
unsigned int getTimer(); | |
void startADC(); | |
void stopADC(); | |
//int isBusyADC(); | |
// high level measure function | |
void measure(float* measurements); | |
void setup() { | |
pinMode(outPin, OUTPUT); | |
pinMode(adcPin, INPUT); | |
setupTimer(); | |
setupADC(); | |
startADC(); | |
Serial.begin(9600); | |
Serial.println("== Start =="); | |
} | |
void loop() { | |
// take the measurements | |
float measurements[3]; | |
measure(measurements); | |
// print out the results | |
Serial.println("Results"); | |
Serial.print("k: "); Serial.println(measurements[0]); | |
Serial.print("l: "); Serial.println(measurements[1]); | |
Serial.print("m: "); Serial.println(measurements[2]); | |
Serial.flush(); | |
// sleep until the next measurement comes around | |
delay(1000); | |
} | |
void setupTimer() { | |
TCCR2A = 0x83; // fast PWM Mode, use output A for output | |
TCCR2B = 0x00; // clock inactive, fast PWM mode | |
TIMSK2 = 0x00; // no interrupts | |
OCR2A = totalPeriodTicks; // set the OCRA to 140 units below maximum | |
} | |
inline void startTimer() { | |
// set timer to top to trigger reset | |
TCNT2 = 0xFF; | |
// set the clock source to 64 prescalar to start the timer | |
TCCR2B |= 0x04; | |
} | |
inline void stopTimer() { | |
// mask out the clock source to stop the timer | |
TCCR2B &= 0xF8; | |
} | |
inline unsigned int getTimer() { | |
return TCNT2; | |
} | |
// ADC functions | |
void setupADC() { | |
ADMUX = 0x40; | |
ADCSRA = 0xA2; | |
ADCSRB = 0x00; | |
DIDR0 = 0x3F; | |
} | |
inline void startADC() { | |
ADCSRA = ADCSRA | 0x40; | |
} | |
inline void stopADC() { | |
// disable the ADC and then re-enable it to stop free-running mode | |
ADCSRA &= ~0x80; | |
ADCSRA &= 0x80; | |
} | |
/* | |
inline int isBusyADC() { | |
// check to see if triggering is done | |
if (ADCSRB & 0x10) { | |
ADCSRB |= 0x10; // deflag the conversion complete to mark as done | |
return 0; | |
} else { | |
return 1; | |
} | |
} | |
*/ | |
inline float getADCValue() { | |
unsigned long low = ADCL; | |
unsigned long hi = ADCH; | |
return (float)(hi<<8)+low; | |
} | |
void measure(float* measurements) { | |
// due to timing requirements, must disable interrupts | |
noInterrupts(); | |
float buffer; | |
// put pin high and start timer | |
startTimer(); | |
while(getTimer() < kTimeTicks); | |
measurements[0] = getADCValue(); | |
while(getTimer() < lTimeTicks); | |
measurements[1] = getADCValue(); | |
while(getTimer() < mTimeTicks); | |
measurements[2] = getADCValue(); | |
while(getTimer() < totalPeriodTicks+1); | |
stopTimer(); | |
// re-enable interrupts | |
interrupts(); | |
} | |
/* | |
@5000H 0-5V | |
Results | |
k: 701.00 | |
l: 761.00 | |
m: 824.00 | |
Results | |
k: 413.00 | |
l: 499.00 | |
m: 549.00 | |
Results | |
k: 359.00 | |
l: 343.00 | |
m: 359.00 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment