Generating Sine Wave

Thread Starter

MCU88

Joined Mar 12, 2015
358
Would it not be faster / less processing / resources -- though if I loaded the entire table array into RAM?
 

OBW0549

Joined Mar 2, 2015
3,566
There ya go. I imagine some kind of syntax like

const code unsigned int SineTable[] = 1234, 5678, 9012, 3456,...

or something like that (I'm guessing) would do the trick.

Does MikroC have forums where you can ask about this?
 

jpanhalt

Joined Jan 18, 2008
11,087
Would it not be faster / less processing / resources -- though if I loaded the entire table array into RAM?
Probably not. What chip are you using? The 16F628A?

RAM is very limited. As I recall, you have 80 bytes per bank of general purpose ram and 16 bytes of common ram (accessible from any bank). A single page of program memory is 256 words. I can't see how switching banks or using the "common" RAM would be more efficient.

I am still betting that C used program memory. Do you have any examples you have written or used?

John
 

OBW0549

Joined Mar 2, 2015
3,566
Would it not be faster / less processing / resources -- though if I loaded the entire table array into RAM?
It might. I don't think it makes any difference with the dsPICs I've been using, but it might with the PIC16F628 or 877 depending on how program memory has to be accessed for data.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
It might. I don't think it makes any difference with the dsPICs I've been using, but it might with the PIC16F628 or 877 depending on how program memory has to be accessed for data.
My gut tells me that it would be faster if straight in RAM. But I did learn something today. I was unaware that it would be so easy to write data straight to program memory. It may come in handy later on for other projects to have hardcoded data in program memory. Particularly if I run out of RAM.
 

jpanhalt

Joined Jan 18, 2008
11,087
My gut tells me that it would be faster if straight in RAM. But I did learn something today. I was unaware that it would be so easy to write data straight to program memory. It may come in handy later on for other projects to have hardcoded data in program memory. Particularly if I run out of RAM.
Why do you think accessing RAM is faster than accessing program memory for a look-up table?

John
 

jpanhalt

Joined Jan 18, 2008
11,087
Confused...

Why do you think the 877A only has 256 bytes of RAM? Are you talking EEPROM? Why would you want to use that for a look-up table?

upload_2015-3-30_18-12-43.png

John
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
So, do you have any examples of tables you have used to share?
The only tables I have created in mikroC before is for an game of Simon Says and an composite video project years back, which I turned into an clock in the end. A black & white 24Hr clock on your TV screen. Gawd that was tricky to do in C. I had to figure out how long each instruction took to execute.

A table in C is nothing more than an array.

Code:
unsigned short simon[32]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                           0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 

jpanhalt

Joined Jan 18, 2008
11,087
No way.

As suggested, just use a spread sheet and calculate your columns. Any program will do it. I used to use Quattro Pro and loved it. Now, I have been forced to use Excel. The hardest part is converting from the spread sheet to what your compiler will like. Entering the spreadsheet columns takes up a lot of space, so I rearranged the results into rows of ten each. I did a lot of that using a wordprocessor and still ended up editing stuff manually. My tables were about 250 points.

John
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Should I make VP 2.5 volt in the formula?

So...
  1. PORTB Output to R2R Ladder 8-Bits
  2. Buffer with Amplification Output of Ladder with Adjustable Amplitude using Pot
  3. Filter Output (butter worth filter)
  4. Feed to OPAMP with Variable DC Offset Adjustment using Pot
  5. Set Impedance of Output to 50 Ohm
Yeah?
 

OBW0549

Joined Mar 2, 2015
3,566
So...
  1. PORTB Output to R2R Ladder 8-Bits
  2. Buffer with Amplification Output of Ladder with Adjustable Amplitude using Pot
  3. Filter Output (butter worth filter)
  4. Feed to OPAMP with Variable DC Offset Adjustment using Pot
  5. Set Impedance of Output to 50 Ohm
Forget making a DIY DAC with resistors, it's a waste of time and won't get you much better than about 6 bits of accuracy, even if you use 0.1% tolerance resistors.

Use a proper DAC, like the MCP4801:

http://www.microchip.com/wwwproducts/Devices.aspx?product=MCP4801

Cheap, easy to use, and guaranteed accurate to 8 bits. And it has its own internal Vref.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Forget making a DIY DAC with resistors, it's a waste of time and won't get you much better than about 6 bits of accuracy, even if you use 0.1% tolerance resistors.

Use a proper DAC, like the MCP4801:
http://www.microchip.com/wwwproducts/Devices.aspx?product=MCP4801
Interesting. And it would make the project more original, since most of the DIY hobby DDS projects I have seen use an R2R ladder for the DAC. But the interface is serial for the MCP4801. It would be faster to have an 8-bit parallel interface. I can dump a byte of data to PORTB quite fast in C. Hoping to get at least 100KHz of bandwidth.

:: Features ::
  • Sine, Square and Triangle waves
  • Variable Frequency (0.1Hz to 100KHz)
  • Variable Duty Cycle (1 - 99%)
  • Variable Amplitude (0 - 12 volt)
  • Adjustable DC Offset
  • Mains Powered
  • Metal Project Enclosure

:: User Interface ::
  • LCD HD44780 to Display Information
  • 4 Push Buttons for Menu Navigation
  • Rotary Encoder to Set Frequency and Duty Cycle
  • Pot for Amplitude
  • Pot for DC Offset
 

alyeomans

Joined Sep 13, 2010
39
Alternately you could use the 4015 IC shift register and filter for producing a very good sine wave. This simple circuit would need a clock source and the MCU PWM output could be used to make it frequency controllable.
I recently made a circuit that has a PWM clock to a 4015 and a bandpass filter to create an AC signal.
This method frees up MCU resources and gets a very reliable signal. Google "4015 sine wave generator" and check the images. It will also need an appropriate filter low pass or band pass at the required frequency.
Al
 

Attachments

Last edited:

Thread Starter

MCU88

Joined Mar 12, 2015
358
Alternately you could use the 4015 IC shift register and filter for producing a very good sine wave. This simple circuit would need a clock source and the MCU PWM output could be used to make it frequency controllable.
I recently made a circuit that has a PWM clock to a 4015 and a bandpass filter to create an AC signal.
This method frees up MCU resources and gets a very reliable signal. Google "4015 sine wave generator" and check the images. It will also need an appropriate filter low pass or band pass at the required frequency.
Al
I need to use original circuits, source code and artwork developed from scratch. Otherwise there will be little interest in the project.
 
Top