#include "msp430x14x.h" #define ad9581_w_clk P2DIR|=BIT0 //写时钟信号于P2.0口 #define ad9581_w_clk_H P2OUT|=BIT0 //P2.0输出高电平 #define ad9581_w_clk_L P2OUT&=~BIT0 //P2.0输出低电平 #define ad9851_fq_up P2DIR|=BIT1 //写频率值于P2.1口 #define ad9851_fq_up_H P2OUT|=BIT1 //P2.1输出高电平 #define ad9851_fq_up_L P2OUT&=~BIT1 //P2.1输出低电平 #define ad9851_rest P2DIR|=BIT2 //写复位信号P2.1口 #define ad9851_rest_H P2OUT|=BIT2 //P2.2输出高电平 #define ad9851_rest_L P2OUT&=~BIT2 //P2.2输出低电平 #define ad9851_bit_data P4DIR|=BIT7 //写数据于P4.7口 #define ad9851_bit_data_H P4OUT|=BIT7 //P4.7输出高电平 #define ad9851_bit_data_L P4OUT&=~BIT7 //P4.7输出低电平 //***************************************************// // ad9851复位(并口模式) // //---------------------------------------------------// void ad9851_reset() { ad9581_w_clk; ad9581_w_clk_L; ad9851_fq_up; ad9851_fq_up_L; //rest信号 ad9851_rest; ad9851_rest_L; ad9851_bit_data_H; ad9851_bit_data_L; } //***************************************************// // ad9851复位(并口模式) // //---------------------------------------------------// void ad9851_reset_serial() { ad9581_w_clk; ad9581_w_clk_L; ad9851_fq_up; ad9851_fq_up_L; //rest信号 ad9851_rest; ad9851_rest_L; ad9851_bit_data_H; ad9851_bit_data_L; //w_clk信号 //ad9581_w_clk; ad9581_w_clk_L; ad9581_w_clk_H; ad9581_w_clk_L; //fq_up信号 //ad9851_fq_up; ad9851_fq_up_L; ad9851_fq_up_H; ad9851_fq_up_L; } //***************************************************// // 向ad9851中写命令与数据(并口) // //---------------------------------------------------// void ad9851_wr_parrel(unsigned char w0,double frequence) { unsigned char w; long int y; double x; //计算频率的HEX值 x=4294967295/180;//适合180M晶振/180为最终时钟频率(或30M六倍频) //如果时钟频率不为180MHZ,修改该处的频率值,单位MHz !!! frequence=frequence/1000000; frequence=frequence*x; y=frequence; P4DIR |= 0xFF; //写w0数据 w=w0; P4OUT |=w;//w0 ad9581_w_clk_H; ad9581_w_clk_L; //写w1数据 w=(y>>24); P4OUT |=w;//w1 ad9581_w_clk_H; ad9581_w_clk_L; //写w2数据 w=(y>>16); P4OUT |=w;//w2 ad9581_w_clk_H; ad9581_w_clk_L; //写w3数据 w=(y>>8); P4OUT |=w;//w3 ad9581_w_clk_H; ad9581_w_clk_L; //写w4数据 w=(y>>=0); P4OUT |=w;//w4 ad9581_w_clk_H; ad9581_w_clk_L; //移入始能 ad9581_w_clk_H; ad9581_w_clk_L; } //***************************************************// // 向ad9851中写命令与数据(串口) // //---------------------------------------------------// void ad9851_wr_serial(unsigned char w0,double frequence) { unsigned char i,w; long int y; double x; ad9851_bit_data; //计算频率的HEX值 x=4294967295/180;//适合180M晶振/180为最终时钟频率(或30M六倍频) //如果时钟频率不为180MHZ,修改该处的频率值,单位MHz !!! frequence=frequence/1000000; frequence=frequence*x; y=frequence; //写w4数据 w=(y>>=0); for(i=0;i<8;i++) { ad9851_bit_data_H|((w>>i)&0x01); ad9581_w_clk_H; ad9581_w_clk_L; } //写w3数据 w=(y>>8); for(i=0;i<8;i++) { ad9851_bit_data_H|((w>>i)&0x01); ad9581_w_clk_H; ad9581_w_clk_L; } //写w2数据 w=(y>>16); for(i=0;i<8;i++) { ad9851_bit_data_H|((w>>i)&0x01); ad9581_w_clk_H; ad9581_w_clk_L; } //写w1数据 w=(y>>24); for(i=0;i<8;i++) { ad9851_bit_data_H|((w>>i)&0x01); ad9581_w_clk_H; ad9581_w_clk_L; } //写w0数据 w=w0; for(i=0;i<8;i++) { ad9851_bit_data_H|((w>>i)&0x01); ad9581_w_clk_H; ad9581_w_clk_L; } //移入始能 ad9851_fq_up_H; ad9851_fq_up_L; } int main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; //---------------------------------------------------// //并行写1000Hz程序 ad9851_reset(); ad9851_wr_serial(0x01,1000); //0X01为开六备频 //ad9851_wr_parrel(0x00,1000); //0X00为不开六备频 //---------------------------------------------------// //---------------------------------------------------// //串行写1000Hz程序 //ad9851_reset_serial(); //ad9851_wr_serial(0x01,1000); //0X01为开六备频 //ad9851_wr_serial(0x00,1000); //0X00为不开六备频 //---------------------------------------------------// while(1) { } return 0; }
MSP430驱动DDS模块AD9851
最新推荐文章于 2021-07-30 14:58:45 发布