基本信息
源码名称:DS8007原厂示例代码
源码大小:0.07M
文件格式:.zip
开发语言:C/C++
更新时间:2019-01-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
DS8007原厂示例代码

/* --------------------------------------------------------------------------------
 *  Copyright (C) 2003-2007 Dallas Semiconductor Corporation, All Rights Reserved.
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
 *  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 * 
 *  Except as contained in this notice, the name of Dallas Semiconductor
 *  shall not be used except as stated in the Dallas Semiconductor
 *  Branding Policy.
 * --------------------------------------------------------------------------------
 */
/**********************************************************************************
*
*		Module Name : Serial library
*		Description : Serial library functions definition
*	   	   Filename : Serial.c
*	Dependant Files : 
*					  intrins.h
*					  Serial.h
*
*		     Author : Seeniraj G Naickar
*		   Compiler : keil C51 Compiler V7.06
*			Version : Version 1.0
*			Created : 09-19-03
*	  Modifications : November 26, 2004
*					  Modified for using with DS8007 pass thru module. Removed all 
*					  400 releated dependancies in the code. the code can be used
*					  with any processor
*                     - Ported for DS5250 processor
*                     - Ported for DS5002 processor
*			  Notes :
*
**********************************************************************************/

#include "common.h"
#include "stdio.h"
//include files
#ifdef DS5250
    #include "reg5240.h"
#endif

#ifdef DS89C390
    #include "reg390.h"
#endif

#ifdef DS89C400
    #include "reg400.h"
#endif

#ifdef DS5002
	#include "reg5000.h"
#endif

#include "serial.h"
#include <intrins.h>

#ifdef DS5002
	#undef  SERIAL_CTS_0 
	#define SERIAL_CTS_0 1
#endif

// the serial_port data structure includes buffers and additional data variables
// needed to manage a serial port.
typedef struct serial_port
{
	unsigned char receive_buffer[BUF_SIZE];     //receive buffer
	unsigned char transmit_buffer[BUF_SIZE];    //transmit buffer
	unsigned int  receive_writeindex;   //receive buffer write index
	unsigned int  receive_readindex;    //receive buffer read index
	unsigned int  transmit_writeindex;  //transmit buffer write index
	unsigned int  transmit_readindex;   //transmit buffer read index
	unsigned char receive_emptystatus;  //receive buffer empty status flag
	unsigned char transmit_emptystatus; //transmit buffer empty status flag
	unsigned char paritytype;           //specifies parity
	serial_uartbaudrates currentbaudrate; //current baudrate value
	unsigned char isportopened;         //flag indicates serialport configuration status
	unsigned char serialmode;           //serial port mode
	unsigned char send_xon;             //flag for ISR to send xon character
	unsigned char parity_err_flag;      //indicates if parity error occurs
} serial_port;

#define XON  0x11
#define XOFF 0x13

#ifdef DS5002
//Serial driver global variables
serial_port port0;
#else
//Serial driver global variables
serial_port xdata port0;
#endif

long serial_baudrate[NUM_BAUD_RATES] = { 1200,
                                         2400, 
										 9600,
										14400,
    									19200,
										28800,
    									38400,
    									57600,
   									   115200
							  		   };

#ifndef DS5002
	extern void serial0_uartisr(void);
	extern int  serial0_getchar(unsigned char *inchar);
	extern void isr_setinterruptvector(int vector_number, void* functionptr);
#else
	int  serial0_getchar(unsigned char *inchar);
#endif

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
void serial0_enableinterruptflags()
{
	port0.isportopened=0;
	port0.transmit_emptystatus=1;
	port0.receive_emptystatus=1;
#ifdef DS5002
    ES=1;
#else
	//Enable Serial interrupt flag
 	ES0=1; 	  //Serial 0 interrupt
#endif	
	IP|=0x50; //Set Serial0 and Serial1 as high periority interrupts
    EA=1; 	  //enable global interrupt flag

	return;
}

