Basing a PWM output on an Analogue input

Status
Not open for further replies.

turbo-brown

New Member
Hi There!

I'm hoping to be able to get a PIC16F690 (which comes with Microchip's PicKit2) to give a PWM output which varies according to an Analogue input.

Unfortunately the kit's instructions assume some level of competence with electronics and whilst I'm really keen to learn, I'm totally lost at the moment!

Can anyone help with some pointers which would get me going in the right direction?

The basic parameters are:

Analogue input
Some kind of look-up table which I can change the values in and then re-flash the PIC
A PWM output with a 15-20Hz period.

I gather that I can't pre or postscale the clock to give that kind of low frequency output and will need to make some kind of delay loop to give a less accurate frequency, but it should be fine for my purposes.

Any help would be hugely appreciated!

Cheers!

Alex
 
You should read the PDF for the PIC16F690 it contains an ADC which can be used for just that purpose.
 
turbo-brown said:
I gather that I can't pre or postscale the clock to give that kind of low frequency output and will need to make some kind of delay loop to give a less accurate frequency, but it should be fine for my purposes.

I don't know what kind of clock dividing possibilities there are for PICs, but if the built-in PWM facility of the PIC is incapable of low frequencies, you can always resort to doing your own PWM without having to sacrifice clock accuracy or doing a lot of busy waiting.

For instance, suppose you want to have PWM at 15 Hz with 256 degrees of resolution. Set up an interrupt routine to be called 15*256 times a second. Inside the routine do:
Code:
counter = (counter+1) % 256;
if (counter >= threshold)
  set output pin = 0
else
  set output pin = 1

This is also called bit-banging PWM.
 
Have a look at the 3rd post in this thread. It reads the ADC and then does software PWM to drive a servo. It should be easy to modify it to do what you need.

Mike.
 
Oh dear. I tried copying and pasting the code into the MPLab thing and just changed the include thing to say pic16f690 but it didn't seem to like it when I hit build all

I know it's a big ask, but would someone be willing to guide me through why it didn't build?

Here's what the MPLab came up with:


Cheers!

Alex
 
You can't just change the pic type. What you can do is start with the template supplied by Microchip for the 16F690. You'll find it in "C:\Program Files\Microchip\MPASM Suite\Template\Code". Copy and past this into your project and then copy the adc/pwm code from above. You should now have 2 errors, NOT_RBPU and VCFG0 not defined. By referring to the data sheet you can see that NOT_RBPU is now called NOT_RABPU and VCFG0 has been renamed VCFG and has moved into ADCON0. Some other things have moved around in the ADC control registers and need fixing. You can find the names of the various register bits in the inc file, located at "C:\Program Files\Microchip\MPASM Suite".

The attached asm file has the corrections.

Mike.
P.S. I don't know if this works as I don't have a 16F690 available.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…