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

📄 serio.c

📁 PROFIBUS SLAVE PROGRAMS PRO FIBUS SLAVE PROGRAMS
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************  Filename: serio.c ****************************/
/* ========================================================================= */
/*                                                                           */
/* 0000  000   000  00000 0  000  0   0 0 0000                               */
/* 0   0 0  0 0   0 0     0 0   0 0   0 0 0   0                              */
/* 0   0 0  0 0   0 0     0 0     0   0 0 0   0      Einsteinstra遝 6        */
/* 0000  000  0   0 000   0 0     00000 0 0000       91074 Herzogenaurach    */
/* 0     00   0   0 0     0 0     0   0 0 0                                  */
/* 0     0 0  0   0 0     0 0   0 0   0 0 0          Tel: ++49-9132-744-200  */
/* 0     0  0  000  0     0  000  0   0 0 0    GmbH  Fax: ++49-9132-744-204  */
/*                                                                           */
/* ========================================================================= */
/*                                                                           */
/* Function:       serial function                                           */
/*                                                                           */
/* ------------------------------------------------------------------------- */
/*                                                                           */
/* Technical support:       P. Fredehorst                                    */
/*                          Tel. : ++49-9132/744-214                         */
/*                          Fax. :              -204                         */
/*                          eMail: pfredehorst@profichip.com                 */
/*                                                                           */
/*****************************************************************************/


/*****************************************************************************/
/* contents:

  - function prototypes
  - data structures
  - internal functions

*/
/*****************************************************************************/
/* include hierarchy */

#include "..\..\dp_inc\platform.h"
#include "..\..\dp_inc\dp_inc.h"

#ifdef RS232_SERIO

/*---------------------------------------------------------------------------*/
/* local function prototypes                                                 */
/*---------------------------------------------------------------------------*/
void start_serial_communication ( void );
void set_baud                   ( UWORD baudrate );

/*---------------------------------------------------------------------------*/
/* global data definitions                                                   */
/*---------------------------------------------------------------------------*/
UBYTE       snd_buff [256]; // for RS232 sending
UBYTE       rec_buff [256]; // for RS232 receiving

UBYTE data  write_rec_ptr;  // Writepointer into receive buffer
UBYTE data  read_rec_ptr;   // Readpointer  from receive buffer
UBYTE data  rec_counter;    // Counter of byte in receive buffer
UBYTE data  write_snd_ptr;  // Writepointer into send buffer
UBYTE data  read_snd_ptr;   // Readpointer  from send buffer
UBYTE data  snd_counter;    // Counter of byte in send buffer
UBYTE       serial_state;   // shows state of serial line
bit         thr_empty;      // Transmitter reg. empty

/*---------------------------------------------------------------------------*/
/* local data definitions                                                    */
/*---------------------------------------------------------------------------*/
//static const UBYTE asccii[16] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46};
UBYTE asccii[16];

bit         parity;         // Save parity bit received

sbit CTS    = 0x93;
sbit RTS    = 0x94;


