天天看点

ADF4002,HMC704 配置

#include "C8051F330.h"
#define OPEN_MODE


sbit SCK_Pad  = P0^3;
sbit SDI_Pad  = P0^4;
sbit LE2_Pad  = P0^5;
sbit SDO2_Pad = P0^6;
sbit LE1_Pad  = P0^7;
sbit SDO3_Pad = P1^1;
sbit LE3_Pad  = P1^2;
sbit CEN_Pad  = P1^3;


void Oscillator_Init()
{
    OSCICN = 0x82;     // 使能内部H-F振荡器,SYSCLK为12.25MHz
            RSTSRC = 0x04;       // 使能时钟丢失检测器,检测到时钟丢失时触发复位
            CLKSEL = 0x00;       // 系统时钟取自内部高频振荡器,分频数由OSCICN寄存器中的IFCN位决定。
}


void PCA_Init()
{
    PCA0MD    &= ~0x40;  // 禁止看门狗定时器
            PCA0MD    = 0x00; // PCA计数器的时钟源为系统时钟的12分频
    PCA0H     = 0x00;
    PCA0L     = 0x00;
    PCA0CPL2  = 0xFF; // PCA捕捉模块2低字节置0xFF,超时间隔64.2ms 
    PCA0MD    |= 0x40; // PCA模块2用作看门狗定时器
    PCA0CN    = 0x40;    // 启动PCA计数器/定时器
}


void Port_IO_Init()
{
    P0MDIN  = 0xFF;
    P0MDOUT = 0xB8;
    P0SKIP  = 0x00;
    P1MDIN  = 0xFF;
    P1MDOUT = 0x0C;
    P1SKIP  = 0x00;
    XBR0    = 0x00;
    XBR1    = 0x40;
}


void delay(unsigned int time)
{
    
    
    CKCON |= 0x02;
    TMOD  |= 0x01;
    TH0    = 0xFE;
    TL0    = 0xFF;
    ET0    = 0x0;
    TR0    = 0x1;
    while(time)
    {
        TF0 = 0x0;
        while(!TF0)
        {
            PCA0CPH2 = 0x00;
        }
        TH0 = 0xFE;
        TL0 = 0xFE;
        time--;
    }
    TR0    = 0x0;
}


