@@ -28,77 +28,144 @@ static int _writeResolution = 8;
28
28
29
29
void analogReadResolution ( int res )
30
30
{
31
- _readResolution = res ;
31
+ switch ( res )
32
+ {
33
+ case 12 :
34
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
35
+ ADC -> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_12BIT_Val ;
36
+ break ;
37
+ case 8 :
38
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
39
+ ADC -> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_8BIT_Val ;
40
+ break ;
41
+ default :
42
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
43
+ ADC -> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_10BIT_Val ;
44
+ break ;
45
+ }
46
+ _readResolution = res ;
32
47
}
33
48
34
49
void analogWriteResolution ( int res )
35
50
{
36
- _writeResolution = res ;
51
+ _writeResolution = res ;
37
52
}
38
53
39
54
static inline uint32_t mapResolution ( uint32_t value , uint32_t from , uint32_t to )
40
55
{
41
- if ( from == to )
56
+ if ( from == to )
42
57
{
43
- return value ;
58
+ return value ;
44
59
}
45
60
46
- if ( from > to )
61
+ if ( from > to )
47
62
{
48
- return value >> (from - to ) ;
63
+ return value >> (from - to ) ;
49
64
}
50
- else
65
+ else
51
66
{
52
- return value << (to - from ) ;
67
+ return value << (to - from ) ;
53
68
}
54
69
}
55
70
56
71
void analogReference ( eAnalogReference ulMode )
57
72
{
58
- // ATTENTION : On this board the default is not 5volts or 3.3volts BUT 1volt
59
-
73
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
60
74
switch ( ulMode )
61
75
{
62
- case AR_DEFAULT :
63
76
case AR_INTERNAL :
64
- default :
65
- ADC -> REFCTRL .bit .REFSEL = ADC_REFCTRL_REFSEL_INT1V_Val ;
77
+ case AR_INTERNAL2V23 :
78
+ ADC -> INPUTCTRL .bit .GAIN = ADC_INPUTCTRL_GAIN_1X_Val ; // Gain Factor Selection
79
+ ADC -> REFCTRL .bit .REFSEL = ADC_REFCTRL_REFSEL_INTVCC0_Val ; // 1/1.48 VDDANA = 1/1.48* 3V3 = 2.2297
66
80
break ;
67
81
68
82
case AR_EXTERNAL :
83
+ ADC -> INPUTCTRL .bit .GAIN = ADC_INPUTCTRL_GAIN_1X_Val ; // Gain Factor Selection
69
84
ADC -> REFCTRL .bit .REFSEL = ADC_REFCTRL_REFSEL_AREFA_Val ;
70
85
break ;
86
+
87
+ case AR_INTERNAL1V0 :
88
+ ADC -> INPUTCTRL .bit .GAIN = ADC_INPUTCTRL_GAIN_1X_Val ; // Gain Factor Selection
89
+ ADC -> REFCTRL .bit .REFSEL = ADC_REFCTRL_REFSEL_INT1V_Val ; // 1.0V voltage reference
90
+ break ;
91
+
92
+ case AR_INTERNAL1V65 :
93
+ ADC -> INPUTCTRL .bit .GAIN = ADC_INPUTCTRL_GAIN_1X_Val ; // Gain Factor Selection
94
+ ADC -> REFCTRL .bit .REFSEL = ADC_REFCTRL_REFSEL_INTVCC1_Val ; // 1/2 VDDANA = 0.5* 3V3 = 1.65V
95
+ break ;
96
+
97
+ case AR_DEFAULT :
98
+ default :
99
+ ADC -> INPUTCTRL .bit .GAIN = ADC_INPUTCTRL_GAIN_DIV2_Val ;
100
+ ADC -> REFCTRL .bit .REFSEL = ADC_REFCTRL_REFSEL_INTVCC1_Val ; // 1/2 VDDANA = 0.5* 3V3 = 1.65V
101
+ break ;
71
102
}
72
103
}
73
104
74
105
uint32_t analogRead ( uint32_t ulPin )
75
106
{
76
107
uint32_t valueRead = 0 ;
77
- pinPeripheral (ulPin , g_APinDescription [ulPin ].ulPinType );
78
108
79
- ADC -> INPUTCTRL . bit . MUXPOS = g_APinDescription [ ulPin ]. ulADCChannelNumber ;
109
+ if ( ulPin < A0 ) ulPin += A0 ;
80
110
81
- // Start conversion
82
- ADC -> SWTRIG .bit .START = 1 ;
111
+ if (ulPin == A0 ) // Disable DAC, if analogWrite(A0,dval) used previously the DAC is enabled
112
+ {
113
+ while ( DAC -> STATUS .bit .SYNCBUSY == 1 );
114
+ DAC -> CTRLA .bit .ENABLE = 0x00 ; // Disable DAC
115
+ //DAC->CTRLB.bit.EOEN = 0x00; // The DAC output is turned off.
116
+ while ( DAC -> STATUS .bit .SYNCBUSY == 1 );
117
+ }
83
118
84
- while ( ADC -> INTFLAG .bit .RESRDY == 0 || ADC -> STATUS .bit .SYNCBUSY == 1 )
119
+ if (ulPin != TEMP )
120
+ {
121
+ pinPeripheral (ulPin , g_APinDescription [ulPin ].ulPinType );
122
+
123
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
124
+ ADC -> INPUTCTRL .bit .MUXPOS = g_APinDescription [ulPin ].ulADCChannelNumber ; // Selection for the positive ADC input
125
+ }
126
+ else
85
127
{
86
- // Waiting for a complete conversion and complete synchronization
128
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
129
+ ADC -> INPUTCTRL .bit .MUXPOS = ulPin & 0x7F ; // Selection for the positive ADC input
130
+ //ADC->INPUTCTRL.bit.MUXPOS = 0x18; // Selection for the positive ADC input
131
+
132
+ SYSCTRL -> VREF .bit .TSEN = 0x1 ; // Temperature sensor is enabled and routed to an ADC input channel.
87
133
}
88
134
89
- // Store the value
90
- valueRead = ADC -> RESULT .reg ;
135
+ // Control A
136
+ /*
137
+ * Bit 1 – ENABLE: Enable
138
+ * 0: The ADC is disabled.
139
+ * 1: The ADC is enabled.
140
+ * Due to synchronization, there is a delay from writing CTRLA.ENABLE until the peripheral is enabled/disabled. The
141
+ * value written to CTRL.ENABLE will read back immediately and the Synchronization Busy bit in the Status register
142
+ * (STATUS.SYNCBUSY) will be set. STATUS.SYNCBUSY will be cleared when the operation is complete.
143
+ *
144
+ * Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
145
+ * configured. The first conversion after the reference is changed must not be used.
146
+ */
147
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
148
+ ADC -> CTRLA .bit .ENABLE = 0x01 ; // Enable ADC
149
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
150
+
151
+ // Start conversion
152
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
153
+ ADC -> SWTRIG .bit .START = 1 ;
91
154
92
155
// Clear the Data Ready flag
93
156
ADC -> INTFLAG .bit .RESRDY = 1 ;
94
157
95
- // Flush the ADC for further conversions
96
- //ADC->SWTRIG.bit.FLUSH = 1;
158
+ // Start conversion again, since The first conversion after the reference is changed must not be used.
159
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
160
+ ADC -> SWTRIG .bit .START = 1 ;
97
161
98
- while ( ADC -> STATUS .bit .SYNCBUSY == 1 || ADC -> SWTRIG .bit .FLUSH == 1 )
99
- {
100
- // Waiting for synchronization
101
- }
162
+ // Store the value
163
+ while ( ADC -> INTFLAG .bit .RESRDY == 0 ); // Waiting for conversion to complete
164
+ valueRead = ADC -> RESULT .reg ;
165
+
166
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
167
+ ADC -> CTRLA .bit .ENABLE = 0x00 ; // Disable ADC
168
+ while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
102
169
103
170
return valueRead ;
104
171
}
@@ -124,7 +191,12 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
124
191
return ;
125
192
}
126
193
127
- DAC -> DATA .reg = ulValue & 0x3FF ; // Dac on 10 bits.
194
+ while ( DAC -> STATUS .bit .SYNCBUSY == 1 );
195
+ DAC -> DATA .reg = ulValue & 0x3FF ; // DAC on 10 bits.
196
+ while ( DAC -> STATUS .bit .SYNCBUSY == 1 );
197
+ DAC -> CTRLA .bit .ENABLE = 0x01 ; //Enable ADC
198
+ while ( DAC -> STATUS .bit .SYNCBUSY == 1 );
199
+ return ;
128
200
}
129
201
130
202
if ( (attr & PIN_ATTR_PWM ) == PIN_ATTR_PWM )
0 commit comments