前段时间忙着画板子搞运放搞滤波了,程序更新的少,发现MSP430不是太好用,尤其Timer,不过也与我使用内部晶振有关,产生正玄波之前用MSP430发出PWM,再进行滤波变为正弦波太麻烦了,这次改用DDS芯片,今天板子回来一会就调了出来很方便。
AD9850为一款DDS芯片,这里不做什么详细介绍了,有两种接口:serial 和 parrel,两种接口驱动均完成
/*
* AD9850.h
*
* Created on: 2013-7-12
* Author: Allen
*/
#ifndef AD9850_H_
#define AD9850_H_
#include <MSP430G2553.h>
#include "MyType.h"
//----------------------------------------------------------------------------------
//AD9850:W_CLK P2.5
#define AD9850_W_CLK_DIR (P2DIR)
#define AD9850_W_CLK_OUT (P2OUT)
#define AD9850_W_CLK_PIN_NUM (BIT5)
#define AD9850_W_CLK_IO_INIT (Set_Bit(P2DIR,AD9850_W_CLK_PIN_NUM))
#define AD9850_W_CLK_H (Set_Bit(P2OUT,AD9850_W_CLK_PIN_NUM))
#define AD9850_W_CLK_L (Clr_Bit(P2OUT,AD9850_W_CLK_PIN_NUM))
//----------------------------------------------------------------------------------
//AD9850:FQ_UP P2.4
#define AD9850_FQ_UP_DIR (P2DIR)
#define AD9850_FQ_UP_OUT (P2OUT)
#define AD9850_FQ_UP_PIN_NUM (BIT4)
#define AD9850_FQ_UP_IO_INIT (Set_Bit(P2DIR,AD9850_FQ_UP_PIN_NUM))
#define AD9850_FQ_UP_H (Set_Bit(P2OUT,AD9850_FQ_UP_PIN_NUM))
#define AD9850_FQ_UP_L (Clr_Bit(P2OUT,AD9850_FQ_UP_PIN_NUM))
//----------------------------------------------------------------------------------
//AD9850:RESET P2.3
#define AD9850_RESET_DIR (P2DIR)
#define AD9850_RESET_OUT (P2OUT)
#define AD9850_RESET_PIN_NUM (BIT3)
#define AD9850_RESET_IO_INIT (Set_Bit(P2DIR,AD9850_RESET_PIN_NUM))
#define AD9850_RESET_H (Set_Bit(P2OUT,AD9850_RESET_PIN_NUM))
#define AD9850_RESET_L (Clr_Bit(P2OUT,AD9850_RESET_PIN_NUM))
//----------------------------------------------------------------------------------
//AD9850:BIT_DATA P1.7
#define AD9850_BIT_DATA_DIR (P1DIR)
#define AD9850_BIT_DATA_OUT (P1OUT)
#define AD9850_BIT_DATA_PIN_NUM (BIT7)
#define AD9850_BIT_DATA_IO_INIT (Set_Bit(P1DIR,AD9850_BIT_DATA_PIN_NUM))
#define AD9850_BIT_DATA_H (Set_Bit(P1OUT,AD9850_BIT_DATA_PIN_NUM))
#define AD9850_BIT_DATA_L (Clr_Bit(P1OUT,AD9850_BIT_DATA_PIN_NUM))
extern void AD9850_INIT_SERIAL(void);
extern void AD9850_RESET_SERIAL(void);
extern void AD9850_WR_SERIAL(unsigned char w0,double frequence);
extern void AD9850_INIT_PARREL(void);
extern void AD9850_RESET_PARREL(void);
extern void AD9850_WR_PARREL(unsigned char w0,double frequence);
#endif /* LED_H_ */
AD9850.h
[color=rgb(51, 102, 153) !important]复制代码
以上是头文件完成了pin定义与位操作,并口的数据线操作并没定义,到时候直接用port操作很方便,AD9850的D0-D7与MSP430的P1.0-P1.7对应,一次写入8位很简单。
其中使用了#include "MyType.h"中一些操作:
// C
#define Set_Bit(val, bitn) (val |= (/*1 <<*/(bitn)))
#define Clr_Bit(val, bitn) (val &= ~(/*1<<*/(bitn)))
#define Get_Bit(val, bitn) (val & (1<<(bitn)) )
先写的串口的程序:
void AD9850_INIT_SERIAL(void)
{
AD9850_W_CLK_IO_INIT;
AD9850_FQ_UP_IO_INIT;
AD9850_RESET_IO_INIT;
AD9850_BIT_DATA_IO_INIT;
}
AD9850_INIT_SERIAL
[color=rgb(51, 102, 153) !important]复制代码
void AD9850_RESET_SERIAL(void)
{
AD9850_W_CLK_L;
AD9850_FQ_UP_L;
//rest信号
AD9850_RESET_L;
AD9850_RESET_H;
AD9850_RESET_L;
//w_clk信号
AD9850_W_CLK_L;
AD9850_W_CLK_H;
AD9850_W_CLK_L;
//fq_up信号
AD9850_FQ_UP_L;
AD9850_FQ_UP_H;
AD9850_FQ_UP_L;
}
AD9850_RESET_SERIAL
[color=rgb(51, 102, 153) !important]复制代码
void AD9850_WR_SERIAL(unsigned char w0,double frequence)
{
unsigned char i,w;
long int y;
double x;
//计算频率的HEX值
x=4294967295/20;//适合125M晶振
//如果时钟频率不为20MHZ,修改该处的频率值,单位MHz !!!
frequence=frequence/1000000;
frequence=frequence*x;
y=frequence;
//写w4数据
w=(y>>=0);
for(i=0;i<8;i++)
{
// ad9850_bit_data=(w>>i)&0x01;
if((w>>i)&0x01)
AD9850_BIT_DATA_H;
else
AD9850_BIT_DATA_L;
AD9850_W_CLK_H;
AD9850_W_CLK_L;
}
//写w3数据
w=(y>>8);
for(i=0;i<8;i++)
{
if((w>>i)&0x01)
AD9850_BIT_DATA_H;
else
AD9850_BIT_DATA_L;
AD9850_W_CLK_H;
AD9850_W_CLK_L;
}
//写w2数据
w=(y>>16);
for(i=0;i<8;i++)
{
if((w>>i)&0x01)
AD9850_BIT_DATA_H;
else
AD9850_BIT_DATA_L;
AD9850_W_CLK_H;
AD9850_W_CLK_L;
}
//写w1数据
w=(y>>24);
for(i=0;i<8;i++)
{
if((w>>i)&0x01)
AD9850_BIT_DATA_H;
else
AD9850_BIT_DATA_L;
AD9850_W_CLK_H;
AD9850_W_CLK_L;
}
//写w0数据
w=w0;
for(i=0;i<8;i++)
{
if((w>>i)&0x01)
AD9850_BIT_DATA_H;
else
AD9850_BIT_DATA_L;
AD9850_W_CLK_H;
AD9850_W_CLK_L;
}
//移入始能
AD9850_FQ_UP_H;
AD9850_FQ_UP_L;
}
AD9850_WR_SERIAL
[color=rgb(51, 102, 153) !important]复制代码
串行连接时候需要注意pin2-4的连接:
void AD9850_INIT_PARREL(void)
{
AD9850_W_CLK_IO_INIT;
AD9850_FQ_UP_IO_INIT;
AD9850_RESET_IO_INIT;
P1DIR=0xff;
}
AD9850_INIT_PARREL
[color=rgb(51, 102, 153) !important]复制代码
void AD9850_INIT_PARREL(void)
{
AD9850_W_CLK_IO_INIT;
AD9850_FQ_UP_IO_INIT;
AD9850_RESET_IO_INIT;
P1DIR=0xff;
}
AD9850_INIT_PARREL
[color=rgb(51, 102, 153) !important]复制代码
void AD9850_INIT_PARREL(void)
{
AD9850_W_CLK_IO_INIT;
AD9850_FQ_UP_IO_INIT;
AD9850_RESET_IO_INIT;
P1DIR=0xff;
}
AD9850_INIT_PARREL
[color=rgb(51, 102, 153) !important]复制代码
主函数:
char mode=0; //0:serial 1:parrel
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
mode=0;
if(mode==0)
{
AD9850_INIT_SERIAL();
AD9850_RESET_SERIAL();
AD9850_WR_SERIAL(0,18000);
}
else if(mode==1)
{
AD9850_INIT_PARREL();
AD9850_RESET_PARREL();
AD9850_WR_PARREL(0,25000);
}
while(1)
{
}
return 0;
}
main(void)
|