void ADF4002_Write(unsigned long value)
{
    unsigned char i;
    unsigned long send_data;
    unsigned char valueL,valueM,valueH;
    
    
    send_data = value;
    valueL = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    valueM = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    valueH = (unsigned char)(send_data & 0x000000FF);
    
    
    LE1_Pad = 0x0;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(valueH & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        valueH <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(valueM & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        valueM <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(valueL & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        valueL <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    LE1_Pad = 0x1;
}


void HMC704_ReadA(unsigned char addr,unsigned long *value)
{
    unsigned char i,j;
    unsigned char send_data;
    unsigned char receive_data;
#ifdef HMC_MODE
    SDI_Pad = 0x1;
    LE2_Pad = 0x1; 
    
    
    send_data = addr;
    send_data <<= 1;
    send_data |= 0x80;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    *value = 0x00;
    for(j=0; j<3; j++)
    {
        send_data = 0x00;
        receive_data = 0x00;
        for(i=0; i<8; i++)
        {
            SCK_Pad = 0x0;
            receive_data <<= 1;
            if(SDO2_Pad)
            {
                receive_data |= 0x01;
            }
            if(send_data & 0x80)
            {
                SDI_Pad = 0x1;
            }
            else
            {
                SDI_Pad = 0x0;
            }
            send_data <<= 1;
            delay(1);
            SCK_Pad = 0x1;
            delay(1);
        }
        *value <<= 8;
        *value |= receive_data;
    }
    LE2_Pad = 0x0;
#endif
    
    
#ifdef OPEN_MODE
    LE2_Pad = 0x1;
    //First cycle
    for(j=0; j<2; j++)
    { 
        send_data = 0x00;
        for(i=0; i<8; i++)
        {
            SCK_Pad = 0x0;
            if(send_data & 0x80)
            {
                SDI_Pad = 0x1;
            }
            else
            {
                SDI_Pad = 0x0;
            }
            send_data <<= 1;
            delay(1);
            SCK_Pad = 0x1;
            delay(1);
        }
    }
    send_data = addr & 0x1F;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    send_data = addr;
    send_data <<= 3;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
        if(i>4)
        {
            LE2_Pad = 0x0; 
        }
    }
    //Second cycle 
    *value = 0x00;
    LE2_Pad = 0x1;
    for(j=0; j<2; j++)
    { 
        send_data = 0x00;
        receive_data = 0x00;
        for(i=0; i<8; i++)
        {
            SCK_Pad = 0x0;
            receive_data <<= 1;
            if(SDO2_Pad)
            {
                receive_data |= 0x01;
            }
            if(send_data & 0x80)
            {
                SDI_Pad = 0x1;
            }
            else
            {
                SDI_Pad = 0x0;
            }
            send_data <<= 1;
            delay(1);
            SCK_Pad = 0x1;
            delay(1);
        }
        *value <<= 8;
        *value |= receive_data;
    }
    *value &= 0x7FFF;
    send_data = addr & 0x1F;
    receive_data = 0x00;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        receive_data <<= 1;
        if(SDO2_Pad)
        {
            receive_data |= 0x01;
        }
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    *value <<= 8;
    *value |= receive_data;
    send_data = addr;
    send_data <<= 3;
    receive_data = 0x00;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        receive_data <<= 1;
        if(SDO2_Pad)
        {
            receive_data |= 0x01;
        }
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
        if(i>1)
        {
            LE2_Pad = 0x0; 
        }
    }
    *value <<= 1;
    *value |= (receive_data >> 7);
#endif
}


void HMC704_ReadB(unsigned char addr,unsigned long *value)
{
    unsigned char i,j;
    unsigned char send_data;
    unsigned char receive_data;
#ifdef HMC_MODE
    SDI_Pad = 0x1;
    LE3_Pad = 0x1; 
    
    
    send_data = addr;
    send_data <<= 1;
    send_data |= 0x80;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    *value = 0x00;
    for(j=0; j<3; j++)
    {
        send_data = 0x00;
        receive_data = 0x00;
        for(i=0; i<8; i++)
        {
            SCK_Pad = 0x0;
            receive_data <<= 1;
            if(SDO3_Pad)
            {
                receive_data |= 0x01;
            }
            if(send_data & 0x80)
            {
                SDI_Pad = 0x1;
            }
            else
            {
                SDI_Pad = 0x0;
            }
            send_data <<= 1;
            delay(1);
            SCK_Pad = 0x1;
            delay(1);
        }
        *value <<= 8;
        *value |= receive_data;
    }
    LE3_Pad = 0x0;
#endif
    
    
#ifdef OPEN_MODE
    LE3_Pad = 0x1;
    //First cycle
    for(j=0; j<2; j++)
    { 
        send_data = 0x00;
        for(i=0; i<8; i++)
        {
            SCK_Pad = 0x0;
            if(send_data & 0x80)
            {
                SDI_Pad = 0x1;
            }
            else
            {
                SDI_Pad = 0x0;
            }
            send_data <<= 1;
            delay(1);
            SCK_Pad = 0x1;
            delay(1);
        }
    }
    send_data = addr & 0x1F;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    send_data = addr;
    send_data <<= 3;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
        if(i>4)
        {
            LE3_Pad = 0x0; 
        }
    }
    //Second cycle 
    *value = 0x00;
    LE3_Pad = 0x1;
    for(j=0; j<2; j++)
    { 
        send_data = 0x00;
        receive_data = 0x00;
        for(i=0; i<8; i++)
        {
            SCK_Pad = 0x0;
            receive_data <<= 1;
            if(SDO3_Pad)
            {
                receive_data |= 0x01;
            }
            if(send_data & 0x80)
            {
                SDI_Pad = 0x1;
            }
            else
            {
                SDI_Pad = 0x0;
            }
            send_data <<= 1;
            delay(1);
            SCK_Pad = 0x1;
            delay(1);
        }
        *value <<= 8;
        *value |= receive_data;
    }
    *value &= 0x7FFF;
    send_data = addr & 0x1F;
    receive_data = 0x00;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        receive_data <<= 1;
        if(SDO3_Pad)
        {
            receive_data |= 0x01;
        }
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    *value <<= 8;
    *value |= receive_data;
    send_data = addr;
    send_data <<= 3;
    receive_data = 0x00;
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        receive_data <<= 1;
        if(SDO3_Pad)
        {
            receive_data |= 0x01;
        }
        if(send_data & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        send_data <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
        if(i>1)
        {
            LE3_Pad = 0x0; 
        }
    }
    *value <<= 1;
    *value |= (receive_data >> 7);
#endif
}


