Browse Source

analogWriteFrequency uses float for frequency

main
PaulStoffregen 9 years ago
parent
commit
28a3eb04f0
2 changed files with 8 additions and 8 deletions
  1. +1
    -1
      teensy3/core_pins.h
  2. +7
    -7
      teensy3/pins_teensy.c

+ 1
- 1
teensy3/core_pins.h View File

@@ -986,7 +986,7 @@ void init_pins(void);
void analogWrite(uint8_t pin, int val);
void analogWriteRes(uint32_t bits);
static inline void analogWriteResolution(uint32_t bits) { analogWriteRes(bits); }
void analogWriteFrequency(uint8_t pin, uint32_t frequency);
void analogWriteFrequency(uint8_t pin, float frequency);
void analogWriteDAC0(int val);
#ifdef __cplusplus
void attachInterruptVector(IRQ_NUMBER_t irq, void (*function)(void));

+ 7
- 7
teensy3/pins_teensy.c View File

@@ -674,18 +674,19 @@ void analogWriteRes(uint32_t bits)
}


void analogWriteFrequency(uint8_t pin, uint32_t frequency)
void analogWriteFrequency(uint8_t pin, float frequency)
{
uint32_t minfreq, prescale, mod;
uint32_t prescale, mod;
float minfreq;

//serial_print("analogWriteFrequency: pin = ");
//serial_phex(pin);
//serial_print(", freq = ");
//serial_phex32(frequency);
//serial_phex32((uint32_t)frequency);
//serial_print("\n");
for (prescale = 0; prescale < 7; prescale++) {
minfreq = (F_TIMER >> 16) >> prescale;
if (frequency > minfreq) break;
minfreq = (float)(F_TIMER >> prescale) / 65536.0f;
if (frequency >= minfreq) break;
}
//serial_print("F_TIMER = ");
//serial_phex32(F_TIMER >> prescale);
@@ -693,8 +694,7 @@ void analogWriteFrequency(uint8_t pin, uint32_t frequency)
//serial_print("prescale = ");
//serial_phex(prescale);
//serial_print("\n");
//mod = ((F_TIMER >> prescale) / frequency) - 1;
mod = (((F_TIMER >> prescale) + (frequency >> 1)) / frequency) - 1;
mod = (float)(F_TIMER >> prescale) / frequency - 0.5f;
if (mod > 65535) mod = 65535;
//serial_print("mod = ");
//serial_phex32(mod);

Loading…
Cancel
Save