Refactor A/D conversion in adc.c

Now the necessary settings are in adc.h. Refactored to allow repeated
ADC reads without reinitialization. Arduino allows setting
analogReference, this is now also implemented.
ADC is now initialized to sane values in apps/arduino/arduino-process.c
dev/arduino/arduino-compat.h now has all hardware independent settings
for arduino (some moved from platform/osd-merkur/dev/hw-arduino.h).
turnOffPWM re-implemented with hw_timer, removed from wiring_digital.c
ADC-specific arduino stuff moved to arduino-compat.h
Arduinos wiring_analog no longer necessary.
arduino-sketch example now reads analog inputs 1 and 5 using analogRead.
This commit is contained in:
Ralf Schlatterbeck 2014-06-29 17:26:15 +02:00
parent f0ad042bfc
commit abdf6f8c6b
12 changed files with 154 additions and 199 deletions

View file

@ -21,10 +21,15 @@
#include "erbium.h"
#include "er-coap-13.h"
extern uint8_t pwm;
extern uint8_t period_100ms;
extern uint8_t pwm;
extern uint8_t period_100ms;
extern uint16_t analog1_voltage;
extern uint16_t analog5_voltage;
extern resource_t resource_led_pwm;
extern resource_t resource_led_period;
extern resource_t resource_analog1_voltage;
extern resource_t resource_analog5_voltage;
#endif // led_pwm_h
/** @} */

View file

@ -69,6 +69,38 @@ GENERIC_RESOURCE \
, period_to_string
);
size_t
analog1_v (const char *name, uint8_t is_json, char *buf, size_t bufsize)
{
return snprintf
(buf, bufsize, "%d.%03d", analog1_voltage / 1000, analog1_voltage % 1000);
}
GENERIC_RESOURCE \
( analog1_voltage, METHOD_GET
, "analog/1"
, Analog 1 voltage
, V
, NULL
, analog1_v
);
size_t
analog5_v (const char *name, uint8_t is_json, char *buf, size_t bufsize)
{
return snprintf
(buf, bufsize, "%d.%03d", analog5_voltage / 1000, analog5_voltage % 1000);
}
GENERIC_RESOURCE \
( analog5_voltage, METHOD_GET
, "analog/5"
, Analog 5 voltage
, V
, NULL
, analog5_v
);
/*
* VI settings, see coding style
* ex:ts=8:et:sw=2

View file

@ -15,8 +15,10 @@ extern "C" {
#include "led_pwm.h"
#define LED_PIN 5
uint8_t pwm = 128;
uint8_t period_100ms = 10; /* one second */
uint8_t pwm = 128;
uint8_t period_100ms = 10; /* one second */
uint16_t analog1_voltage = 0;
uint16_t analog5_voltage = 0;
}
void setup (void)
@ -24,12 +26,16 @@ void setup (void)
rest_init_engine ();
rest_activate_resource (&resource_led_pwm);
rest_activate_resource (&resource_led_period);
rest_activate_resource (&resource_analog1_voltage);
rest_activate_resource (&resource_analog5_voltage);
}
void loop (void)
{
/* Use 255 - pwm, LED on merkur-board is wired to +3.3V */
analogWrite (LED_PIN, 255 - pwm);
analog1_voltage = analogRead (1) * 1600L / 1023L;
analog5_voltage = analogRead (5) * 1600L / 1023L;
printf ("clock : %u\nmillis: %lu\n", clock_time (), millis ());
delay (period_100ms * 100);
analogWrite (LED_PIN, 255); /* OFF: LED on merkur-board is wired to +3.3V */