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.

PWM on Pic16f877a

Status
Not open for further replies.

FinalTrigger

New Member
Can anyone please help me? I am trying to build a IR Theremin for my senior project but im a bit lost when it comes to the programing aspect of it. I read up on the data sheet on Capture/Compare/PWM but im not understanding what needs to be set to activate an output to a speaker?

Im not looking for any code, just a different explination than whats given in the data sheet.

thank you
 
PWM...pulse width modulation, while frequency stays the same. To deliver a 500Hz tone to a speaker the PWM does not matter. Once it's between 0 and 100% the speaker will happily burp out a 500Hz sound.
The pulse width is the ratio of the on time or logic hi of the pulse to the overall period = Ontime +offtime.

Therefore a 500Hz freq. sound requires the MCU pin to drive 500 'on' pulses per second. This implies 500 'off' pulses as well as it can't be pulsing unless u have both on & off. The PWM bit (which doesn't matter to a speaker) comes in on the duration of the On or off pulses which are limited by the period or 1/freq.

In a 500Hz waveform u have a period of 2 millisec...for both on & off cycles. if the on culce = 1 ms and the off cycle = 1 ms (1+1=2) then the PWM is 1/(1+1) = 50%. U can also have On cycle = 0.5ms and off cycle = 1.5ms, (.5+1.5=2ms). Thus the PWM is now .5/(.5+1.5) = 25%. Frequency is still 500Hz...sounds the same as well. So what's the use? Well insofar as sound goes I'd say keep the on cycle small => say a PWM of 5%, u get the same sound for a lot less power usage.
 
@Mosaic:
That makes a lot of sense and actually i didnt think of it that way to use less power on a lower PWM cycle.

But my main problem is im not fully understanding on what registers i am using for the PWM to work. Hence i am taking a IR photo LED and reflecting the beam back to a IR phototransistor and then sending the value to the pic, following a Analog to Digital conversion.

My overall idea is depending on how far away my hand is from the IR LED (higher/lesser Analog Value) to output either a different tone, or intensity of the tone (loudness).

Any ideas and mainly description of how the registers are used for PWM (noob language)
 
FT:

If u want to vary the tone based on the ADC of the phototransistor voltage, u don't need pwm. U need to modulate the frequency.. FM. Amplitude modulation AM is a bit trickier as logic outputs a fixed voltage. The AM stuff I haven't done, but on the surface of it, perhaps an opamp summing amp with an FET in the feed back loop, driven linear by PWM with an RC DAC on it's gate can deliver AM.

So back to FM. Setup a standard freq based on the Timer overflow interrupt. Say 2Khz. That requires a period of .5 ms to contain the on/off cycle. Quite doable on the PIC. Then u alter the Timer overflow setting based on the ADC sample from the phototransistor and a bit of integer math to scale the range so that your Timer now has a variable range of say 500Hz to 2Khz equivalent to perhaps 1V to 4V from the Phototransistor? You can also scale the sound by altering the Photo transistor load resistor, which then scales what the ADC sees. So use a trimmer pot for that and u may not need to scale the ADc with any math. The interrupt itself only toggles (xorwf) the Port pin driving your speaker, and clears the interrupt flag, plus save/restore wreg & status data. The main code should do the adc sample/math and Timer overflow updates.

Check out Piclist.com for the math routines.

Edit: Note that the Timer overflow period must = half the sound freq. period!
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top