void print_chr(unsigned char c)
{
int i;
switch (c) {
case '\n': UART_1_PutCRLF(); break; // carriage return
case '\r': break; // dummy
default: UART_1_PutChar(c); break; // normal character
}
}
extern void printf(const unsigned char *string, ...);
:
printf("\nDATA:%d",data);
//extern void print_chr(unsigned char c); // mainの関数をそのまま使わず、ポインタを使うように変更
static void (*print_chr)(unsigned char c);
//void printf(const unsigned char *string, unsigned int arg) // 出力ルーチンを指定出来るように変更
void xprintf( void(*outfunc)(unsigned char c), const unsigned char *string, unsigned int arg)
{
print_chr = outfunc; // 関数ポインタをコピー
prtout(string, &arg);
}
void txout(unsigned char c)
{
int i;
switch (c) {
case '\f': UART_1_PutCRLF(); break; // carriage return
case '\n': UART_1_PutCRLF(); break; // carriage return
case '\b': break; // dummy
case '\r': break; // dummy
default: UART_1_PutChar(c); break; // normal character
}
}
void lcdout(unsigned char c)
{
int i;
switch (c) {
case '\f': LCD_1_Control(0x01); // clear screen (1st line home)
LCD_1_Delay50uTimes(40); break; // (Execution delay 2ms)
case '\n': LCD_1_Position(1,0); break; // carriage return (2nd line home)
case '\b': LCD_1_Control(0x10); break; // backward
case '\r': break; // dummy
default: LCD_1_WriteData(c); break; // normal character
}
}
extern void xprintf(void(*outfunc)(unsigned char c), const unsigned char *string, ...);
xprintf(lcdout,">Data:%d",xxx); // LCDへ出力
xprintf(txout,">Data:%d",xxx); // UARTへ出力
DWORD data;
data=0xFE000000;
printf("DATA:%08LX",data);
data=0x12345678;
printf("DATA:%08LX",data);
DATA:0000FE00
DATA:12345678
if (data.word[1]) { // 32bit data
}
else { // 16bit data
}
static void print_val(unsigned int width,
unsigned int padchar,
unsigned int base,
unsigned int lflag, // longflagを渡すように
union DATAS data)
{
:
:
// if (data.word[1]) { // word[1]で判断するのは止めて
if (lflag) { // longflagで判断するように
: // 32bitデータの表示
print_val(width,padchar,16,longflag,udat); // これはXの場合
data=0x12345678;
printf("DATA:%08LX",data);
DATA:1F001234
const char string_data[] = "ABCDE";
const char name[4][6] = {"Alpha","Beta ","Gamma","Delta"};
xprintf(lcdout,"\f>%s:%d",name[n],data); // nを0〜3に変えれば、Alpha〜Deltaと表示が変わる
static void print_strx(unsigned char *c)
{
while(*c)
print_chr(*c++);
}
case 'S': width -= slen((unsigned char *)(*param));
print_pads(width,' ');
print_strx((unsigned char *)(*param));
param--;
break;
const char cstring_data[] = "ABCDE"; // フラッシュROM上に文字列をセット
char string_data[] = "ABCDE"; // RAM上に文字列をセット
xprintf(lcdout,"\fcstring:%s",cstring_data): // 通常はフラッシュ上の文字列表示
xprintf(lcdout,"\n string:%S",string_data); // RAM上の文字列を表示する場合
#include <stdio.h>
int putchar(char c)
{
LCD_1_WriteData(c);
}
void main(void)
{
LCD_1_Start();
cprintf("Hello World!");
}
#include <stdio.h>
static void (*putchar_out)(unsigned char c);
void txout(unsigned char c)
{
int i;
switch (c) {
case '\f': break; // dummy
case '\n': UART_1_PutCRLF(); break; // CRLF
case '\b': break; // dummy
case '\r': /*UART_1_PutChar(13);*/ break; // CR
default: UART_1_PutChar(c); break; // normal character
}
}
void lcdout(unsigned char c)
{
int i;
switch (c) {
case '\f': LCD_1_Control(0x01); // clear screen (1st line home)
LCD_1_Delay50uTimes(40); break; // (Execution delay 2ms)
case '\1': LCD_1_Position(0,0); break; // carriage return (1nd line home)
case '\2': LCD_1_Position(1,0); break; // carriage return (2nd line home)
case '\n': break; // dummy
case '\b': LCD_1_Control(0x10); break; // backward
case '\r': break; // dummy
default: LCD_1_WriteData(c); break; // normal character
}
}
int putchar(char c)
{
putchar_out(c);
return c;
}
putchar_out = lcdout;
cprintf("\fPSoC LCD TEST");
putchar_out = txout;
cprintf("PSoC UART TEST\n");
#define xprintf(outproc, ...) \
putchar_out = outproc; \
cprintf(__VA_ARGS__)
xprintf(lcdout,"Hello World!");
cprintf("%04X",data);
int putchar(char c)
{
char str[] = " ";
strncpy(str,&c,1);
LCD_1_PrString(str);
return c;
}
Conclusion how to use "printf()"
For all compillers add include to stdio library:
#include
For PSoC 3(Keil) revise putchar() function with communication component which has to send data:
char putchar( char c)
{
UART_1_PutChar(c);
return c;
}
For PSoC 5(GCC) revise _write() function:
int _write(int file, char *ptr, int len)
{
int i;
for (i = 0; i < len; i++)
{
UART_1_PutChar(*ptr++);
}
return len;
}
Add an explicit reference to the floating point printf library to allow the usage of floating point conversion specifier:
#if defined (__GNUC__)
asm (".global _printf_float");
#endif
Now you can use printf() which will send formatted data through "UART_1".
void main()
{
uint32 i = 444444444;
float f = 55.55555f;
CyGlobalIntEnable; /* Enable interrupts */
UART_1_Start(); /* Start communication component */
printf("Test printf function.long:%ld,float:%f \r\n",i,f);
}
The log from terminal software:
Test printf function.long:444444444,float:55.555553
Hope it will help.
void USBUART_printf(const char *form, ...)
{
va_list argp;
char line[256];
char *pline;
va_start(argp,form);
vsnprintf(line,256,form,argp);
va_end(argp);
pline = line;
while(*pline) {
while(!USBUART_1_CDCIsReady());
USBUART_1_PutChar(*pline++);
}
}
void USBUART_putc(unsigned char c)
{
while(!USBUART_1_CDCIsReady());
USBUART_1_PutChar(c);
}
unsigned char USBUART_getc(void)
{
while(!USBUART_1_DataIsReady());
return USBUART_1_GetChar();
}
xdev_out(USBUART_putc);
xdev_in(USBUART_getc);
#include "xprintf.h"
char line[16];
char *pline;
long data;
xgets(line,16);
xprintf(">>>>Program Start!\n\r");
for(;;) {
xprintf("> parameter? ");
xgets(line,16);
pline = line;
xatoi(&pline,&data);
:
:
:
}