#ifdef DS5250
void serial_interrupt(void) interrupt ISR_SERIAL0
{
	serial0_uartisr();
}
#endif

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
unsigned int serial_version(void)
{
	return SERIAL_VERSION;
}

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
void serial0_open()
{
	//Initialize port variables
	port0.receive_writeindex=0;
	port0.receive_readindex=0;
	port0.transmit_writeindex=0;
	port0.transmit_readindex=0;
	port0.receive_emptystatus=1;
	port0.transmit_emptystatus=1;
	port0.paritytype=SERIAL_NO_PARITY;

#ifdef PNP_SUPPORT
	port0.currentbaudrate=BAUD_RATE_1200; 
#else
#ifdef DS5002
	port0.currentbaudrate=BAUD_RATE_38400; 
#else
	port0.currentbaudrate=BAUD_RATE_115200; 
#endif
#endif
	port0.isportopened=1;
	port0.serialmode=SERIAL_MODE1; //serial port mode
	port0.parity_err_flag=0;
	port0.send_xon=0;

#ifndef DS5002  
	//Configure port0 to work at port0.currentbaudrate
	//10 bit serial 0, use timer baud rate, enable receiving
   	SCON0=0x50;  
	
	//RCAP2H,RCAP2L=65536-((OSCILLATOR_FREQ/2)/(16*baudrate))
    RCAP2H=((0x10000-(OSCILLATOR_FREQ/(32 * serial_baudrate[port0.currentbaudrate])))/0x100);
			
	//Set timer2 reload low bye
    RCAP2L=((0x10000-(OSCILLATOR_FREQ/(32 * serial_baudrate[port0.currentbaudrate])))%0x100);

    T2CON=0x30;  //Enable timer 2 for serial0
    TR2=1;		 //Set timer 2 to run
#endif

#ifdef DS5002
    TMOD = 0x21;
	TH1  =(char) (256-((OSCILLATOR_FREQ*2L)/(12*serial_baudrate[port0.currentbaudrate]*32L)));
	PCON|= 0x80;
	TCON|= 0x50;
	SCON= 0x50;
	TI=1;
    RI=0;
#endif

#ifndef DS5002
#ifndef DS5250	
	//Install Interrupt handler for Serial0 and initialize variables
	isr_setinterruptvector(ISR_SERIAL0, &serial0_uartisr); 
#endif
#endif
  
	return;
}

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
int serial0_setparameters(unsigned char param_type,
						  unsigned int param_value )
 {
	switch(param_type)
	{
		case SERIAL_MODE_TYPE:

#ifdef DS5002
				SCON=((((unsigned char)param_value)*64)|(SCON & 0x3F));
#else
				SCON0=((((unsigned char)param_value)*64)|(SCON0 & 0x3F));
#endif
				break;

		case SERIAL_BAUD_TYPE:
#ifdef DS5002
  	            TH1  = (256-((OSCILLATOR_FREQ*2)/(12*serial_baudrate[param_value]*32)));
#endif

#ifndef DS5002
				TR2=0;//stop timer
				//Set timer2 reload high byte
				RCAP2H=((0x10000-(OSCILLATOR_FREQ/(32 * serial_baudrate[param_value])))/0x100);
				//Set timer2 reload low bye
				RCAP2L=((0x10000-(OSCILLATOR_FREQ/(32 * serial_baudrate[param_value])))%0x100);
				TR2=1;//start timer
#endif
				break;

		case SERIAL_PARITY_TYPE:
				if(param_value!=SERIAL_NO_PARITY && param_value != SERIAL_ODD_PARITY \ 
				   && param_value != SERIAL_EVEN_PARITY)	
				   return SERIAL_ERR_INVALID_PARITY_TYPE;
			
				port0.paritytype=param_value;
				break;

		default:
				return SERIAL_ERR_VALUE_IS_NOT_SUPPORTED;
	}
	return SERIAL_STATUS_SUCCESS;
}

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
int serial0_getparameters( unsigned char param_type,
						  unsigned int *param_value )
{
	switch(param_type)
	{
			case SERIAL_MODE_TYPE:
				*param_value=port0.serialmode;
				break;
    
			case SERIAL_BAUD_TYPE:
				*param_value=port0.currentbaudrate;
				break;

			case SERIAL_PARITY_TYPE:
				*param_value=port0.paritytype;
				break;

			default:
				return SERIAL_ERR_VALUE_IS_NOT_SUPPORTED;
	}
	return SERIAL_STATUS_SUCCESS;
}

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
int serial0_putchar(unsigned char outchar)
{
	bit preservedie;

	if((port0.transmit_readindex-port0.transmit_writeindex==1) || \
	   (port0.transmit_writeindex-port0.transmit_readindex==BUF_SIZE-1))
	{
		return SERIAL_ERR_BUFFER_IS_FULL;
	}

	port0.transmit_buffer[port0.transmit_writeindex] = outchar;

	preservedie=_testbit_(EA); // protect this section from the UART ISR

	port0.transmit_writeindex  ;

	port0.transmit_writeindex %= BUF_SIZE ;

	if(port0.transmit_emptystatus)
	{
		port0.transmit_emptystatus=0;
		TI=1;
	}

 	EA=preservedie;//restore old value of EA

	return SERIAL_STATUS_SUCCESS;
}

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
int serial0_write(unsigned char *string, unsigned int num)
{
    unsigned int count;

	count=0;
    do
	{
		if(serial0_putchar(string[count])==SERIAL_STATUS_SUCCESS)
        {
            count  ;
        }
    }while(count<num);
	return SERIAL_STATUS_SUCCESS;
}

