📄 printp.c
字号:
/*********************************************************************/
/* printp() ------------ a simply version of printf() */
/**********************************************************************/
/* with the help of printp() function, you can get the out format as below:
printp("This first line outputs the format string.\n");
printp("%s\n","This second line outputs a string.");
printp( "Notice that the first %s\n","two output lines had CR/LF at the end.");
printp( "%c%c%c%c line outputs 'This' as chars.\n",'T', 'h', 'i', 's');
printp( "Dec values: %d %d Hex value: %x\n",dec_value, neg_value, hex_value);
printp( "Long dec values: %ld %ld\n",ldec_value,lneg_value);
printp( "Output sized strings: %20s %-20s\n","Right string","Left string");
printp( "Output sized values: %20d %-20d\n",dec_value,neg_value);
printp( " %20x %-20x\n",hex_value,hex_value);
printp( " %20ld %-20ld\n",ldec_value,ldec_value);
where the variables can be as:
int hex_value = 0x1234;
int dec_value = 1234;
int neg_value = -1234;
long ldec_value = 12345678L;
long lneg_value = -12345678L;
修改了部分printp()代码,
打印时如果 %后面的x,d为大写,打印的数不区分正负数
为小写,区分正负数
加l为取长型变量。
例如打印如下语句:
printp("\n here i is 0xF123ABCD, j is 0xF123");
printp("\n 1 : %%lX i is: %lX",i);
printp("\n 2 : %%lx i is: %lx",i);
printp("\n 3 : %%lD i is: %lD",i);
printp("\n 4 : %%ld i is: %ld",i);
printp("\n 5 : %%8X i is: %8X",i);
printp("\n 6 : %%8x i is: %8x",i);
printp("\n 7 : %%8D i is: %8D",i);
printp("\n 8 : %%8d i is: %8d",i);
printp("\n 9 : %%X j is: %X",j);
printp("\n 10: %%x j is: %x",j);
printp("\n 11: %%D j is: %D",j);
printp("\n 12: %%d j is: %d",j);
结果为:
here i is 0xF123ABCD, j is 0xF123
1 : %lX i is: F123ABCD
2 : %lx i is: -EDC5433
3 : %lD i is: 4045646797
4 : %ld i is: -249320499
5 : %8X i is: F123
6 : %8x i is: -EDD
7 : %8D i is: 61731
8 : %8d i is: -3805
9 : %X j is: F123
10: %x j is: -EDD
11: %D j is: 61731
12: %d j is: -3805 */
/************************************************************************/
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define Baud_9600 55
#define Baud_19200 27
typedef struct {
unsigned int SCCR0;
unsigned int SCCR1;
unsigned int SCSR;
unsigned int SCDR;
} SCIStruct;
/*******************User defines ***************************/
//the start address of SCI control registers
#define SCI_ADDR 0xFFFC08
#define BAUD_RATE Baud_9600 // 设置波特率
#define CR_as_CRLF TRUE // if true , you can use "\n" to act as CR/LF,
// if false , you have to use "\n\r",but can get a higher speed
//#define USE_INT_FOR_PRINT // 是否使用中断
/*------------------------------------------------------------------*/
#ifdef USE_INT_FOR_PRINT //do not care if not use interrupt
#define QSM_BASE_INTV 0x60 // 设置QSM模块中断向量地址
#define SCI_INT_LEVEL 0x01 // sci中断优先级,相当与INT7-1
#define VECT_BASE_ADDR 0x00 // 中断向量基地址
#define COMM_RX_BUF_SIZE 256
#define COMM_TX_BUF_SIZE 4096
#define COMM_NO_ERR 0x00
#define COMM_TX_FULL 0x01
#define COMM_RX_EMPTY 0x02
#define CommTxIntDis() SCI.SCCR1 &= 0xFF7F
#define CommTxIntEn() SCI.SCCR1 |= 0x0080
#ifndef QILR
#define QILR 0xFFFFFC04 //QSM interrupt level, high byte
#endif
#ifndef QIVR
#define QIVR 0xFFFFFC05 //QSM interrupt vector,low byte
#endif
/*-------------------------------------------------------------------*/
#endif
/***********************************************************/
SCIStruct SCI @SCI_ADDR;
#ifdef USE_INT_FOR_PRINT
typedef struct {
unsigned int RingBufRxCtr; /* Number of characters in the Rx ring buffer */
unsigned char *RingBufRxInPtr; /* Pointer to where next character will be inserted */
unsigned char *RingBufRxOutPtr; /* Pointer from where next character will be extracted */
unsigned char RingBufRx[COMM_RX_BUF_SIZE]; /* Ring buffer character storage (Rx) */
unsigned int RingBufTxCtr; /* Number of characters in the Tx ring buffer */
unsigned char *RingBufTxInPtr; /* Pointer to where next character will be inserted */
unsigned char *RingBufTxOutPtr; /* Pointer from where next character will be extracted */
unsigned char RingBufTx[COMM_TX_BUF_SIZE]; /* Ring buffer character storage (Tx) */
} COMM_RING_BUF;
COMM_RING_BUF CommBuf;
#pragma TRAP_PROC
#pragma NO_ENTRY
#pragma NO_EXIT
void TC_ISR(void)
{ COMM_RING_BUF *pbuf;
unsigned int SCSRtemp;
unsigned char SCDRtemp;
asm MOVEM.L A0-A6/D0-D7,-(A7)
asm LINK.W A6,#-8;
OSIntEnter();
SCSRtemp=SCI.SCSR;
//串口一发送中断
if((SCSRtemp & 0x100)!=0)
{
pbuf = &CommBuf;
while (!(SCI.SCSR & 0x100)); //clear TDRE flag
if (pbuf->RingBufTxCtr > 0) /* See if buffer is empty */
{
pbuf->RingBufTxCtr--; /* No, decrement character count */
SCI.SCDR = *pbuf->RingBufTxOutPtr++; /* Get character from buffer */
if (pbuf->RingBufTxOutPtr == &pbuf->RingBufTx[COMM_TX_BUF_SIZE]) pbuf->RingBufTxOutPtr = &pbuf->RingBufTx[0]; /* Wrap OUT pointer */
if(pbuf->RingBufTxCtr ==0) CommTxIntDis();
}
}
//串口一接受中断
if((SCSRtemp & 0x0040)!=0)
{
pbuf = &CommBuf;
while (!(SCI.SCSR & 0x0040));
if (pbuf->RingBufRxCtr < COMM_RX_BUF_SIZE) /* See if buffer is full */
{ pbuf->RingBufRxCtr++; /* No, increment character count */
*pbuf->RingBufRxInPtr++ = SCI.SCDR; /* Put character into buffer */
if (pbuf->RingBufRxInPtr == &pbuf->RingBufRx[COMM_RX_BUF_SIZE]) pbuf->RingBufRxInPtr = &pbuf->RingBufRx[0];/* Wrap IN pointer */
}
else SCDRtemp=SCI.SCDR;
}
asm UNLK A6;
OSIntExit();
asm MOVEM.L (A7)+,A0-A6/D0-D7;
asm rte;
}
void TERMIO_Init(void) {
COMM_RING_BUF *pbuf;
pbuf = &CommBuf;
*(unsigned char*) QIVR = QSM_BASE_INTV;
*(unsigned char*) QILR |= SCI_INT_LEVEL;
*(unsigned int *) 0xFFFC00 = 0x000F; //QSMCR
SCI.SCCR0 = BAUD_RATE;
*(long *)((QSM_BASE_INTV*4)+VECT_BASE_ADDR)=(long)TC_ISR;
pbuf->RingBufRxCtr = 0;
pbuf->RingBufRxInPtr = &pbuf->RingBufRx[0];
pbuf->RingBufRxOutPtr = &pbuf->RingBufRx[0];
pbuf->RingBufTxCtr = 0;
pbuf->RingBufTxInPtr = &pbuf->RingBufTx[0];
pbuf->RingBufTxOutPtr = &pbuf->RingBufTx[0];
SCI.SCCR1 = 0x002C; //enable TDRE interrupt
}
unsigned char IsCommTxFull()
{
unsigned char IsEmpty;
COMM_RING_BUF *pbuf;
unsigned int SR_temp;
pbuf = &CommBuf;
asm move.w SR,SR_temp;
DisableInterrupts;
if (pbuf->RingBufTxCtr < COMM_TX_BUF_SIZE) IsEmpty = FALSE; /* See if buffer is full */
else IsEmpty = TRUE;
asm move.w SR_temp,SR;
return (IsEmpty);
}
unsigned char CheckCommRxBuf()
{
COMM_RING_BUF *pbuf;
unsigned int SR_temp;
unsigned char Rxsize;
pbuf = &CommBuf;
asm move.w SR,SR_temp;
DisableInterrupts;
Rxsize = pbuf->RingBufRxCtr ;
asm move.w SR_temp,SR;
return (Rxsize);
}
unsigned char IsCommRxEmpty()
{
unsigned char IsEmpty;
COMM_RING_BUF *pbuf;
unsigned int SR_temp;
pbuf = &CommBuf;
asm move.w SR,SR_temp;
DisableInterrupts;
if (pbuf->RingBufRxCtr > 0) IsEmpty = FALSE; /* See if buffer is empty */
else IsEmpty = TRUE;
asm move.w SR_temp,SR;
return (IsEmpty);
}
/*
*********************************************************************************************************
* SEND A CHARACTER TO THE OUT BUFFER
*
*
* Description : This function is called by your application to send a character on the communications
* channel. The character to send is first inserted into the Tx buffer and will be sent by
* the Tx ISR. If this is the first character placed into the buffer, the Tx ISR will be
* enabled. If the Tx buffer is full, the character will not be sent (i.e. it will be lost)
* Arguments : 'Device' is the COMM port channel number and can either be:
* Dev_UART0,Dev_UART1,Dev_UART2
* 'ch' is the data to be send
* Returns : COMM_NO_ERR OK
* COMM_TX_FULL TX buffer is full, you should wait
*********************************************************************************************************
*
* 调用前最好使用 IsCommTxFull()检查一下
*
*********************************************************************************************************/
unsigned char TERMIO_PutChar(unsigned char ch)
{
COMM_RING_BUF *pbuf;
unsigned int SR_temp;
pbuf = &CommBuf;
asm move.w SR,SR_temp;
DisableInterrupts;
if (pbuf->RingBufTxCtr < COMM_TX_BUF_SIZE) /* See if buffer is full */
{
pbuf->RingBufTxCtr++; /* No, increment character count */
*pbuf->RingBufTxInPtr++ = ch; /* Put character into buffer */
if (pbuf->RingBufTxInPtr == &pbuf->RingBufTx[COMM_TX_BUF_SIZE]) {pbuf->RingBufTxInPtr = &pbuf->RingBufTx[0]; }
if (pbuf->RingBufTxCtr == 1) CommTxIntEn(); /* See if this is the first character */
/* Yes, Enable Tx interrupts */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -