void interrupt(){
IFS3bits.PWM1IF = 0;
if(P1OVDCONbits.POVD1H){
PWMCON1bits.PEN1H = 0;
PWMCON1bits.PEN1L = 1;
}
else{
PWMCON1bits.PEN1H = 1;
PWMCON1bits.PEN1l = 0;
}
}
void main(void){
PORTA = 1;
TRISA = 0x00;
TRISB = 0x0000; // Configure RB0 as a digital output for an indicator LED
PWMCON1bits.PMOD1 = 1; // PMOD1 = 1; // Enable PWM channel 1 in independent mode
PWMCON1bits.PEN1H = 1; // PEN1H = 1; // Enable PWM1H pin
PWMCON1bits.PEN1L = 1; // PEN1L = 1; // Enable PWM1L pin
P1OVDCONbits.POUT1L = 0; // POUT1L = 0; // When PWM1L is overriden, set it low
P1OVDCONbits.POUT1H = 0; // POUT1H = 0; // When PWM1H is overriden, set it low
P1OVDCONbits.POVD1L = 1; // POVD1L = 1; // Initially, enable override on PWM1L
P1OVDCONbits.POVD1H = 1; // POVD1H = 0; // Initially, enable override on PWM1H
PWM1CON2bits.OSYNC = 1; // OSYNC = 1; // Synchronise PWM override changes with PTMR reset
IEC3bits.PWM1IE = 1;
P1TCONbits.PTCKPS = 3; // PTCKPS = 0; // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
P1TPER = 1569; // Set PWM frequency to 50 Hz
PDC1 = 1050; // Set duty cycle to 10%
PTCONbits.PTEN = 1; // PTEN = 1; // Enable PWM time base
while(1){
}
}
void interrupt(){
IFS3bits.PWM1IF = 0;
if(P1OVDCONbits.POVD1H){
PWMCON1bits.PEN1H = 0;
PWMCON1bits.PEN1L = 1;
}
else{
PWMCON1bits.PEN1H = 1;
PWMCON1bits.PEN1l = 0;
}
}
void main(void){
PORTA = 1;
TRISA = 0x00;
TRISB = 0x0000; // Configure RB0 as a digital output for an indicator LED
PWMCON1bits.PMOD1 = 0; // PMOD1 = 1; // Enable PWM channel 1 in independent mode
PWMCON1bits.PEN1H = 1; // PEN1H = 1; // Enable PWM1H pin
PWMCON1bits.PEN1L = 1; // PEN1L = 1; // Enable PWM1L pin
P1OVDCONbits.POUT1L = 0; // POUT1L = 0; // When PWM1L is overriden, set it low
P1OVDCONbits.POUT1H = 0; // POUT1H = 0; // When PWM1H is overriden, set it low
P1OVDCONbits.POVD1L = 1; // POVD1L = 1; // Initially, enable override on PWM1L
P1OVDCONbits.POVD1H = 1; // POVD1H = 0; // Initially, enable override on PWM1H
PWM1CON2bits.OSYNC = 1; // OSYNC = 1; // Synchronise PWM override changes with PTMR reset
IEC3bits.PWM1IE = 1;
P1TCONbits.PTCKPS = 3; // PTCKPS = 0; // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
P1TPER = 1569; // Set PWM frequency to 50 Hz
PDC1 = 1050; // Set duty cycle to 10%
PTCONbits.PTEN = 1; // PTEN = 1; // Enable PWM time base
while(1){
}
}
unsigned int current_duty, old_duty, current_duty1, old_duty1;
unsigned int pwm_period1, pwm_period2;
void InitMain() {
ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
// configure PORTB pins as input
PORTB = 0; // set PORTD to 0
TRISB = 0; // designate PORTD pins as output
}
void main() {
InitMain();
current_duty = 16; // initial value for current_duty
current_duty1 = 16; // initial value for current_duty1
pwm_period1 = PWM_Init(5000 , 1, 1, 2);
pwm_period2 = PWM_Init(10000, 2, 1, 3);
PWM_Start(1);
PWM_Start(2);
PWM_Set_Duty(current_duty, 1); // Set current duty for PWM1
PWM_Set_Duty(current_duty1, 2); // Set current duty for PWM2
while (1) { // endless loop
if (RB0_bit) { // button on RB0 pressed
Delay_ms(20);
current_duty++; // increment current_duty
if (current_duty > pwm_period1) { // if we increase current_duty greater then possible pwm_period1 value
current_duty = 0; // reset current_duty value to zero
}
PWM_Set_Duty(current_duty, 1); // set newly acquired duty ratio
}
if (RB1_bit) { // button on RB1 pressed
Delay_ms(20);
current_duty--; // decrement current_duty
if (current_duty > pwm_period1) { // if we decrease current_duty greater then possible pwm_period1 value (overflow)
current_duty = pwm_period1; // set current_duty to max possible value
}
PWM_Set_Duty(current_duty, 1); // set newly acquired duty ratio
}
if (RB2_bit) { // button on RB2 pressed
Delay_ms(20);
current_duty1++; // increment current_duty1
if (current_duty1 > pwm_period2) { // if we increase current_duty1 greater then possible pwm_period2 value
current_duty1 = 0; // reset current_duty1 value to zero
}
PWM_Set_Duty(current_duty1, 2); // set newly acquired duty ratio
}
if (RB3_bit) { // button on RB3 pressed
Delay_ms(20);
current_duty1--; // decrement current_duty1
if (current_duty1 > pwm_period2) { // if we decrease current_duty1 greater then possible pwm_period1 value (overflow)
current_duty1 = pwm_period2; // set current_duty to max possible value
}
PWM_Set_Duty(current_duty1, 2);
}
Delay_ms(5); // slow down change pace a little
}
}
unsigned int current_duty, old_duty, current_duty1, old_duty1;
unsigned int pwm_period1, pwm_period2;
void InitMain() {
ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
PORTB = 0; // set PORTB to 0
TRISB = 0; // designate PORTB pins as output
PORTC = 0; // set PORTC to 0
TRISC = 0xFFFF; // designate PORTC pins as input
}
void main() {
InitMain();
current_duty = 16; // initial value for current_duty
current_duty1 = 16; // initial value for current_duty1
pwm_period1 = PWM_Init(5000 , 1, 1, 2);
pwm_period2 = PWM_Init(10000, 2, 1, 3);
PWM_Start(1);
PWM_Start(2);
PWM_Set_Duty(current_duty, 1); // Set current duty for PWM1
PWM_Set_Duty(current_duty1, 2); // Set current duty for PWM2
while (1) { // endless loop
if (RC1_bit) { // button on RB0 pressed
Delay_ms(20);
current_duty++; // increment current_duty
if (current_duty > pwm_period1) { // if we increase current_duty greater then possible pwm_period1 value
current_duty = 0; // reset current_duty value to zero
}
PWM_Set_Duty(current_duty, 1); // set newly acquired duty ratio
}
if (RC2_bit) { // button on RB1 pressed
Delay_ms(20);
current_duty--; // decrement current_duty
if (current_duty > pwm_period1) { // if we decrease current_duty greater then possible pwm_period1 value (overflow)
current_duty = pwm_period1; // set current_duty to max possible value
}
PWM_Set_Duty(current_duty, 1); // set newly acquired duty ratio
}
if (RC3_bit) { // button on RB2 pressed
Delay_ms(20);
current_duty1++; // increment current_duty1
if (current_duty1 > pwm_period2) { // if we increase current_duty1 greater then possible pwm_period2 value
current_duty1 = 0; // reset current_duty1 value to zero
}
PWM_Set_Duty(current_duty1, 2); // set newly acquired duty ratio
}
if (RC4_bit) { // button on RB3 pressed
Delay_ms(20);
current_duty1--; // decrement current_duty1
if (current_duty1 > pwm_period2) { // if we decrease current_duty1 greater then possible pwm_period1 value (overflow)
current_duty1 = pwm_period2; // set current_duty to max possible value
}
PWM_Set_Duty(current_duty1, 2);
}
Delay_ms(5); // slow down change pace a little
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?