-1

I am trying to generate sine wave using programmable waveform generator AD9833 with ATmega32-A micro controller.(MCLK =8MHz clock frequency). I am using USART communication and so if i change frequency or phase in hyper terminal then the waveform frequency and phase has to change. I wrote small code for this as shown below.

but from the above code I am generating sine wave. If i enter frequency then waveform frequency also changing exactly but i tried to change phase then here is the problem. If i enter phase as 90 then it is changing 10 degree it is not changing 90. I don't know why it is generating like that and what mistake I did? am i missing any bits ? problem with phase only.

Thanks in advance.

If i want enter float value of phase for example 2.5 degree, what i have to change. either "unsigned int phase" into "float phase" in the function. if i try like that the "<<" is not compatible with float.what i have to do if i want to enter phase as 35.8 degree. please suggest me.

Thanks in advance.

verendra
  • 223
  • 3
  • 18
  • is the angle in radian? 90 radian will actually be around 14 degrees – maths-help-seeker Oct 02 '12 at 22:09
  • 1
    Could you provide the datasheet and point us to the relevant pages where the phase-setting command is documented, just to show that you have done your research? – David Grayson Oct 02 '12 at 22:10
  • @ David Grayson data sheet page number 15, we have both frequency and phase registers writing commands. – verendra Oct 03 '12 at 07:10
  • If i want enter float value of phase for example 2.5 degree, what i have to change. either "unsigned int phase" into "float phase" in the function. if i try like that the "<<" is not compatible with float.what i have to do if i want to enter phase as 35.8 degree. pls suggest me – verendra Oct 03 '12 at 15:09

1 Answers1

1

From the datasheet page 15:

This signal is phase shifted by 2π/4096 × PHASEREG

If you want to use degrees, modify phase as follows before sending it (uint32_t will probably require the including of the stdint.h file):

phase = (uint32_t)phase * 4096 / 360;

If you aren't particularly concerned about precision you can do the following which would use 16-bit divide instead of 32-bit divide:

phase = phase * (4096 / 360);
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • from the data sheet it should be like this phase=360/phase*4096; – verendra Oct 03 '12 at 07:22
  • Note that `4096 / 360` is not an even integer number. If correct rounding of that result is important, you need to write (int)(4096.0 / 360.0). You may have to link a float number library to the project, but the calculation will be done by the pre-processor. – Lundin Oct 03 '12 at 09:40
  • @Lundin Incorrect, notice my order of operations, it will get the correct value within +/- 1. (4096/360 is not calculated first). – CrazyCasta Oct 03 '12 at 15:48
  • 1
    @verendra The number you have given is the value of the phase that is used based on the value in the register, the OP wants the opposite, the value to put in the register to get the desired phase. – CrazyCasta Oct 03 '12 at 15:50
  • @CrazyCasta Yes but you have the very same problem if you do `unsigned int / 360`. And then if phase is larger than 65535/4096, you will also get an integer overflow. as int will be 16 bits on this platform. – Lundin Oct 03 '12 at 19:17
  • @Lundin Thanks, good catch, I changed to add convert to 32-bit. Using float on a microcontroller should be saved for those times when it is absolutely necessary. – CrazyCasta Oct 03 '12 at 19:54
  • @CrazyCasta when i use this line to get exact precision, phase = (uint32_t)phase * 4096 / 360; but it gives uint32_t undefined error. I have to add any header file to use this? or any other way to overcom this. – verendra Oct 04 '12 at 14:04
  • @verendra You'll need to include stdint.h – CrazyCasta Oct 04 '12 at 18:19
  • @CrazyCasta Can you give me some explanation how the above formula will change phase in degree's. – verendra Oct 22 '12 at 11:42
  • @verendra I'm not sure what you mean by "will change phase". I just took the formula that I posted, solved for PHASEREG and changed to 360 instead of 2*pi since you want degrees instead of radians. – CrazyCasta Oct 23 '12 at 22:26