Skip to content

Commit ede735e

Browse files
committed
0.1.1 version
1 parent 90f2f1f commit ede735e

File tree

8 files changed

+296
-2
lines changed

8 files changed

+296
-2
lines changed

AsyncAnalog.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// FILE: AsyncAnalog.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.1
5+
// DATE: 2018-09-05
6+
// PURPOSE: async version of analogRead, prevent blocking wait
7+
//
8+
// backgrounder
9+
// https://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-adc?name=PNphpBB2&file=viewtopic&t=56429
10+
//
11+
// HISTORY:
12+
// 0.1.0 2018-09-05 initial version, based upon analogRead()
13+
// 0.1.1 2020-03-26 minor refactor
14+
//
15+
16+
#include "AsyncAnalog.h"
17+
18+
19+
#if defined(ARDUINO_ARCH_AVR)
20+
21+
AsyncAnalog::AsyncAnalog(const uint8_t pin)
22+
{
23+
_pin = pin;
24+
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
25+
if (_pin >= 54) _pin -= 54;
26+
#else
27+
if (_pin >= 14) _pin -= 14;
28+
#endif
29+
}
30+
31+
void AsyncAnalog::start()
32+
{
33+
#if defined(ADCSRB) && defined(MUX5)
34+
// the MUX5 bit of ADCSRB selects whether we're reading from channels
35+
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
36+
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((_pin >> 3) & 0x01) << MUX5);
37+
#endif
38+
39+
#if defined(ADMUX)
40+
// set the analog reference (high two bits of ADMUX) and select the
41+
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
42+
// to 0 (the default).
43+
ADMUX = (DEFAULT << 6) | (_pin & 0x07);
44+
#endif
45+
46+
sbi(ADCSRA, ADSC);
47+
}
48+
49+
50+
bool AsyncAnalog::ready()
51+
{
52+
// ADSC is cleared when the conversion finishes
53+
return bit_is_set(ADCSRA, ADSC) == 0;
54+
}
55+
56+
57+
int AsyncAnalog::value()
58+
{
59+
// we have to read ADCL first; doing so locks both ADCL
60+
// and ADCH until ADCH is read. reading ADCL second would
61+
// cause the results of each conversion to be discarded,
62+
// as ADCL and ADCH would be locked when it completed.
63+
int low = ADCL;
64+
int high = ADCH;
65+
// combine the two bytes
66+
return (high << 8) | low;
67+
}
68+
69+
#endif // ARDUINO_ARCH_AVR
70+
71+
// -- END OF FILE --

