[PIC] PIC16f887 how to achieve 60HZ pwm frequency

Status
Not open for further replies.
i have made a little progress i changed the code to this
Code:
unsigned char sin_table[64]={0, 6, 12, 18, 24, 30, 36, 42, 48, 53, 59, 64, 69, 74, 79, 84, 88, 93, 97, 100, 104, 107, 110, 113, 115, 118, 120, 121, 123, 124, 124, 125, 125, 125, 124, 124, 123, 121, 120, 118, 115, 113, 110, 107, 104, 100, 97, 93, 88, 84, 79, 74, 69, 64, 59, 53, 48, 42, 36, 30, 24, 18, 12, 6};   //100% duty cycle

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

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 >> 10;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
                }
}
void Run_PWM(){
PORTC.F2 = 0;
PORTD.F5 = 0;
PORTD.F6 = 0;
PORTD.F7 = 0;
SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
PR2 = 0b01111100; //124
CCP1CON = 0b01001100; // duty lowest bits + PWM mode
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;
}
void Stop_PWM(){
PORTC = 0;
PORTD = 0;
}


void main() {
ANSEL = 0; //Disable ADC
ANSELH = 0x00;
CM1CON0 = 0; //Disable Comparator
CM2CON0 = 0;
TRISD.F0 = 1;
TRISC.F2 = 0;
TRISD.F5 = 0;
TRISD.F6 = 0;
TRISD.F7 = 0;
TRISB.F0 = 0;
PORTC.F2 = 0;
PORTD.F5 = 0;
PORTD.F6 = 0;
PORTD.F7 = 0;
PORTB.F0 = 0;
PORTD.F0 = 0;
SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
PR2 = 0b01111100; //124
CCP1CON = 0b01001100; // duty lowest bits + PWM mode

do{
if (PORTD.F0==0){
   PORTB.F0 = 0;
   Stop_PWM();
        }
else {
   Run_PWM();
   PORTB.F0 = 1;
  }

}while(1);
}

now i have the folllowing issue i am getting

start of program

when i set the PORTB.F0 high

when i set the portb.f0 back to low

I want it to start with all 4 port as low then when PORTB.F0 is set high it should have wave form of picture 3 And when I set PORTB.F0 back to low all 4 pins should go low. I want the pwm to start from scratch whenever I set PORTB.F0 high.
 

the changes i made are wrong. i have chaged them again this time i am calling the enhanced pwm shutdown to stop the pwm and start it again here is the code.
Code:
unsigned char sin_table[64]={0, 6, 12, 18, 24, 30, 36, 42, 48, 53, 59, 64, 69, 74, 79, 84, 88, 93, 97, 100, 104, 107, 110, 113, 115, 118, 120, 121, 123, 124, 124, 125, 125, 125, 124, 124, 123, 121, 120, 118, 115, 113, 110, 107, 104, 100, 97, 93, 88, 84, 79, 74, 69, 64, 59, 53, 48, 42, 36, 30, 24, 18, 12, 6};   //100% duty cycle

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

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 >> 10;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
                }
}
void Run_PWM(){
PORTC.F2 = 0;
 PORTD.F5 = 0;
 PORTD.F6 = 0;
 PORTD.F7 = 0;
 SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
//ECCPAS = 0b00000101; //autoshutodwm disabled
PR2 = 0b01111100; //124
CCP1CON = 0b01001100; // duty lowest bits + PWM mode
TMR2IF_bit = 0;
T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
while (TMR2IF_bit == 0);
TMR2IF_bit = 0;     // Timer2 interrupt flag
TRISC = 0;
TMR2IE_bit = 1;      //interrupt when TMR2 matches PR2
GIE_bit = 1;      // Global interrupt enabled
PEIE_bit = 1;     // peripheral interrupt enabled
ECCPAS =0b11000000;       //autoshutodwm on INT pin high
PWM1CON    = PWM1CON +   0b10000000  ;
}

void main() {
ANSEL = 0; //Disable ADC 
ANSELH = 0x00;
CM1CON0 = 0; //Disable Comparator
CM2CON0 = 0;
TRISC.F2 = 0;
TRISD.F5 = 0;
TRISD.F6 = 0;
TRISD.F7 = 0;
TRISB.F0 = 1;
 PORTC.F2 = 0;
 PORTD.F5 = 0;
 PORTD.F6 = 0;
 PORTD.F7 = 0;
  PORTB.F7 = 0;
  Run_PWM();

while(1);
 }
i have a new problem the pwm runs at the start of the program and starts responding to the portb.fo/int interrupt after the second time i set the portb.fo high. i want it to not run at the beginning and run after the first time i set the pin portb.fo high. please get back to me as soon as possible.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…