#include "AD9833.h"
#include "main.h"
// ------------------- Variables ----------------
uint16_t FRQLW = 0; // MSB of Frequency Tuning Word
uint16_t FRQHW = 0; // LSB of Frequency Tuning Word
uint16_t phaseVal=0; // Phase Tuning Value uint32_t idi
uint8_t WKNOWN=0,init=1; // Flag Variable
extern float frqfl,phsfl;
// -------------------------------- Functions --------------------------------
void Delay(volatile uint32_t delay){
while(delay--){};
}
// ------------------------------------------------ Software SPI Function
void writeSPI(uint16_t word) {
HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
/*ASM_NOP();ASM_NOP();*/Delay(1);
for (uint8_t i = 0; i < 16 ; i++) {
Delay(1);
if(word & 0x8000) HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_SET); //bit=1, Set High
else HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_RESET); //bit=0, Set Low
/*ASM_NOP(); ASM_NOP();*/Delay(1);
HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_RESET); //Data is valid on falling edge
/*ASM_NOP(); ASM_NOP();*/Delay(1);
HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_SET);
word = word<<1; //Shift left by 1 bit
}
HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_RESET); //Idle low
/*ASM_NOP();ASM_NOP();*/Delay(1);
HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
Delay(1);
}
// ------------------------------------------------ Sets Output Wave Type
void AD9833_SetWave(uint16_t Wave){
switch(Wave){ // SYNC ?
case 0:
// HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
writeSPI(0x2000); // Value for Sinusoidal Wave
// HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
WKNOWN=0;
break;
case 1:
// HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
writeSPI(0x2028); // Value for Square Wave
// HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
WKNOWN=1;
break;
case 2:
// HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
writeSPI(0x2002); // Value for Triangle Wave
// HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
WKNOWN=2;
break;
default:
break;
}
}
// ------------------------------------------------ Sets Wave Frequency & Phase (In Degree) In PHASE0 & FREQ0 Registers
void AD9833_SetWaveData(uint8_t sel){
float Frequency,Phase;
Delay(5);
Frequency=frqfl;Phase=phsfl;
// ---------- Tuning Word for Phase ( 0 - 360 Degree )
if(Phase<0)Phase=0; // Changing Phase Value to Positive
if(Phase>360)Phase=360; // Maximum value For Phase (In Degree)
phaseVal = ((int)(Phase*(4096/360)));
if(sel==0)phaseVal|=0XC000; // 4096/360 = 11.37 change per Degree for Register And using 0xC000 which is Phase 0 Register Address
if(sel==1)phaseVal|=0XE000;
// ---------- Tuning word for Frequency
long freq=0;
freq=(int)(((Frequency*pow(2,28))/FMCLK)+1); // Tuning Word
FRQHW=(int)((freq & 0xFFFC000) >> 14); // FREQ MSB
FRQLW=(int)(freq & 0x3FFF); // FREQ LSB
if (sel==0)FRQLW |= 0x4000;
if (sel==0)FRQHW |= 0x4000;
if (sel==1)FRQLW |= 0x8000;
if (sel==1)FRQHW |= 0x8000;
// ------------------------------------------------ Writing DATA
HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_SET);
HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_SET);
//HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
//HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET); //low = selected
Delay(1);
//if(init) writeSPI(0x2100); // enable 16bit words and set reset bit !!!once 0x2000 yolla
writeSPI(0x2028);
writeSPI(FRQLW);
writeSPI(FRQHW);
writeSPI(phaseVal);
writeSPI(0x2028);
// if(init) writeSPI(0x2000); // clear reset bit
//Delay(5);
//HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET); //high = deselected
//AD9833_SetWave(WKNOWN);
//Delay(1);
init=0;
return;
}
// ------------------------------------------------ Initializing AD9833
void AD9833_Init(/*uint16_t WaveType,float FRQ,float Phase*/void){
// float FRQ,Phase;
HAL_GPIO_WritePin(AD9833PORT,AD9833DATA,GPIO_PIN_SET); // Set All SPI pings to High
HAL_GPIO_WritePin(AD9833PORT,AD9833SCK,GPIO_PIN_SET); // Set All SPI pings to High
HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET); // Set All SPI pings to High
AD9833_SetWave(1);//WaveType); // Type Of Wave
//FRQ= frqfl;Phase=phsfl;
AD9833_SetWaveData(0);
AD9833_SetWaveData(1);
//FRQ,Phase); // Frequency & Phase Set
return;
}