📄 chapter7.htm
字号:
.
.
.
add_link(list1,lnknull); /* add null object to list */
add_link(list1,lnk1); /* first,then real objects */
add_link(list1,lnk2);
.
.
.
attach_method(t3,walk_list);/* attach a method to timer*/
attach_object(t3,list1); /* and its argument */
.
.
.
</code></pre>
<h2><a name="look_up_table">Data Sheet LOOK UP TABLE</a></h2>
<h3>Device Name and description</h3>
<h3>Look_up</h3>
<p>--The class Look_up defines a look-up table. This component provides a means
of searching a table for the closest value and then returning a linear interpolated value
between the two points in the table between which the value lies. This look-up table
operation is called two dimensional because it receives a single input and returns a single
value.
<h3>Super Classes</h3>
<pre><code>
None
</code></pre>
<h3>Attributes</h3>
<pre><code>
int x_length;
Entry *table;
</code></pre>
<h3>Methods</h3>
<pre><code>
BYTE (*get_val)(struct LU *this, BYTE value);
</code></pre>
<strong>Constructor</strong>
<pre><code>
Look_up *look_up_(int x_length, Entry *table);
</code></pre>
<strong>Destructor</strong>
<pre><code>
void look_up__(Look_up * this);
</code></pre>
<strong>Macro Methods</strong>
<pre><code>
#define get_value_2d(a,b) (*a->get_val)(a,b)
a is a pointer to a look-up table component and
b is the BYTE value to be calculated.
</code></pre>
<h3>Header File</h3>
<pre><code>
#ifndef LUTOBJ_H
#define LUTOBJ_H
#include "defines.h"
/* table used below is a pointer to an array of the type Entry.
The number of entries in the array must be equal to x_length.
Of course, each entry contains two values which are of the
type BYTE */
typedef struct
{
BYTE x,y;
}Entry;
#define LOOK_UP_CLASS \
int x_length; \
Entry *table; \
BYTE (*get_val)(struct LU *this, BYTE value);
typedef struct LU
{
LOOK_UP_CLASS
} Look_up;
#define get_value_2d(a,b) (*a->get_val)(a,b)
Look_up *look_up_(int x_length, Entry *table);
void look_up__(Look_up * this);
int find_index(Look_up *this, BYTE value);
BYTE interpolate(BYTE value,BYTE x0,BYTE y0,BYTE x1,BYTE y1);
#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>
<p> One of the parametes sent to the constructor is a pointer to a table of the type
Entry. Entry contains two BYTE members, the first being the ordinate and the second
being the abscissa of the input data points. There can be as many input data points as is
needed. The range of values for both the x and the y values is that of an unsigned
character, 0 to 255. The number of points contained in the table is passed in as the
parameter length.
<h3>Coding Examples</h3>
<pre><code>
#include <lutslope.h>
const Entry taba[]={
0,0,
96,40,
144,68,
166,128,
221,160,
255,160 };
void main(void)
{
int i;
Look_up *a=look_up_(6,taba), /* create three look-up tables */
get_value(b,i),get_value(c,i));
look_up__(a); /* destroy all objects when no longer needed*/
</code></pre>
<h2><a name="object">Data Sheet-- Object</a></h2>
<h3>Device Name and description</h3>
<h3>Object</h3><p> -- The class Object is the base class of many of the other classes. It is, however,
not an abstract class and it can be instantiated by itself whenever needed.
<h3>Super Classes</h3>
<pre><code>
None
</code></pre>
<h3> Attributes</h3>
<pre><code>
None
</code></pre>
<h3>Methods</h3>
<pre><code>
void (*exercise)(struct object*);
</code></pre>
<h3>Constructor</h3>
<pre><code>
Object *object_(void (*exercise)());
</code></pre>
<h3>Destructor</h3>
<pre><code>
void object__(Object *);
</code></pre>
<h3>Macro Methods</h3>
<pre><code>
None
</code></pre>
<h3>Header File</h3>
<pre><code>
#ifndef OBJECT_H
#define OBJECT_H
#include "defines.h"
#define OBJECT \
void (*exercise)(struct object*);
typedef struct object
{
OBJECT
} Object;
Object *object_(void (*exercise)());
void object__(Object *);
#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>
<p> When instantiated, Object receives a parameter that is a pointer to a function in
the calling program. This function is convenient in operations like linked lists where the
program might need to execute a method, not necessarily the same method, in several
objects contained within the list.
<h3>Coding Examples</h3>
<p> See the Linked List class shown above for a coding example that uses the
component Object.
<h2><a name="pulse_width_modulator_#1">Data Sheet -- Pulse Width Modulator #1</a></h2>
<h3>Device Name and description</h3>
<h3>Pwm</h3><p> -- The class Pwm creates a pulse width modulator type digital-to-analog converter.
Such a component is quite useful in the implementation of contol systems. Pwm makes
use of two Output Compare timer subsystem devices. One of these output compares
controls the period of the output signal and the other controls the on time.
<h3>Super Classes</h3>
<p>Pwm inherits the features of the error handler. The error handler inherits the features
of the class object.
<h3>Attributes</h3>
<pre><code>
WORD time_on, /* unsigned integer time on */
WORD period; /* unsigned integer period */
BYTE on_timer; /* timer that controls on time, 2 through 4 */
</code></pre>
<h3>Methods</h3>
<pre><code>
void error_hand(void *, int, int) /* from Error_handler */
arguments /* see Error_handler */
void exercise( Object * ) /* from Object */
arguments /* See Object */
</code></pre>
<h4>Constructor</h4>
<pre><code>
Pwm *pwm_( WORD period, /* Period of the output signal */
WORD time_on, /* initial on time of the output */
BYTE on_timer); /* timer, 2-4, that controls on time */
</code></pre>
<h4>Destructor</h4>
<pre><code>
void pwm__(Pwm *);
</code></pre>
<h4>Macro Methods</h4>
<pre><code>
#define set_on(a,b) ((a)->time_on=(b))
a is a pointer to the Pwm being controlled
b is the unsigned integer value of the new time on
</code></pre>
<h3>Header File</h3>
<pre><code>
#ifndef PWM_H
#define PWM_H
#include "hc16y1.h"
#include "gpt.h"
#include "timer.h"
#include "defines.h"
#define PWM_CLASS \
ERROR_HANDLER \
WORD time_on, period; \
BYTE on_timer;
typedef struct
{
PWM_CLASS
} Pwm;
#define set_on(a,b) ((a)->time_on=(b))
Pwm *pwm_(WORD period,WORD time_on,BYTE on_timer);
void pwm__(Pwm *);
#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>
<p> This component, Pwm, instantiates two output compare timer subsystems.
Output compare 1 is always used as the timer to control the period. This timer event can
be coupled to any other output compare, and the code couples it to the timer specified as
a parameter in the Pwm constructor call. The Pwm system takes control of the two
timers without testing to see if they are in use. The timer clock speed is set by the
prescaler in the calling program. Numbers--period and time_on--are unsigned integer
values and they are counts applied to the output compare timers when they are initialized
or set-up. Therefore, the programmer has complete control over the period of the Pwm
as well as the on time of the signal. The on time can be changed at any time by use of the
macro method set_on(). This new on time becomes effective at the completion of the
current on time. The system interrupts must be enabled whenever a Pwm is being used.
<p> The resolution of this Pwm is the number of bits required to specify the period. If
the period is a 10 bit number, the resolution of the Pwm will also be 10 bits. The Pwm
will provide a minimum on-time of 1 timer clock period. It does not work well at nearly
100% on times. If it is necessary to work at on times greater than 90%, it will be
necessary to modify the code and reverse the role to the two output compare timers. In
this mode, the Pwm system works well near 100% on time, but its performance fails at
small on times.
<p>Of course, if the full range of operation is needed, a new Pwm can be defined that will
choose its control strategy to suit the specified on time.
<h3>Coding Examples</h3>
<pre><code>
#include <pwm.h>
.
.
.
Pwm *pw;
.
.
.
pw=pwm_(MAX_ON,0,2);
cli(); /* enable the system interrupts */
.
.
.
set_on(pw,value);
.
.
.
</code></pre>
<h2><a name="pulse_width_modulator_#2">Data Sheet--Pulse Width Modulator #2</a></h2>
<h3>Device Name and description</h3>
<h3>Pwmi</h3><p>--Pwmi is a class that implements a pulse width modulator that uses either of the
internal pulse width modulators found in the general purpose timer. These pwms are
both eight bit timers and the resolution cannot be adjusted. The speed of the timers is set
by a special prescaler that has two modes, fast and slow.
<h3>Super Classes</h3>
<p>Pwmi inherits the features of the error handler. The error handler inherits the features
of the class object.
<h3>Attributes</h3>
<pre><code>
BYTE channel; /* channel is either A or B corresponding to the
specific internal pwm channel */
</code></pre>
<h3>Methods</h3>
<pre><code>
void error_hand(void *, int, int) /* from Error_handler */
arguments /* see Error_handler */
void exercise( Object * ) /* from Object */
arguments /* See Object */
</code></pre>
<h4>Constructor</h4>
<pre><code>
Pwmi *pwmi_(BYTE channel, /* Specific channel A or B */
BYTE time_on, /* on tiime from 0 to 255 */
BYTE prescale, /* Prescaler value 0 to 6 */
Boolean speed); /* FAST or SLOW */
</code></pre>
<h4> Destructor</h4>
<pre><code>
void pwmi__(Pwmi *);
</code></pre>
<h4>Macro Methods</h4>
<pre><code>
#define ON_TIME(a,b) ((a)->channel==A)?(PWMA.PWA=(b)) : (PWMB.PWB=(b))
</code></pre>
<p> The parameter a is a pointer to the controlled Pwmi, either A or B, and b is a
BYTE with a range of 0 to 255 which is the requested on time.
<h3>Header File</h3>
<pre><code>
#ifndef PWMI_H
#define PWMI_H
#include <hc16y1.h>
#include <gpt.h>
#include <timer.h>
#include <defines.h>
#include <error.h>
enum channels{A,B};
typedef struct
{
unsigned PWM_NULL0 : 9;
unsigned PR02 :3;
unsigned PWM_NULL1 : 4;
} Pwmcspec;
#define PWMCSPEC (*(@far Pwmcspec*)(GPT_Register+0x24))
#define PWMI_CLASS \
ERROR_HANDLER \
BYTE channel;
typedef struct
{
PWMI_CLASS
} Pwmi;
#define ON_TIME(a,b) ((a)->channel==A)?(PWMA.PWA=(b)) : (PWMB.PWB=(b))
Pwmi *pwmi_(BYTE channel,BYTE time_on,
BYTE prescale,Boolean speed);
void pwmi__(Pwmi *);
#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>
<p> Both pwm devices must run at the same pwm clock frequency; however, each
may be run in either the slow or the fast mode which will give the two pwm outputs
widely different frequencies. The table shown below provides a summary of the pwm
clock speeds with different pwm prescaler values when operated in the slow or the fast
modes.
<table>
<th>PPR[2:0] <th>Prescaler Tap <th>Fast Mode <th>Slow Mode<tr>
<td>0 <td>Div 2=8.39 MHz <td>32.8 kHz <td>256 Hz<tr>
<td>1 <td>Div 4=4.19 MHz <td>16.4 kHz <td>128 Hz<tr>
<td>10 <td>Div 8=2.10 MHz <td>8.19 kHz <td>64.0 Hz<tr>
<td>11 <td>Div 16=1.08 Mhz <td>4.09 kHz <td>32.0 Hz<tr>
<td>100 <td>Div 32=524 kHz <td>2.05 kHz <td>16.0 Hz<tr>
<td>101 <td>Div 64=262 kHz <td>1.02 kHz <td>8.0 Hz<tr>
<td>110 <td>Div 128=131 kHz <td>512 Hz <td>4.0 Hz<tr>
<td>111 <td>PCLK <td>PCLK/256 <td>PCLK/32768<tr>
</table>
<p>Of course, the digital to analog output range with these pwms is 8 bits or 0 to 255.
<h3>Coding Examples</h3>
<pre><code>
#include <scim.h>
#include <pwmi.h>
#include <defines.h>
.
.
.
Pwmi *pwa, *pwb; /* define pwmi pointers */
.
.
.
pwa=pwmi_(A,0x80,2,FAST); /* instantiate two pwmis */
pwb=pwmi_(B,0x80,2,SLOW);
.
.
.
ON_TIME(pwa,(BYTE)value); /* send data to the pwms */
ON_TIME(pwb,(BYTE)value);
</code></pre>
<h2><a name="semaphore">Data Sheet -- Semaphore</a></h2>
<h3>Device Name and description</h3>
<h3>Semaphore</h3><p>--Semaphore is a flag that is used to grant exclusive access of a
resource to a process.
<h3>Super Classes</h3>
<pre><code>
None
</code></pre>
<h3>Attributes</h3>
<pre><code>
sem; /* the Boolean status of the semaphore */
</code></pre>
<h3>Methods</h3>
<pre><code>
Boolean attach_semaphore(struct semaphor *);
void release_semaphore(struct semaphor *);
Boolean semaphore_status(struct semaphor*);
void wait_for_semaphore(struct semaphor*);
</code></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -