⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter7.htm

📁 嵌入式软件开发.rar
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<h4>Constructor</h4>
<pre><code>
	Semaphore* semaphore_(void);
</code></pre>
<h4>Destructor</h4>
<pre><code>
	semaphore__(Semaphore *);
</code></pre>
<h4>Macro Methods</h4>
<pre><code>
	None
</code></pre>
<h3>Header File</h3>
<pre><code>
	#ifndef SEMAPHORE_H
	#define SEMAPHORE_H

	#include "defines.h"

	typedef struct semaphor
	{
	    Boolean sem; 
	}Semaphore;

	Boolean attach_semaphore(struct semaphor *); 
	void release_semaphore(struct semaphor *);
	Boolean semaphore_status(struct semaphor*);
	void wait_for_semaphore(struct semaphor*);

	Semaphore* semaphore_(void);
	void semaphore__(Semaphore *);
	#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>

<p>	The semaphore component is used to exclude simultaneous access by different 
processes to a resource.   The semaphore is usually created at an operating system level 
in a program and when a task or process needs exclusive access to a resource, it first tests 
to determine if the semaphore is available and then attaches the semaphore to itself and 
proceeds to use the resource.  When the process is done with the resource, it releases the 
semaphore so other processes can access the resource.


<h3>Coding Examples</h3>
<pre><code>
	#include *ltsemaphor.h&gt
	#include &lttimer.h&gt
	#include &ltdelay.h&gt
		.
		.
		.
	    Semaphore *S1=semaphore_();   /* create the semaphore */
		.
		.
		.
	    delay_(T1,S1,2000); /* execute 2000 ms delay */
		.
		.
		.
	    wait_for_semaphore(S1); /* wait here for the semaphore to be
					     released by the delay component */
		.
		.
</code></pre>


 
<h2><a name="serial_input/output_basic">Data Sheet -- Serial Input/Output Basic</a></h2>

<h3>Device Name and description</h3>

<h3>Serial</h3><P>--The class Serial implements a serial input/output system through serial port A on 
the Multichannel Communications Interface.  This asynchronous serial interface is block 
oriented and all blocks are terminated with a NULL character.  A NULL character is a 
pointer to a type BYTE with a zero address.  This component works asynchronous serial 
port A only. 

<h3>Super Classes</h3>
<p>
Serial inherits the features of  the error handler.  The error handler inherits the features 
of the class object.
<h3>Attributes</h3>
<pre><code>
 	BYTE (*i_o)(struct serio *,io_command_block *); /* i/o function */
</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 */
	static BYTE i_o(Serial_io * this,	   	/* a pointer to the Serial_io object */
			   io_command_block *block);  	/* i/o command block */
	BYTE getch(void);				    	/* standard byte input routine */
	void putch(BYTE ) ;			    		/* standard byte output routine */
	void do_receive(void);			    	/* block receive routine */
	void do_transmit(void);			    	/* block transmit routine */
</code></pre>											
<h4>Constructor</h4>
<pre><code>
	Serial_io *serial_io_(int baud_rate,        /* baud rate in bits/second  */
			    	      int vector,           /* serial io interrupt vector */
			  			  int iarb,             /* interrupt arbitration level */
			     		  int int_level);       /* interrupt level */
</code></pre>
<h4>Destructor</h4>
<pre><code>
	void serial_io__(Serial_io *);
</code></pre>
<h4>Macro Methods</h4>
<pre><code>
	#define serial_io(a,b) (*a->i_o)(a,b) 
</code></pre>
<p>The parameter a is a pointer to an instantiated Serial_io.  The argument b is a pointer to 
an io_command_block with instructions to execute on this i/o transaction.


<h3>Header File</h3>
<pre><code>
	#ifndef SERIO_H
	#define SERIO_H

	#include "error.h"
	#include "iodefs.h"
	#include "defines.h"

	#define EOM             0      /* End Of Message */

	#define SERIO_METHODS \
			void (*do_rec)(void);  \
			void (*do_trans)(void);

	typedef struct
	{
		SERIO_METHODS
	} Serio_Methods;

	enum io_commands{INPUT,OUTPUT };
	              
	enum io_errors{NO_ERROR, 
	               BUFFER_OVERFLOW,
	               COMMAND_ERROR};

	/* i/o command block */               

	typedef struct /* See enumerates above for commands and error returns */
	{
	    int io_command;                 /* input  parameters */
	    BYTE *buffer;         
	    int buffer_length;
	    int characters_transferred;     /* output  parameters */
	    Boolean call_complete;
	    int error;
	} io_command_block;

	#define  SERIAL_IO	\
		ERROR_HANDLER	\
	    BYTE (*i_o)(struct serio *,io_command_block *);
	   
	typedef struct serio
	{
		SERIAL_IO
	} Serial_io;

	#define serial_io(a,b) (*a->i_o)(a,b)

	/* Function prototypes */
	Serial_io *serial_io_(int baud_rate,int vector,int iarb,int 		
				int_level);
	void serial_io__(Serial_io *);
	static BYTE i_o(Serial_io * this, io_command_block *block);
	BYTE getch(void);
	void putch(BYTE ) ;
	void do_receive(void);
	void do_transmit(void);

	#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>

<p>	Observe in the header file that there is a definition of an i/o parameter block.  The 
contents of this block is needed for successful completion of every input or output 
transaction.  In operation, the i/o will be established by execution of the constructor.  
Whenever an i/o operation is needed,   the function serial_io(a,b) s executed with a being 
a pointer to a properly instantiated Serial_io and b a pointer to an io_parameter_block.  
This class uses virtual functions in the case of the block receive and transmit commands.

<h3>Coding Examples</h3>
<pre><code>
	#include &ltserio.h&gt
		.
		.
		.
	#define SERIO_BAUD_19200 19200  /* baud rate value with */
	#define SERIO_INT_VECTOR 0x40   /* interrupt vector, */
	#define SERIO_IARB 10           /* interrupt arbitration level, */
	#define SERIO_INT_LEVEL 3       /* interrupt level */
		.
		.
		.
	/* function prototype */
	void output_time(void);
		.
		.
		.
	BYTE pszMessage[]="         \r"; /* array to hold the time */
	io_command_block *oob1,ob1;     /* an io command block */
	Serial_io *sio;                 /* and a Serial_io pointer */
		.
		.
		.
	    /* initialization portion of the program */

	    /* set up the serial communications */
		sio=serial_io_(SERIO_BAUD_19200,     /* set the baud rate   */ 
				  SERIO_INT_VECTOR,     /* interrupt vector */
			      SERIO_IARB,           /* iarb   */
				  SERIO_INT_LEVEL);     /* and interrupt level */

	    /* set up the io blocks */      
	    oob1=&ob1;
		.
		.
		.
	    cli();                      /* enable the system interrupts */
		.
		.
		.
	/* applications portion of the program */
	/* a function here prepares the message to go out and puts it into
	   pszMessage[].  it also executes the function output_time() */

	void output_time(void)
	{
		while(!oob1->call_complete)
			;                     /* wait until any other message is out */     
		oob1->io_command=OUTPUT;    /* prepare the io command block */
		oob1->buffer=pszMessage;    /* message to go out */
		oob1->buffer_length= sizeof pszMessage;
		serial_io(sio,oob1);        /* message to the i/o object */
	}
</code></pre>
 
<h2><a name="serial_input/output_keyboard">Data Sheet -- Serial Input/Output - Keyboard Specific</a></h2>

<dh3>Device Name and description</h3>

<h3>Serkb</h3><p>--The class Serkb is a serial input/output system.  This class is derived from the 
class Serial_io.  It works the same as Serial_io with two exceptions.  The first is that both 
the input and output buffer is terminated with a '\r' character rather than a NULL as was 
seen earlier.  Also, with this class, a key-board hit routine is included.  This component 
works asynchronous serial port A only.

<h3>Super Classes</h3>

<p>Serkb inherits the attributes and methods of the class Serial_io.  Serial_io inherits the 
features of Error_handler which in turn inheits the features of Object.

<h3>Attributes</h3>
<pre><code>
	BYTE (*i_o)(struct serio *,io_command_block *);
</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 */
	static BYTE i_o(Serial_io * this,      /* From Serial_io--see Serial_io */
			   io_command_block *block);   
	BYTE getch(void);				     
	void putch(BYTE ) ;			     
	void do_receive_kb(void);              /* special block receive */
	void do_transmit_kb(void);             /* and transmit routines */
	Boolean kbhit(void);		           /* tests for a keyboard hit */
</code></pre> 
<h3>Constructor</h3>
<pre><code>
	Serial_io *serkb_io_(int baud_rate,        /* baud rate in bits/second  */
			             int vector,           /* serial io interrupt vector */
			             int iarb,             /* interrupt arbitration level */
			             int int_level);       /* interrupt level */
</code></pre>
<h3>Destructor</h3>
<pre><code>
	void serial_io__(Serial_io *this);
</code></pre>
<p>	This component uses its super class destructor.

<h3>Macro Methods</h3>
<pre><code>
	#define serial_io(a,b) (*a->i_o)(a,b) 
	</code></pre>
<p>The parameter a is a pointer to an instantiated Serial_io.  The argument b is a pointer to 
an io_command_block with instructions to execute on this i/o transaction.


<h3>Header File</h3>
<pre><code>
	#ifndef SERKB_H
	#define SERKB_H

	#include "serio.h"

	#define EOL     '\r'      /* End Of Line */

	/* Function prototypes */
	Serial_io *serkb_io_(int baud_rate,int vector,int iarb,int int_level);
	void do_receive_kb(void);
	void do_transmit_kb(void);
	Boolean kbhit(void);

	#endif
</code></pre>
<h3>Other Features and Special Calling Procedures</h3>

