Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# cy22150.c

    00001
    00002 // system includes
    00003 #include "global.h"
    00004
    00005 // local includes
    00006 #include "i2c.h"
    00007 #include "cy22150.h"
    00008
    00009 // functions
    00010 void cy22150Init(void)
    00011 {
    00012     // set dividers to default
    00013     cy22150WriteReg(CY22150_REG_DIV1, 8);
    00014     cy22150WriteReg(CY22150_REG_DIV2, 8);
    00015
    00016     // set crystal capacitor loading to 20pf
    00017     // Capload = (Cload-(Cboard+Cchip))/0.09375pF
    00018     // (20pF-(2pF+6pF))/0.09375 = 128
    00019     cy22150WriteReg(CY22150_REG_CAPLOAD, (u08)((20-(2+6))/0.09375) );
    00020 }
    00021
    00022
    00023 void cy22150SetOutput(u08 clkpin, u08 enable)
    00024 {
    00025     // enable/disable requested output pin
    00026     if(enable)
    00027         cy22150WriteReg(CY22150_REG_CLKOE, cy22150ReadReg(CY22150_REG_CLKOE) | (1<<clkpin));
    00028     else
    00029         cy22150WriteReg(CY22150_REG_CLKOE, cy22150ReadReg(CY22150_REG_CLKOE) & ~(1<<clkpin));
    00030 }
    00031
    00032 void cy22150SetDiv(u08 divreg, u08 div)
    00033 {
    00034     switch(divreg)
    00035     {
    00036     case 0: cy22150WriteReg(CY22150_REG_DIV1, div); break;
    00037     case 1: cy22150WriteReg(CY22150_REG_DIV2, div); break;
    00038     }
    00039 }
    00040
    00041 void cy22150SetQ(int q)
    00042 {
    00043     if(q<2)
    00044         return; // error: q must be at least 2
    00045     // write q value with PO bit masked
    00046     cy22150WriteReg(CY22150_REG_QCNT, ((q-2)&0x7F) );
    00047 }
    00048
    00049 void cy22150SetP(int p)
    00050 {
    00051     u08 pump;
    00052     int pb;
    00053
    00054     // calculate pump value from p multiplier
    00055     if(p<45)        pump = 0;
    00056     else if(p<480)  pump = 1;
    00057     else if(p<640)  pump = 2;
    00058     else if(p<800)  pump = 3;
    00059     else            pump = 4;
    00060
    00061     // calculate pb from p value
    00062     pb = (p/2)-4;
    00063
    00064     // write p value
    00065     cy22150WriteReg(CY22150_REG_PBHI, 0xC0 | (pump<<2) | ((pb>>8)&0x03) );
    00066     cy22150WriteReg(CY22150_REG_PBLO, (pb&0xFF) );
    00067 }
    00068
    00069 void cy22150WriteReg(u08 addr, u08 data)
    00070 {
    00071     u08 packet[2];
    00072     // prepare write packet
    00073     packet[0] = addr;
    00074     packet[1] = data;
    00075     // write to device
    00076     i2cMasterSend(CY22150_I2C_ADDR, 2, packet);
    00077 }
    00078
    00079 u08 cy22150ReadReg(u08 addr)
    00080 {
    00081     u08 data;
    00082     // select register to read
    00083     i2cMasterSend(CY22150_I2C_ADDR, 1, &addr);
    00084     // read value
    00085     i2cMasterReceive(CY22150_I2C_ADDR, 1, &data);
    00086     // return value
    00087     return data;
    00088 }