Maxim DS1077 EconOscillator/Divider Library

Maxim DS1077 EconOscillator/Divider

This class wraps the functions of the DS1077 oscillator into a usable class for mBed. The oscillator uses I2C to communicate with the host MCU. This oscillator output a square wave, so if you're looking for a sine wave - this isn't it.

Be sure to download the DATASHEET (You'll need it to work with this chip). You can also purchase a breakout board from SparkFun. Be sure to ground the CTRL1 and CTRL0 pins if you don't plan on using them.

This library was created using the mbed NXP LPC11U24. Pins p27 and p28 were used for the I2C functions. Be sure to install 1K pull-up resistors on both lines.

Here's the output from the OUT1 pin on my scope after running the sample code below:

/media/uploads/sophtware/ds1077-32khz.png

Below is some sample code that sets the oscillator OUT2 pin to 16.67MHz and the OUT1 pin to 32kHz. It then outputs the content of all the control registers. You'll need the serial debug setup for your mBed. If you still need the serial driver for your mbed, you can get it here.

Sample Code

#include "mbed.h"
#include "DS1077.h"

I2C i2c(p28, p27);       // sda, scl

// Comment out all of the references to 'pc' on this page if you don't have the 
// serial debug driver for your mbed board installed on your computer. If you do,
// I personally like to use Putty as the terminal window to capture debug messages.
Serial pc(USBTX, USBRX); // tx, rx

// Again, remove the '&pc' parameter if you're not debugging.
DS1077 osc(&i2c, &pc);

DigitalOut myled(LED1);

int main() 
{
    pc.printf("** DS1077 EconOscillator **\r\n");

    osc.setSelect();
    wait_ms(1);
    // Set OUT2 to ~16.67MHz
    osc.setPrescalerP0Divisor(DS1077::X8);
    wait_ms(1);
    // Set OUT1 to ~32kHz
    osc.setPrescalerP1Divisor(DS1077::X4);
    wait_ms(1);
    osc.setDivisor(0xFFF);
    wait_ms(1);

    pc.printf("DIV: 0x%X\r\n", osc.divRegister()&0x0000FFFF);
    wait_ms(1);
    pc.printf("MUX: 0x%X\r\n", osc.muxRegister()&0x0000FFFF);
    wait_ms(1);
    pc.printf("BUS: 0x%X\r\n", osc.busRegister());

//    osc.write();

    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}
Committer:
sophtware
Date:
Sun Apr 06 16:46:15 2014 +0000
Revision:
0:4e016941c366
Child:
2:86284a0a46ca
Initial check in

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sophtware 0:4e016941c366 1 /*
sophtware 0:4e016941c366 2 Maxim DS1077 EconOscillator/Divider Library
sophtware 0:4e016941c366 3 By: Michael Lange
sophtware 0:4e016941c366 4 Date: April 5, 2014
sophtware 0:4e016941c366 5 License: This code is public domain.
sophtware 0:4e016941c366 6
sophtware 0:4e016941c366 7 This class wraps the functions of the DS1077 oscillator divider
sophtware 0:4e016941c366 8 into a usable class that exposes all functions to your project.
sophtware 0:4e016941c366 9
sophtware 0:4e016941c366 10 */
sophtware 0:4e016941c366 11
sophtware 0:4e016941c366 12
sophtware 0:4e016941c366 13 #ifndef DS1077_H
sophtware 0:4e016941c366 14 #define DS1077_H
sophtware 0:4e016941c366 15
sophtware 0:4e016941c366 16 #include "mbed.h"
sophtware 0:4e016941c366 17
sophtware 0:4e016941c366 18 #define DS1077_ADDRESS 0xB0 // Default (A0=0, A1=0, A3=0) unprogrammed address.
sophtware 0:4e016941c366 19
sophtware 0:4e016941c366 20 #define READ_ACK 1 // For mbed I2C Read method.
sophtware 0:4e016941c366 21 #define READ_NAK 0
sophtware 0:4e016941c366 22
sophtware 0:4e016941c366 23 #define CMD_DIV 0x01 // Command registers
sophtware 0:4e016941c366 24 #define CMD_MUX 0x02
sophtware 0:4e016941c366 25 #define CMD_BUS 0x0D
sophtware 0:4e016941c366 26 #define CMD_E2 0x3F
sophtware 0:4e016941c366 27
sophtware 0:4e016941c366 28 class DS1077
sophtware 0:4e016941c366 29 {
sophtware 0:4e016941c366 30 public:
sophtware 0:4e016941c366 31 //! Constructs an DS1077 object and associates an I2C and optional
sophtware 0:4e016941c366 32 //! Serial debug object.
sophtware 0:4e016941c366 33 //! @param *i2c The I2C object to use for the sensor.
sophtware 0:4e016941c366 34 //! @param *pc An optional serial debug connection object.
sophtware 0:4e016941c366 35 DS1077(I2C *i2c, Serial *pc = NULL);
sophtware 0:4e016941c366 36
sophtware 0:4e016941c366 37 //! Shutdown control configuration for PINs CTRL1 and CTRL0. If not
sophtware 0:4e016941c366 38 //! using these pins they should be grounded.
sophtware 0:4e016941c366 39 enum pdnType { NONE, CTRL1, CTRL0, EITHER };
sophtware 0:4e016941c366 40 //! Prescaler divisor by 1, 2, 4, or 8. This does an initial divide
sophtware 0:4e016941c366 41 //! by the chip's master clock, which is selected by the part number.
sophtware 0:4e016941c366 42 //! For the SparkFun board this is 133.333MHz
sophtware 0:4e016941c366 43 enum prescalerType { X1, X2, X4, X8 };
sophtware 0:4e016941c366 44
sophtware 0:4e016941c366 45 //! Sets the function for the shutdown pins PDN1 and PDN0 in the MUX word.
sophtware 0:4e016941c366 46 void setShutdownControl(DS1077::pdnType pdn);
sophtware 0:4e016941c366 47
sophtware 0:4e016941c366 48 //! Sets the EN0 bit in the MUX word.
sophtware 0:4e016941c366 49 void setEnable() { setTwoByteRegisterBit(CMD_MUX, 0x800); }
sophtware 0:4e016941c366 50 //! Clears the EN0 bit in the MUX word.
sophtware 0:4e016941c366 51 void clearEnable() { clearTwoByteRegisterBit(CMD_MUX, 0x800); }
sophtware 0:4e016941c366 52 //! Sets the SEL0 bit in the MUX word.
sophtware 0:4e016941c366 53 void setSelect() { setTwoByteRegisterBit(CMD_MUX, 0x1000); }
sophtware 0:4e016941c366 54 //! Clears the SEL0 bit in the MUX word.
sophtware 0:4e016941c366 55 void clearSelect() { clearTwoByteRegisterBit(CMD_MUX, 0x1000); }
sophtware 0:4e016941c366 56 //! Sets the DIV1 bit in the MUX word.
sophtware 0:4e016941c366 57 void setDivisor() { setTwoByteRegisterBit(CMD_MUX, 0x40); }
sophtware 0:4e016941c366 58 //! Clears the DIV1 bit in the MUX word.
sophtware 0:4e016941c366 59 void clearDivisor() { clearTwoByteRegisterBit(CMD_MUX, 0x40); }
sophtware 0:4e016941c366 60 //! Sets the WC bit in the BUS byte.
sophtware 0:4e016941c366 61 void setWriteContent() { setTwoByteRegisterBit(CMD_BUS, 0x8); }
sophtware 0:4e016941c366 62 //! Clears the WC bit in the BUS byte.
sophtware 0:4e016941c366 63 void clearWriteContent() { clearTwoByteRegisterBit(CMD_BUS, 0x8); }
sophtware 0:4e016941c366 64
sophtware 0:4e016941c366 65 //! Gets the address bits A2, A1, and A0 from the BUS byte.
sophtware 0:4e016941c366 66 char address();
sophtware 0:4e016941c366 67 //! Sets the address bits A2, A1, and A0 in the BUS byte.
sophtware 0:4e016941c366 68 void setAddress(char addr);
sophtware 0:4e016941c366 69
sophtware 0:4e016941c366 70 //! Gets the prescaler divisor set for P0 from 0M1 and 0M0 from the MUX word.
sophtware 0:4e016941c366 71 DS1077::prescalerType prescalerP0Divisor();
sophtware 0:4e016941c366 72 //! Sets the prescaler divisor for P0 into 0M1 and 0M0 in the MUX word.
sophtware 0:4e016941c366 73 void setPrescalerP0Divisor(DS1077::prescalerType prescaler);
sophtware 0:4e016941c366 74 //! Gets the prescaler divisor set for P1 from 1M1 and 1M0 from the MUX word.
sophtware 0:4e016941c366 75 DS1077::prescalerType prescalerP1Divisor();
sophtware 0:4e016941c366 76 //! Sets the prescaler divisor for P1 into 1M1 and 1M0 in the MUX word.
sophtware 0:4e016941c366 77 void setPrescalerP1Divisor(DS1077::prescalerType prescaler);
sophtware 0:4e016941c366 78
sophtware 0:4e016941c366 79 //! Gets the divisor set for OUT2 in the DIV word, which further divides the prescaled output.
sophtware 0:4e016941c366 80 short divisor() { return (i2cTwoByteRead(CMD_DIV) >> 6); }
sophtware 0:4e016941c366 81 //! Sets the divisor for OUT2 in the DIV word, which further divides the prescaled output.
sophtware 0:4e016941c366 82 void setDivisor(short divisor) { divisor <<= 6; i2cTwoByteWrite(CMD_DIV, (char)(divisor>>8), (char)divisor); }
sophtware 0:4e016941c366 83
sophtware 0:4e016941c366 84 //! Writes the contents of the registers to EEPROM when the WC bit is set.
sophtware 0:4e016941c366 85 void write();
sophtware 0:4e016941c366 86
sophtware 0:4e016941c366 87 //! Returns the contents of the DIV register as a short.
sophtware 0:4e016941c366 88 short divRegister() { return i2cTwoByteRead(CMD_DIV); }
sophtware 0:4e016941c366 89 //! Returns the contents of the MUX register as a short.
sophtware 0:4e016941c366 90 short muxRegister() { return i2cTwoByteRead(CMD_MUX); }
sophtware 0:4e016941c366 91 //! Returns the contents of the BUS register as a char.
sophtware 0:4e016941c366 92 char busRegister() { return i2cRead(CMD_BUS); }
sophtware 0:4e016941c366 93
sophtware 0:4e016941c366 94 private:
sophtware 0:4e016941c366 95 //! The I2C object we use to communicate with. It is not part of the
sophtware 0:4e016941c366 96 //! class so that it can be shared with other peripherals on the bus.
sophtware 0:4e016941c366 97 I2C *_i2c;
sophtware 0:4e016941c366 98
sophtware 0:4e016941c366 99 //! Set this in the constructor if you want the class to output debug messages.
sophtware 0:4e016941c366 100 //! If you need to pair down your code, you can remove this and all the
sophtware 0:4e016941c366 101 //! references to it in the code.
sophtware 0:4e016941c366 102 Serial *_debug;
sophtware 0:4e016941c366 103
sophtware 0:4e016941c366 104 //! Debug method that mimics the printf function, but will output nothing if _debug has not
sophtware 0:4e016941c366 105 //! been set. This means you can safely us it in your code and nothing will happen if you don't
sophtware 0:4e016941c366 106 //! assign the _debug object.
sophtware 0:4e016941c366 107 void debugOut(const char * format, ...);
sophtware 0:4e016941c366 108
sophtware 0:4e016941c366 109 void clearRegisterBit(const char regAddr, const char bitMask);
sophtware 0:4e016941c366 110 void setRegisterBit(const char regAddr, const char bitMask);
sophtware 0:4e016941c366 111 void clearTwoByteRegisterBit(const char regAddr, const short bitMask);
sophtware 0:4e016941c366 112 void setTwoByteRegisterBit(const char regAddr, const short bitMask);
sophtware 0:4e016941c366 113
sophtware 0:4e016941c366 114 //! Helper functions to read one value from the I2C bus using the oscillator's address.
sophtware 0:4e016941c366 115 char i2cRead(char regAddr);
sophtware 0:4e016941c366 116 //! Helper functions to write one value from the I2C bus using the oscillator's address.
sophtware 0:4e016941c366 117 void i2cWrite(char regAddr, char msb);
sophtware 0:4e016941c366 118 //! Helper functions to read one value from the I2C bus using the oscillator's address.
sophtware 0:4e016941c366 119 short i2cTwoByteRead(char regAddr);
sophtware 0:4e016941c366 120 //! Helper functions to write one value from the I2C bus using the oscillator's address.
sophtware 0:4e016941c366 121 void i2cTwoByteWrite(char regAddr, char msb, char lsb);
sophtware 0:4e016941c366 122 };
sophtware 0:4e016941c366 123
sophtware 0:4e016941c366 124 #endif // DS1077_H