Searching \ for '[PIC]: ADC based PWM' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/ios.htm?key=adc
Search entire site for: 'ADC based PWM'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: ADC based PWM'
2004\08\18@112121 by Zack

picon face
Hello everyone,

           I'm looking to make a circuit that uses two analogue inputs to
drive a PWM.  I am trying to make the frequency of the output anywhere from
10 KHz to 250 KHz based on a potentiometer.  Also, the duty of the circuit
needs to have an approximate 10% duty, but needs to be able to vary, again,
based on a potentiometer.  I would like to use a 16F87x for this, and in
assembly (or if someone could help me find a decent compiler for some other
language, that would be simpler).  The 16F877 already has a hardware PWM,
and I plan to use that, but I'm confused on how to make variable frequency
and variable pulsewidth.



Any suggestions about where to start, or some example codes would be
helpful.

Thanks,

~Zack S.

--
http://www.piclist.com hint: To leave the PICList
spam_OUTpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu

2004\08\18@114200 by Alan B. Pearce

face picon face
>I would like to use a 16F87x for this, and in
>assembly (or if someone could help me find a decent
>compiler for some other language, that would be simpler).
>The 16F877 already has a hardware PWM, and I plan to
>use that, but I'm confused on how to make variable
>frequency and variable pulsewidth.

This sounds to me like it would be an ideal project for something like a
dsPIC, for which there is a free C compiler available from Microchip. It
also has hardware PWM.

Another possibility would be an 18F family chip, AFAIK these all come with
hardware PWM, and the Microchip C compiler has a demo version which is
easily kept working for the life of a project by re-installing.

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestKILLspamspam@spam@mitvma.mit.edu

2004\08\18@134203 by Zack

picon face
I found a great C compiler and IDE for it, so I think I'll use that to code
it.  I'll find out more about his dsPIC you speak of.  Thanks for the
suggestions.

{Original Message removed}

2004\08\18@135031 by Dave VanHorn

flavicon
face
At 10:12 AM 8/18/2004, Zack wrote:

>Hello everyone,
>
>            I'm looking to make a circuit that uses two analogue inputs to
>drive a PWM.  I am trying to make the frequency of the output anywhere from
>10 KHz to 250 KHz based on a potentiometer.  Also, the duty of the circuit
>needs to have an approximate 10% duty, but needs to be able to vary, again,
>based on a potentiometer.  I would like to use a 16F87x for this, and in
>assembly (or if someone could help me find a decent compiler for some other
>language, that would be simpler).  The 16F877 already has a hardware PWM,
>and I plan to use that, but I'm confused on how to make variable frequency
>and variable pulsewidth.

Not too hard really..
The frequency part is controlled by one pot, that ends up setting the off-time, so that on+off is some number.
The pulse width is set by the other pot. If you do it proportionally, IOW the pot controls duty-cycle rather than pulse width, then you never "collide" with a pulse width setting too wide for the frequency.

You have to choose wether you want to control it as "on-time" and "off-time" or frequency and duty-cycle.

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestspamKILLspammitvma.mit.edu

2004\08\18@140655 by Zack

picon face
The concept isn't what confuses me.  It's the prescaler!  I have no idea how
I'm going to get nice, even integers from the pot, and then some how use it
to create the correct frequency. Maybe I don't need a prescaler, I'm rather
new to PIC's...

{Original Message removed}

2004\08\18@141136 by Spehro Pefhany

picon face
At 10:12 AM 8/18/2004 -0500, you wrote:
>Hello everyone,
>
>             I'm looking to make a circuit that uses two analogue inputs to
>drive a PWM.  I am trying to make the frequency of the output anywhere from
>10 KHz to 250 KHz based on a potentiometer.  Also, the duty of the circuit
>needs to have an approximate 10% duty, but needs to be able to vary, again,
>based on a potentiometer.  I would like to use a 16F87x for this, and in
>assembly (or if someone could help me find a decent compiler for some other
>language, that would be simpler).  The 16F877 already has a hardware PWM,
>and I plan to use that, but I'm confused on how to make variable frequency
>and variable pulsewidth.
>
>
>
>Any suggestions about where to start, or some example codes would be
>helpful.

You are going to get pretty crummy resolution in the timebase (especially)
at the top end of the frequency range, and in the on time. Read the
midrange reference manual's description of PWM mode carefully to see what
I mean. 250kHz means you have only 4usec for the entire PWM cycle, with
400nsec on time. If the machine cycle is 200nsec (16F with 20MHz crystal)
then that's the best you can do.

Why do you want to change the timebase? It's called PWM for a reason-
normally you just fiddle with the duty cycle (Width of the on time pulse)
and leave the timebase alone.

