PIC 16f887 user controlled pwm

Here you can find latest news on mikroElektronika products.
Post Reply
Author
Message
Tamim neak
Posts: 2
Joined: 23 Feb 2015 22:40

PIC 16f887 user controlled pwm

#1 Post by Tamim neak » 23 Feb 2015 22:48

Hi all i have the folllowing code for SPWM. i want to make it run only if pORTB.F0=1;

can anyone help please.

Code: Select all

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};
 
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 >> 10;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
}
}
 
void main() {
SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
ANSEL = 0; //Disable ADC
CM1CON0 = 0; //Disable Comparator
CM2CON0 = 0;
TRISC.F2 = 0;
TRISD.F5 = 0;
 TRISD.F6 = 0;
 TRISD.F7 = 0;
 TRISC.F0 = 1;
 TRISC.F1 = 1;
 PORTC.F2 = 0;
 PORTD.F5 = 0;
 PORTD.F6 = 0;
 PORTD.F7 = 0;
 
PR2 = 0b01111100;
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;
 
while(1);
}

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: PIC 16f887 user controlled pwm

#2 Post by filip » 27 Feb 2015 15:22

Hi,

Basically, you need to do something like this :
if (PORTB.F0 == 1) {
// your code
}
Make sure you have set this pin to digital input.

Regards,
Filip.

Post Reply

Return to “Product Announcements”