/**************************************************************************
* See serial.h header file for function description
***************************************************************************/
int serial0_read(unsigned char *string, unsigned int num)
{
    unsigned int count;
       	
    count=0;
	do
	{
		if((serial0_getchar(&string[count]))==SERIAL_STATUS_SUCCESS)
        {
         count  ;
        }
	}while(count<num);

	return SERIAL_STATUS_SUCCESS;
}

//Clears Serial0 receive buffer
void serial0_flush(void)
{
    EA=0;
    port0.receive_writeindex=0;
	port0.receive_readindex=0;
	port0.receive_emptystatus=1;
    EA=1;

    return;
}

//Returns 1 if serial transmit buffer is empty. returns zero otherwise
char isserial_writebufferempty(void)
{
    return port0.transmit_emptystatus;
}

#ifdef DS5002
int  serial0_getchar(unsigned char *inchar)
{
	unsigned int data val;
	bit preservedie;

	if(port0.receive_emptystatus)
	{
		return SERIAL_ERR_BUFFER_IS_EMPTY;
	}

	val = port0.receive_readindex;
	*inchar = port0.receive_buffer[val];
	val  ;
	val%=BUF_SIZE;

	preservedie=_testbit_(EA); // protect this section from the UART ISR

	if(val==port0.receive_writeindex)
	{
		port0.receive_emptystatus=1;
	}
	
	port0.receive_readindex=val;

 	EA=preservedie;//restore old value of EA

	return SERIAL_STATUS_SUCCESS;
}
#endif

#ifdef DS5002
static void serial0_uartisr(void) interrupt ISR_SERIAL0 
{
	unsigned int  data val;
	unsigned char data val1;

    if (RI)
    {
		RI=0;
		val1=SBUF;
		val=port0.receive_writeindex;
		port0.receive_buffer[val]=val1;
		if(val==port0.receive_readindex)
		{
			port0.receive_emptystatus=0;
		}
		val  ;val%=BUF_SIZE;
		port0.receive_writeindex=val;
    }
    if (TI)
    {
		TI=0;
		val=port0.transmit_readindex;
		if(val!=port0.transmit_writeindex)
		{
			SBUF=port0.transmit_buffer[val];
			val  ;val%=BUF_SIZE;
			port0.transmit_readindex=val;	
			if(port0.transmit_emptystatus==1)	
				port0.transmit_emptystatus=0;
		}
		else
		{
			port0.transmit_emptystatus=1;
		}
    }
	return;
}
#endif