Reduce code size by almost 95% - No ASM required

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

Reduce code size by almost 95% - No ASM required

#1 Post by rmteo » 22 Aug 2008 23:05

I have been working on an application that requires reading an analog input and outputting a PWM with a duty cycle of 0-100% depending on the input. I tested the following code based on the mB PWM library:

Code: Select all

  pwm1_period = Pwm_Init(3900, 1, 1, 2)
  Pwm_Start(1)  
l1:
  v = adc_read(1)
  Pwm_Set_Duty(v << 2, 1)
  delay_ms(100)
  goto l1
Excluding the code for the A/D and delays, the amount of ROM used was 502 for the above code. Since working with PWM's involves basically setting registers, I decided to look closely at the datasheet (PIC24FJ32GA002) and family reference manual to see if there was a more efficient method to do it. By setting the registers manually, this is the new code:

Code: Select all

' TIMER2 PERIOD FOR 3.9kHZ, PRESCALE 1:1-DEFAULT
  PR2 = $FFF          ' Timer2 Period for 3.9KHz
  OC1RS = 0           ' Initial Duty Cycle
  OC1R = 0            ' Set to 0
  T2CON.15 = 1        ' Start Timer 2
  OC1CON = %110       ' Start PWM
  
l1:
  v = adc_read(1)
  OC1RS = v << 2           ' Set Duty Cycle
  delay_ms(100)
  goto l1
Again, excluding the code for the A/D and delays, the amount of ROM required is now just 28. Almost a 95% reduction. BTW, both versions of code were tested on hardware and output verified on a scope.
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

GregoryArndt
Posts: 6
Joined: 31 Jul 2008 03:17
Location: California, USA
Contact:

#2 Post by GregoryArndt » 28 Aug 2008 02:25

Thanks for the tip. I applied this to my MikroC code and save over 500 bytes of rom for 4 pwm outs. Excellent idea, thank you!

Gregory

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#3 Post by rmteo » 28 Aug 2008 06:00

Good to know that it worked for you, Gregory.

Please note that this post is NOT meant to imply anything negative about the mE compilers. They are great products that allow me to quickly develop an application. When the application is working correctly, I like to modify the code in critical areas to improve performance.

An example is the PWM routine. mikroBASIC makes it really easy to work with the CCP/PWM module. Because it does all the math to calculate the period and duty cycle, the code consumes a lot of ROM. If you already know what frequency you want, then by doing the math externally, you can save a lot ROM and instruction cycles - especially if you are constantly changing the frequency and/or duty cycle. BTW, here is simple method to compute the value to load into the PR register for whatever frequency you want:

PRx VALUE = (1,000,000/FREQ*MIPS)-1

Example: To get 5kHZ using PIC24FJ at 16MIPS (32MHz clock)
(1,000,000/5,000*16)-1 = 3199 (0xC7F)
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”