Skip to content

Commit 0c72911

Browse files
committed
Serial output is affected while sampling temperature
When running the Temperature example, the output for the word “Temperature:” was being corrupted when sampling the temperature. Moved away from using sleep_mode and altering the restoration of the registers. Both changes were needed to fix the serial output issue.
1 parent 308a9fd commit 0c72911

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

utility/halTemperature.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,31 @@
1515

1616
/*****************************************************************************
1717
*****************************************************************************/
18-
static volatile uint8_t halAdcFinished;
1918
static int8_t halAdcOffset;
2019

20+
static inline void sleep (void)
21+
{
22+
asm volatile("sleep");
23+
}
24+
2125
/*****************************************************************************
2226
*****************************************************************************/
2327
static inline int16_t HAL_AdcMeasure(void) {
24-
set_sleep_mode(SLEEP_MODE_ADC);
2528
/* dummy cycle */
26-
halAdcFinished = false;
2729
do
2830
{
29-
sleep_mode();
30-
/* sleep, wake up by ADC IRQ */
31-
/* check here for ADC IRQ because sleep mode could wake up from * another source too */
31+
sleep();
3232
}
33-
while (false == halAdcFinished);
34-
//while (ADCSRA & (1 << ADSC));
35-
/* set by ISR */
33+
while (ADCSRA & (1 << ADSC));
34+
35+
/* set by ISR */
3636
return ADC;
3737
}
3838

3939
/*****************************************************************************
4040
*****************************************************************************/
4141
int8_t HAL_MeasureTemperature(void) {
4242
int32_t val;
43-
uint8_t adcsrc = ADCSRC;
44-
uint8_t adcsrb = ADCSRB;
45-
uint8_t adcsra = ADCSRA;
46-
uint8_t admux = ADMUX;
4743

4844
ADCSRC = 10 << ADSUT0;
4945
ADCSRB = (1 << MUX5);
@@ -55,20 +51,11 @@ int8_t HAL_MeasureTemperature(void) {
5551
ADCSRA |= (1 << ADIF); /* clear flag */
5652
ADCSRA |= (1 << ADIE);
5753

58-
/* dummy cycle after REF change (suggested by datasheet) */
54+
// dummy cycle after REF change (suggested by datasheet)
5955
HAL_AdcMeasure();
6056

6157
val = HAL_AdcMeasure();
6258

63-
ADCSRA = adcsra;
64-
ADCSRB = adcsrb;
65-
ADCSRC = adcsrc;
66-
ADMUX = admux;
59+
ADCSRA = 0;
6760
return (int)((1.13 * val - 272.8)) + HAL_TEMPERATURE_CALIBRATION_OFFSET - 3;
6861
}
69-
70-
/*****************************************************************************
71-
*****************************************************************************/
72-
ISR(ADC_vect, ISR_BLOCK) {
73-
halAdcFinished = true;
74-
}

0 commit comments

Comments
 (0)