#include<p18cxxx.h>
#include<usart.h>
#pragma config OSC=HS
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF
#pragma config MCLRE=OFF
////////////////////////////////////////////////////////////////////
#pragma interrupt MyHighInt
#pragma code high_vector=0x08
void high_vector(void)
{
_asm GOTO MyHighInt _endasm
}
#pragma interruptlow MyLowInt
#pragma code low_vector=0x18
void low_vector(void)
{
_asm GOTO MyLowInt _endasm
}
//////////////////////////////////////////////////////////////////
char inQueue[16];
char outQueue[16];
char inPi;
char inPo;
char outPi;
char outPo;
////////////////////////////////////////////
#pragma code
int outInQueue(void)
{
int temp;
if(inPi==inPo){
return 0x100;
}
temp=inQueue[inPo];
inPo=(inPo+1)&0x0f;
return temp;
}
int inInQueue(char data)
{
if(inPi==((inPo+1)&0x0f)){
return 0x100;
}
inQueue[inPi]=data;
inPi=(inPi+1)&0x0f;
return 0;
}
int outOutQueue(void)
{
int temp;
if(outPi==outPo){
return 0x100;
}
temp=outQueue[outPo];
outPo=(outPo+1)&0x0f;
return temp;
}
int inOutQueue(char data)
{
if(outPi==((outPo+1)&0x0f)){
return 0x100;
}
outQueue[outPi]=data;
outPi=(outPi+1)&0x0f;
PIE1bits.TXIE=1;
return 0;
}
////////////////////////////////////////////
void main(void)
{
int temp;
ADCON1=0x0f;
TRISC=0x80;
PORTC=0;
IPR1bits.TXIP=1;
IPR1bits.RCIP=1;
inPo=inPi=outPo=outPi=0;
OpenUSART(USART_TX_INT_ON&
USART_RX_INT_ON&
USART_ASYNCH_MODE&
USART_EIGHT_BIT&
USART_CONT_RX&
USART_BRGH_HIGH,
25);
RCONbits.IPEN=1;
INTCONbits.GIEH=1;
INTCONbits.GIEL=0;
inOutQueue('I');
inOutQueue(' ');
inOutQueue('a');
inOutQueue('m');
inOutQueue(' ');
inOutQueue('P');
inOutQueue('I');
inOutQueue('C');
inOutQueue('1');
inOutQueue('8');
inOutQueue('F');
inOutQueue('2');
inOutQueue('2');
inOutQueue('2');
inOutQueue('0');
while(1)
{
}
}
//////////////////////////////////////////////////
void MyHighInt(void)
{
int temp;
if(PIR1bits.RCIF==1)
{
PIR1bits.RCIF=0;
inInQueue(RCREG); //ПРИЕМ ДАННЫХ
}
else if(PIR1bits.TXIF==1&&PIE1bits.TXIE==1)
{
PIR1bits.TXIF=0;
temp=outOutQueue();
if(temp==0x100)
{
while(TXSTAbits.TRMT==0);
PIE1bits.TXIE=0;
}
else
TXREG=temp; //ПЕРЕДАЧА ДАННЫХ
}
}
///////////////////////////////////////////////////
void MyLowInt(void)
{
}
void CmyCOMDlg::OpenCom(CString port,DWORD baudrate)
{
COMMTIMEOUTS Timeout;
hPort=CreateFile(
port,
GENERIC_WRITE|GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
GetCommState(hPort,&dcb);
dcb.BaudRate=baudrate;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
SetCommState(hPort,&dcb);
Timeout.ReadIntervalTimeout=500;
Timeout.ReadTotalTimeoutConstant=500;
Timeout.ReadTotalTimeoutMultiplier=100;
Timeout.WriteTotalTimeoutConstant=500;
Timeout.WriteTotalTimeoutMultiplier=100;
SetCommTimeouts(hPort,&Timeout);
}
void CmyCOMDlg::SendPort(CString str)
{
DWORD byteswritten;
WriteFile(hPort,str,1,&byteswritten,0);
return;
}
CString CmyCOMDlg::ReadPort(void)
{
char mes[100];
DWORD transferred;
DWORD error;
COMSTAT status;
do{
ClearCommError(hPort,&error,&status);
mes[status.cbInQue]=0;
ReadFile(hPort,&mes,status.cbInQue,&transferred,0);
}while(status.cbInQue==0);
return mes;
}