Generate 32.768 kHz Clock Signal?

Hello, I am trying to interface the Particle Photon board and a MS5540C barometric sensor. I need to generate a clock signal for the sensor board at 32 kHz. Is this possible with the Photon board and how would I go about doing that? Using GPIO?
Tried tone() but that has a limit of 20 kHz.

Thanks!

You could use the Spark Interval Timer library. That library allows sub- millisecond timers. I was playing around with it the other night. I don’t know what kind of precision that library offers though. Maybe @peekay123 can comment on using that library for what you are trying to do.

1 Like

You should be able to set the PWM frequency to 32kHz and then use a 50% duty cycle.

I haven’t gone through the datasheet, but what exactly does this sensor need the 32kHz MCLK for?

4 Likes

@youmster, the specs call for an MCLK frequency of 30KHz to 35KHz with no specific requirement for a 32.768Hz frequency. As such, you could use SparkIntervalTimer but at 16us to produce a half period, interrupt latency may become an issue. Instead, as @ScruffR pointed out, you should be able to use a PWM output set for 50% duty cycle.

3 Likes

I went through the datasheet. The protocol reminded me of SPI. Why not use the SPI protocol to communicate with your device? It’s already integrated in the Photon.
I googled “MS5540C SPI” it and found an interresting article https://www.amsys.info/sheets/amsys.en.an510_e.pdf

1 Like

Thanks all for the help! This helped make a breakthrough. We ended up stting the PWM freq to 32kHz awith a 50% duty cycle. Looks like it’s working like a charm!

3 Likes

Unfortunately we were already using the SPI protocol for another sensor. Thanks for the help!

Hmm, what's the obstacle in using multiple sensors on a single SPI bus?

BTW, the Photon features two SPI interfaces (SPI & SPI1), just in case you need to run different modes and don't want to switch mode.

3 Likes

Oh, wouldn’t the SPI bus get clobbered from 2 devices trying to communicate over it?

Nope, SPI is meant to support multiple devices which can be explicitly selected via the dedicated CS (chip select) or SS (slave select) pins, which need to be pulled LOW for max. one and only one slave at any given time.

If a save does not see its own SS pin pulled LOW it has to stay silent and should also ignore any data transmitted over the bus.

2 Likes

@youmster, what @ScruffR says is right. However, anytime you have more than one of those sensors on the bus, you will need to add tri-state buffers on the MISO line since the MS5540C does not tri-state that line by itself. All SPI devices must tri-state their data lines when not selected but that is not always the case with many devices.

2 Likes

This community is top notch :smile: Thank you for your inputs @peekay123 and @ScruffR. I’ll have to reassess my approach.

2 Likes

@youmster, don’t be afraid to bounce your ideas on that approach back for feedback :wink:

3 Likes