CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problems with duplicate code

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pasini



Joined: 12 Dec 2005
Posts: 50
Location: Curitiba - Brazil

View user's profile Send private message Send e-mail

Problems with duplicate code
PostPosted: Mon Jan 30, 2006 6:55 am     Reply with quote

I´m using a PIC16F689 for a small application, and I need use the I2C to read some devices , using the I2C ready func by firmware.
I need optimize best I can my code , so sometimes I want to see if my code it´s fine enough
But now I notice some duplicate code in the assembly list like this:

My MPLAB version is 7.22 and the CCS version is 3.242

.................... i2c_stop(); // [STOP]
*
08E1: BSF 03.5
08E2: BCF 06.4
08E3: NOP
08E4: BSF 06.6
08E5: BCF 03.5
08E6: BTFSC 06.6
08E7: GOTO 0EA
08E8: BSF 03.5
08E9: GOTO 0E5
08EA: NOP
08EB: NOP
08EC: NOP
08ED: NOP
08EE: BSF 03.5
08EF: BSF 06.4
08F0: NOP
*
091C: BSF 03.5
091D: BCF 06.4
091E: NOP
091F: BSF 06.6
0920: BCF 03.5
0921: BTFSC 06.6
0922: GOTO 125
0923: BSF 03.5
0924: GOTO 120
0925: NOP
0926: NOP
0927: NOP
0928: NOP
0929: BSF 03.5
092A: BSF 06.4
092B: NOP
*
0960: BSF 03.5
0961: BCF 06.4
0962: NOP
0963: BSF 06.6
0964: BCF 03.5
0965: BTFSC 06.6
0966: GOTO 169
0967: BSF 03.5
0968: GOTO 164
0969: NOP
096A: NOP
096B: NOP
096C: NOP
096D: BSF 03.5
096E: BSF 06.4
096F: NOP
*
099C: BSF 03.5
099D: BCF 06.4
099E: NOP
099F: BSF 06.6
09A0: BCF 03.5
09A1: BTFSC 06.6
09A2: GOTO 1A5
09A3: BSF 03.5
09A4: GOTO 1A0
09A5: NOP
09A6: NOP
09A7: NOP
09A8: NOP
09A9: BSF 03.5
09AA: BSF 06.4
09AB: NOP
*
09EE: BSF 03.5
09EF: BCF 06.4
09F0: NOP
09F1: BSF 06.6
09F2: BCF 03.5
09F3: BTFSC 06.6
09F4: GOTO 1F7
09F5: BSF 03.5
09F6: GOTO 1F2
09F7: NOP
09F8: NOP
09F9: NOP
09FA: NOP
09FB: BSF 03.5
09FC: BSF 06.4
09FD: NOP
*
0A2A: BSF 03.5
0A2B: BCF 06.4
0A2C: NOP
0A2D: BSF 06.6
0A2E: BCF 03.5
0A2F: BTFSC 06.6
0A30: GOTO 233
0A31: BSF 03.5
0A32: GOTO 22E
0A33: NOP
0A34: NOP
0A35: NOP
0A36: NOP
0A37: BSF 03.5
0A38: BSF 06.4
0A39: NOP
*
0A66: BSF 03.5
0A67: BCF 06.4
0A68: NOP
0A69: BSF 06.6
0A6A: BCF 03.5
0A6B: BTFSC 06.6
0A6C: GOTO 26F
0A6D: BSF 03.5
0A6E: GOTO 26A
0A6F: NOP
0A70: NOP
0A71: NOP
0A72: NOP
0A73: BSF 03.5
0A74: BSF 06.4
0A75: NOP
*
0AB3: BSF 03.5
0AB4: BCF 06.4
0AB5: NOP
0AB6: BSF 06.6
0AB7: BCF 03.5
0AB8: BTFSC 06.6
0AB9: GOTO 2BC
0ABA: BSF 03.5
0ABB: GOTO 2B7
0ABC: NOP
0ABD: NOP
0ABE: NOP
0ABF: NOP
0AC0: BSF 03.5
0AC1: BSF 06.4
0AC2: NOP
*
0CF2: BSF 03.5
0CF3: BCF 06.4
0CF4: NOP
0CF5: BSF 06.6
0CF6: BCF 03.5
0CF7: BTFSC 06.6
0CF8: GOTO 4FB
0CF9: BSF 03.5
0CFA: GOTO 4F6
0CFB: NOP
0CFC: NOP
0CFD: NOP
0CFE: NOP
0CFF: BSF 03.5
0D00: BSF 06.4
0D01: NOP
*
0E73: BSF 03.5
0E74: BCF 06.4
0E75: NOP
0E76: BSF 06.6
0E77: BCF 03.5
0E78: BTFSC 06.6
0E79: GOTO 67C
0E7A: BSF 03.5
0E7B: GOTO 677
0E7C: NOP
0E7D: NOP
0E7E: NOP
0E7F: NOP
0E80: BSF 03.5
0E81: BSF 06.4
0E82: NOP
*
0EC0: BSF 03.5
0EC1: BCF 06.4
0EC2: NOP
0EC3: BSF 06.6
0EC4: BCF 03.5
0EC5: BTFSC 06.6
0EC6: GOTO 6C9
0EC7: BSF 03.5
0EC8: GOTO 6C4
0EC9: NOP
0ECA: NOP
0ECB: NOP
0ECC: NOP
0ECD: BSF 03.5
0ECE: BSF 06.4
0ECF: NOP


