Searching \ for '[PIC] PWM ASM code' 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=pwm
Search entire site for: 'PWM ASM code'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] PWM ASM code'
2010\02\04@190033 by MCH

flavicon
face
Maybe I'm getting too specific here. Let's make the net larger.

Does anyone have an example of "working" ASM code that will configure
and output PWM signals?

All I can find on the net is BASIC or C language.

I found one great tutorial site that says it has a section on PWM, but
"PWM" is not mentioned once in their tutorial. :-\

I also tried looking for app notes on the Microchip website. So, if
anyone knows of a URL for that, it would greatly help as well.

Again, just looking for a 'how to' with code examples of ASM code for
the PWM output. It doesn't matter what is being controlled with the output.

I'm using an 18F4520, but any ASM code would be appreciated, as I'm sure
I could translate it.

Thanks,
Joe M.

2010\02\04@192140 by Jan-Erik Soderholm

face picon face
MCH wrote:
> Maybe I'm getting too specific here. Let's make the net larger.
>
> Does anyone have an example of "working" ASM code that will configure
> and output PWM signals?
>

Yes, the datasheet for the specific part. There usualy is a
1-2-3 guide on howe to configure the PWM module. You *are*
talkig about the *hardware* PWM module, right ?


> All I can find on the net is BASIC or C language.
>

Since it's mainly a questions about setting up a few registers
correctly, it would/should be easy to do the same thing as the
BASIC or C examples do, but in ASM instead.

{Quote hidden}

OK, then. Chapter 15.4.4 "SETUP FOR PWM OPERATION" on page 145.
It's 5 steps, 1 to 5. It's not actual code, but it says which
register to set to what. If you can't translate that into ASM,
well, then you have other issues to fix first.

> but any ASM code would be appreciated, as I'm sure I could translate it.
>
>

If you want ASM code, why "translate" ASM code that you get ?


> Thanks, Joe M.

2010\02\04@195415 by Marcel Duchamp

picon face
On 2/4/2010 4:00 PM, MCH wrote:
>
> Does anyone have an example of "working" ASM code that will configure
> and output PWM signals?
>
>
> Thanks,
> Joe M.

See Scott Dattalo's excellent explanations:

http://www.dattalo.com/technical/software/pic/pwm256.txt

http://www.dattalo.com/technical/software/pic/pwm.txt

www.dattalo.com/technical/theory/pwm.html

2010\02\04@204151 by ivp

face picon face
part 1 2387 bytes content-type:text/plain; format=flowed; charset="iso-8859-1"; (decoded 7bit)

Joe,

Try a Microchip search for PWM in the application notes

http://search.microchip.com/searchapp/searchhome.aspx?q=pwm&resperpage=10&id=26

For example

http://ww1.microchip.com/downloads/en/AppNotes/00564b.pdf

and (not personally viewed)

http://ww1.microchip.com/downloads/en/AppNotes/01050a.pdf

http://ww1.microchip.com/downloads/en/AppNotes/00654a.pdf

http://ww1.microchip.com/downloads/en/AppNotes/00539c.pdf

This code does do what is intended at the PIClist link. I've included some
of the original text. Attached is what comes out of C2 with various values
for duty (500, 100 and 900) and either /4 or /16 pre-scaler on TMR2

Note that the first crystal I grabbed was a 4.096MHz, so the frequency is
slightly more than 1000Hz (ie 1000 * 4.096/4.000 or 0.976ms period). I
used a 452, which works just like the 4520

wbr

===========================

Definition -

duty = d500
d_lo = low(duty) & b'00000011'
d_hi = high(duty << 6)

;d_lo  = two lowest bits need for the duty cycle 'on' time
;d_hi  = highest 8 bits. It's left-shifted 6 times to get it into the duty
;high byte at assembly time

;If duty = 500 => 0111 1101 00
;This needs splitting into 0111 1101 and 00, not 01 and 1111 0100,
;which would happen if you used high(duty) and low(duty), ;therefore
; the left shifts

=======================

