Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sine wave sweep

Status
Not open for further replies.

adnan012

New Member
hi.
I want to generate sweeping sinewave from 1kHz to 10Khz using pic controller.

At this page https://www.romanblack.com/one_sec.htm under the heading

Making multiple sinewaves of highly accurate frequency New 21st Feb 2011.



#define BDA_697Hz 365 // constant to make 697 Hz
unsigned int wave absolute 0x15; // 16bit accumulator for the sinewave
unsigned char wave_1 absolute 0x16; // overload for fast access to byte 1
const unsigned char sine64[64] = {
50,54,59,64,68,73,77,81,85,88,91,93,95,97,98,99,99,99,98,97,95,93,91,88,85,81,77,73,68,64,59,54,
50,45,40,35,31,26,22,18,14,11,8,6,4,2,1,0,0,0,1,2,4,6,8,11,14,18,22,26,31,35,40,45};

// loop and generate dual sinewave DTMF tone
PR2 = (128-1); // PWM at period = 128
while(1)
{
while(!PIR1.TMR2IF); // sync to start of PWM cycle
PIR1.TMR2IF = 0;

// calc the sinewave, and load into PWM module CCPR2L
wave += BDA_697Hz; // zero error Accumulation
CCPR2L = sine64[wave_1 & 0x3F]; // Binary Divide output (/256) and keep 6 bits
}


can anyone explain the code. is isr required for the code.
 
Last edited:
and I want to be hansom.

It can be done. How good a sine wave?
One option is to use the PWM output on some PICs. Then a low pass filter. Run the PWM at 100khz.
OR
Use a external DAC and low pass filter.

Make a 1/4 sine look up table in memory and send it out.

This has been talked about many times here. Search the forum for sine and PIC.
 
thanks forreply.

in the above code
CCPR2L = sine64[wave_1 & 0x3F];
wave_1 is not updating how this code will work?
 
Last edited:
Hi, that code is from my web page. :)

wave is a 16bit variable, and wave_1 is the top byte of the 16bit variable.

so;
wave += BDA_697Hz;
adds a fixed amount to the 16bit variable

and
CCPR2L = sine64[wave_1 & 0x3F];
takes 6bits from the top byte of the 16bit variable (wave_1), and uses those 6bits to select a PWM value from a lookup table with 64 "sine" entries.

If you change the value you add to the 16bit variable you get a different output sine frequency but it will be in steps, and due to the low speed of a PIC those steps will get coarse as you get closer to 10kHz.

My examples on that page use PWM with a period of 128, so at PIC 20MHz (5 MHz timer) the PWM frequency is 5million / 128 = 39kHz. So to try to get 10kHz will only give you 3.9 PWM cycles per "sine" wave.

I second Nigel's suggestion in post #2, that Mondo projectuses some very carefully tuned fast assembler code to make a pretty decent function generator and all the work is already done for you...
 
I second Nigel's suggestion in post #2, that Mondo projectuses some very carefully tuned fast assembler code to make a pretty decent function generator and all the work is already done for you...

The only 'down side' is that they don't use MPASM, so either use their HEX file, download the asembler they use, or convert to MPASM.
 
thanks for reply.

I want to do the following

1} pic16f876 measure signal from a signal generator. the signal is limited from 60hz to few hundred Hz

2} After this the pic controller should construct or produce the same waveform/frequency.

3} the user can change the signal generator frequency so pic must synchronize with it.


i am using mplab with ccs picc compiler and i am using pickit2 debugger.
 
thanks for reply.

I want to do the following

1} pic16f876 measure signal from a signal generator. the signal is limited from 60hz to few hundred Hz

2} After this the pic controller should construct or produce the same waveform/frequency.

3} the user can change the signal generator frequency so pic must synchronize with it.

That's a rather different question to your original request.
 
Sample the input signal, place them into an array. Then you've got to compare when two samples are the same, then you have the frequency. Make a lookup table with the DAC values (define it), send these samples to the DAC using a timer with a delta T that's the same as the 1/frequency. At last use a LPF.

Have fun
 
Well my page that you linked to in post #1 has quite a few examples, with all C source code included.

You could also search this forum (and google etc) for "PIC c-code sine wave" and that should give you a lot more examples. :)
 
thanks for reply.

i have successfully generated the sine wave using your technique (code in my first post). can i drive half bridge using the same technique.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top