AsyncAnalog.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
//
3+
// FILE: AsyncAnalog.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.1.1
6+
// DATE: 2018-09-05
7+
// PURPOSE: async version of analogRead
8+
// URL: https://github.com/RobTillaart/AsyncAnalog
9+
//
10+
11+
#if !defined(ARDUINO_ARCH_AVR)
12+
#error “AsyncAnalog library 0.1.1 only supports boards with an AVR processor .”
13+
#endif
14+
// (ARDUINO_ARCH_SAM) future
15+
16+
17+
#include "Arduino.h"
18+
#include "wiring_private.h"
19+
#include "pins_arduino.h"
20+
21+
#define ASYNCANALOG_LIB_VERSION "0.1.1"
22+
23+
class AsyncAnalog
24+
{
25+
public:
26+
AsyncAnalog(uint8_t pin);
27+
28+
void start();
29+
bool ready();
30+
int value();
31+
32+
private:
33+
uint8_t _pin;
34+
};
35+
36+
// -- END OF FILE --

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Rob Tillaart
3+
Copyright (c) 2018-2020 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
# AsyncAnalog
2-
Arduino Library for async reading of an analog pin [AVR only for now]
2+
3+
Arduino Library for async reading of an analog pin. **\[AVR ONLY\]**
4+
5+
6+
## Description
7+
AsyncAnalog is a library to read the analog port in an asynchronous way.
8+
This means that the user must explicitly **start** the ADC, check if it is **ready**
9+
and read out its **value**.
10+
11+
By using this class, the user prevents the (112 uSec) blocking of the
12+
**analogRead()** call, and gives the user the ability to do some processing.
13+
14+
The library works only for AVR boards now, other platforms might be supported in the future.
15+
16+
As the UNO has only one ADC that is multiplexed, one can only read one analog pin
17+
in async way simultaneously.
18+
19+
## Operation
20+
The library consists of three main function:
21+
22+
* **void start()**
23+
* **bool ready()**
24+
* **int value()**
25+
26+
The example **asyncAnalogTest2.ino** shows a loop of 1000 analogReads and prints
27+
over Serial at 115200 baud. The async test does this in less time. Note that faster
28+
baudrates shows an even bigger difference.
29+
30+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// FILE: asyncAnalogTest.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.1
5+
// DATE: 2018-09-05
6+
7+
#include "AsyncAnalog.h"
8+
9+
AsyncAnalog AA(A0);
10+
11+
uint32_t start = 0;
12+
uint32_t duration = 0;
13+
uint16_t count = 0;
14+
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
Serial.println("start: ");
19+
Serial.println(analogRead(0));
20+
21+
AA.start();
22+
start = micros();
23+
}
24+
25+
void loop()
26+
{
27+
28+
// if sample ready
29+
if (AA.ready())
30+
{
31+
// process sample
32+
duration = micros() - start;
33+
34+
Serial.print(duration);
35+
Serial.print("\t");
36+
Serial.print(AA.value());
37+
Serial.print("\t");
38+
Serial.print(count);
39+
Serial.println();
40+
41+
// request a new sample
42+
AA.start();
43+
start = micros();
44+
count = 0;
45+
}
46+
47+
// do other stuff here
48+
count++;
49+
}
50+
51+
// END OF FILE
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// FILE: asyncAnalogTest2.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// DATE: 2020-03-27
6+
7+
#include "AsyncAnalog.h"
8+
9+
AsyncAnalog AA(A0);
10+
11+
uint32_t start = 0;
12+
uint32_t duration = 0;
13+
14+
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
Serial.println("start: ");
19+
Serial.println(analogRead(0));
20+
}
21+
22+
23+
void loop()
24+
{
25+
normal_test();
26+
delay(1000);
27+
28+
async_test();
29+
delay(1000);
30+
}
31+
32+
33+
void normal_test()
34+
{
35+
Serial.println();
36+
Serial.println(__FUNCTION__);
37+
38+
start = micros();
39+
for (int i = 0; i < 1000; i++)
40+
{
41+
int x = analogRead(A0);
42+
Serial.println(x);
43+
}
44+
duration = micros() - start;
45+
Serial.print(duration);
46+
Serial.print("\n\n");
47+
}
48+
49+
50+
void async_test()
51+
{
52+
int x = 0;
53+
54+
Serial.println();
55+
Serial.println(__FUNCTION__);
56+
57+
start = micros();
58+
AA.start();
59+
for (int i = 0; i < 1000; i++)
60+
{
61+
if (AA.ready())
62+
{
63+
x = AA.value(); // read the value
64+
AA.start(); // request next conversion
65+
Serial.println(x); // print value while conversion runs in the background
66+
}
67+
}
68+
duration = micros() - start;
69+
Serial.print(duration);
70+
Serial.print("\n\n");
71+
}
72+
73+
// END OF FILE

library.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "AsyncAnalog",
3+
"keywords": "analogRead,smooth,analog,pin,noise,reduction",
4+
"description": "Class for smoothing analogReads.",
5+
"authors":
6+
[
7+
{
8+
"name": "Rob Tillaart",
9+
"email": "Rob.Tillaart@gmail.com",
10+
"maintainer": true
11+
}
12+
],
13+
"repository":
14+
{
15+
"type": "git",
16+
"url": "https://github.com/RobTillaart/AsyncAnalog.git"
17+
},
18+
"version":"0.1.1",
19+
"frameworks": "arduino",
20+
"platforms": "*",
21+
"export": {
22+
"include": "AsyncAnalog"
23+
}
24+
}

library.properties

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name=AsyncAnalog
2+
version=0.1.1
3+
author=Rob Tillaart <rob.tillaart@gmail.com>
4+
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
5+
sentence=Arduino Library for async reading of an analog pin
6+
paragraph=AVR only for now.
7+
category=Signal Input/Output
8+
url=https://github.com/RobTillaart/AsyncAnalog
9+
architectures=avr
10+
include=AsyncAnalog.h
11+
depends=

0 commit comments

Comments
 (0)