Fixed truncation error in AT1846S setFrequency()

Fixed error in compuation of values for AT1846S frequency registers
causing the output frequency to have an effective resolution of 1kHz
instead of 62.5Hz.
pull/200/head
Silvano Seva 2023-10-22 01:30:36 +02:00
rodzic b4c8fca767
commit e362a80d45
1 zmienionych plików z 6 dodań i 2 usunięć

Wyświetl plik

@ -101,8 +101,12 @@ public:
*/
void setFrequency(const freq_t freq)
{
// The value to be written in registers is given by: 0.0016*freqency
uint32_t val = (freq/1000)*16;
// AT1846S datasheet specifies a frequency step of 1/16th of kHz per bit.
// Computation of register value is done using 64 bit to avoid overflows,
// result is then truncated to 32 bits to fit it into the registers.
uint64_t val = ((uint64_t) freq * 16) / 1000;
val &= 0xFFFFFFFF;
uint16_t fHi = (val >> 16) & 0xFFFF;
uint16_t fLo = val & 0xFFFF;