基于stm32cubeMX的AD9850驱动程序

首先致谢我龙,守护全世界最好的小小龙
在这里插入图片描述


cubeMX配置4个推挽输出引脚即可
添加 ad9850.c 和 ad9850.h

ad9850.c

#include "ad9850.h"
#include "stm32f4xx_hal.h" //改为自己32的hal库头文件
typedef unsigned int u16;
typedef unsigned char u8;
void AD9850_Init(void)
{
	AD9850_WCLK_L;
	AD9850_FQUD_L;

	AD9850_RESET_L;
	AD9850_RESET_H;
	AD9850_Delay(0xffff);		
	AD9850_RESET_L;

	AD9850_WCLK_L;
	AD9850_WCLK_H;
	AD9850_Delay(0xffff);		
	AD9850_WCLK_L;

	AD9850_FQUD_L;
	AD9850_FQUD_H;
	AD9850_Delay(0xffff);		
	AD9850_FQUD_L;
}

void AD9850_Write(u8 w0,double frequence)
{
	unsigned char i;
	long int y;
	double x;
	u8 wdata;
	x=4294967295U/125;//适合125M晶振
	//如果时钟频率不为180MHZ,修改该处的频率值,单位MHz  !!!
	frequence=frequence/1000000;
	frequence=frequence*x;
	y=frequence;
	//写w4数据
	wdata=(y>>=0);
	for(i=0;i<8;i++)
	{
		if(wdata&0x01)
			AD9850_DATA_H;
		else
			AD9850_DATA_L;
		AD9850_WCLK_H;
		wdata=wdata>>1;
		AD9850_WCLK_L;
	}
	wdata=(y>>8);
	for(i=0;i<8;i++)
	{
		if(wdata&0x01)
			AD9850_DATA_H;
		else
			AD9850_DATA_L;
		AD9850_WCLK_H;
		wdata=wdata>>1;
		AD9850_WCLK_L;
	}
	wdata=y>>16;
	for(i=0;i<8;i++)
	{
		if(wdata&0x01)
			AD9850_DATA_H;
		else
			AD9850_DATA_L;
		AD9850_WCLK_H;
		wdata=wdata>>1;
		AD9850_WCLK_L;
	}
	wdata=y>>24;
	for(i=0;i<8;i++)
	{
		if(wdata&0x01)
			AD9850_DATA_H;
		else
			AD9850_DATA_L;
		AD9850_WCLK_H;
		wdata=wdata>>1;
		AD9850_WCLK_L;
	}
	wdata=w0;
	for(i=0;i<8;i++)
	{
		if(wdata&0x01)
			AD9850_DATA_H;
		else
			AD9850_DATA_L;
		AD9850_WCLK_H;
		wdata=wdata>>1;
		AD9850_WCLK_L;
	}
	AD9850_FQUD_H;
//	AD9850_Delay(0xffff);		
	AD9850_FQUD_L;

}

void AD9850_Delay(unsigned int z) 
{  
    
	for(;z>0;z--) 
    
	{;} 
}


ad9850.h

#ifndef _AD9850_H
#define _AD9850_H
#include "stm32f4xx_hal.h"  //改成自己32的hal库头文件
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long int u32;

#define AD9850_CONTROL_PORT  GPIOD
#define AD9850_FQUD   GPIO_PIN_3  //改成自己连接的引脚
#define AD9850_WCLK   GPIO_PIN_4  //改成自己连接的引脚
#define AD9850_DATA   GPIO_PIN_2  //改成自己连接的引脚
#define AD9850_RST    GPIO_PIN_5  //改成自己连接的引脚

#define AD9850_WCLK_H	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_WCLK, GPIO_PIN_SET)
#define AD9850_WCLK_L	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_WCLK, GPIO_PIN_RESET)
#define AD9850_FQUD_H	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_FQUD, GPIO_PIN_SET)
#define AD9850_FQUD_L	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_FQUD, GPIO_PIN_RESET)
#define AD9850_DATA_H	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_DATA, GPIO_PIN_SET)
#define AD9850_DATA_L	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_DATA, GPIO_PIN_RESET)
#define AD9850_RESET_H	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_RST, GPIO_PIN_SET)
#define AD9850_RESET_L	HAL_GPIO_WritePin(AD9850_CONTROL_PORT, AD9850_RST, GPIO_PIN_RESET)


void AD9850_Delay(unsigned int z); 
void AD9850_Write(unsigned char w0,double frequence);
//void AD9850_Write(u8 w0,double frequence);
void AD9850_Init(void);

#endif


main()

  u32 fre=200000; //设定频率
  AD9850_Init();
  AD9850_Write(0x00,(double)fre);

连接方式基于stm32cubeMX的AD9850驱动程序_第1张图片


波形显示
基于stm32cubeMX的AD9850驱动程序_第2张图片

你可能感兴趣的:(stm32)