<p>	This component works exactly the same as Serial_io with the exception that the 
buffer terminator is a '\r' rather than a NULL character and the Boolean function kbhit() 
has been added.

<h3>Coding Examples</h3>
<pre><code>
	#include &ltserkb.h&gt
		.
		.
		.
	#define SERIO_BAUD_19200 19200
	#define SERIO_INT_VECTOR 0x40
	#define SERIO_IARB 10
	#define SERIO_INT_LEVEL 3
		.
		.
		.
	/* function prototypes */
	void output_time(void);
		.
		.
		.
	BYTE pszMessage[]="         \r"; /* array to hold the output time */
	BYTE pszMessage1[]="Enter the new time HH:MM > ";
	BYTE pszMessage2[]="                                   \r"; /* empty array */
	BYTE pszMessage3[]="         \r"; /* array to hold the time input*/
	io_command_block *oob1,ob1,*iob1,ib1;
	Serial_io *sio;
		.
		.
		.
	    /* in the initialization portion of the program */

	    /* set up the serial communications */
		sio=serkb_io_(SERIO_BAUD_19200,     /* set the baud rate   */ 
				 SERIO_INT_VECTOR,     /* interrupt vector */
			        SERIO_IARB,           /* iarb   */
				 SERIO_INT_LEVEL);     /* and interrupt level */
	                                    
	    /* set up the io blocks */      
	    oob1=&ob1;                  /* output block */
	    oob1->call_complete=FALSE;  /* have no message going out */
	    oob1->io_command=OUTPUT;    /* oob1 is always output  */
	    iob1=&ib1;                  /* a block for inputs */
	    iob1->io_command=INPUT;     /* iob1 is always input */
	    iob1->call_complete=FALSE;  /* need to have no message in progress */
		.
		.
		.
		 /* in the applications portion of the program */

	        /* reset the time clock from the keyboard whenever any key
	           on the keyboard is hit */
	        if(kbhit())             
	        {
	            BYTE *p;  /* need a pointer here */
	            Boolean message_ok, time_ok; /* and a couple of flags */
	            BYTE temphours,tempminutes;

	            do
	            {
	                putch('\r');    /* put cursor in correct location */
	 	         	oob1->buffer=pszMessage1; /* send out message */
	 	         	oob1->buffer_length= sizeof pszMessage1;
		         	serial_io(sio,oob1);    /* message to the i/o object */
	                iob1->buffer=pszMessage3;
	                iob1->buffer_length= sizeof pszMessage3;
	                serial_io(sio,iob1);      /* get input message */
				.
				.
				.
			             putch('\r');  /* do a carriage return and */    
		                 oob1->buffer=pszMessage2; /* clear the screen */
		                 oob1->buffer_length= sizeof pszMessage2;
						 serial_io(sio,oob1);   
				.
				.
				.
 </code></pre>	        	               
<h2><a name="serial_input/output_special">Data Sheet -- Serial Input/Output Special Baud Rate Handling</a></h2>

<h3>Device Name and description</h3>

<h3>Serial1</h3><p> -- The component Serial1 is identical to Serkb except that it takes a baud rate 
count value rather than the actual baud rate of the port as a constructor argument.  This 
component works asynchronous serial port A only.

<h3>Super Classes</h3>
<p>
Serial1 inherits the features of  the error handler.  The error handler inherits the 
features of the class object.  It also inherits the features of Serkb and Serial_io.
<h3>Attributes</h3>
<pre><code>
	BYTE (*i_o)(struct serio *,io_command_block *);
</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 */
	static BYTE i_o(Serial_io * this,       /* From Serial_io--see Serial_io */
			   io_command_block *block);   
	BYTE getch(void);				     
	void putch(BYTE ) ;			     
	void do_receive_kb(void);              /* special block receive */
	void do_transmit_kb(void);             /* and transmit routines */
	Boolean kbhit(void);		      /* tests for a keyboard hit */
</code></pre>	 
<h4>Constructor</h4>
<pre><code>
	Serial_io *serial1_io_(WORD baud_value,     /* baud rate internal divisor  */
				 		  int vector,           /* serial io interrupt vector */
						  int iarb,             /* interrupt arbitration level */
						  int int_level);       /* interrupt level */
</code></pre>
<h4>Destructor</h4>
<pre><code>
	void serial1_io__(Serial_io *this);
</code></pre>
<p>	This component uses its super class destructor.

<h4>Macro Methods</h4>
<pre><code>
	#define serial_io(a,b) (*a->i_o)(a,b)
</code></pre> 
<p>The parameter a is a pointer to an instantiated Serial_io.  The argument b is a pointer to 
an io_command_block with instructions to execute on this i/o transaction.


<h3>Header File</h3>
<pre><code>
	#ifndef SERIO1_H
	#define SERIO1_H

	#include "serkb.h"

	Serial_io *serial1_io_(WORD baud_value,int vector,int iarb,int 
	int_level);
	void serial1_io__(Serial_io *this);

	#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -