Menu

pwm code translation

Help
Aaron
2017-03-25
2017-03-25
  • Aaron

    Aaron - 2017-03-25
    pls i need help in translating this code from C into gcb so i can complete a project, the chip involved is PIC16F684....THE CODE is for a pure sine wave inverter..this is the code..
    

    unsigned char sin_table[32]={0,25,49,73,96,118,137,
    159,177,193,208,220,231,239,245,249,250,249,245,
    239,231,220,208,193,177,159,137,118,96,73,49,25};

    unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
    unsigned int TBL_temp;
    unsigned char DUTY_CYCLE;

    void interrupt(){
    if (TMR2IF_bit == 1){
    TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
    if (TBL_POINTER_NEW < TBL_POINTER_OLD){
    CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
    }
    TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
    DUTY_CYCLE = TBL_POINTER_SHIFT;
    CCPR1L = sin_table[DUTY_CYCLE];
    TBL_POINTER_OLD = TBL_POINTER_NEW;
    TMR2IF_bit = 0;
    }
    }

    void main() {
    SET_FREQ = 410;
    TBL_POINTER_SHIFT = 0;
    TBL_POINTER_NEW = 0;
    TBL_POINTER_OLD = 0;
    DUTY_CYCLE = 0;
    ANSEL = 0; //Disable ADC
    CMCON0 = 7; //Disable Comparator
    PR2 = 249;
    TRISC = 0x3F;
    CCP1CON = 0x4C;
    TMR2IF_bit = 0;
    T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
    while (TMR2IF_bit == 0);
    TMR2IF_bit = 0;
    TRISC = 0;
    TMR2IE_bit = 1;
    GIE_bit = 1;
    PEIE_bit = 1;

     while(1);
    

    }

     
  • Anobium

    Anobium - 2017-03-25

    Please send vasts amount of money to my eBay account. I cannot test but this should work.

    You do not not all the complexity of the source with Great Cow BASIC.

    ; ----- Configuration
      #chip 16F684,8
    
        Dim TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ as Integer
    
    
        SET_FREQ = 410
        TBL_POINTER_SHIFT = 0
        TBL_POINTER_NEW = 0
        TBL_POINTER_OLD = 0
        DUTY_CYCLE = 0
    
    
        Dir portc.2 out
        HPWM 1, SET_FREQ, DUTY_CYCLE
        On Interrupt Timer2Match call HandleSineWave
    
    
        Do
    
        Loop
    
        Sub HandleSineWave
    
            TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ
            if (TBL_POINTER_NEW < TBL_POINTER_OLD) then
                P1M1 = !P1M1 ' Reverse direction of full-bridge
            End if
    
            TBL_POINTER_SHIFT =  FnLSR(TBL_POINTER_NEW, 11)
            DUTY_CYCLE = TBL_POINTER_SHIFT
    
            readtable sin_table, DUTY_CYCLE,  CCPR1L
    
            TBL_POINTER_OLD = TBL_POINTER_NEW
            TMR2IF = 0
    
        End Sub
    
        table sin_table
        0,25,49,73,96,118,137,
        159,177,193,208,220,231,239,245,249,250,249,245,
        239,231,220,208,193,177,159,137,118,96,73,49,25
        end table
    
     

    Last edit: Anobium 2017-03-25
  • Anobium

    Anobium - 2017-03-25

    To my amazement - I just hooked up an 18f with the same code and I get.... PWM and the frequency is changing.

     

    Last edit: Anobium 2017-03-25
  • Aaron

    Aaron - 2017-03-25

    Anobium you are the boss, with this i can now finish my project, thank alot

     
  • Aaron

    Aaron - 2017-03-26

    pls while making use of the interupt i do not understand the timer2match

     
  • Anobium

    Anobium - 2017-03-26

    From your source the Interrupt was checking TMR2IF ad the source was setting TMR2IE_bit. These relate to Timer2Match,

    So, we call Timer2Match enabling TMR2IE and we need to clear,TMR2IF

    I may be incorrect but this still looks ok today.

     
  • William Roth

    William Roth - 2017-03-27

    HPWM uses uses timer 2 as a clock source. Timer2 has a timer register (TMR2) and a period register (PR2) . TMR2 increments until its value "matches" the value in the PR2 register. At that time the TMR2IF flag bit is set and, if enabled, an interrupt is triggered and TMR2 reset to zero, The time between interrupts is the "period". To change the period, you can change the value of the PR2 Register.

    The default value of the PR2 register is 255, So after 255 ticks TMR2 "matches" PR2 and "overlows" to zero and starts over. If the PR2 register is loaded with a value of 127, then TMR2 will increment unitil it reaches a value of 127 (matches PR2) and then reset to zero. This will divide the period by 2, thus doubling the frequency,

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.