Fastest Sinus????

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Fastest Sinus????

Post by Jan Lichtenbelt »

I still have a Textronix TM5003 function generator/counter. Is about 40 years old and makes a lot of noise. Time to get updated. Looking at internet I found function generator and counters but rather expensive.

But as PIC microcontroller user, I thought that can be done easily!

First some literature learned me that a Digital-Analog Convertor DAC, which I need, can be realised by means of:
1) PWM by changing the pulse width duty and with a RC-circuit. (Easy setup)
2) Setting the digital output on a ladder of resistances (2R-R)

I first tried the PWM, without success. The fastest 'sinus' was about 400 Hz. That can be realised also by setting my wife on a bike and use the dynamo output.
Second I tried the 2R-R ladder. I thought the PIC output should be buffered, but I figured out in the meanwhile that this can be deleted. But in the picture you will see two hex buffers (4010). And what was the fastest sinus I got? You see the answer on the scope picture. With the sinus generated in 256 steps, the frequency is just 0.1 msec, or 10 kHz.
Is that what can be expected? in the assembler program it can be seen that about 485 steps are necessary for 1 period of 100 usec. That means about 206 nsec per step. This is close to the official 203 nsec for an instruction cycle time with a 19.6608 MHz crystal.

Conclusion:
It took much more time, but was very interesting. The sinus is not ideal. The reason can be easily found in the assembler translation of the flowcode.

Question:
Is this the fastest sinus in 256 steps accuracy?

Jan Lichtenbelt

ref: http://en.wikipedia.org/wiki/Resistor_ladder

DAC_8_bits_Ladder_t.jpg
DAC_8_bits_Ladder_t.jpg (216.64 KiB) Viewed 5749 times
Fastest Sinus PIC16F877A_t.jpg
Fastest Sinus PIC16F877A_t.jpg (95.54 KiB) Viewed 5777 times
Attachments
Sinus generator_ladder_V2.fcf
(9.72 KiB) Downloaded 366 times

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Fastest Sinus????

Post by Spanish_dude »

That can be realised also by setting my wife on a bike and use the dynamo output.
That made my day :lol: .

Back on the interesting stuff, a "selfmade" DAC with an R2R network isn't that difficult to make but the problem with this is that using low frequency sinus will make a "stair-like" signal (if I remember well from my R2R network I tried).
There are programmable IC's from Analog devices where you can generate sinus/square/triangle waves.

This one is a link that Martin (medelec) sent me a while back : http://uk.farnell.com/analog-devices/ad ... dp/1274251
I'm not a fan of smd and I never tried it but I will.

Cheers

Nicolas L. F.

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Fastest Sinus????

Post by Jan Lichtenbelt »

Dear Nikolas,

Of coarse commercials DAC will be better. But we are do-it-selfers.

In the mean while I improved the 10 KHz sinus to a nearly perfect sinus. The deviation is 1:512, due to the fact that the loop makes the period of the sinus 1 programming step to long (1 period is in this particular case 512 programming steps).

ASSEMBLER:
What I did, is deleting all stuff not essential in the assembler file.
In assembler, if the sinus changes value, 2 program steps are needed, while if the sinus does not change value, only 1 step is enough. To make that also 2 steps I put into the assembler NOP's which takes 1 programming step time, See asm file attached.
Translation into a hex file has been done by means of MPLAB and loading the hex file into the chip by means of PPP with the EB006 block connected to the PC. The hex file has also be attached. For details see http://www.matrixmultimedia.com/mmforum ... f=6&t=7825.

FREQUENCY
The crystal used is 19.6608 MHz, which is 50.8 nsec. That means a program step will take 4x 50.8 nsec= 203 nsec. The period of the sinus is 513 program steps and that equals 104 usec or 9.58 KHz. And that is exactly what I measured with a frequency counter, see also the scope picture.
The first perfect sinus is possible with 3 program steps. The period time will be 203 x 3 x 256 = 156 usec of 6.4 KHz.
Of coarse other frequency are possible, by changing the number of step for one period. This has been taken in this example to be 256. But any number can be taken, if the accuracy is acceptable.

ACCURACY
E.g. if 25 steps are acceptable, 100 kHz sinus wave is possible. On the other side a very accurate sinus with 1024 steps and 3 program steps will have a freqency of 2.1 KHz.
Using the R-2R ladder, own made, does have only an accuracy of 1% for each resistance which will decrease the anloge output accuracy. If better accuracy is wanted, commercial DAC has to be taken.

Good Luck

Jan Lichtenbelt
10 KHZ Sinus V2.jpg
10 KHZ Sinus V2.jpg (153.14 KiB) Viewed 5693 times
Attachments
10 kHz Sinus V2.HEX
(2.84 KiB) Downloaded 321 times
10 kHz Sinus V2.asm
(7.88 KiB) Downloaded 333 times

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Fastest Sinus????

Post by Spanish_dude »

Hi Jan,

I only programmed in assembler for a ADuC832 (8051 architecture) at high school so I'm not sure if what I'll say will be correct for PICs.

From what I understand in the ASM file, your are loading a value and then outputing that value on PORTC.
Wouldn't it be easier to make like an array of output data ?

Something like this : (this is C as I don't know how to program in ASM for PICs)

Code: Select all

#define SIZE_ARRAY X // this is just like a constant value

char outputData[SIZE_ARRAY] = {A,B,C,D;} // X is the size of the array and A,B,C and D are the output data
char i;

// Set PORTC as output

while (1)
{
    PORTC = outputData[i];
    i++;

    if (i >= SIZE_ARRAY)
        i = 0;

    // Delay here
}

If you use a potentiometer to change the frequency, you could make an algorithm for the delay based on the value of the ADC.
To read the ADC value I would use an interrupt which will have a "constant read time" of the ADC. The polling method will be affected by the delay.

Anyways, this will (I think) optimize the code a bit.

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Fastest Sinus????

Post by Jan Lichtenbelt »

Dear Nikolas,

There are restrictions in the possible variable delay. You can find the restrictions if you look more in detail in the assembler file. I explained that in an other article more in detail, see http://www.matrixmultimedia.com/mmforum ... f=6&t=8709.

Never used assembler? Take once time to learn it a little bit and you will learn better the possibilities and restrictions of Flowcode and C.

Kind regards,

Jan Lichtenbelt

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Fastest Sinus????

Post by Spanish_dude »

I understand there are some restriction, the generated sine won't be perfectly accurate, but as you said, we are "do-it-selfers" :).
Jan Lichtenbelt wrote:Never used assembler? Take once time to learn it a little bit and you will learn better the possibilities and restrictions of Flowcode and C.
I did learn assembler but that was for an 8051 architecture microcontroller (ADuC832 from Analog Devices).
I know ASM for PIC is different as they have a different architecture. I might have a book that explains PICs ASM somewhere, I'll read it some day .

Btw, my name is Nicolas with a 'c' :mrgreen:

BR,

Nicolas L. F.

Post Reply