Thread: C advice - How to call values from LUT for sine PWM generator.... Help please!

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    9

    C advice - How to call values from LUT for sine PWM generator.... Help please!

    Hi, just learning C and need a little help writing some code for a dsPIC33 to produce PWM sine o/p from a sine LUT. The main problem I have is how to call the individual values from the LUT.........

    ########################
    Code:
    SineCounter = 0;
    static int Sine[];		// declared so only called once
    
    //##  Sine table => 100off values
    const rom int Sine[] = {512,544,576,608,639,670,700,730,759,786,813,838,862,885,907,926,944,961,975,988,999,1008,1015,1020,1023,1023,
    				        1023,1020,1015,1008,999,988,975,961,944,926,907,885,862,838,813,786,759,730,700,670,639,608,576,544,512,
    				        480,448,416,385,354,324,294,265,238,211,186,162,139,117,98,80,63,49,36,25,16,9,4,1,1,1,
    				        4,9,16,25,36,49,63,80,98,117,139,162,186,211,238,265,294,324,354,385,416,448,480};
    
    void PWM_Main (void)
    
    if (SineCounter == 99)
       {
       SineCounter = 0;
       }
    
       else
          {
          DutyCycleReg = Sine[SineCounter];  //IS THIS CORRECT???????
           }
         SineCounter++;
    }
      
    void "turn on PWM o/ps"(void)v.............
    {
    PWM ctrl.....
    }
    
    Repeat.............
    ######################

    Any help would be most appreciated....
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, something like that should be OK.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Your index value manipulation seems suspect.

    Do you have 99 or 100 values in the look up table? Looks like 100 to me.

    Your code, however, never uses index value 99 (which is the 100th term):
    Code:
    if (SineCounter == 99) {  //<--- probably should be if(SineCounter > 99)
        SineCounter = 0;
        }
    Also, the else is wrong. Why would you NOT do the assignment in the event that you wrapped to the end of the lookup table?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Hi Thanks for the info mike65535 and yes youre correct there are 100terms so should be > 99, as you say.

    "Also, the else is wrong. Why would you NOT do the assignment in the event that you wrapped to the end of the lookup table? "

    Could you explain your last comment a little more, not quite sure what you mean?

    Thanks

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That means that (a) you never use element 99, instead wrapping around one spot too early and (b) you go through your processing loop (or whatever you're doing) with a repeated value, since you use the last element twice -- when you wrap around you deliberately don't change the value of your variable.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I should have said
    Also, the else is wrong. Why would you NOT do the assignment in the event that you wrapped AT the end of the lookup table?
    Your code should look like this:
    Code:
     
       if (SineCounter > 99) {
          SineCounter = 0;
       }
    
       DutyCycleReg = Sine[SineCounter];
       SineCounter++;

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    ok I see now, thanks for the pointers..........

    I'll see if that helps now.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. values of sine
    By jacek in forum C Programming
    Replies: 2
    Last Post: 10-31-2009, 04:17 PM
  2. Sine (Sin) Algorithm Help
    By StaticKyle in forum C Programming
    Replies: 52
    Last Post: 05-13-2008, 08:37 AM
  3. need some advice on displaying values
    By rEtard in forum Windows Programming
    Replies: 7
    Last Post: 06-16-2005, 09:55 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM