PDA

View Full Version : My first foray into PWM



Demon
- 16th December 2014, 21:05
Before I get too deep in this, can I blink an LED ON for 500uS, OFF for 500uS using standard PWM module?

Or am I trying to use a Ferrari as a baby stroller?

I know I can use DT_INT but I know how to do that and want to avoid using cycles. The PWM hardware is there doing nothing and I was hoping I could use it as a heartbeat during my tests.

I've printed pages 186-189 of the 18F44K22 datasheet and it doesn't seem overwhelming at first glance. The only part I can't determine by reading that is if I can use it as slow as I want. I know this is meant to drive motors so it's entirely possible that it can't physically go this slow.

Robert


EDIT: I'm running at 64MHz so I can guarantee persistence of vision while blinking at least 50 LEDs, manage a keypad and handle USART comms (at the very least). But I'm getting a feeling by looking at that PWM PERIOD formula at 14-1 that I could only do this if I slow to a crawl.

richard
- 16th December 2014, 21:43
if you are using the hpwm command the max freq is 32767hz the min @64mhz ck is 3905 hz on a pic18
max period 256uS min 30.5uS
so you could at 50% duty get 128uS on 128uS off

Demon
- 16th December 2014, 23:56
Yes, I definitely want the hardware PWM. But it's not the 50% duty that I was concerned with, it was the 1 second period.

I see now the relation between clock speed and the PWM output. So there's no way I can get it slow enough to use it as a heartbeat (1 sec period) in the background if we limitted with a 4MHz lower range.

I was hoping to use it as a debugging tool; to show that a PIC is indeed working without taking any clock cycles (disturbing cycle sensitive logic). And I wanted to try out HPWM, I'm nowhere near ready to get back on my CNC project; have to complete my residential alarm system first (and I'd like to get that interfacing with android, text messenging and web page - lots of learning).

Robert

Amoque
- 17th December 2014, 01:37
You might, if you have one laying around, use a clock divider chip... or a counter... Here's (http://www.resistors-and-diodes-and-picchips-oh-my.co.uk/?p=779)a link that explains my thinking better than I can.

richard
- 17th December 2014, 02:28
You might be able to achieve something using compare mode and the external clock pin for timer1. if you have enough spare pins and if you were really clever
use the hpwm output to drive tmr1ck then you could change the flash rate on the fly

Demon
- 17th December 2014, 03:55
Euh, I think I'll just get back to finishing my Charlie-plexing work for now. LOL

Thanks though, I will have to jump into PWM one day, hopefully next summer.

Robert

HenrikOlsson
- 17th December 2014, 06:20
Hi Robert,
In your first post you say LED ON for 500uS, OFF for 500uS but then you say you want a period of 1s - that's a difference of $03E8 times.
I know you didn't want to do it with an normal interrupts but I'm going to suggest a simple timer interrupt anyway.... At such low frequencies (even though you're probably not going to get 500ms interrupt period in "on go") the interrupt processing will have very little impact on the overall performance.

/Henrik.

gadelhas
- 17th December 2014, 13:26
Hi Robert;

An HeartBeat Led should be do it with a timer interrupt like Henrik sujested....
If you cannot achieve tht time that you want, you do for instance, a 100ms interrupt and with a counter you can achieve the time that you want counting in 100ms spaces.
You can count 5 x 100ms and then toogle the led.

Demon
- 17th December 2014, 15:56
Hi Robert,
In your first post you say LED ON for 500uS, OFF for 500uS but then you say you want a period of 1s - that's a difference of $03E8 times.
...


(without considering oscillator speed and PWM range limitations)

Then there's something about PWM that I didn't understand. HIGH/LOW using PAUSE for 500uS is not the same as a 1000uS period with 50% duty cycle?

7531
Ooop, uS, not Us.

Robert

gadelhas
- 17th December 2014, 17:23
(without considering oscillator speed and PWM range limitations)

Then there's something about PWM that I didn't understand. HIGH/LOW using PAUSE for 500uS is not the same as a 1000uS period with 50% duty cycle?

7531
Ooop, uS, not Us.

Robert

Yes, from that point of view, it is the same thing! (without considering oscillator speed and PWM range limitations)

HenrikOlsson
- 17th December 2014, 20:13
Then there's something about PWM that I didn't understand. HIGH/LOW using PAUSE for 500uS is not the same as a 1000uS period with 50% duty cycle?
Yes of course, 500us ON and 500us OFF is 50% dutycyle. But the period is 500us+500us = 1000us = 1ms = 0.001s - NOT 1s which you later wrote.

In your first post you indicated you wanted a PWM period of 1000us (a frequency of 1000Hz). In your second post you said you wanted a period of 1s (a frequency of 1Hz). It doesn't add up - that's all I was trying to say.

/Henrik.

Demon
- 17th December 2014, 21:29
Ooops, my bad. Didn't explain myself properly.

richard
- 18th December 2014, 03:27
how to flash a pulse led @ 1 hz on a pic18 @64mhz with no code overhead and no interrupts

for demon

'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/11/2014 *
'* Version : 1.0 *
'* Notes : pic18f45k20 *
'* : *
'************************************************* ***************
#CONFIG
CONFIG FOSC = INTIO67
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRT = OFF
CONFIG BOREN = SBORDIS
CONFIG BORV = 18
CONFIG WDTEN = ON
CONFIG WDTPS = 512
CONFIG CCP2MX = PORTC
CONFIG PBADEN = OFF
CONFIG LPT1OSC = OFF
CONFIG HFOFST = ON
CONFIG MCLRE = ON
CONFIG STVREN = ON
CONFIG LVP = OFF
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG

define OSC 64

osccon=$70 '4 mhz

trisc=%11111001

trisd.1=0 ' another led
T2CON = $4; // tmr2 prescale 0 and on
PR2 = 255; // pwm period ?
CCP1CON = $c; // pwm mode
CCPR1L = 127; // pw
t1con=$43
CCP2CON = 2
t3con=3
OSCTUNE.6=1

; ccp1 out connects to t1ck in and pulse led connected to ccp2 out
; on pic18f45k20 ccp1 out = portc.2
; ccp2 out = portc.1
; ti ck in = portc.0








Main:
latd.1= !latd.1 ;flash another led
pause 500
goto main

Demon
- 18th December 2014, 04:58
Neat, first I have to get my Charlie-plex going again. I broke it. LOL

Robert