📄 host.c
字号:
#include "hardware.h"
#include "spi.h"
#include "anticollision.h"
#include "globals.h"
#include "tiris.h"
#include "14443.h"
#include "host.h"
#include "epc.h"
#include "automatic.h"
unsigned char RXdone;
unsigned char ENABLE;
unsigned char FirstSPIdata = 1;
/*
=======================================================================================================================
=======================================================================================================================
*/
void USARTset(void)
{
U0CTL |= SWRST; /* disable USART */
ME1 |= UTXE0 + URXE0; /* transmitter and reciever enable */
U0CTL &= ~SWRST; /* enable USART */
U0CTL |= CHAR; /* 8 data bits */
U0TCTL = SSEL0; /* source clock is main clock */
U0BR0 = BAUD0; /* baud rate register */
U0BR1 = BAUD1;
IE1 |= URXIE0; /* interrupt enable fo RX */
P3DIR |= BIT4; /* P3.4 - output */
P3OUT |= BIT4; /* P3.4 - output 1 */
P3SEL |= BIT4 + BIT5; /* P3.4, P3.5 UART mode */
} /* USARTset */
/*
=======================================================================================================================
=======================================================================================================================
*/
void BaudSet(unsigned char mode)
{
if(mode == 0x00)
{
U0BR0 = BAUD0; /* baud rate register */
U0BR1 = BAUD1;
}
else
{
U0BR0 = BAUD0EN; /* baud rate register */
U0BR1 = BAUD1EN;
}
} /* BaudSet */
/*
=======================================================================================================================
=======================================================================================================================
*/
void kputchar(char TXchar)
{
while(!(IFG1 & UTXIFG0));
/* wait for TX register to be empty */
U0TXBUF = TXchar; /* send the character */
} /* putchar */
/*
=======================================================================================================================
=======================================================================================================================
*/
void put_bksp(void)
{
kputchar('\b');
kputchar(' ');
kputchar('\b');
} /* put_bksp */
/*
=======================================================================================================================
=======================================================================================================================
*/
void put_space(void)
{
kputchar(' ');
} /* put_space */
/*
=======================================================================================================================
=======================================================================================================================
*/
void put_crlf(void)
{
kputchar('\r');
kputchar('\n');
} /* put_crlf */
/*
=======================================================================================================================
Send a character string to USART ;
=======================================================================================================================
*/
void send_cstring(char *pstr)
{
while(*pstr != '\0')
{
kputchar(*pstr++);
}
}
/*
=======================================================================================================================
convert a nibble to ASCII hex byte ;
=======================================================================================================================
*/
unsigned char Nibble2Ascii(unsigned char anibble)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned char AsciiOut = anibble;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(anibble > 9) /* If req ASCII A-F then add 7(hex) */
AsciiOut = AsciiOut + 0x07;
/* Add offset to convert to Ascii */
AsciiOut = AsciiOut + 0x30;
return(AsciiOut);
}
/*
=======================================================================================================================
end of Nibble2Ascii output a binary coded byte as two hex coded ascii bytes ;
=======================================================================================================================
*/
void Put_byte(unsigned char abyte)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned char temp1, temp2;
/*~~~~~~~~~~~~~~~~~~~~~~~~~*/
temp1 = (abyte >> 4) & 0x0F; /* get high nibble */
temp2 = Nibble2Ascii(temp1); /* convert to ASCII */
kputchar(temp2); /* output */
temp1 = abyte & 0x0F; /* get low nibble */
temp2 = Nibble2Ascii(temp1); /* convert to ASCII */
kputchar(temp2); /* output */
}
/*
=======================================================================================================================
end of Put_byte get a hex coded nibble
=======================================================================================================================
*/
unsigned char Get_nibble(void)
{
/*~~~~~~~~~~~~~~~~~~~~*/
unsigned char reading;
unsigned char rxdata;
/*~~~~~~~~~~~~~~~~~~~~*/
reading = 1; /* flag: reading not yet finished */
while(reading)
{ /* loop and read characters */
LPM0; /* sync, wakeup by irq */
if(rxdata >= 'a')
{
rxdata -= 32;
} /* change to uppercase */
/* echo if hex */
if(((rxdata >= '0') && (rxdata <= '9')) || ((rxdata >= 'A') && (rxdata <= 'F')))
{
reading = 0;
kputchar(rxdata); /* echo */
if(rxdata > '9')
{ /* If ASCII A-F then add 9 */
rxdata = (rxdata & 0x0F) + 9;
}
}
/* else discard */
}
return(rxdata);
}
/*
=======================================================================================================================
end of Get_nibble get a line of characters ;
returns 0 if no error, <> 0 if error input ;
allows only "0,..,9","A,..,F" or "a,..,f", <CR>,<LF>,<backspace> ;
ignores other characters ;
=======================================================================================================================
*/
unsigned char Get_line(unsigned char *pline)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned char reading, err_flg;
unsigned char pos; /* counts number of received nibbles */
unsigned char Length_Byte; /* max 256 bytes allowed (512 nibbles) */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
err_flg = 0; /* assume no error */
Length_Byte = 0xff; /* assume max number of bytes */
/* wait for SOF: '0' followed by '1' */
if(!FirstSPIdata)
{
LPM0; /* sync, wakeup by irq */
while(rxdata != '0')
{
}
}
else
{
FirstSPIdata = 0;
}
kputchar('0');
LPM0; /* sync, wakeup by irq */
while(rxdata != '1')
{
}
kputchar('1');
RXdone = 0;
pos = 0; /* character position counter - 8bit */
reading = 1; /* flag: reading not yet finished */
while(reading)
{ /* loop and read characters */
while(RXdone == 0);
/* sync, wakeup by irq */
RXdone = 0;
switch(rxdata)
{
/* process RETURN key */
case '\r':
break; /* ignore CR */
case '\n':
reading = 0; /* exit read loop */
break;
/* backspace */
case '\b':
if(pos > 0)
{ /* is there a char to delete? */
pos--; /* remove it from buffer */
put_bksp(); /* go back and erase on screen */
if(pos & 0x01 > 0)
{ /* (high) even byte */
*pline--;
*pline &= 0xF0; /* clear lo nibble */
}
}
break;
/* other characters */
default:
if(rxdata >= 'a')
{
rxdata -= 32;
} /* change to uppercase */
/* discard if not hex */
if((rxdata < '0') || ((rxdata > '9') && (rxdata < 'A')) || (rxdata > 'F'))
{
break;
}
/* only store characters if buffer has space */
if(pos++ < 2 * BUF_LENGTH)
{
kputchar(rxdata); /* echo */
if(rxdata > '9')
{ /* If ASCII A-F then add 9 */
rxdata = (rxdata & 0x0F) + 9;
}
if((pos & 0x01) == 0)
{ /* (low) odd nibble */
*pline += (rxdata & 0x0F); /* store */
if(pos == 2)
{
/*
* just finished receiving 2 nibbles containing number of expected data bytes ;
* change Length_Bytes (total number of expected data bytes)
*/
Length_Byte = *pline;
}
pline++;
if(((Length_Byte - 1) * 2) == pos)
{
reading = 0; /* flag loop exit - done */
}
}
else
{ /* (high) even nibble */
rxdata = rxdata << 4; /* Move to high nibble */
*pline = rxdata; /* store */
}
}
else
err_flg = 1;
}
}
return(err_flg); /* normal exit */
}
/* end of Get_line */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -