//注:本程序需添加头文件才能编译同过,仅供大家参考
//============================================
// Copyright (C), 2006, HUST.
// ----Filename: MB1504.c
// -Description: MB1504芯片的驱动程序
// -----History: 06/9/2 V1.0 Created By L.F.
// 06/9/4 V2.0 Modified By L.F.
//============================================
#include "SPCE061A.h"
//定义MB1504与SPCE061A的接口
#define M_LE 0x0001 //使能端
#define M_DATA 0x0002 //串行数据
#define M_CLK 0x0004 //时钟
//一般宏定义
#define Set_IOA_Bit(x) (*P_IOA_Data = *P_IOA_Buffer | x) //置高
#define Clear_IOA_Bit(x) (*P_IOA_Data = *P_IOA_Buffer & ~x) //置低
//============================================
// ----Function: void Init_MB1504(void)
// -Description: 初始化与MB1504相连接的IO口,并向MB1504写入参考频率分频比数据
// --Parameters: 无
// ------Return: 无
// -------Notes: 这里设定的参考频率为2KHz ,外部晶振选择12MHz
//============================================
void Init_MB1504(void)
{
//初始化所需IO口为低电平输出
*P_IOA_Dir |= (M_DATA + M_LE + M_CLK);
*P_IOA_Attrib |= (M_DATA + M_LE + M_CLK);
*P_IOA_Data &= ~(M_DATA + M_LE + M_CLK);
}
//============================================
// ----Function: void Write_MB1504(void)
// -Description: 向MB1504写入可编程分频比以及吞吐计数器数据
// --Parameters: uiFreq_Out 设置锁定频率
// ------Return: 无
// -------Notes: 无
//============================================
void Write_MB1504(float fFreq_Out)
{
unsigned int i;
unsigned int uiMask = 0x2000;
unsigned int uiTotal_Counter; //程序分频器总分频比
unsigned int uiSwal_Counter;
unsigned int uiPrgm_Counter;
unsigned int uiRef_Counter = 12000; //参考频率选择1KHz
Clear_IOA_Bit(M_LE);//M_LE置低
//送一位SM信号
Clear_IOA_Bit(M_CLK);
Clear_IOA_Bit(M_DATA);//SW=0 (选择64/65分频模式)
Set_IOA_Bit(M_CLK);
//送14位的参考分频计数器数据
for(i = 0;i < 14;i++)
{
Clear_IOA_Bit(M_CLK);
if(uiRef_Counter & uiMask)
{
Set_IOA_Bit(M_DATA); //uiRef_Counter第i位为1就往M_DATA连接的IO口送1
}
else
{
Clear_IOA_Bit(M_DATA);//否则送0
}
Set_IOA_Bit(M_CLK);
uiMask = uiMask >> 1;
}
//送一位的控制位
Clear_IOA_Bit(M_CLK);
Set_IOA_Bit(M_DATA); //C=1;
Set_IOA_Bit(M_CLK);
Set_IOA_Bit(M_LE); //上升沿输出使能
uiMask = 0x0400;
//根据所要频率计算相关数据
uiTotal_Counter = (unsigned int)((fFreq_Out )* 1000);
uiPrgm_Counter = uiTotal_Counter >> 6;
uiSwal_Counter = uiTotal_Counter - (uiPrgm_Counter << 6);
//送11位可编程分频比计数器数据和7位吞吐计数器数据
Clear_IOA_Bit(M_LE);//M_LE置低
for(i = 0;i < 11;i++)
{
Clear_IOA_Bit(M_CLK);
if(uiPrgm_Counter & uiMask)
{
Set_IOA_Bit(M_DATA); //uiPrgm_Counter第i位为1就往M_DATA连接的IO口送1
}
else
{
Clear_IOA_Bit(M_DATA);//否则送0
}
Set_IOA_Bit(M_CLK);
uiMask = uiMask >> 1;
}
uiMask = 0x0040;
for(i = 0;i < 7;i++)
{
Clear_IOA_Bit(M_CLK);
if(uiSwal_Counter & uiMask)
{
Set_IOA_Bit(M_DATA); //uiSwal_Counter第i位为1就往M_DATA连接的IO口送1
}
else
{
Clear_IOA_Bit(M_DATA);//否则送0
}
Set_IOA_Bit(M_CLK);
uiMask = uiMask >> 1;
}
//送1位的控制位
Clear_IOA_Bit(M_CLK);
Clear_IOA_Bit(M_DATA); //C=0;
Set_IOA_Bit(M_CLK);
Set_IOA_Bit(M_LE); //上升沿输出使能
}
//============================================
// End of MB1504.c
//============================================