;Set the PWM period (1000Hz = 0.001s) by writing to the PR2 register.
;PWM Period = [PR2 + 1]*4*Tosc*TMR2 PreScale value
;Use 4 for TMR2 prescale. You can try other values but make sure it
;works with the duty cycle calcs as well.
;
;Tosc = 1/4000000 = 2.5*10-7
;Rearranging the equation gives...
;
;PR2 = (Period/(4 * Tosc * TMR2 Prescale)) - 1
;Therefore...
;
;PR2 = (0.001/(4 * 2.5 * 10-7 * 4)) - 1 = 249

        movlw   .249            ;TMR2 period register
        movwf   pr2

        movlw   d_hi            ;set duty cycle bits 9-2
        movwf   ccpr1l

        movlw   d_lo            ;put duty cycle bits 1-0
        swapf   wreg
        iorwf   ccp1con         ;into bits 5-4 of CCP1CON

        movlw   b'00000101'
;                       1        ;TMR2 on
;                        01      ;pre-scaler = 1:4
        movwf   t2con

        movf    ccp1con,w
        andlw   b'00110000'
        iorlw   b'00001111'     ;enable PWM mode
        movwf   ccp1con

here     bra      here


part 2 5997 bytes content-type:image/gif; (decode)


part 3 35 bytes content-type:text/plain; charset="us-ascii"
(decoded 7bit)

2010\02\04@205502 by ivp

face picon face
Just one little point

        movlw   d_lo            ;put duty cycle bits 1-0
        swapf   wreg
        iorwf   ccp1con         ;into bits 5-4 of CCP1CON

will not put the bits into 'fresh' locations

        movlw b'11001111'
        andwf ccp1con

clears ccp1con <5:4> before including the duty cycle bits as above

wbr

2010\02\04@205610 by Richard Seriani

picon face

----- Original Message -----
From: "MCH" <spam_OUTmchTakeThisOuTspamnb.net>
To: "Microcontroller discussion list - Public." <.....piclistKILLspamspam@spam@mit.edu>
Sent: Thursday, February 04, 2010 7:00 PM
Subject: [PIC] PWM ASM code


{Quote hidden}

" I also tried looking for app notes on the Microchip website. So, if
anyone knows of a URL for that, it would greatly help as well."

http://www.microchip.com

Go to the search box in the upper right of the page and type the letters PWM
and press Enter. It is about that easy! Sure, you'll have to do a little
reading, but just on the first page I saw three application notes on PWM.

Others have already suggested your device datasheet. Great resource.

You can also try the microcip forum (access from home page). Look for PWM in
the list of subjects under the headings, then click on it. Lots and lots of
posts, some with everything you want to copy.

If all you want is simple PWM:
Set pin HI
Wait a little bit A
Set pin LO
Wait a little bit B
Repeat

Set the "little bit" A and B to the values you want for whatever PWM signal
you are trying to achieve.
I don't really know what your goals are. If you just want to play with PWM
and if you really can translate any asm code to the 18F asm code, you should
be able to write this simple asm program to achieve that. Later, you can
learn to use the built-in capabilities of your chip to generate PWM more
elegantly (and without tying up the chip for one function).

Have fun.

Richard




2010\02\04@224338 by MCH

flavicon
face
I ended up doing just that.

What I was looking for was something on the lines of:

----------------------------------
(make sure to TRIS port C,2 as an output)
;------------------------------ SETUP PWM --------------------

       MOVLW        B'11111111'                ;SET PERIOD OF PWM
       MOVWF        PR2

       MOVLW        B'11111111'                ;SET DUTY CYCLE (127 DEFAULT)
       MOVWF        CCPR1L

       MOVLW        B'00001111'                ;SET OTHER CONFIG OF PWM
       MOVWF        CCP1CON

       MOVLW        B'00000100'                
       MOVWF        T2CON                        ;WRITE TO T2CON
----------------------------------

Yes, I know my 'default' isn't the same as this setting. While my
default dim setting is 127, I started at 255 to start the backlight off
then 'ramp it up' while the boot sequence is underway. Yes, I like
little tricks like that. :-)

But, my backlight dim function is working perfect now.

I did take a shortcut which I will detail in my next email (provided you
didn't see it above)

Joe M.


Jan-Erik Soderholm wrote:
{Quote hidden}

2010\02\04@225256 by MCH

flavicon
face
I "cheated" since I don't need the resolution of the two LSBs. I just
set them both to 0 and don't write them. That greatly simplifies my
code, as I only need to set CCPR1L.

I have a routine for the user to set the value of CCPR1L, and all is
good. I go from full off (actually, I set the lower limit to '5' to
prevent a full-off backlight) to full on, and 250 steps between.

I did have a minor 'trip-up' since PWM1 is port C,2 and PWM2 is C,1.
But, I quickly found that.

I also had to 'invert' my settings since I wanted 0 to be full off and
255 full on. My interface inverts the on/off state. Again, minor issue.

I was thinking/making PWM much more difficult than it is. But, when you
don't know squat about it, even the simple things help.

Now, on to the fan part of the code! I already have a step up on that,
but my PWM output may make the counting a challenge if the timers are
interdependent.

Joe M.

ivp wrote:
{Quote hidden}

2010\02\04@231727 by ivp

face picon face
> I have a routine for the user to set the value of CCPR1L

Another little exercise would be to set the brightness with an LDR
connected to an ADC input

> I was thinking/making PWM much more difficult than it is. But,
> when you don't know squat about it, even the simple things help

I know, they really do. It's so much easier when you have something
that works which you can experiment with

> but my PWM output may make the counting a challenge if the timers
> are interdependent

Wouldn't be a problem in the h/w as all timers are distinct. *your*
management of the events is something else. The art is knowing exactly
what will and could happen. For example, what priority do I put on
various events ? Can this one wait ? Do I need to change the duty cycle
urgently ?

wbr

2010\02\05@001710 by MCH

flavicon
face
The first reply you gave was great info, but way more than I needed. For
example, it was figuring out the exact period in microseconds (or some
higher math such as that). I really don't care what the period or duty
cycle is for my purposes, as it's just cycling an LED. My duty cycle is
programmable from (5) to 255 (well, ignoring the two LSBs).

On the other hand, I don't think that page made mention of what
registers to set and this and that for the basic PWM setup.

As another example, I don't need to worry about the prescaler or
postscaler. Maybe I will for the counter, but I think changing those
will not break my PWM code if I need to use them for the counter (and
perhaps a prescaler of 2 or 4 might be useful for counting the fan
pulses correctly).

Regardless, the PWM matter is water under the bridge now. But, I thank
everyone for the replies.

Joe M.

ivp wrote:
>
>> I was thinking/making PWM much more difficult than it is. But,
>> when you don't know squat about it, even the simple things help
>
> I know, they really do. It's so much easier when you have something
> that works which you can experiment with

2010\02\05@013122 by ivp

face picon face
> The first reply you gave was great info, but way more than I needed.
> For example, it was figuring out the exact period in microseconds (or
> some higher math such as that)

ISTR that was for measuring the fan speed, not PWM

> I really don't care what the period or duty cycle is for my purposes, as
> it's just cycling an LED

Those two terms and their relationship are intrinsic to PWM operation
so of course it is an advantage if you do care

> On the other hand, I don't think that page made mention of what
> registers to set and this and that for the basic PWM setup.

Well, I pretty much typed it verbatim into asm. Compare my code and
the page with what you know now

wbr

2010\02\05@115547 by MCH

flavicon
face
Are we talking about the same page? I'm talking about:
<http://www.piclist.com/techref/microchip/16F877/pwm.htm>

BTW, now that I understand the basics, that page is sinking in much better.

Joe M.

ivp wrote:
>> The first reply you gave was great info, but way more than I needed.
>> For example, it was figuring out the exact period in microseconds (or
>> some higher math such as that)
>
> ISTR that was for measuring the fan speed, not PWM

2010\02\05@150627 by ivp

face picon face

> Are we talking about the same page? I'm talking about:
> <http://www.piclist.com/techref/microchip/16F877/pwm.htm>

Oh, yes, I get you now. I think the reason it was done that way was
because Tony, as an example, started with a particular frequency and
duty cycle. Setting up any PWM will involve some calculations. There
are some times for instance when you might want to stay away from
frequencies in the audio range, which could be annoying, so you'd need
to work out timer loadings and processor speed

wbr

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