본문 바로가기

Embedded/dsPIC

MPLab C30 printf 사용하기


write()함수를 오버라이딩.

#include <stdio.h> 추가하고 아래 write()함수를 추가해 주면 된다.

int write(int handle, void *buffer, unsigned int len)

{

int i;
for (i = len; i; --i)
{
char c = *(char*)buffer++;

U1TXREG = c;
while(!U1STAbits.TRMT);
}
return(len);
}

그리고 PIC은 Heap영역을 따로 할당해 주어야 한다.
MPLAB Project Builder Option에서 힙영역을 할당해 주면 된다.



물론 printf를 사용하면 20%정도 오버헤드가 걸린다.

printf 사용하지 않았을 경우
section address length (PC units) length (bytes) (dec)
------- ------- ----------------- --------------------
.reset 0 0x4 0x6 (6)
.ivt 0x4 0xfc 0x17a (378)
.aivt 0x104 0xfc 0x17a (378)
.text 0x200 0x2cc 0x432 (1074)
.const 0x4cc 0x32 0x4b (75)
.dinit 0x4fe 0x8 0xc (12)
.isr 0x506 0x2 0x3 (3)
__FOSCSEL 0xf80006 0x2 0x3 (3)
__FOSC 0xf80008 0x2 0x3 (3)
__FWDT 0xf8000a 0x2 0x3 (3)
Total program memory used (bytes): 0x78f (1935) 5%
Total data memory used (bytes): 0x2 (2) <1%


printf 사용했을경우
section address length (PC units) length (bytes) (dec)
------- ------- ----------------- --------------------
.reset 0 0x4 0x6 (6)
.ivt 0x4 0xfc 0x17a (378)
.aivt 0x104 0xfc 0x17a (378)
.text 0x200 0x116a 0x1a1f (6687)
.const 0x136a 0x3c 0x5a (90)
.dinit 0x13a6 0xbc 0x11a (282)
.isr 0x1462 0x2 0x3 (3)
__FOSCSEL 0xf80006 0x2 0x3 (3)
__FOSC 0xf80008 0x2 0x3 (3)
__FWDT 0xf8000a 0x2 0x3 (3)
Total program memory used (bytes): 0x1e99 (7833) 23%
Total data memory used (bytes): 0xd0 (208) 10%