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,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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;
}
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.
Last edit: 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
Anobium you are the boss, with this i can now finish my project, thank alot
pls while making use of the interupt i do not understand the timer2match
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.
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,