Project
Listing 4. I’m sure there are some really weird analog things that you can do
with a 555, but the PIC10F206 can hold its own against the 555 when it comes
to handling and generating pulses.
F
o
r
E
l
e
c
t
r
o
n
i
c
s
NUTS & VOLTS
E
v
e
r
y
t
h
i
n
g
//*********************************************************************
//* HI-TECH C SOURCE CODE FOR PULSE GENERATOR MODULE
//*********************************************************************
void main()
{
TRIS = 0b00001000;
FOSC4 = 0;
CMCON = 0b11110111;
OPTION = 0b11000101;
//GP3 input : all others output
//GP2 is an I/O pin
//comparator off:pullups off:wakeup off
//prescaler assigned to TMR0 @ 1:64
while(1)
{
//loop forever
GP2 = 1; //GP2 = HIGH
TMR0 = 0x80; //load TMR0
while(TMR0 & 0x80);//count from 0b10000000 to 0b00000000
GP2 = 0; //GP2 = LOW
TMR0 = 0x80; //load TMR0
while(TMR0 & 0x80);//count from 0x80 to 0x00
}//while(1)
}//main
//*********************************************************************
//* HI-TECH C SOURCE CODE FOR LED DIMMER MODULE
//*********************************************************************
void main()
{
unsigned char x,y,z;
TRIS = 0b00001000;
FOSC4 = 0;
CMCON = 0b11110111;
OPTION = 0b11000010;
y = 0x04;
z = 0x80;
while(1)
{
do
{
x=0xFF;
do
{
GP2 = 1;
TMR0 = y;
while(TMR0 & y);
GP2 = 0;
TMR0 = z;
while(TMR0 & z);
}while(--x);
y *= 2;
z /= 2;
}while(y);
y = 0x04;
z = 0x80;
}//while(1)
}//main
//GP3 input : all others output
//GP2 is an I/O pin
//comparator off:pullups off:wakeup off
//prescaler assigned to TMR0 @ 1:8
//initialize y
//initialize z
//loop forever
//outer do loop
//inner do loop
//GP2 = HIGH
//load TMR0
//count for y time
//GP2 = LOW
//load TMR0
//count for z time
//do inner loop until x is decremented to 0
//multiply y x 2
//divide z by 2
//do outer loop until y = 0
//reinitialize y
//reinitialize z
(continued)
44
It stands to reason that, if we can
control the frequency of a pulse train
generated by TMR0, we can also control the
duty cycle of that pulse train. A % 50 duty
cycle means that every cycle has equal
high and low logic levels with respect to
time. If we apply that voltage to an LED, it
will be on for half the time and off for half
the time. If the frequency is high enough,
the LED may appear to be dimmer than it
would seem to be when full voltage is
applied to it. If we switch between on and
off fast enough, our eyes and mind will fool
us into thinking that the LED is really never
turning off. We can use this phenomenon
to our advantage with the second code
listing you see in Listing 4.
Let’s work our way through the LED
dimmer code inside out, beginning with the
inner do loop. The GP2 = 1 code is exactly
the same as our 60 Hz pulse train generator
except that the 0x80 hard-coded value is
replaced with a variable of y. The y variable
is initialized to a value of 0x04 in the beginning of the code sequence. Recall that the
TMR0 register increments every instruction
cycle. So, the y count begins at
0b00000100 and ends at 0b00001000
when the TMR0 register rolls over to
0b00001000 from 0b00000111.
Looking at the GP2 = 0 code, it is exactly
the same as our previous 60 Hz code
for GP2 in a low logic state with the only
exception being the variable z holding the
0x80 value, which was loaded right after the y
value. What all of this means is that, initially,
the high part of the cycle is much smaller
than the low part of the cycle with respect to
time, which, in turn, says that the LED will
initially be off longer than it is on and will
appear to be very dim. Each duty cycle
period is alive as long as x is not equal to
zero. The while(—x) decrements x with each
pass through the inner do loop. The outer do
loop initializes x, doubles y, and halves z at
the completion of each pulse train cycle.
Since y is the variable that determines
the high level time of each cycle and z is the
variable that determines the low level time
of each cycle and the values are approaching each other from the opposite directions,
when y is equal to 0b10000000, z will be
equal to 0b00000010. At this point, the
LED will be at its brightest, since the high
part of the cycle (y) will be much longer
than the low part of the cycle (z). The
JANUARY 2005