Exact match. Not showing close matches.
'[PIC] PWM ASM code'
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.
> 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.
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.
part 1 2387 bytes content-type:text/plain; format=flowed; charset="iso-8859-1"; (decoded 7bit)
Try a Microchip search for PWM in the application notes
and (not personally viewed)
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
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
;PR2 = (0.001/(4 * 2.5 * 10-7 * 4)) - 1 = 249
movlw .249 ;TMR2 period register
movlw d_hi ;set duty cycle bits 9-2
movlw d_lo ;put duty cycle bits 1-0
iorwf ccp1con ;into bits 5-4 of CCP1CON
; 1 ;TMR2 on
; 01 ;pre-scaler = 1:4
iorlw b'00001111' ;enable PWM mode
here bra here
part 2 5997 bytes content-type:image/gif; (decode)
part 3 35 bytes content-type:text/plain; charset="us-ascii"
Just one little point
movlw d_lo ;put duty cycle bits 1-0
iorwf ccp1con ;into bits 5-4 of CCP1CON
will not put the bits into 'fresh' locations
clears ccp1con <5:4> before including the duty cycle bits as above
----- Original Message -----
From: "MCH" <nb.net> mch
To: "Microcontroller discussion list - Public." <mit.edu> piclist
Sent: Thursday, February 04, 2010 7:00 PM
Subject: [PIC] PWM ASM code
" 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."
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
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).
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
MOVLW B'11111111' ;SET DUTY CYCLE (127 DEFAULT)
MOVLW B'00001111' ;SET OTHER CONFIG OF PWM
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)
Jan-Erik Soderholm wrote:
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
> 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
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
Regardless, the PWM matter is water under the bridge now. But, I thank
everyone for the replies.
>> 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
> 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
Are we talking about the same page? I'm talking about:
BTW, now that I understand the basics, that page is sinking in much better.
>> 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
> Are we talking about the same page? I'm talking about:
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
More... (looser matching)
- Last day of these posts
- In 2010
, 2011 only
- New search...