void HMC704_WriteA(unsigned char addr,unsigned long value)
{
    unsigned char i;
    unsigned long send_data;
    unsigned char value1,value2,value3,value4;
#ifdef HMC_MODE
    send_data   = addr;
    send_data <<=24;
    send_data  |= (value & 0x00FFFFFF);
    send_data <<= 1; 
            value4 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value3 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value2 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value1 = (unsigned char)(send_data & 0x000000FF);
    
    SDI_Pad = 0x0;
    LE2_Pad = 0x1; 
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value1 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value1 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value2 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value2 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value3 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value3 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value4 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value4 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    LE2_Pad = 0x0;
#endif
    
    
#ifdef OPEN_MODE
    send_data   = (value & 0x00FFFFFF);
    send_data <<= 8;
    send_data  |= (addr << 3);
    value4 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value3 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value2 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value1 = (unsigned char)(send_data & 0x000000FF);
    LE2_Pad = 0x1; 
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value1 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value1 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value2 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value2 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value3 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value3 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value4 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value4 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
        if(i>4)
        {
            LE2_Pad = 0x0;
        }
    }
    LE2_Pad = 0x1;
    delay(1);
    LE2_Pad = 0x0;
#endif 
}


void HMC704_WriteB(unsigned char addr,unsigned long value)
{
    unsigned char i;
    unsigned long send_data;
    unsigned char value1,value2,value3,value4;
#ifdef HMC_MODE
    send_data   = addr;
    send_data <<=24;
    send_data  |= (value & 0x00FFFFFF);
    send_data <<= 1; 
            value4 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value3 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value2 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value1 = (unsigned char)(send_data & 0x000000FF);
    
    SDI_Pad = 0x0;
    LE3_Pad = 0x1; 
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value1 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value1 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value2 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value2 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value3 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value3 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value4 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value4 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    LE3_Pad = 0x0;
#endif
    
#ifdef OPEN_MODE
    send_data   = (value & 0x00FFFFFF);
    send_data <<= 8;
    send_data  |= (addr << 3);
    value4 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value3 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value2 = (unsigned char)(send_data & 0x000000FF);
    send_data >>= 8;
    value1 = (unsigned char)(send_data & 0x000000FF);
    LE3_Pad = 0x1; 
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value1 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value1 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value2 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value2 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value3 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value3 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
    }
    for(i=0; i<8; i++)
    {
        SCK_Pad = 0x0;
        if(value4 & 0x80)
        {
            SDI_Pad = 0x1;
        }
        else
        {
            SDI_Pad = 0x0;
        }
        value4 <<= 1;
        delay(1);
        SCK_Pad = 0x1;
        delay(1);
        if(i>4)
        {
            LE3_Pad = 0x0;
        }
    }
    LE3_Pad = 0x1;
    delay(1);
    LE3_Pad = 0x0;
#endif 
}


unsigned long chip1_id;
unsigned long chip2_id;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main(void)
{
    PCA0MD &= ~0x40;
    Oscillator_Init();
    Port_IO_Init();
    PCA_Init();
    EA = 0x1;
    delay(1000);
    LE1_Pad = 0x1;
    LE2_Pad = 0x0;
    LE3_Pad = 0x0;
    SDI_Pad = 0x1;
    SCK_Pad = 0x1;
    CEN_Pad = 0x1;
    
    
    //400MHz
    ADF4002_Write(0x1FFE93);
    ADF4002_Write(0x1FFE92);
    ADF4002_Write(0x000004);
    ADF4002_Write(0x000A01);
    //1280MHz
    /*  HMC704_ReadA(0x00,&chip1_id);
    HMC704_WriteA(0x01,0x000002);
    HMC704_WriteA(0x02,0x000001);
    HMC704_WriteA(0x09,0x4C0000 | ((60<<7) | 60));
    HMC704_WriteA(0x06,0x0003FA);
    HMC704_WriteA(0x03,0x000020);
    HMC704_WriteA(0x04,0x000000);  //*/
    //800MHz
    HMC704_ReadA(0x00,&chip1_id);
    HMC704_WriteA(0x01,0x000002);
    HMC704_WriteA(0x02,0x000001);
    HMC704_WriteA(0x09,0x4C0000 | ((60<<7) | 60));
    HMC704_WriteA(0x06,0x0003FA);
    HMC704_WriteA(0x03,20); //40*20=800  20150309
    HMC704_WriteA(0x04,0x000000);  //*/
            //1360MHz
            HMC704_ReadB(0x00,&chip2_id);
    HMC704_WriteB(0x01,0x000002);
    HMC704_WriteB(0x02,0x000001);
    HMC704_WriteB(0x09,0x4C0000 | ((60<<7) | 60));
    HMC704_WriteA(0x06,0x0003FA);
    HMC704_WriteB(0x03,0x000022);
    HMC704_WriteB(0x04,0x000000);
    while(1)
    {
        PCA0CPH2 = 0x00;
    }
}
           
上一篇: STM32 IAP升级
下一篇: 三态缓冲器

继续阅读