Best regards,

Spehro Pefhany --"it's the network..."            "The Journey is the reward"
.....speffKILLspamspam.....interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspam_OUTspamTakeThisOuTmitvma.mit.edu

2004\08\18@141759 by Dave VanHorn

flavicon
face
At 01:08 PM 8/18/2004, Zack wrote:

>The concept isn't what confuses me.  It's the prescaler!  I have no idea how
>I'm going to get nice, even integers from the pot, and then some how use it
>to create the correct frequency. Maybe I don't need a prescaler, I'm rather
>new to PIC's...

Well, you may or may not need a prescaler, or even a timer.
Depends on the range of operations, and what else is happening in the box.

The ADC has to give you the pot positions, which conveniently will be a binary number. So, you turn on for X counts, and turn off for Y, where X and Y are the raw ADC counts.  Now you have independent control of on and off time, but frequency is the product.

Using a timer, you can just set it X counts below overflow, and use the overflow into to toggle the output pin, and load the Y count, then when that hits, toggle and load X... repeat forever.  Meanwhile, the ADC is putting new X and Y values into ram or registers, for the next cycle.

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestspamspam_OUTmitvma.mit.edu

2004\08\18@143248 by Zack

picon face
I'm using the pot for frequency adjust for a sort of "set-and-forget" value,
tuned for the operation I'm using for.  The frequency may change every once
and a while, so I may need to make small adjustments.

You're right about the resolution, but how can I increase the resolution?
New crystal?  Is there a limit for oscillator frequency on PIC's?

{Original Message removed}

2004\08\18@143913 by Jan-Erik Soderholm

face picon face
Zack wrote :

>  Is there a limit for oscillator frequency on PIC's?

Of course not !
Why would there ?

But, if you'd rather prefer the truth, I'd suggest you check
with the data sheet for the PIC you are planning to use...

Jan-Erik.

--
http://www.piclist.com hint: To leave the PICList
@spam@piclist-unsubscribe-requestKILLspamspammitvma.mit.edu

2004\08\18@145330 by Zack

picon face
OK, I have made a very rough sketch of how the program would work (in C):

void define_frequency() {
       //Do ADC
       return x;
}
void define_duty() {
       //Do ADC
       return z;
}

for (x){
       if (z != 0){
               out_pin = 1;
       } else {
               out_pin = 0;
       }
       if (x == 0){
               define_frequency();
               define_duty();
       } else {
               x--;
               z--;
       }

}

Am I doing this right?  (my first C program.. lol)

{Original Message removed}

2004\08\18@161050 by Spehro Pefhany

picon face
At 01:52 PM 8/18/2004 -0500, you wrote:
>OK, I have made a very rough sketch of how the program would work (in C):
>
>void define_frequency() {
>         //Do ADC
>         return x;
>}


void functions do not return values, maybe you want something like:

int define_frequency(void)
   {
   int frequency;               // the frequency in wombat-cycles per cow-acre
   // Do ADC and calculate
   return frequency;
   }

Then you could write:

int current_frequency;                          // the current frequency in
w-c/c-a
...
current_frequency = define_frequency();


>void define_duty() {
>         //Do ADC
>         return z;
>}

Ditto, and you could also use x, since the name will be local to the function.
But it is nice to use variable names that mean something in most cases.

I get the impression that you are expecting the variables to be global in
scope. You need to learn the differences, and should (generally) avoid
global variables in favor of automatic or static variables. While you are
at it,
learn what volatile and register mean.

{Quote hidden}

Assuming some appropriate declarations etc., it is almost a valid C program
("for" needs a different syntax), but I'm not sure it will do anything
especially pleasant or desirable. Suggest you spend
some time learning C in your desktop environment first.

Best regards,

Spehro Pefhany --"it's the network..."            "The Journey is the reward"
KILLspamspeffKILLspamspaminterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu

2004\08\18@162546 by Zack

picon face
Thank you for the suggestions, and yes, I do need some time to work with C.
I have barely read the first chapter in a C++ book.  I have learned the
correct syntax for the "for" command (which is identical to PHP, my main
programming language).

I think I need to use interrupts too, for the AD conversion anyway, so the
program will have to change a lot.  When I get data from the ADC, then I'll
change the freq and duty variables, instead of calling a function to define
them.

Do you (or anyone else) know how to use the hardware PWM of the 16F877?
Also, how does calling functions affect timing and operations in a PIC?

{Original Message removed}

2004\08\18@164659 by Jan-Erik Soderholm

face picon face
Zack wrote :

> Do you (or anyone else) know how to use the hardware PWM of
> the 16F877?

