AD9833波形信號發生器(STM32F407ZGT6驅動,hal庫)源碼


  1 /*************************************************************************************
  2  Title    :   Analog Devices AD9833 DDS Wave Generator Library for STM32 Using HAL Libraries
  3  Author:    Bardia Alikhan Afshar <bardia.a.afshar@gmail.com>  
  4  Software:  IAR Embedded Workbench for ARM
  5  Hardware:  Any STM32 device
  6 *************************************************************************************/
  7 #include "AD9833.h"
  8 
  9 /*
 10   VCC   ---     3.3V
 11   SDATA ---     PC12 
 12   SCLK  ---     PC10
 13   FSYNC ---     PC11   
 14 
 15 */
 16 // ------------------- Variables ----------------
 17 uint16_t FRQLW = 0;    // MSB of Frequency Tuning Word
 18 uint16_t FRQHW = 0;    // LSB of Frequency Tuning Word
 19 uint32_t  phaseVal=0;  // Phase Tuning Value
 20 uint8_t WKNOWN=0;      // Flag Variable
 21 // -------------------------------- Functions --------------------------------
 22 
 23 // ----------------- Software SPI Function
 24 void writeSPI(uint16_t word) {
 25     for (uint8_t i = 0; i < 16 ; i++) {
 26           if(word & 0x8000) HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_SET);   //bit=1, Set High
 27         else HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_RESET);        //bit=0, Set Low
 28         ASM_NOP();
 29         HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_RESET);             //Data is valid on falling edge
 30         ASM_NOP();
 31         HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_SET);
 32         word = word<<1; //Shift left by 1 bit
 33         }
 34     HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_RESET);                    //Idle low
 35     ASM_NOP();
 36 }
 37 
 38 // ------------------------------------------------ Sets Output Wave Type
 39 void AD9833_SetWave(uint16_t Wave){
 40   switch(Wave){
 41   case 0:
 42   HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
 43     writeSPI(0x2000); // Value for Sinusoidal Wave
 44     HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
 45     WKNOWN=0;
 46     break;
 47   case 1:
 48     HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
 49     writeSPI(0x2028); // Value for Square Wave
 50     HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
 51     WKNOWN=1;
 52     break;
 53   case 2:
 54         HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
 55     writeSPI(0x2002); // Value for Triangle Wave
 56     HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
 57     WKNOWN=2;
 58     break;
 59   default:
 60     break;
 61   }
 62 }
 63 
 64 // ------------------------------------------------ Sets Wave Frequency & Phase (In Degree) In PHASE0 & FREQ0 Registers
 65 void AD9833_SetWaveData(float Frequency,float Phase){
 66 ASM_NOP();
 67  // ---------- Tuning Word for Phase ( 0 - 360 Degree )
 68  if(Phase<0)Phase=0; // Changing Phase Value to Positive
 69  if(Phase>360)Phase=360; // Maximum value For Phase (In Degree)
 70  phaseVal  = ((int)(Phase*(4096/360)))|0XC000;  // 4096/360 = 11.37 change per Degree for Register And using 0xC000 which is Phase 0 Register Address
 71  
 72  // ---------- Tuning word for Frequency      
 73 long freq=0;
 74 freq=(int)(((Frequency*pow(2,28))/FMCLK)+1); // Tuning Word
 75 FRQHW=(int)((freq & 0xFFFC000) >> 14); // FREQ MSB
 76 FRQLW=(int)(freq & 0x3FFF);  // FREQ LSB 
 77 FRQLW |= 0x4000;
 78 FRQHW |= 0x4000; 
 79  // ------------------------------------------------ Writing DATA
 80   HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_SET);
 81   HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_SET);
 82   HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);  
 83   HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET); //low = selected
 84     ASM_NOP();
 85     writeSPI(0x2100); // enable 16bit words and set reset bit
 86     writeSPI(FRQLW);
 87     writeSPI(FRQHW);
 88         writeSPI(phaseVal);
 89     writeSPI(0x2000); // clear reset bit 
 90     ASM_NOP();
 91     HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET); //high = deselected 
 92 AD9833_SetWave(WKNOWN);
 93 ASM_NOP();
 94 return;
 95 }
 96 
 97 // ------------------------------------------------ Initializing AD9833
 98 void AD9833_Init(uint16_t WaveType,float FRQ,float Phase){
 99 HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_SET); // Set All SPI pings to High
100 HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_SET);  // Set All SPI pings to High
101 HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);   // Set All SPI pings to High
102 AD9833_SetWave(WaveType);                              // Type Of Wave 
103 AD9833_SetWaveData(FRQ,Phase);                         // Frequency & Phase Set
104 return;
105 }
 1 #ifndef _AD_9833_H
 2 #define _AD_9833_H
 3 #include <math.h>
 4 #include "stm32f4xx_hal.h"
 5 
 6 // ------------------------- Defines -------------------------
 7 #define FMCLK 25000000        // Master Clock On AD9833
 8 #define AD9833PORT GPIOG      // PORT OF AD9833
 9 #define AD9833DATA GPIO_PIN_6 // SPI DATA PIN
10 #define AD9833SCK GPIO_PIN_7  // SPI Clock PIN
11 #define AD9833SS GPIO_PIN_8   // SPI Chip Select
12 #define ASM_NOP() 
13 // Assembly NOPE (Little Delay)
14 enum WaveType{SIN, SQR, TRI}; // Wave Selection Enum
15 
16 // ------------------ Functions  ---------------------
17 void AD9833_SetWave(uint16_t Wave);                      // Sets Output Wave Type
18 void AD9833_SetWaveData(float Frequency,float Phase);    // Sets Wave Frequency & Phase
19 void AD9833_Init(uint16_t Wave,float FRQ,float Phase);   // Initializing AD9833
20 #endif

****************************************************************************************************************************************************************

****************************************************************************************************************************************************************

備注:代碼有兩部分,第一部分是AD9833.c文件,第二部分是AD9833.h文件,可根據行號區分兩部分內容(第一次寫博客,不太會排版)。

****************************************************************************************************************************************************************

****************************************************************************************************************************************************************


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM