‘——— 683PWMLED.bas ———
‘ PWM LED Dimmer
‘ Jurgen G Schmidt
‘ Target: PIC12F683
‘ Compiler: PICBasicPro 2.50b
‘ IDE: MicroCode Studio
‘—- internal clock
‘—- disable MCLR
@ DEVICE INTRC_OSC_NOCLKOUT, MCLR_OFF
DEFINE OSC 8
OSCCON = $70 ‘8mhz for internal
ANSEL = 0 ‘All Digital
CMCON0 = 7 ‘Comparators off
‘——— Port Assignments & Variables
pinHi var GPIO.0 ‘Brighter
pinLo var GPIO.5 ‘Dimmer
duty var BYTE
‘——— Initialize System
read 0, duty ‘read EEPROM
MainLoop:
up with some improvements. The original program saved
the speed or brightness setting in EEPROM so that when I
turned the fan or light back on, it would start at the same
level. The problem with fans is that you can slow them
down to a 10% level when they are running, but you can’t
Heartbeat LED
When I first started working with microcontrollers, I would
frequently chase software bugs that were in reality hardware
issues. Now when I prototype, I ALWAYS include a heartbeat
LED on the breadboard. If I’m working with a development
board — which usually has one or more LEDs — I make one
of them the heartbeat. I never take it out until I’m finished.
Blinking an LED at the beginning of an embedded program
(and throughout) verifies that your system is alive. The code
is very simple; just a few lines to turn the LED on and off.
Get this working first and then later on if the LED is not
blinking, there is probably something wrong other than your
code. Once I implemented the heartbeat LED consistently,
I’ve saved myself considerable aggravation. Where initially I
suspected my code, I discovered that batteries had depleted,
programming or prototyping wires had come loose, a critical
component had been harvested from a breadboard for use
elsewhere, and so on. These are simple, silly things that are
easily fixed, but if overlooked can make you doubt your
sanity. If the LED does not blink on power-up, I check the
hardware and environment before I mistrust my code.
Using a heartbeat LED and writing the universal
microcontroller equivalent of “Hello world” is also useful
when starting with new hardware. It verifies that you have a
viable configuration. It can also provide feedback on the
correct oscillator or crystal settings.
Once I have the heartbeat, I add the serial output routines
for more detailed diagnostics. Then it’s on to the rest of the
project. Once you have the heartbeat and serial routines
under control, you have a good framework for proceeding.
hpwm 1,Duty,24000 ‘GPIO.2 at pin 5
pause 200
if pinHi = 0 then gosub Brighter
if pinLo = 0 then gosub Dimmer
goto MainLoop
end
‘————————————————
Brighter:
if Duty => 255 then return
Duty = Duty + 15
write 0, duty ‘save to EEPROM
return
‘————————————————-
Dimmer:
if Duty = 0 then return
Duty = Duty - 15
write 0, duty ‘save to EEPROM
return
‘—————————————————
‘——— end of 683PWMLED.bas ———
FIGURE 6. PICBASIC PRO listing.
start them at that level. I added a feature that would boost
the initial fan speed if it was below a certain level to get it
started, and then drop back down to the slower saved
speed. This resulted in two versions of the program: one for
lights that did not have the “boost” feature; and one for
Terminal Programs
For debugging, I connect my PIC projects directly to a terminal
program running on my development PC. I have not had any
trouble connecting an output pin from my microcontrollers
directly to a PC serial port, even at 3. 3 volts. I just have a
ground wire to pin 5 and another from the serial output pin to
pin 2 on the DB- 9 connector. With the disappearance of
physical serial ports on PCs, I’m usually connecting to a serial-to-USB cable, such as shown in Figure 5.
Microsoft Vista and Windows 7 do not include a serial terminal
program. If you are still using Windows XP — as I am for my
development systems — then you can use Hyperterm. I find it a
nuisance to work with and have found some free alternatives
that work well on all systems. Aside from the simplicity of use,
some also support TCP and UDP which makes them handy for
testing TCP/IP communications programs. My favorites are:
The Hercules Setup utility from HW Group (
www.hw-group.com/products/hercules/index_en.html).
Tera Term Pro is available from www.ayera.com/teraterm/.
NetBurner has utilities for monitoring and debugging
communications. These are available from www.netburner
.com/support/ public_downloads.html. The serial terminal
program is mtty.exe.
None of these programs require installation — they run straight
from the exe so they are easy to carry around and use. Some
of them (Hercules and Tera Term) do not list the available COM
ports; that is, you have to know ahead of time which COM
port you want to use.
SERVO 06.2010 57