PDA

View Full Version : Using HPWM to blink an LED?



Christopher4187
- 1st August 2006, 11:24
Is there a way to use the HPWM to blink an LED? When I read the manual, it seems like you can only use high frequencies and the LED only appears to be bright or dim. I know you can connect external hardware, I guess like a shift register to do it but I'd rather just use the output of the PIC if possible.

Thanks,

Chris

Dave
- 1st August 2006, 11:53
Christopher4187, There is no way to change the hardware divide string to make it any longer. You can only set the prescaler to it's maximum value of 16X and set the PR2 register for a maximum of 255. This will give you a minimum frequency of about 1.22 khz. at a 20 Mhz clock.

Dave Purola,
N8NTA

Acetronics2
- 1st August 2006, 12:18
Christopher,

IF you'd wanted to use HPWM ... there's a Timer1 unused aboard your chip ... YESS !

So, running TMR1 as a free timer and toggling the led at each TMR1 overflow interrupt ... will do the job. A basic interrupt should be enough ...

Without any "external" counter, you should get a ~ 1 sec maximum period @ 4 Mhz clock ...

Alain

PS: It's a bit like giving marmelade to a pig ... but, if unused ...

mister_e
- 1st August 2006, 16:06
Yup TIMER interrupt is the way to go.

Christopher4187
- 4th August 2006, 23:41
I'm a bit confused about using the timer1. I have read Darrel's posts on his timers and trying to follow the code myself is very confusing. Is there a step by step explanation on how to do it. I didn't see anything in the manual and if there was I didn't see it.

Thanks,

Chris

Darrel Taylor
- 5th August 2006, 02:14
Chris,

You've read this post?
http://www.picbasic.co.uk/forum/showthread.php?p=23259

What part don't you understand?

How the Timer works?
How the Interrupts work?
How the program works?
<br>

Christopher4187
- 5th August 2006, 13:57
Darrel,

Yes, I have read that post. I changed the prescaler and it does work but it's not what I am looking for because it only made the LED blink faster, not slower. I guess I was trying to solve my problem more simply. When using the HPWM command, the code is one line and as you know looks like this:

hpwm 1, 1000,1000

but when using the hpwm command, the control of the LED is more of a dimming function because it's just too fast for the naked eye see.

In my current program within the main loop, I am using a pause 1 command and counting to 1000 and then toggling the LED. I guess I was just hoping that there was a 1 line command when you can say "I want to blink the LED every 2 seconds" or something like that.

Chris

Darrel Taylor
- 6th August 2006, 02:31
Oh, I see. Sorry about that Chris, with the talk of HPWM and duty-cycle, I thought you were going for the Fader. It helps to know as many details as possible at the beginning.

I was just hoping that there was a 1 line command when you can say "I want to blink the LED every 2 seconds" or something like that.

How's this look?
<font color="#0000FF"><b><i>; Initialise your Hardware first

</i></b></font><b>LED </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<b>2
ONstate </b><font color="#008000"><b>CON </b></font><b>1 </b><font color="#0000FF"><b><i>; 0=ON when LOW, 1=ON when HIGH(default)

</i></b></font><font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;SingleLEDblinker.bas&quot;

</font><font color="#000080">@ Blink 10, 190 </font><font color="#0000FF"><b><i>; Blink 1/10 sec every 2 seconds

</i></b></font><b>Main</b>:
<font color="#008000"><b>PAUSE </b></font><b>1000
</b><font color="#008000"><b>GOTO </b></font><b>Main</b>

It uses DT_INTS-18 to get interrupts from timer1 every 10 ms, or 100hz.
But you can just change it to DT_INTS-14 if using a 16F.

It simply turns the LED ON, OFF for a number of periods specified by the constants provided.

@ Blink 10, 190

Will turn the LED ON for 10/100th's sec (0.1 sec). and OFF for 190/100th's (1.9 sec). The total of the two, determines the length of time between pulses (2.0 sec) and must be under 65535, which is 655 seconds.

Other included commands are ...

@ NoBlink ONtime, OFFtime

This is the same as Blink, except that it doesn't start Blinking. It simply defines the ON and OFF times in preperation for a ...

@ BlinkON

which can then be placed somewhere else in the program to actually Start the LED blinking.

Of course you need the ...

@ BlinkOFF

To round things out. And now it allows you to make a more custom blinking sequence.

For instance, this one will "FLASH" the LED every second, instead of just Blinking it. Giving more of an "ALERT!" type of indication.
<font color="#0000FF"><b><i>; Initialise your Hardware first

</i></b></font><b>LED </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<b>2
ONstate </b><font color="#008000"><b>CON </b></font><b>1 </b><font color="#0000FF"><b><i>; 0=ON when LOW, 1=ON when HIGH(default)

</i></b></font><font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;SingleLEDblinker.bas&quot;

</font><font color="#000080">@ NoBlink 3, 3 </font><font color="#0000FF"><b><i>; set Blink to 3/100 ON, 3/100 OFF
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>2000 </b><font color="#0000FF"><b><i>; just to verify it's not blinking

</i></b></font><b>Main</b>:
<font color="#000080">@ BlinkON </font><font color="#0000FF"><b><i>; Start Flashing LED
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>1000 </b><font color="#0000FF"><b><i>; continue for 1 second
</i></b></font><font color="#000080">@ BlinkOFF </font><font color="#0000FF"><b><i>; Stop Flashing LED
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>1000 </b><font color="#0000FF"><b><i>; for another second
</i></b></font><font color="#008000"><b>GOTO </b></font><b>Main
</b>

Or here's several different Blink sequences ...
<font color="#0000FF"><b><i>; Initialise your Hardware first

</i></b></font><b>LED </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<b>2
ONstate </b><font color="#008000"><b>CON </b></font><b>1 </b><font color="#0000FF"><b><i>; 0=ON when LOW, 1=ON when HIGH

</i></b></font><font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;SingleLEDblinker.bas&quot;


</font><b>Main</b>:
@ <b>Blink 2</b>, <b>10 </b><font color="#0000FF"><b><i>; .02 ON, .1 OFF
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>4000

</b>@ <b>Blink 10</b>, <b>10 </b><font color="#0000FF"><b><i>; .1 ON, .1 OFF
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>4000

</b>@ <b>Blink 20</b>, <b>10 </b><font color="#0000FF"><b><i>; .2 ON, .2 OFF
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>4000

</b>@ <b>Blink 50</b>, <b>50 </b><font color="#0000FF"><b><i>; .5 ON, .5 OFF
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>4000

</b>@ <b>Blink 50</b>, <b>10 </b><font color="#0000FF"><b><i>; .5 ON, .1 OFF
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>5000

</b>@ <b>BlinkOFF </b><font color="#0000FF"><b><i>; stop blinking
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>3000

</b>@ <b>BlinkON </b><font color="#0000FF"><b><i>; resume blinking at same rate
</i></b></font><font color="#008000"><b>PAUSE </b></font><b>5000
</b><font color="#008000"><b>GOTO </b></font><b>Main

</b>

Christopher4187
- 7th August 2006, 00:48
Darrel,

Wow, that's great! I didn't expect you to write it for me but I'm certainly not complaining. I'm still trying to learn more about PBP and this will help me a lot.

Thanks,

Chris