找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3618|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

AD9851的STM32驅(qū)動程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:410231 發(fā)表于 2019-9-17 21:13 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
在下載過一個AD9851的51單片機(jī)的驅(qū)動程序,結(jié)果串口無法使用。后閱讀AD9851芯片資料發(fā)現(xiàn)串口需要模式轉(zhuǎn)化,方法是W0輸入xxxxx011然后FQ_UD給一個上升沿則進(jìn)入串行通信模式,再給一個RESET信號則回到并行模式。奉上我自己寫的STM32的驅(qū)動程序以供各位參考和使用。

單片機(jī)源程序如下:
  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.         //串行數(shù)據(jù)初始化,記得此時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. }
復(fù)制代碼

所有資料51hei提供下載:
AD9851.zip (4.08 KB, 下載次數(shù): 52)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表