Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 04, 2016, 12:53:34 00:53


Login with username, password and session length


Pages: [1]
Print
Author Topic: CCS printf() is not too bright  (Read 2172 times)
0 Members and 1 Guest are viewing this topic.
caveman508
Junior Member
**
Offline Offline

Posts: 41

Thank You
-Given: 12
-Receive: 6

READ, REAd, REad, Read


« on: February 18, 2008, 01:27:16 01:27 »

Hi all,

I have been doing development on a good size project and became very aware
that I was gobbling up program space very quickly with the scattered printf().
Well, I didn't care until the end when I ran out of ROM, so I did some
playing around. CCS is not the brightest bulb in the box when it come to some
things, and the following code shows it. Look how it uses a different call
address when doing:
   printf("TEST_OK 1?\r");
   printf("TEST_OK 2?\r");
   printf("TEST_OK 3?\r");
   printf("TEST_OK 4?\r");

So, me and a friend came up with the print() method (without a f), I saved
over 1K of space in 30 prints of a constant string, all with no reference to a
variable, just a constant string.  I first tried the MyPrint but they won't
allow a pointer to const, but what the %^&*)( is "printf("TEST_OK 1?\r"); "
doing then?HuhHuh

Look how elegant it does the code for:
   print(T4);
   print(T5);
   print(T6);

Anyone out there that could shed some further light on other ways would be
great.

Thanks,

Caveman


Code:
#include <18f4423.h>
#define CLOCKSPEED 40000000
#define BAUDRATE   57600

#fuses H4, PUT, BROWNOUT, NOPBADEN, MCLR, NOWDT, NOLVP, DEBUG, NOPROTECT, STVREN, NOCPB, NOCPD, NOWRT
#zero_ram
#device ADC=12
#use delay( clock = CLOCKSPEED, RESTART_WDT )
#case
#use rs232( baud=BAUDRATE, parity=n, xmit=PIN_C6, rcv=PIN_C7, bits=8 )

typedef enum _str_num {
T1, \
T2, \
T3, \
T4, \
T5, \
T6, \
T7, \
T8, \
T9
};

const char c_str[][*] =
{
"TEST 1",
"TEST 2",
"TEST 3",
"TEST 4",
"TEST 5",
"TEST 6",
"TEST 7",
"TEST 8",
"TEST 9"
};
void print(_str_num num) {
printf("%s\n\r", c_str[num]);
}
typedef enum _some_status { STAT_OK, STAT_1 =3, STAT_2 =6, STAT_3 =10, STAT_4, STAT_5, STAT_6, STAT_7 };

void PrintAStatus( _some_status stat ){
const char stat_str[][*] =
{
"OK",
"1",
"2",
"Stat: 1",
"4",
"5",
"Stat: 2",
"7",
"8",
"9",
"Stat: 3",
"Stat: 4",
"Stat: 5",
"Stat: 6",
"Stat: 7",
};
if ( stat > STAT_7 ) {
    printf( "Stat : ERR\n\r");
} else {
    printf( "Status : %s\n\r", stat_str[stat] );
}
}

//void MyPrint(const char * cptr) {
// printf("%s", cptr);
//}

void main(void) {
_some_status my_stat = STAT_2;

PrintAStatus(my_stat);
PrintAStatus(++my_stat);
PrintAStatus(STAT_5);
PrintAStatus(9);
PrintAStatus(14);
PrintAStatus(104);

printf("TEST_OK?\r");
printf("TEST_OK?\r");
    printf("TEST_OK?\r");
    printf("TEST_OK 1?\r");
    printf("TEST_OK 2?\r");
    printf("TEST_OK 3?\r");
    printf("TEST_OK 4?\r");
print(T4);
print(T5);
print(T6);
// MyPrint("Test MyPrint\n\r");
for (;;);
}

Here is the listing:
Code:


[CCS PCH C Compiler, Version 4.050, 40210               17-Feb-08 17:21

               Filename: C:\Projects\Firmware\DREAM\THE_TH~1.lst

               ROM used: 1022 bytes (6%)
                         Largest free fragment is 15362
               RAM used: 7 (1%) at main() level
                         12 (2%) worst case
               Stack:    2 locations

*
0000:  GOTO   02D6
.................... #include <18f4423.h>
.................... //////// Standard Header file for the PIC18F4423 device ////////////////
.................... #device PIC18F4423
.................... #list
.................... 
.................... #define CLOCKSPEED 40000000
.................... #define BAUDRATE   57600
.................... 
.................... #fuses H4, PUT, BROWNOUT, NOPBADEN, MCLR, NOWDT, NOLVP, DEBUG, NOPROTECT, STVREN, NOCPB, NOCPD, NOWRT
.................... #zero_ram
.................... #device ADC=12
.................... #use delay( clock = CLOCKSPEED, RESTART_WDT )
.................... #case
.................... #use rs232( baud=BAUDRATE, parity=n, xmit=PIN_C6, rcv=PIN_C7, bits=8 )
.................... 
.................... typedef enum _str_num {
.................... T1, \
.................... T2, \
.................... T3, \
.................... T4, \
.................... T5, \
.................... T6, \
.................... T7, \
.................... T8, \
.................... T9
.................... };
.................... 
.................... const char c_str[][*] =
.................... {
.................... "TEST 1",
.................... "TEST 2",
.................... "TEST 3",
.................... "TEST 4",
.................... "TEST 5",
.................... "TEST 6",
.................... "TEST 7",
.................... "TEST 8",
.................... "TEST 9"
.................... };
.................... void print(_str_num num) {
.................... printf("%s\n\r", c_str[num]);
*
0288:  CLRF   09
028A:  MOVFF  07,08
028E:  MOVLW  01
0290:  MOVWF  0B
0292:  CLRF   0A
0294:  RCALL  01E4
0296:  MOVFF  02,03
029A:  MOVFF  01,08
029E:  MOVFF  02,09
02A2:  MOVFF  02,0B
02A6:  MOVFF  01,0A
02AA:  MOVFF  0B,FF7
02AE:  MOVF   0A,W
02B0:  RCALL  0004
02B2:  IORLW  00
02B4:  BZ    02C4
02B6:  INCF   0A,F
02B8:  BTFSC  FD8.2
02BA:  INCF   0B,F
02BC:  BTFSS  F9E.4
02BE:  BRA    02BC
02C0:  MOVWF  FAD
02C2:  BRA    02AA
02C4:  MOVLW  0A
02C6:  BTFSS  F9E.4
02C8:  BRA    02C6
02CA:  MOVWF  FAD
02CC:  MOVLW  0D
02CE:  BTFSS  F9E.4
02D0:  BRA    02CE
02D2:  MOVWF  FAD
.................... }
02D4:  RETLW  00
.................... typedef enum _some_status { STAT_OK, STAT_1 =3, STAT_2 =6, STAT_3 =10, STAT_4, STAT_5, STAT_6, STAT_7 };
.................... 
.................... void PrintAStatus( _some_status stat ){
.................... const char stat_str[][*] =
.................... {
.................... "OK",
.................... "1",
.................... "2",
.................... "Stat: 1",
.................... "4",
.................... "5",
.................... "Stat: 2",
.................... "7",
.................... "8",
.................... "9",
.................... "Stat: 3",
.................... "Stat: 4",
.................... "Stat: 5",
.................... "Stat: 6",
.................... "Stat: 7",
.................... };
.................... if ( stat > STAT_7 ) {
*
0202:  MOVF   07,W
0204:  SUBLW  0E
0206:  BC    0222
....................      printf( "Stat : ERR\n\r");
0208:  CLRF   08
020A:  MOVF   08,W
020C:  RCALL  011E
020E:  INCF   08,F
0210:  MOVWF  00
0212:  MOVF   00,W
0214:  BTFSS  F9E.4
0216:  BRA    0214
0218:  MOVWF  FAD
021A:  MOVLW  0C
021C:  SUBWF  08,W
021E:  BNZ   020A
.................... } else {
0220:  BRA    0286
....................      printf( "Status : %s\n\r", stat_str[stat] );
0222:  CLRF   09
0224:  MOVFF  07,08
0228:  MOVLW  01
022A:  MOVWF  0B
022C:  CLRF   0A
022E:  RCALL  01E4
0230:  MOVFF  02,03
0234:  MOVFF  01,08
0238:  MOVFF  02,09
023C:  CLRF   0A
023E:  MOVF   0A,W
0240:  RCALL  013C
0242:  INCF   0A,F
0244:  MOVWF  00
0246:  MOVF   00,W
0248:  BTFSS  F9E.4
024A:  BRA    0248
024C:  MOVWF  FAD
024E:  MOVLW  09
0250:  SUBWF  0A,W
0252:  BNZ   023E
0254:  MOVFF  09,0C
0258:  MOVFF  08,0B
025C:  MOVFF  0C,FF7
0260:  MOVF   0B,W
0262:  RCALL  0086
0264:  IORLW  00
0266:  BZ    0276
0268:  INCF   0B,F
026A:  BTFSC  FD8.2
026C:  INCF   0C,F
026E:  BTFSS  F9E.4
0270:  BRA    026E
0272:  MOVWF  FAD
0274:  BRA    025C
0276:  MOVLW  0A
0278:  BTFSS  F9E.4
027A:  BRA    0278
027C:  MOVWF  FAD
027E:  MOVLW  0D
0280:  BTFSS  F9E.4
0282:  BRA    0280
0284:  MOVWF  FAD
.................... }
.................... }
0286:  RETLW  00
.................... 
.................... //void MyPrint(const char * cptr) {
.................... // printf("%s", cptr);
.................... //}
.................... 
.................... void main(void) {
*
02D6:  CLRF   FF8
02D8:  BCF    FD0.7
02DA:  MOVLW  FE
02DC:  MOVWF  00
02DE:  MOVLW  03
02E0:  MOVWF  01
02E2:  MOVLW  02
02E4:  MOVWF  FE9
02E6:  MOVLW  00
02E8:  MOVWF  FEA
02EA:  CLRF   FEE
02EC:  DECFSZ 00,F
02EE:  BRA    02EA
02F0:  DECFSZ 01,F
02F2:  BRA    02EA
02F4:  CLRF   FEA
02F6:  CLRF   FE9
02F8:  BSF    FB8.3
02FA:  MOVLW  AD
02FC:  MOVWF  FAF
02FE:  MOVLW  00
0300:  MOVWF  FB0
0302:  MOVLW  A6
0304:  MOVWF  FAC
0306:  MOVLW  90
0308:  MOVWF  FAB
030A:  MOVF   FC1,W
030C:  ANDLW  C0
030E:  IORLW  0F
0310:  MOVWF  FC1
0312:  MOVLW  07
0314:  MOVWF  FB4
.................... _some_status my_stat = STAT_2;
0316:  MOVLW  06
0318:  MOVWF  06
.................... 
.................... PrintAStatus(my_stat);
031A:  MOVFF  06,07
031E:  RCALL  0202
.................... PrintAStatus(++my_stat);
0320:  INCF   06,F
0322:  MOVFF  06,07
0326:  RCALL  0202
.................... PrintAStatus(STAT_5);
0328:  MOVLW  0C
032A:  MOVWF  07
032C:  RCALL  0202
.................... PrintAStatus(9);
032E:  MOVLW  09
0330:  MOVWF  07
0332:  RCALL  0202
.................... PrintAStatus(14);
0334:  MOVLW  0E
0336:  MOVWF  07
0338:  RCALL  0202
.................... PrintAStatus(104);
033A:  MOVLW  68
033C:  MOVWF  07
033E:  RCALL  0202
.................... 
.................... printf("TEST_OK?\r");
0340:  CLRF   07
0342:  MOVF   07,W
0344:  RCALL  015A
0346:  INCF   07,F
0348:  MOVWF  00
034A:  MOVF   00,W
034C:  BTFSS  F9E.4
034E:  BRA    034C
0350:  MOVWF  FAD
0352:  MOVLW  09
0354:  SUBWF  07,W
0356:  BNZ   0342
.................... printf("TEST_OK?\r");
0358:  CLRF   07
035A:  MOVF   07,W
035C:  RCALL  015A
035E:  INCF   07,F
0360:  MOVWF  00
0362:  MOVF   00,W
0364:  BTFSS  F9E.4
0366:  BRA    0364
0368:  MOVWF  FAD
036A:  MOVLW  09
036C:  SUBWF  07,W
036E:  BNZ   035A
....................     printf("TEST_OK?\r");
0370:  CLRF   07
0372:  MOVF   07,W
0374:  RCALL  015A
0376:  INCF   07,F
0378:  MOVWF  00
037A:  MOVF   00,W
037C:  BTFSS  F9E.4
037E:  BRA    037C
0380:  MOVWF  FAD
0382:  MOVLW  09
0384:  SUBWF  07,W
0386:  BNZ   0372
....................     printf("TEST_OK 1?\r");
0388:  CLRF   07
038A:  MOVF   07,W
038C:  RCALL  0174
038E:  INCF   07,F
0390:  MOVWF  00
0392:  MOVF   00,W
0394:  BTFSS  F9E.4
0396:  BRA    0394
0398:  MOVWF  FAD
039A:  MOVLW  0B
039C:  SUBWF  07,W
039E:  BNZ   038A
....................     printf("TEST_OK 2?\r");
03A0:  CLRF   07
03A2:  MOVF   07,W
03A4:  RCALL  0190
03A6:  INCF   07,F
03A8:  MOVWF  00
03AA:  MOVF   00,W
03AC:  BTFSS  F9E.4
03AE:  BRA    03AC
03B0:  MOVWF  FAD
03B2:  MOVLW  0B
03B4:  SUBWF  07,W
03B6:  BNZ   03A2
....................     printf("TEST_OK 3?\r");
03B8:  CLRF   07
03BA:  MOVF   07,W
03BC:  RCALL  01AC
03BE:  INCF   07,F
03C0:  MOVWF  00
03C2:  MOVF   00,W
03C4:  BTFSS  F9E.4
03C6:  BRA    03C4
03C8:  MOVWF  FAD
03CA:  MOVLW  0B
03CC:  SUBWF  07,W
03CE:  BNZ   03BA
....................     printf("TEST_OK 4?\r");
03D0:  CLRF   07
03D2:  MOVF   07,W
03D4:  RCALL  01C8
03D6:  INCF   07,F
03D8:  MOVWF  00
03DA:  MOVF   00,W
03DC:  BTFSS  F9E.4
03DE:  BRA    03DC
03E0:  MOVWF  FAD
03E2:  MOVLW  0B
03E4:  SUBWF  07,W
03E6:  BNZ   03D2
.................... print(T4);
03E8:  MOVLW  03
03EA:  MOVWF  07
03EC:  RCALL  0288
.................... print(T5);
03EE:  MOVLW  04
03F0:  MOVWF  07
03F2:  RCALL  0288
.................... print(T6);
03F4:  MOVLW  05
03F6:  MOVWF  07
03F8:  RCALL  0288
.................... // MyPrint("Test MyPrint\n\r");
.................... for (;;);
03FA:  BRA    03FA
.................... }
03FC:  SLEEP

Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC