1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-23 19:21:59 +03:00

Added setAnalogResolution() method.

This commit is contained in:
Cristian Maglie
2012-08-01 13:40:41 +02:00
parent 3d9672d902
commit 66747ec688
3 changed files with 44 additions and 23 deletions

View File

@ -22,6 +22,12 @@
extern "C" { extern "C" {
#endif #endif
static int _analogResolution = 10;
void setAnalogResolution(int res) {
_analogResolution = res;
}
eAnalogReference analog_reference = AR_DEFAULT; eAnalogReference analog_reference = AR_DEFAULT;
void analogReference(eAnalogReference ulMode) void analogReference(eAnalogReference ulMode)
@ -110,7 +116,7 @@ uint32_t analogRead(uint32_t ulPin)
#if defined __SAM3X8E__ || defined __SAM3X8H__ #if defined __SAM3X8E__ || defined __SAM3X8H__
switch ( g_APinDescription[ulPin].ulAnalogChannel ) switch ( g_APinDescription[ulPin].ulAnalogChannel )
{ {
// Handling ADC 10 bits channels // Handling ADC 12 bits channels
case ADC0 : case ADC0 :
case ADC1 : case ADC1 :
case ADC2 : case ADC2 :
@ -135,10 +141,16 @@ uint32_t analogRead(uint32_t ulPin)
; ;
// Read the value // Read the value
ulValue = adc_get_latest_value(ADC) >> 2; ulValue = adc_get_latest_value(ADC);
// Disable the corresponding channel // Disable the corresponding channel
adc_disable_channel(ADC, ulChannel); adc_disable_channel(ADC, ulChannel);
// Map result into user selected resolution
if (_analogResolution > ADC_RESOLUTION)
ulValue <<= _analogResolution - ADC_RESOLUTION;
if (_analogResolution < ADC_RESOLUTION)
ulValue >>= ADC_RESOLUTION - _analogResolution;
break; break;
// Compiler could yell because we don't handle DAC pins // Compiler could yell because we don't handle DAC pins

View File

@ -20,7 +20,7 @@
#define _WIRING_ANALOG_ #define _WIRING_ANALOG_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* /*
@ -56,6 +56,13 @@ extern void analogWrite( uint32_t ulPin, uint32_t ulValue ) ;
*/ */
extern uint32_t analogRead( uint32_t ulPin ) ; extern uint32_t analogRead( uint32_t ulPin ) ;
/*
* \brief Set the resolution of analogRead return values. Default is 10 bits (0..1024).
*
* \param res
*/
void setAnalogResolution(int res);
extern void analogOutputInit( void ) ; extern void analogOutputInit( void ) ;
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -162,12 +162,14 @@ static const uint8_t DA0 = 66;
static const uint8_t DA1 = 67; static const uint8_t DA1 = 67;
static const uint8_t CANRX0 = 68; static const uint8_t CANRX0 = 68;
static const uint8_t CANTX0 = 69; static const uint8_t CANTX0 = 69;
#define ADC_RESOLUTION 12
/* /*
* DACC * DACC
*/ */
#define DACC_INTERFACE DACC #define DACC_INTERFACE DACC
#define DACC_INTERFACE_ID ID_DACC #define DACC_INTERFACE_ID ID_DACC
#define DACC_RESOLUTION 12
/* /*
* PWM * PWM