Hi.
The *data sheet* knows how to use the PWM.

If you could be a bit more specific about what's unclear in the
data sheet (yes, there definitly can be unclear parts in them),
it would be easier to help/answer. And do not ask if you havn't
even *read* the data sheet ! It shows to easily ;-) ;-)

> Also, how does calling functions affect timing and operations
> in a PIC?

Are you talking about "functions" as they are used
in C ? Or any other definition of "function" ?
A PIC by itself knows nothing about "functions".

And what specific "timing and operations" are you refering to ?

Let me ask another thing...
I've got the impression that you are fairly new to
*both* PIC's and C, right  ?

It must be hard to both have a new language to learn,
and at the sam time doing it on an unknown platform !!
Was there any specific reason you selected to jump
directly to a HLL such as C ?

It's generaly thought that it's easier to learn the PIC
architecture of one begin with coding in assembler (ASM).
Give it a thought. The actual project, as you have described
it, could be a nice PIC-learning project, if done in ASM.
And, at last, you will normaly get more replies/help for
problems if they are expressed in ASM then in C or any
other high level language.

I'm sorry if I have missed something early in the thread !

Regards,
Jan-Erik.

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestspamBeGonespammitvma.mit.edu

2004\08\19@003120 by Zack

picon face
>If you could be a bit more specific about what's unclear in the
>data sheet (yes, there definitly can be unclear parts in them),
>it would be easier to help/answer. And do not ask if you havn't
>even *read* the data sheet ! It shows to easily ;-) ;-)

I find the data sheet rather confusing, but I will take a closer look.  I do
know however that it only tells how to do it in assembly.

>Are you talking about "functions" as they are used
>in C ? Or any other definition of "function" ?
>A PIC by itself knows nothing about "functions".
>
>And what specific "timing and operations" are you refering to ?

Since C has to "jump" from the main loop to the function, I was wondering
how that effects the number of operations executed on the PIC.

>It must be hard to both have a new language to learn,
>and at the sam time doing it on an unknown platform !!
>Was there any specific reason you selected to jump
>directly to a HLL such as C ?

Since I'm learning C++ anyway, why not learn it on a PIC?  Two birds with
one stone.  I find programming languages rather easy to learn, and since I
know other languages, it comes even easier (just new syntax).  Verbal
languages on the other hand... That's tough.

I have considered learning in ASM, but I may be programming on a variety of
devices, and I'd rather not learn each one's individual instruction set
(even though they do share many instructions).  C is like the universal
language for all of the ;-)  I also decided to pick a HLL because it often
takes hundreds of lines of code and compresses it into nice little portions
of code.  I find ASM a bit confusing, seeing as I'm used to seeing one line
variable definitions, now has turned into "movlw  0x0FD; movwf  PORTB".
Loops and goto's are a bit weird too.

But, I guess the real reason I'm not learning asm is that I'm LAZY. Lol. HLL
are a lot simpler.


{Original Message removed}

2004\08\19@040731 by Jan-Erik Soderholm

face picon face
Zack wrote :

> >If you could be a bit more specific about what's unclear in the
> >data sheet (yes, there definitly can be unclear parts in them),
> >it would be easier to help/answer. And do not ask if you havn't
> >even *read* the data sheet ! It shows to easily ;-) ;-)
>
> I find the data sheet rather confusing,...

Again, specifics please ! So we can help/explain.
Exactly *what* is confusing ???
Data sheet file name, page, section and the confusing wording.

> but I will take a closer look.

Perfect ! And *then* ask the 2000+ list members... :-)

> I do know however that it only tells how to
> do it in assembly.

So your C compiler comes without any docs at all, right ?
Or maybe those docs are also "confusing" ?

> Since C has to "jump" from the main loop to the function, I
> was wondering how that effects the number of operations
> executed on the PIC.

That is highly depending on how the actual compiler was designed.
Most (all ?) C compilers can produce a list of the actual
assembly code generated, and reading that listing should
tell you a lot about how the C compiler works "under the hood".

> Since I'm learning C++ anyway, why not learn it on a PIC?

Is there a C'++ compiler for PIC's ? I didn't know that...

> Two birds with one stone.  I find programming languages
> rather easy to learn, and since I know other languages,
> it comes even easier (just new syntax).

I think you are missing one important thing. This isn't
like when you know Visual Basic under Windows, and then
switch to using Visual C under Windows. Both languages
are using the same plattform, API and so on.

Now you are also switching to programming on the PIC
"platform", without (obviously) knowing how that platform
works.

> I have considered learning in ASM, but I may be programming
> on a variety of devices, and I'd rather not learn each one's
> individual instruction set (even though they do share many
> instructions)...

