找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2747|回复: 0
打印 上一主题 下一主题
收起左侧

AD9851的STM32驱动程序

[复制链接]
跳转到指定楼层
楼主
在下载过一个AD9851的51单片机的驱动程序,结果串口无法使用。后阅读AD9851芯片资料发现串口需要模式转化,方法是W0输入xxxxx011然后FQ_UD给一个上升沿则进入串行通信模式,再给一个RESET信号则回到并行模式。奉上我自己写的STM32的驱动程序以供各位参考和使用。

单片机源程序如下:
  1. #include "AD9851.h"

  2. void AD9851_init(unsigned char w0, double frequence)
  3. {
  4.         GPIO_InitTypeDef  GPIO_InitStructure;
  5.         unsigned char w;
  6.         long int y;
  7.         double x;
  8.         //计算频率的HEX值
  9.         x=4294967295/125;//适合125M晶振
  10.         //如果时钟频率不为125MHZ,修改该处的频率值,单位MHz !!!
  11.         frequence=frequence/1000000;
  12.         frequence=frequence*x;
  13.         y=frequence;
  14.        
  15.         RCC_APB2PeriphClockCmd (AD9851_port_clk, ENABLE);
  16.        
  17.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18.         GPIO_InitStructure.GPIO_Pin = AD9851_W_CLK_Pin | AD9851_FQ_UD_Pin | AD9851_RESET_Pin;
  19.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  20.        
  21.         GPIO_Init(AD9851_port, &GPIO_InitStructure);
  22.        
  23.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  24.         GPIO_Init(AD9851_data_port, &GPIO_InitStructure);
  25.        
  26.         AD9851_W_CLK_0;
  27.         AD9851_FQ_UD_0;
  28.         AD9851_RESET_0;
  29.        
  30.         AD9851_byte((w0 << 3));
  31.         AD9851_byte((y >> 24) & 0xff);
  32.         AD9851_byte((y >> 16) & 0xff);
  33.         AD9851_byte((y >> 8) & 0xff);
  34.         AD9851_byte(y & 0xff);
  35. }

  36. uchar AD9851_byte(uchar data)
  37. {
  38.         GPIO_Write(AD9851_data_port, data);
  39.         AD9851_W_CLK_1;
  40.         AD9851_DELAY;
  41.         AD9851_W_CLK_0;
  42. }

  43. void AD9851_init_serial(unsigned char w0, double frequence)
  44. {
  45.         GPIO_InitTypeDef  GPIO_InitStructure;
  46.         long int y = 0;
  47.         double x;
  48.         //计算频率的HEX值
  49.         x=4294967295/125;//适合125M晶振
  50.         //如果时钟频率不为125MHZ,修改该处的频率值,单位MHz !!!
  51.         frequence=frequence/1000000;
  52.         frequence=frequence*x;
  53.         y=frequence;
  54.        
  55.         RCC_APB2PeriphClockCmd (AD9851_port_clk, ENABLE);
  56.        
  57.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  58.         GPIO_InitStructure.GPIO_Pin = AD9851_W_CLK_Pin | AD9851_FQ_UD_Pin | AD9851_RESET_Pin | AD9851_D7_s_Pin;
  59.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  60.        
  61.         GPIO_Init(AD9851_port, &GPIO_InitStructure);
  62.         AD9851_W_CLK_0;
  63.         AD9851_FQ_UD_0;
  64.         AD9851_RESET_0;
  65.         AD9851_D7_s_0;
  66.         //串行数据初始化,记得此时W0出入xxxxx011.
  67.         AD9851_W_CLK_1;
  68.         AD9851_DELAY;
  69.         AD9851_W_CLK_0;
  70.         AD9851_DELAY;
  71.        
  72.         AD9851_FQ_UD_1;
  73.         AD9851_DELAY;
  74.         AD9851_FQ_UD_0;
  75.         AD9851_DELAY;
  76.        
  77.         AD9851_serial_byte((w0 << 3));
  78.         AD9851_serial_byte((y >> 24) & 0xff);
  79.         AD9851_serial_byte((y >> 16) & 0xff);
  80.         AD9851_serial_byte((y >> 8) & 0xff);
  81.         AD9851_serial_byte(y & 0xff);
  82.        
  83.         AD9851_FQ_UD_1;
  84.         AD9851_DELAY;
  85.         AD9851_FQ_UD_0;
  86. }

  87. uchar AD9851_serial_byte(uchar data)
  88. {
  89.         int i, bit;
  90.         for(i = 8;i > 0;i--)
  91.         {
  92.                 if(data & (0x01 << (i - 1)))
  93.                 {
  94.                         AD9851_D7_s_1;
  95.                         AD9851_W_CLK_1;
  96.                         AD9851_DELAY;
  97.                         AD9851_W_CLK_0;
  98.                 }
  99.                 else
  100.                 {
  101.                         AD9851_D7_s_0;
  102.                         AD9851_W_CLK_1;
  103.                         AD9851_DELAY;
  104.                         AD9851_W_CLK_0;
  105.                 }
  106.         }
  107. }

  108. void ad9850_reset()
  109. {
  110.         AD9851_W_CLK_0;
  111.         AD9851_FQ_UD_0;
  112.         //rest信号
  113.         AD9851_RESET_0;
  114.         AD9851_DELAY;
  115.         AD9851_RESET_1;
  116.         AD9851_DELAY;
  117.         AD9851_RESET_0;
  118. }
复制代码

所有资料51hei提供下载:
AD9851.zip (4.08 KB, 下载次数: 50)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表