📄 sci.txt
字号:
The sci functions and macros to implement asynchronouscommunication on the Serial Communication Interface (SCI)module.CONTENTS########1 Setting Up2 Asynchronous Function Definitions3 Examples4 Using Interrupts1) Setting Up#############The file sci.h should be #included into your sourcefiles. This file contains a macro which specifiesthe Fosc frequency. This affects the baud ratecalculations and should be adjusted to suit yourapplication.2) Asynchronous Function and Macro Definitions##############################################unsigned charsci_Init(unsigned long int baud, unsigned char ninebits)~~~~~~~~This function is used to set up the appropriate registersassociated with the sci module. Specify the desiredbaud rate. If this is possible using the currentvalue of Fosc, the value specified will be used - seethe PIC manual for details on baud rate selection.If ninebits is true, 9-bit data values will be used forboth transmission and reception. The function returnstrue if the desired baud rate could not be achieved;false otherwise.SCI_EIGHT and SCI_NINE~~~~~~~~~ ~~~~~~~~~These macros can beused with sci_Init() to indicateeight- and nine-bit communication, respectively.voidsci_PutByte(unsigned char byte)~~~~~~~~~~~This function is used to send an 8-bit quantity tothe SCI. The function first waits until TXIF isset then loads the transmit register.sci_PutNinth(bitnine)~~~~~~~~~~~~This macro is used to send the ninth bit tothe SCI when in nine-bit data mode. It should becalled before calling sci_PutByte().unsigned charsci_GetByte(void)~~~~~~~~~~~This function waits until the receive register is notempty and returns the received 8-bit data.unsigned charsci_GetNinth(void)~~~~~~~~~~~~This function waits until the receive register is notempty and returns the received ninth-bit when in nine-bit mode. It should be called before callingsci_GetByte()as the ninth bit is lost after callingthis function.unsigned charsci_GetFERR(void)~~~~~~~~~~~This function waits until the receive register is notempty and returns the received frame error status bit.It should be called before calling sci_GetByte() asframe error information is lost after calling thisfunction.unsigned charsci_CheckOERR(void)~~~~~~~~~~~~~This function checks for an overrun error and resets thereceiver, by toggling the CREN bit, and returns true ifthis has occured. The function returns false if no erroroccured. If an overrun error occurs, the receiver iscompletely disabled.3) Examples###########// 8-bit mode at 9600 baud using pollingsci_Init(9600,SCI_EIGHT);sci_PutByte(0xaa); /* send 0xaa when device is ready */data = sci_GetByte(); /* read data when device is ready */// 9-bit mode at 19200 baud using pollingsci_Init(19200,SCI_NINE);sci_PutNinth(0x00); /* ninth bit is zero */sci_PutByte(0xff); /* and data is 0xff */if(sci_getNinth()) ; /* bit nine was true */data = sci_GetByte(); /* get 8-bit value */if(sci_CheckOERR()) ; /* an overrun error occured */4) Using Interrupts###################To use interrupts with the SCI, there are several thingswhich must be attended to.Firstly there must be an interrupt service routine whichcan process the interrupts when they occur. There can beonly one interrupt routine associated with the PICprocessor and so this routine must be able to serviceany interrupt which occurs, not just those associatedwith the SCI. The following example shows an ISR whichhandles reception and transmission. The RCIF, TXIF,RCIE and TXIE bits can be used to ascertain what causedthe interrupt. In the example, if the receive registeris full (RCIF), a character is read from the SCI andechoed back to the source. If the transmitter isempty and the transmitter interrupts are enabled thenanother byte from a message is transmitted. If allthe message has been sent, the interrupts are disabledto prevent further transmission. If any other interruptsare enabled, the code to handle these will also have toappear in this ISR.unsigned char byte, i;void interrupt isr(void){ if(RCIF) { byte = sci_GetByte(); sci_PutByte(byte); /* echo char */ } if(TXIF && TXIE) { sci_PutByte(message[i]); if(message[++i] == '\0') TXIE = 0; /* finished */ }}The interrupts can be enabled by setting the appropriatebits in the INTCON register. To use the SCI, enable thePEIE bit. If other interrupts are to be used, set thebits corresponding to these interrupts in this register.The global interrupt enable bit must also be set. TheTXIE and RCIE bits can then be used to mask and unmaskthe interrupts at various points in your code.sci_Init(9600, SCI_EIGHT);PIR1 = 0; /* clear any pending interrupts */PEIE = 1; /* enable perhipheral interrupts */GIE = 1; /* global interrupts enabled *//* perform other setup */RCIE = 1; /* unmask receiver interrupts... *//* an interrupt could now come from the SCI receiverat any time. *//* process data read in, if any */RCIE = 0; /* mask receive interrupts *//* no more interrupts can come from the receiver */See the PIC appropriate manual for further detailson the use of PIC interrupts.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -