实例介绍
【实例截图】
【核心代码】
/* -------------------------------------------------------------------------------- * 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
标签: DS8007
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论