little pwm - adc quest

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
hi All

Attached is columns of frequencies and PR2 values

I initially set my Fosc at 8MHz...and tried computing corresponding PR2 value and realized that the first 10 PR2 values (at 8Mhz) are greater than 255. And adjusting the TMR2 prescaler is not helping...(best prescaler value is 16!!)

So I decided to set my Fosc at 4Mz, good idea, right?

As for the adc...I might have a quest that why I already included that in my title.

thanks!
 

Attachments

Last edited:

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Oh just realised I also have a problem with the first 3 values for 4Mhz...

Everything would work OK with Fosc = 2Mhz but I think this is too slow!!!
Another solution would be choosing freq. values from 246.94 at 4Mhz, correct?
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,415
Seems you want to make some musical notes using the PWM. I make note of that fact as some people may not recognize the pattern of frequencies in your attachment, and you never stated your ultimate goal.

I've always found it best to work these things out in Excel (not with pictures in Word). The int() function helps keep it real.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Seems you want to make some musical notes using the PWM. I make note of that fact as some people may not recognize the pattern of frequencies in your attachment, and you never stated your ultimate goal.

I've always found it best to work these things out in Excel (not with pictures in Word). The int() function helps keep it real.
Yes you are correct Sir!

Actually this thread is the continuation of my thread *sound circuit resistor* from the electronic chat forum but as i am dealing with coding now I decided to create another thread in this forum!

I have worked these values with Excel but apparently there's no format for excel in the attachment windows...so I copied those values into word so I could be able to attach.

yes It about music with PWM...I am currently writing a code...I'll be more explicit of what I trying to do in the next posts!

BTW, I got those notes from here: http://www.intmath.com/trigonometric-graphs/music.php

Thanks for your comment!
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
I can't open the attachment. Normally the Linux document editor works on Word files, but not that one.
I'd also recommend using a spreadsheet, saves a lot of typing.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I can't open the attachment. Normally the Linux document editor works on Word files, but not that one.
I'd also recommend using a spreadsheet, saves a lot of typing.

ErnieM was able to open it...why can't you open it? I've saved it for Ms Word 97 - 2003. maybe I should re - attached it again...
 

Markd77

Joined Sep 7, 2009
2,806
Tables are a bit of a pain, you have to add the "|" character to the end of each cell (but that can be done with a formula) and then copy and paste, select the text and click the table icon in advanced reply mode. Probably easier to save as .csv which everyone can view.
Anyway it looks like this:

How it looks in the post:

[table=NOHEAD]QTY | Newark PART # | DESCRIPTION | UNIT COST | TOTAL COST|
3| 93K5434 | PCB mount 12V relay | $ 2.28 | $ 6.84|
2| 26R4228 | RESISTOR, METAL GLAZE, 20 MOHM, 500 MW, ± 5% | $ 0.12 | $ 0.24|
1| 96M9178 | 12V wall wart p/s | $ 13.53 | $ 13.53|
10| 70R0064 | TERM BLK, PCB, SCREW, 5.0MM, 2WAY | $ 0.73 | $ 7.30|
1| 13T9276 | Enclosure for the Arduino Duemilanove or Mega | $ 9.46 | $ 9.46|
1| 13T9277 | ARDUINO UNO Board w/ Workshop Starter Kit (just buy the 25$ arduino, not the kit) | $ 71.38 | $ 71.38|
2| 13T9280 | PROTOTYPE BOARD FOR ARDUINO UNO | $ 10.42 | $ 20.84|
3| 89K1072 | TIP120 DARLINGTON TRANSISTOR, NPN, 60V, TO-220| $ 0.43 | $ 1.29|
1| 01M7886 | CONVERTER, DC/DC, 1.5W, +1000V | $ 183.00 | $ 183.00|
1| 31K7040 | OPTOCOUPLER, DARLINGTON, 3750VRMS | $ 7.02 | $ 7.02| [/table]

The result:

QTY | Newark PART # | DESCRIPTION | UNIT COST | TOTAL COST|
3| 93K5434 | PCB mount 12V relay | $ 2.28 | $ 6.84|
2| 26R4228 | RESISTOR, METAL GLAZE, 20 MOHM, 500 MW, ± 5% | $ 0.12 | $ 0.24|
1| 96M9178 | 12V wall wart p/s | $ 13.53 | $ 13.53|
10| 70R0064 | TERM BLK, PCB, SCREW, 5.0MM, 2WAY | $ 0.73 | $ 7.30|
1| 13T9276 | Enclosure for the Arduino Duemilanove or Mega | $ 9.46 | $ 9.46|
1| 13T9277 | ARDUINO UNO Board w/ Workshop Starter Kit (just buy the 25$ arduino, not the kit) | $ 71.38 | $ 71.38|
2| 13T9280 | PROTOTYPE BOARD FOR ARDUINO UNO | $ 10.42 | $ 20.84|
3| 89K1072 | TIP120 DARLINGTON TRANSISTOR, NPN, 60V, TO-220| $ 0.43 | $ 1.29|
1| 01M7886 | CONVERTER, DC/DC, 1.5W, +1000V | $ 183.00 | $ 183.00|
1| 31K7040 | OPTOCOUPLER, DARLINGTON, 3750VRMS | $ 7.02 | $ 7.02|
 

Markd77

Joined Sep 7, 2009
2,806
ErnieM was able to open it...why can't you open it? I've saved it for Ms Word 97 - 2003. maybe I should re - attached it again...
It's just because I don't use Windows. Ubuntu opens most Word files OK, but that one just looks like a blank page to me. A lot of nerds use Linux so if possible go for a universal format like .csv.
 

Markd77

Joined Sep 7, 2009
2,806
Thanks, it looks like you have to start at a slightly higher frequency if you are going to use the PWM module. I don't think it's going to sound very much like a scale at the top end. It might be worth putting a column in for percentage error and seeing how bad it is. You could put the PR2 values for a different prescaler in the table for the higher frequencies, and then change the prescaler according to the table position. That would give better accuracy at high frequencies.
 

ErnieM

Joined Apr 24, 2011
8,415
I played around with the numbers some. Seems the highest frequency you can clock this is 3.39MHz: at 4MHz you loose the lowest 3 notes.

You can gain a little accuracy if you cut back the divider for anything above about 880Hz, as this gives some range back into the PR2 number (makes it larger).

Excel worksheet & cvs attached in the zip.
 

Attachments

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I played around with the numbers some. Seems the highest frequency you can clock this is 3.39MHz: at 4MHz you loose the lowest 3 notes.

You can gain a little accuracy if you cut back the divider for anything above about 880Hz, as this gives some range back into the PR2 number (makes it larger).

Excel worksheet & cvs attached in the zip.
I was going to post on what i been working on...and I have seen this post!

Yes, that's correct the first 3 notes get lost at 4Mhz...that why I tarted with 246.94Hz to make my life easier...

I had a look at your numbers...BUT you CAN'T set your internal oscillator at 3.39 Mhz...and I don't want to work with 2MHz Fosc.

would you mind working with 2MHz Fosc?

I think I'll just start from 246.94Hz

Thanks again!
 

Markd77

Joined Sep 7, 2009
2,806
You can change the processor frequency and prescaler values in ErnieM's spreadsheet and it recalculates everything.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
You can change the processor frequency and prescaler values in ErnieM's spreadsheet and it recalculates everything.
So I' ll just keep my original spreadsheet! I have added a column with CCPR1L:CCP1CON<5:4> that I will post soon but I have a quest in da mean time!
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I am a bit confused about one thing...

I am configuring my PWM as single output, P1A modulated...(PIC16F690)
So I set the RC5/CCP1/P1A as output (TRISC,5 value) as it has to generate an output (wave) at that pin...but when I read the datasheet on page 129. section:11.3.12 I get a bit confused although I have used PWM before...:)

my confusion comes from point 1. and 9.
It's like initially that pin needs to be set as input then later as output, is that so? I don't really see the point here. it's like that pineeds to be set as input BUT when it is time to generate the waveform, it as to be set as output, correct? and if so why not just leave it as output?

Hope I expressed my concern clearly and hope to be clarified on this one...

thanks
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I wasted too much time trying to figure out how to use that little duty cycle formula...lol :D finally figured out and came up with the following as attached.

So I think Im correct to say that for each frequency,there's going to be different CCP1CON, CCPR1L values but same T2CON just that I'll have to turn it on and off...

I am going to complete the code and post it for correction if any illogical thinking or whatsoever
 

Attachments

Markd77

Joined Sep 7, 2009
2,806
Just for fun I worked out the errors for doing it without using the PWM module. It needs a 16 bit value so doubles the size of the lookup table and could use Timer1 interrupts. The minimum frequency would be under 20Hz.
(This is for 4MHz clock speed).
 

Attachments

Last edited:

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
BTW, what's the difference between frequency and actual frequency?
and what's the error really represents? I think I miss this point!
 

Markd77

Joined Sep 7, 2009
2,806
Frequency is the desired note, actual frequency is what the PIC would output. Error is the difference between them.
I'm not actually sure how much error is OK, but the difference between two semitones (1/12 octave) is about 6% so I think even 1% error might sound quite sharp or flat.
I just noticed that there may be missing notes from some of the tables, I'd expect to see either 8 or 12 notes per octave.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Frequency is the desired note, actual frequency is what the PIC would output. Error is the difference between them.
I'm not actually sure how much error is OK, but the difference between two semitones (1/12 octave) is about 6% so I think even 1% error might sound quite sharp or flat.
I just noticed that there may be missing notes from some of the tables, I'd expect to see either 8 or 12 notes per octave.
Yes you are correct!

There are some missing notes from the table...I just chose some in order to have *thirty two* sounds in that *range* (2 hundreds to 3 thousands Herz)
 
Top