📄 term.c
字号:
/*
Minimum Terminal interface
Si-Tecno Juha Niinikoski 11.03.2003
DDS modifications 03.04.2004
PC interface additions 17.05.2004
*/
void serial_setup(void)
{
#define DIVIDER 103 // 103 divider for 9600 16MHz
#define HIGH_SPEED 1
SPBRG=DIVIDER;
BRGH=HIGH_SPEED; //data rate for sending
SYNC=0; //asynchronous
SPEN=1; //enable serial port pins
SREN=0; //no effect
TXIE=0; //disable tx interrupts
RCIE=0; //disable rx interrupts
TX9=0; //8-bit transmission
RX9=0; //8-bit reception
CREN=0; //reset rx
TXEN=0; //reset transmitter
CREN=1; //enable reception
TXEN=1; //enable the transmitter
}
/* Writes a character to the serial port */
void sio_putch(unsigned char c)
{
while(!TXIF) //set when register is empty
{
if (OERR) // this is neded to prevent UART lockup ?
{
TXEN=0;
CREN=0;
CREN=1;
TXEN=1;
}
}
TXREG=c;
}
/* Gets a character from the serial port without timeout */
unsigned char sio_getch(void)
{
unsigned char dummy;
while(!RCIF)
{
if (OERR) // reset receiver if overrun errors
{
TXEN=0;
TXEN=1;
CREN=0;
CREN=1;
}
if (FERR) // clear UART if framing error
{
dummy=RCREG;
TXEN=0;
TXEN=1;
}
}
return RCREG;
}
/* Check if character available, but do not read character serial port
returns 0 if no characters available. Returns 1 if character available. */
unsigned char keypressed(void)
{
unsigned char dummy;
if (OERR) // reset UART if overrun error
{
TXEN=0;
TXEN=1;
CREN=0;
CREN=1;
return(0); // if errors return not pressed
}
if (FERR) // clear UART if framing error
{
dummy=RCREG;
TXEN=0;
TXEN=1;
return(0); // if errors return not pressed
}
if(RCIF) // check if character available
return(1);
else
return(0);
}
/* Get unsigned long from serial port */
/* Get unsigned long */
long getlong(void)
{
long val;
char tmp;
val = 0;
do
{
tmp = sio_getch() & 0x7F;
putch(tmp);
if(tmp >= '0' && tmp <= '9')
val = 10 * val + (tmp - '0');
}
while (tmp != 0x0D);
return(val);
}
// Writes a character to the serial port in hex
void sio_putchhex(unsigned char c)
{
unsigned char temp;
temp=c;
c=(c >> 4);
if (c<10) c+=48; else c+=55;
sio_putch(c);
c=temp;
c=(c & 0x0F);
if (c<10) c+=48; else c+=55;
sio_putch(c);
}
void sio_putinthex(unsigned int c)
{
#define ramuint(x) (*((unsigned int *) (x)))
#define ramuint_hibyte(x) (*(((unsigned char *)&x)+1))
#define ramuint_lobyte(x) (*(((unsigned char *)&x)+0))
#define ramuchar(x) (*((unsigned char *) (x)))
sio_putchhex(ramuint_hibyte(c));
sio_putchhex(ramuint_lobyte(c));
#undef ramuint(x)
#undef ramuint_hibyte(x)
#undef ramuint_lobyte(x)
#undef ramuchar(x)
}
// Writes a character to the serial port in decimal
void sio_putchdec(unsigned char c)
{
unsigned char temp;
temp=c;
//hundreds
if ((c/100)>0) sio_putch((c/100)+'0');
c-=(c/100)*100;
//tens
if (((temp/10)>0) || ((temp/100)>0)) sio_putch((c/10)+'0');
c-=(c/10)*10;
//ones
sio_putch((c/1)+'0');
}
// Write int to serial port
void sio_putintdec( int c, char dp)
{
int temp;
unsigned char neg_sign;
if (c > 9999) /* clip to max reading */
c = 9999;
if (c < 0) /* check if negative value */
{
neg_sign = 1;
c = 0 - c; /* invert it */
if (c > 999)
c = 999; /* max negative reading */
}
else
neg_sign = 0;
temp=c;
//thousands
if (neg_sign == 1)
sio_putch('-');
else
{
if ((c/1000)>0 || (dp >= 3))
sio_putch((c/1000)+'0');
else
sio_putch(' ');
}
c-=(c/1000)*1000;
if (dp == 3)
sio_putch('.');
//hundreds
if ( ((temp/100)>0) || ((temp/1000)>0) || (dp >= 2))
sio_putch((c/100)+'0');
else
sio_putch(' ');
c-=(c/100)*100;
if (dp == 2)
sio_putch('.');
//tens
if ( ((temp/10)>0) || ((temp/100)>0) || ((temp/1000)>0) || (dp >= 1) )
sio_putch((c/10)+'0');
else
sio_putch(' ');
c-=(c/10)*10;
if (dp == 1)
sio_putch('.');
//ones
sio_putch((c/1)+'0');
}
/* Write long in decimal format to serial port, no separator points */
/* if mode !=0 do "short" frequency display */
void sio_putlongdec( long x, char mode )
{
if(mode == 0) // full display
{
//giga
sio_putch((x/1000000000)+'0');
x-=(x/1000000000)*1000000000;
//100_million
sio_putch((x/100000000)+'0');
x-=(x/100000000)*100000000;
}
//10_million
sio_putch((x/10000000)+'0');
x-=(x/10000000)*10000000;
//million
sio_putch((x/1000000)+'0');
x-=(x/1000000)*1000000;
//100_thousands
sio_putch((x/100000)+'0');
x-=(x/100000)*100000;
//10_thousands
sio_putch((x/10000)+'0');
x-=(x/10000)*10000;
//thousands
sio_putch((x/1000)+'0');
x-=(x/1000)*1000;
//hundreds
sio_putch((x/100)+'0');
x-=(x/100)*100;
//tens
sio_putch((x/10)+'0');
x-=(x/10)*10;
//ones
sio_putch((x/1)+'0');
}
// Writes a character to the serial port in binary
void sio_putchbin(unsigned char ch)
{
unsigned char x;
for(x=0; x < 8; x++)
{
if (ch & 0x80)
sio_putch('1');
else
sio_putch('0');
ch = ch << 1;
}
}
// Put string
void sio_putst(register const char *str)
{
while((*str)!=0)
{
sio_putch(*str);
if (*str==13) sio_putch(10);
if (*str==10) sio_putch(13);
str++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -