anotherbrick
Full Member level 4
- Joined
- Jan 10, 2009
- Messages
- 217
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,298
- Location
- Istanbul , Turkey
- Activity points
- 3,144
hello dear forum
I am trying to generate 20 Khz square wave with AD9833
however AD9833 only generate square wave with correct freq. at the init command
if I want to change the frequency afterwards inside while loop, for example sweep around 20 Khz
it changes the output to sinus wave with a stupid frequency 19830 without sweep
i.e. it doesnot obey my code ( STM32F103C8)
what I m doing wrong ? or , isnot AD9833 original ?
thanks for any advice
I am trying to generate 20 Khz square wave with AD9833
however AD9833 only generate square wave with correct freq. at the init command
if I want to change the frequency afterwards inside while loop, for example sweep around 20 Khz
it changes the output to sinus wave with a stupid frequency 19830 without sweep
i.e. it doesnot obey my code ( STM32F103C8)
what I m doing wrong ? or , isnot AD9833 original ?
thanks for any advice
C:
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
// NVIC_Configuration();
writeSPI(0x2100); HAL_Delay(100); writeSPI(0x2000);
AD9833_Init();
-----------------------
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(100);
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
disptimer++;
frqfl=20000.5F+(float)(disptimer*5);phsfl=100.0F
AD9833_SetWaveData();
------------------------------------
// ------------------------------------------------ Software SPI Function
void writeSPI(uint16_t word) {
HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_RESET);
ASM_NOP();ASM_NOP();//Delay(5);
for (uint8_t i = 0; i < 16 ; i++) {
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(5);
HAL_GPIO_WritePin(AD9833PORT,AD9833SS,GPIO_PIN_SET);
//Delay(5);
}
--------------------------------
// ------------------------------------------------ Sets Wave Frequency & Phase (In Degree) In PHASE0 & FREQ0 Registers
void AD9833_SetWaveData(void){
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)))|0XC000; // 4096/360 = 11.37 change per Degree for Register And using 0xC000 which is Phase 0 Register Address
// ---------- 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
FRQLW |= 0x4000;
FRQHW |= 0x4000;
// ------------------------------------------------ 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;
}
------------------------------------
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;
}
}
----------------------------------------
// ------------------------------------------------ Initializing AD9833
void AD9833_Init(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();//FRQ,Phase); // Frequency & Phase Set
return;
}