This is very strange for me, because I´m using a large amount of memory for the same code. Why this is happening?
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 30, 2006 10:38 am     Reply with quote

..

Last edited by treitmey on Mon Jan 30, 2006 10:40 am; edited 1 time in total
Ttelmah
Guest







PostPosted: Mon Jan 30, 2006 10:39 am     Reply with quote

Generally, CCS puts any small routines like this 'inline', with the code duplicated for each occurence. Hence the multiple entries. You can switch to having just one copy, by generating a 'wrapper' function, using #seperate, and calling this. The saving will not be large. You will lose a RAM address for the variable (assuming you do need the ACK/NACK ability), and the overhead of calling the function (setting up the register, and the actual call), will be close to the size of the inline function. I'd expect that with only perhaps four occurences, the inline version would take no more space. With your eleven cases, seperate will be smaller, but I have to ask why you have so many calls to the function?. CCS's approach is based on the assumption, that you will probably only need a couple of stop commands, inside your own handler routines, and for this configuration, the inline approach is smaller.

Best Wishes
pasini



Joined: 12 Dec 2005
Posts: 50
Location: Curitiba - Brazil

View user's profile Send private message Send e-mail

Duplicate code
PostPosted: Mon Jan 30, 2006 11:59 am     Reply with quote

I've tested the #separate function, and save lots of space in my firmware .
Really, in the entire firmware I call i2c_stop() four times only. But I call another function that contains i2c_stop() lots of times

If you want I can send to you the entire project.


#separate
void rtc_write(void)
{
write_byte(RTC_ADD, 0x00, 0x00); // milisegundos --> sempre zero !!! não mudar
write_byte(RTC_ADD, 0x01, 0x00); // segundos --> sempre zero !!! não mudar
write_byte(RTC_ADD, 0x02, 0x00); // minutos
write_byte(RTC_ADD, 0x03, 0x12); // horas
write_byte(RTC_ADD, 0x04, 0x02); // dia da semana
write_byte(RTC_ADD, 0x05, 0x19); // dia do mês
write_byte(RTC_ADD, 0x06, 0x01); // mês
write_byte(RTC_ADD, 0x07, 0x06); // ano
write_byte(RTC_ADD, 0x13, 0xF0); // square wave output - 1Hz
write_byte(RTC_ADD, 0x0A, 0x40); // square wave output enabled
write_byte(RTC_ADD, 0x0C, 0x00); // HT must be set 0 to begin the update
}

#separate
void write_byte(int8 addr, int8 pointer, int8 data)
{
i2c_start(); // [START]
i2c_write(addr|0x00); // [DEVICE ADDRESS]+[WRITE]
i2c_write(pointer); // [REGISTER POINTER]
i2c_write(data); // [DATA]
i2c_stop(); delay_us(30);

}// End Function
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group