Академический Документы
Профессиональный Документы
Культура Документы
a)
6.1
Introduction
As you might know that a digital logic voltage is normally at 0 or 5 V if it is not at the
float (or high impedance) state. So an output bit can carry only the information of 1 or 0
that is obviously too limited. As you may want a signal to be more expressive, the method
of pulse width modulation may be useful to you. That is, apart from the voltage level, the
time duration of the signal is also used to carry information.
T =Period 1ms
Toff2
S1
Toff1
time
Ton1
S2
Ton2
Lets look at the above figure. The two pulse width modulated signals (S1 and S2) have
the same period of 1ms, thus their frequency is 1KHz. However, it can be seen that their
relative on-time (Ton) and off-time (Toff) are different. It is by changing the ratio of
Ton/Toff, we may be able to encode a range of different information using one single bit.
So how much information can be represented by one bit? Of course it depends on the
resolution of the Ton and Toff times. For example, if the resolution or quantized level is
1s in the above case, that means the minimum Ton is 1s and maximum is 999s (for
the period is 1000s), therefore the encoded information would have the range from 1 to
999.
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
6.2
(MEL)
DC Motor
Battery +
Figure 6. 2 The on/off ratio of the power source determines the motor speed
An important application is speed control for Direct Current (DC) motors. If we change
the ratio of value Ton/Toff, and use it to turn on and off a DC motor regularly, we control
different amount of energy transmitted to the motor. Hence alters the speed of rotation of
the motor. The mathematical prove and relation between the rotation speed and Ton/Toff
are left to be an exercise for our students. (Note that the relation is directly proportional
but not linearly.)
Another application is to control servo (positional) motors. This kind of pulse width
modulation control scheme is widely used in servomotors for remote-control model toys.
6.3
This chapter is organized as follows: We will first describe the theory of interrupt driven
software counter methods. Then we will discuss an experiment for the software counter
method. Finally, we will deal with the interrupt driven hardware counter method and
show you the design examples.
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
6.4
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
If we look at one period of the output out cycle, which is the same as one period of the
counting of count1, we see the followings.
Interrupt
occurrences
Time-axis
dt
Out
T=256/1024s
dt
Ton*dt(s)
Ton*dt(s)
main( )
{
}
If no interrupt
return occurs
again.
3rd interrupt
Will hang
Since the
stack will
overflow.
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
Exercise 6. 1: Write the algorithm to generate a pulse train of period 16ms, dt=1/64s and Ton ranges from 1* dt to
64 * dt. Hints: what is the required interrupt rate? What is the size (data width) of the software counter required?
We choose the motor control quantized level (throttle) to be 32, that means Ton can
range from 1 unit (minimum energy delivery) to 32 units (full throttle). Said another
way, we can have 32 different motor rotation speeds. However it is found that DC
motor control and pulse width Ton are not linearly related. That means, at a certain
Ton the motor starts to move very slowly, and the increase in speed is proportional to
the increase in Ton afterwards but not linearly. It is found by experimentation that a
reasonable result can be obtained if we choose a quantized level of 32 units. However,
the first 20 units may not have any meaning at all, because the motor may not start to
move till Ton reaches 20 units. And when it reaches 32 units (all time on) it is
moving at the fastest speed. These figures are all depend on the efficiency of the
particular DC motor used.
Throttle control
Figure 6. 6 Throttle control
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
Now it is time to put everything into perspective. If we want to control the pulse width at
Y, where Y is any number from 0 to 31 (32 quantized levels), we can do it by the
following method.
From the requirement (interrupt rate 1024Hz and quantized levels = 32), we need a 5 bit
software counter to keep track of the operation. At each interrupt the ISR executes, which
increments the software 5-bit counter, and if the counter is less than Y we set an digital
IO bit (through one of the IO pin of the 8255 chip on the 8031RL SBC) to 1, otherwise
reset it to 0. If we reset the counter when the count reaches 32, the pulse period is
(1/1024)*32 seconds = 31.25ms, so the frequency is 1/31.25 = 32Hz. Since the interrupt
rate is 1024Hz, so the smallest Ton time we can control is dt = 1/1024=0.977ms, which
corresponds to one quantized level.
T=1/32=31.25ms
Out
dt=1/1024s=0.977ms
Ton
(When
Count1<Y)
Toff
(When
Count1=>Y)
Figure 6. 7 Tthe DC pulse width modulation output (by interrupt, software counter)
For example, for Y=32, then the % of Ton against T period is 100%.
Another example is, for Y=25, then the % of Ton against T period is 78.1%.
In our experiment we found that the motor starts to move at Y=20 and increases gradually
till it reaches the maximum at Y=32.
The relation of the 8031, 8255, real-time clock and the motor are as follows.
The 8031 RL SB
8031
/int0
(pin12)
8255 output (pb0)
Already
connected in
8031RL SBC
Motor on/off
control circuit
Pin19 of
DS1287A
Real Time Clock
Interrupt rate=
1024 Hz
DC motor
Figure 6. 8 Connections of the experiment for PWM Type I: Interrupt driven, software
counter method
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
In this particular implementation (test61.c) , getch( ) is incorporated and one can use the
keyboard to control the motor speed. See the message on the screen when you are running
the program.
Exercise 6. 2 Change the interrupt rate to 512 Hz and the control quantized levels to 20.
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
_asm
pop ACC
_endasm;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//external _getchar function expected by serial.h used in Paulmon2
// #include <reg51.h> //needed, ACC is the accumulator
char getch()
{ char cin;
_asm
push ACC
_endasm;
_asm
lcall 0x0032 ;
_endasm;
cin=ACC;
_asm
pop ACC
_endasm;
return(cin);
}
main()
{
//SETUP INTERRUPT
IE=IE | 0x85;
//ie.7 global enable interrupt set;
//ie.2 external int. 1 set;
//ie.1external int 0 set;
//set interrupt pin int1 active low (pin13 of 8031)
TCON= TCON | 0x01;
// init 8255-A,
*p8255_e800_cnt=0x80;
*p8255_e000_cnt=0x80;
*p8255_e000_a=0xff;
*p8255_e800_a=0xff;
pattern=0x0f;
//read it once to make /irq(pin19 of ds1287) return to 1
foo=*rtc_ec0c_regc;
//interrupt-case-3 periodic interrupt
//rtc registers setting
//0x20 for slower, l.s.b.-4bit =rate
//0x20 for slower, l.s.b.-4bit =rate
//0x28=>256Hz; 0x29=>128Hz; 0x2f=>2Hz; 0x21=>256Hz;
//0x26=>1.024KHz; 0x25=>2.048KHz; 0x23=>8.192KHz
*rtc_ec0a_rega= 0x26;
*rtc_ec0b_regb= 0x40;
foo=*rtc_ec0c_regc; //make sure pin19 of ds21287 /irq=1
printf("'+' to increase speed, '-' to reduce speed, 'q' to quit");
putchar(0x0a);
putchar(0x0d);
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
6.5
PWM Typ2 II: Interrupt driven, hardware interval timer (8253) method:
Reason for using a timer chip instead of software counter method
It is found that in the above discussion that the interrupt rate for generating a PWM signal
of 32Hz of 32 control levels is 1024Hz. If we want to increase the PWM frequency or
control levels we have to increase the interrupt rate. In fact, 1024HZ is already a very
high interrupt rate for an 8031 processor, and increasing the interrupt rate may crash the
system because of stack overflow as described earlier. However, in practice we often
encounter requirement for high PWM frequency, such as the smooth control of DC
motors and the positional control of servomotors.
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
A Futaba servomotor is a positional control system that has a strong rotating shaft that
can rotate within a range up to 200 degrees. It is controlled through a PWM signal of
around 50Hz (period=20ms) that the rotational angle corresponds to the Ton of the PWM
signal. In particular, when the pulse Ton is at about 1ms, it swings to the left most
position, and when the Ton increases to about 2ms it is at the right most position. When
Ton is around 1.5ms, it is at the neutral position.
Ton=1~2ms
20ms
At 1.5ms
dt=1ms/256=3.9s
At
1ms
At 2ms
A servomotor
Figure 6. 10 The relation of the PWM signal and servo motor rotational positions
To produce such a signal that has a resolution of less than 1 rotational degree (say divide
the 200degree range into 256 divisions = 200/256 degrees) is not easy for an interrupt
based software counter method. The reason is, unlike the previous case of controlling a
DC motor that Ton will cover the whole period, it now only ranges from 1ms to 2ms, as
compared to the whole PWM pulse period of 20ms. That means, our algorithm should be
able to increase or decrease Ton by dt = 1ms/256=3.9s, and at the same time produces a
pulse period of 20ms. Since the smallest change of a quantized level (dt) of Ton is the
time between two successive interrupts, so the interrupt rate is 1/(1ms/256) = 256KHz!
Even a Pentium V processor may result in stack overflow because of that.
So what is the solution? One way is to use an 8253 (or 8254) programmable interval
timer device together with interrupt to achieve our goal.
10
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
Figure 6. 11 From [1] 8254.pfd (8254 is the superset of the 8253 timer)
Interfacing signals of the 8253 are very similar to that of an 8255: A0,A1 are for selecting
registers, /CS is for chip select and /WR, /RD are for read/write control. If necessary, one
can add this chip to the 8031 external-memory data space. What it requires is only 4
address locations and should be easy to accommodate in a typical 8031 system.
Each of the three independent counters has one clock input (cki), one gate input (gatei)
and one output (outi), for i=1,2,3 representing the three independent counters in a chip.
On programming, there is a 16-bit counter for each counter, and a globe mode control
word for the whole chip.
A1
Counter 0 data
Counter 1 data
Counter 2 data
Control register
Table 6. 1 Registers of 8253/4
First we have to setup the mode of each counter, and then after the counter data are sent to
the data locations, the counters will start to count. The mode control word has the
following format:
D7
SC1
11
D0
SC0
RL1
RL0
M2
M1
M0
BCD
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
12
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
PA4(8)
PA5(9)
PA6(10)
PA7(11)
PA3(7)
PA2(6)
PA1(5)
PA0(4)
PB0-PB7
(21)/CS0_8253
/CS1_8253
/CS2_8253
/CS3_8253
8253_0
Ck1(pin 15)<-1MHz
Gate1(pin14) <-5V
Out1(pin13) ->servo
(22)/RD
(23)/WR
(20)A1
(19)A0
D0-D7
Ck2(pin 18)<-1MHz
Gate1(pin 16) <-5V
Out2(pin17) ->servo
(5)/E2 ->0V
(6)E3->5V
74138
(pin8)0
(pin16)5V
Connector: con7 of
8031RL board
(Pin385V
(Pin1 and pin2)0V
(1)A0 Y3(12)
(2)A1 Y2(13)
(3)A2 Y1(14)
(4)/E1 Y0(15)
8255 PA4(8)
(con7) PA5(9)
PA6(10)
PA7(11)
(21)/CS0_8253_0
8253_0
(pin 24)5V
(pin12)0V
PA3(7)
PA2(6)
PA1(5)
PA0(4)
PB0-PB7
(22)/RD
(23)/WR
(20)A1
(19)A0
D0-D7
Servo_30
Servo_31
Servo_32
(21) /cs3_8253_3
Servo_20
Servo_21
Servo_22
(21) /cs2_8253_2
(21) /cs1_8253_1
Servo_10
Servo_11
Servo_12
D0 pin 8 of 8253
D1 pin 7
D2 pin 6
D3 pin 5
D4 pin 4
D5 pin 3
D6 pin 2
D7 pin 1
sig
5V
0V
Futaba
Servomotor
White
Red
Black
13
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
1Mhz
clock
module
GND
(pin7)
(Pin 14) 5V
(Pin 8) output
1MHz clock
It is shown in the interface circuit diagram that there are altogether 4 chip select signals
emulated by PA4-PA7. That means one can connect four 8253s in this system.
For connecting four (can be up to 8 in this design using the 74138) 8253s to the 8255, all
the corresponding pins of the four 8253s are connected in parallel, except that, the chip
select pin of each 8253 is connected to one of the chip select output at PA4-PA7. By
carefully setting the output pattern of PA4-PA7 by programming we can select the target
8253 as required. In other words programming the 8253 is a sequence of 8255 data
writing tasks. It is a little tedious perhaps, but rather practical.
6.5.4 Using an 8253 and interrupt for generating the PWM signal for a
servomotor.
Now lets come back to our discussion on the 8253 counting model. We found that
mode0 is useful in our PWM pulse generation. From the timing diagram of 8253 mode0
(INTERRUPT ON TERMINAL COUNT), it is like a countdown alarm. We will now use
counter 0 to illustrate our example. After we set the counter data register to a certain value
X (2 in the diagram) , it will first reset out0 to 0 and starts to decrement the counter at the
rising edge of the clock pulse at ck0. When the counter decrements (2 times in the
diagram) to 0, out0 will be set to 1.
14
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
8255
8031
/int0
Counter0
16-bit
counter
ck0
Gate0
Interrupt clock
50Hz.
From pin19 of
the real time
clock DS1287
1MHz
Oscillator
5V
Out0
5V
GND
Futaba
Servomotor
White
Red
Black
Figure 6. 16 Showing how to connect the 8031, 8255, 8253 and the servomotor together
In order to reduce the use of chips, we have the above design and will be explained as
follows. The 1MHz clock is connected to the clock input of 8253. And out0 is connected
to the input (white line) of the servo motor On the other hand the 8031 is interrupted by a
50Hz clock from the real time clock.
With the help of this counter, our PWM ISR algorithm will look like this.
Main ()
{
:
:
}
(ISR executes
at a rate of
50Hz)
ISR_for_8253
{set the 8253 mode0,
save counter0 a 16-bit value correspond to the
required Ton
}
Using the calculation we had earlier, we need our PWM signal to have a period of 20ms
and Ton can range from 1ms to 2ms at a resolution of dt=3.9s. Since we need a
resolution of 256 divisions inside 1ms (1ms/256=3.9s).
Regular Interrupt occurs
At 50Hz
20ms
Time
Count0=0
starts
Out0
Ton
count0=X
ends
Count0=0
Starts
count0=X
ends
Toff
At each interrupt, the ISR will program the 8253 counter0 to work in mode 0, and saves a
value X corresponds to the desired Ton time in the 16-bit counter. Then the ISR will
return to the main program. Therefore the 8253 output out0 to the servomotor will be
reset to 0 just after each interrupt occurs. Then, the counter in 8253 will starts to
decrement at a rate of 1MHz, when reaching X, the servomotor input would be set to 1.
So what should be the value of X? The requirement is to have Ton to be ranging from 1
ms to 2 ms with the PWM period of 20ms. Then Toff would be ranging from _____ to
______ms. Therefore X can be ranging from
to
ms. Please work
out these values for your experiments.
15
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
look at the schematic and find out how to use and connect them.
2. Construct the circuit and write the program to drive it.
3. The requirement are:
the interrupt signal from the DS1287 real-time clock. I dont think it is
very difficult but requires some investigation of how to use internal
timers of the 8031. No hardware connection is necessary, since the
internal timers and interrupt logic are linked internally in the 8031.
16
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
^
|
(count0 out divided by 4 to ck1)
Count 0 in
^
|
1MHz clock
17
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
18
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
19
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
20
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
6.6
Conclusion
The theory and practical issues of generating pulse width modulation signals have been
discussed. In particular, software and hardware based techniques are generating pulse
width modulated pulses have been mentioned. Experiments to illustrate these ideas have
been introduced.
6.7
References
1. 8254.pdf from Intel (http://support.intel.com/sites/support/index.htm)
2. The chapter about motors in:
ftp://cherupakha.media.mit.edu/pub/projects/6270/docs-1992/6270guide-92.pdf
21
Mobile Robot chapter 6: PWM and the use of programmable interval timers (v4.a)
3.
22