PWM interfacing Code
char current_duty;
char current_duty1;
void main(){
DDB0_bit = 0; // Set PORTB pin 0 as input
DDB1_bit = 0; // Set PORTB pin 1 as input
DDC0_bit = 0; // Set PORTC pin 0 as input
DDC1_bit = 0; // Set PORTC pin 1 as input
current_duty = 32; // initial value for current_duty
current_duty1 = 32; // initial value for current_duty
DDB3_bit = 1; // Set PORTB pin 3 as output pin for the PWM (according to datasheet)
DDD7_bit = 1; // Set PORTD pin 7 as output pin for the PWM (according to datasheet)
PWM1_Init(_PWM1_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, current_duty);
PWM2_Init(_PWM2_FAST_MODE, _PWM2_PRESCALER_8, _PWM2_NON_INVERTED, current_duty1);
do {
if (PINB0_bit) { // Detect if PORTB pin 0 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty++; // Increment duty ratio
PWM1_Set_Duty(current_duty); // Set incremented duty
}
else
if (PINB1_bit) { // Detect if PORTB pin 1 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty--; // Decrement duty ratio
PWM1_Set_Duty(current_duty); // Set decremented duty ratio
}
else
if (PINC0_bit) { // Detect if PORTC pin 0 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty1++; // Increment duty ratio
PWM2_Set_Duty(current_duty1); // Set incremented duty
}
else
if (PINC1_bit) { // Detect if PORTC pin 1 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty1--; // Decrement duty ratio
PWM2_Set_Duty(current_duty1); // Set decremented duty ratio
}
} while(1); // Endless loop
}