Well, there are actaly just two "sets" that you will possibly
see. The "midrange" instruction set on the 12F and 16F, and the
slightly different (mostly extended) set on the 18F line. The
*hardware* differences between the PIC models are far more
different !

> C is like the universal language for all of the ;-)

Maybe, but you are still missing the point.
You are trying to program PIC's without even trying/wanting
to understand how they works.

The general idea is to use ASM to learn the PIC architecture,
*then*, when you think you know that, switch to C (or whetever)
if that's what you'd like to use. Note that many long time PIC
programmers still stick to ASM. And also that by using the
assembler functionallity (preprocssing, macros and so on)
fully, you can get en environment with many HLL like
features. Olin Lathrops envionment is one example of that.

> I also decided to pick a HLL  because it often takes hundreds
> of lines of code and compresses it into nice little portions
> of code.  I find ASM a bit confusing, seeing as I'm used to
> seeing one line variable definitions, now has turned into
> "movlw  0x0FD;  movwf  PORTB".
> Loops and goto's are a bit weird too.

But then, how will you ever learn how the PIC's works ???

> But, I guess the real reason I'm not learning asm is that I'm
> LAZY. Lol. HLL are a lot simpler.

I'd suggest you'd browse the PIClist archives to get
an idea about what the list generaly thinks about
those to lazy to read the data sheets and asking "RTFM"
questions on-line instead... :-) :-)

Regards,
Jan-Erik.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2004\08\19@040939 by hael Rigby-Jones

picon face
>-----Original Message-----
>From: pic microcontroller discussion list
>[TakeThisOuTPICLISTEraseMEspamspam_OUTMITVMA.MIT.EDU] On Behalf Of Spehro Pefhany
>Sent: 18 August 2004 21:22
>To: RemoveMEPICLISTspamTakeThisOuTMITVMA.MIT.EDU
>Subject: Re: [PIC]: ADC based PWM
>
>    int frequency;               // the frequency in
>wombat-cycles per cow-acre

Sounds like the SI has been taken over by the European Union :-)

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2004\08\19@043055 by Alan B. Pearce

face picon face
>>If you could be a bit more specific about what's unclear in the
>>data sheet (yes, there definitly can be unclear parts in them),
>>it would be easier to help/answer. And do not ask if you havn't
>>even *read* the data sheet ! It shows to easily ;-) ;-)
>
>I find the data sheet rather confusing, but I will take a closer
>look. I do know however that it only tells how to do it in assembly.

Also look at the mid-range manual on the microchip website. You do not need
to download the whole manual, as it is also available in the individual
chapters, with each chapter covering one aspect of the peripherals, so you
only need the one covering the CCP module. These chapters broaden the
information in the data sheet.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2004\08\19@081242 by Olin Lathrop

face picon face
Zack wrote:
> I find the data sheet rather confusing, but I will take a closer
> look.  I do know however that it only tells how to do it in assembly.

Actually it mostly talks about what the machine can do.  However, this is
necessary knowledge even if eventually using a high level language for some
tasks.  On such small resource-limited systems, you need to be aware of what
the hardware is doing.

In any case, you should do the first few projects in assembly to get this
understanding.  Then you can use a compile later once you've "graduated".

> Since I'm learning C++ anyway, why not learn it on a PIC?

Lots of good reasons:

- The language has to be contorted a bit to work on a tiny machine like a
PIC.  It was really designed for large von Neuman machines, which the PIC
certainly isn't.  The C++ you'll be learning on a PIC won't be the same as
you'd use on a Pentium.

- Unlike on a large machine with an operating system, you have to be aware
what the hardware is doing and how it works on a PIC.

- Some high level language constructs (like objects and classes for example)
are useful in decreasing human error, especially when multiple people write
code for a large project.  However, they cost cycles and memory which can be
in short supply on a PIC.

> I find programming languages rather easy to learn,
> and since I know other languages, it comes even easier (just new
> syntax).

So go learn C++ on a machine it was intended for with a compiler that
millions have used before.  If you want to learn PICs, you have to get
intimate with the details.  If you don't like doing that, then
microcontrollers aren't for you and you should quit now.

{Quote hidden}

You may be a fine HLL programmer on a large machine, but this is exactly the
wrong attitude for programming microcontrollers.  Seriously, these things
aren't for you, at least not with your current mindset.

You'll also find that people are much less willing to give you an answer, as
opposed to explaining how it works so that you can figure it out yourself.
Go away and and write code for large systems, or come back with a different
attitude.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

More... (looser matching)
- Last day of these posts
- In 2004 , 2005 only
- Today
- New search...