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

what should one know about multiplications

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



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

what should one know about multiplications
PostPosted: Thu Nov 17, 2011 12:11 pm     Reply with quote

I have this vector, which represent a sine wave 0-255.
Code:

long int SINE_WAVE[200] = {
128,132,136,139,143,147,150,154,158,161,165,169,172,176,179,
182,186,189,192,195,199,202,204,207,210,213,215,218,220,223,
225,227,229,231,233,235,237,238,240,241,242,243,244,245,246,
247,247,247,248,248,248,248,248,247,247,247,246,245,244,243,
242,241,240,238,237,235,233,231,229,227,225,223,220,218,215,
213,210,207,204,202,199,195,192,189,186,182,179,176,172,169,
165,161,158,154,150,147,143,139,136,132,128,124,120,117,113,
109,106,102,98,95,91,87,84,80,77,74,70,67,64,61,57,54,52,49,
46,43,41,38,36,33,31,29,27,25,23,21,19,18,16,15,14,13,12,11,
10,9,9,9,8,8,8,8,8,9,9,9,10,11,12,13,14,15,16,18,19,21,23,
25,27,29,31,33,36,38,41,43,46,49,52,54,57,61,64,67,70,74,77,
80,84,87,91,95,98,102,106,109,113,117,120,124};


I want to use it as reference to generate a PWM sinewave. The interrupt is at around 200 Hz for now, and the PWM frequency 1 kHz.
Code:
#int_rtcc
void isr() {

   set_rtcc(6);         // frequency of interrrupt = (clock/(4*divisor))/(256-reload)
                          //                 2029 hz = (20000000/(4*16))/(256-102)
   if(++sine_index==200) {
      sine_index=0;
   }
   
   dc_val=(SINE_WAVE[sine_index])*64;
   //dc_val=10*DC*((period>>8)+1); //Ton
   sine=(SINE_WAVE[sine_index]);
   printf(lcd_putc, "\fdc_val=%LU", sine);
   printf(lcd_putc, "\ni=%u",sine_index);
   printf(lcd_putc, "\ndc_val=%Lu",dc_val);
   
   set_power_pwm0_duty(dc_val); //16383
   //set_power_pwm0_duty(DC * ((period>>8)+1));   //max ADC value is 1023
                                                      //max duty cycle is (period+1)*4-1
                                                      //basically the same as max_adc * period/256
   
}


The problem using int16 for the vector, I consumes A LOT of RAM. I think that the problem is the multiplication *64.
If I leave the vector in int8, the program doesn't work.
How can I do?

I am suppose to generate a three phase 20 Hz sine wave with a 18F4431, for a university assignment.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 17, 2011 12:55 pm     Reply with quote

Instead of making the sine table be int16, just cast the table value to
int16 in the calculation:
Quote:

dc_val=((int16)SINE_WAVE[sine_index])*64;
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 17, 2011 1:19 pm     Reply with quote

also get rid of those printf(....) !! in the ISR.

ISRs are supposed to be short and sweet !!
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

PostPosted: Fri Nov 18, 2011 11:04 am     Reply with quote

Oh, yes, it works now. I didn't know the option to "cast".
printf removed from the isr ;)
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