/*---------------------------------------------------------------------------*/
/* functions                                                                 */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*   void print_string__(UBYTE *p)                                           */
/*                                                                           */
/*   function:     print string                                              */
/*                                                                           */
/*---------------------------------------------------------------------------*/
void print_string__( UBYTE *p )
{
    while ( *p )
    {
        snd_buff [write_snd_ptr++] = *p++;
        snd_counter++;
    }
    start_serial_communication();
}//void print_string__( UBYTE *p )


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*   UBYTE print_hexbyte__(UBYTE b)                                          */
/*                                                                           */
/*   function:     konverts integer to asccii                                */
/*                                                                           */
/*---------------------------------------------------------------------------*/
void print_hexbyte__(UBYTE b)
{
    snd_buff [write_snd_ptr++] = asccii[(b >> 4)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(b & 0x0F)];
    snd_counter++;
    start_serial_communication();
}//void print_hexbyte__(UBYTE b)


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*   UBYTE print_hexword__(UWORD w)                                          */
/*                                                                           */
/*   function:     konverts integer to asccii                                */
/*                                                                           */
/*---------------------------------------------------------------------------*/
void print_hexword__(UWORD w)
{
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((w >> 12) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((w >> 8) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((w >> 4) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)(w & 0x000F)];
    snd_counter++;
    start_serial_communication();
}//void print_hexword__(UWORD w)


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*   UBYTE print_hexlong__(ULONG dw)                                          */
/*                                                                           */
/*   function:     konverts integer to asccii                                */
/*                                                                           */
/*---------------------------------------------------------------------------*/
void print_hexlong__(ULONG dw)
{
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 28) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 24) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 20) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 16) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 12) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 8) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)((dw >> 4) & 0x000F)];
    snd_counter++;
    snd_buff [write_snd_ptr++] = asccii[(UBYTE)(dw & 0x000F)];
    snd_counter++;
    start_serial_communication();
}//void print_hexlong__(ULONG dw)


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*   void start_serial_communication(void)                                   */
/*                                                                           */
/*   function:     starts serial communication                               */
/*                                                                           */
/*---------------------------------------------------------------------------*/
void start_serial_communication( void )
{
    //if(write_snd_ptr != read_snd_ptr)
    if(snd_counter > 0)
    {
        // disable interrupts
        DISABLE_ALL_INTERRUPT_BIT__;

        if( thr_empty )
        {
            thr_empty = FALSE;
            // set RTS handshake
            //RTS = 0;
            // transmit trigger via softw. interrupt
            TI = 1;
        }

        // enable interrupts
        ENABLE_ALL_INTERRUPT_BIT__;
    }
    return;
}//void start_serial_communication( void )


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*   void init_serio__(UWORD baudrate)                                        */
/*                                                                           */
/*   function:     initialize serial communication                           */
/*                                                                           */
/*---------------------------------------------------------------------------*/
#ifdef EvaBoard_AT89C5132
	
	#define Set_SMOD1()           	(PCON |= MSK_SMOD1)

void init_serio__( UWORD baudrate )
{
    memset(&snd_buff[0], 0, sizeof(snd_buff));
    memset(&rec_buff[0], 0, sizeof(snd_buff));

    write_rec_ptr = 0;      // Writepointer into receive buffer
    read_rec_ptr  = 0;      // Readpointer  from receive buffer
    rec_counter   = 0;      // Counter of byte in receive buffer
    write_snd_ptr = 0;      // Writepointer into send buffer
    read_snd_ptr  = 0;      // Readpointer  from send buffer
    snd_counter   = 0;      // Counter of byte in send buffer

	asccii[0] = 0x30;
	asccii[1] = 0x31;
	asccii[2] = 0x32;
	asccii[3] = 0x33;
	asccii[4] = 0x34;
	asccii[5] = 0x35;
	asccii[6] = 0x36;
	asccii[7] = 0x37;
	asccii[8] = 0x38;
	asccii[9] = 0x39;
	asccii[10] = 0x41;
	asccii[11] = 0x42;
	asccii[12] = 0x43;
	asccii[13] = 0x44;
	asccii[14] = 0x45;
	asccii[15] = 0x46;

    serial_state  = ON;     // shows state of serial line
    thr_empty     = TRUE;   // Transmitter reg. empty

    TXD = 1;
    RXD = 1;
	SCON = 0x50;					//uart mode: mode 1, 8bit, receive enable
   	BDRCON &=0xEC;               	//BRR=0; SRC=0;
   	BDRCON |=0x0E;               	//TBCK=1; RBCK=1; SPD=1

   	#ifdef AT89C5132_MODE_X2
   		switch( baudrate )
   		{
   			case 115200:
   			{
				Set_SMOD1();		//SMOD1 = 1
   				BRL = 245;
   				break;
   			}	

   			case 57600:
   			{
				Set_SMOD1();		//SMOD1 = 1
   				BRL = 234;
   				break;
   			}	

   			case 19200:
   			{

⌨️ 快捷键说明

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