Datasheet

Library Example
The example changes PWM duty ratio continually by pressing buttons on PORC (0-
3). If LED is connected to PORTB.1 or PORTB.2 ,you can observe the gradual
change of emitted light. This example is written for ATmega168. This AVR MCU has
only Timer/Counter1 split over two channels A and B. In this example we are chang-
ing the duty ratio on both of these channels.
char current_duty;
char current_duty1;
void main(){
DDC0_bit = 0; // Set PORTC pin 0 as input
DDC1_bit = 0; // Set PORTC pin 1 as input
DDC2_bit = 0; // Set PORTC pin 2 as input
DDC3_bit = 0; // Set PORTC pin 3 as input
current_duty = 255; // initial value for current_duty
current_duty1 = 255; // initial value for current_duty
DDRB.B1 = 1; // Set PORTB pin 1 as output pin
for the PWM (according to datasheet)
DDRB.B2 = 1; // Set PORTB pin 2 as output pin
for the PWM (according to datasheet)
PWM16bit_Init(_PWM16_FAST_MODE_9BIT, _PWM16_PRESCALER_16bit_1,
_PWM16_INVERTED, 255, 1);
do {
if (PINC.B0) { // Detect if PORTC pin 0 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty++; // Increment duty ratio
PWM16bit_Change_Duty(current_duty, _TIMER1_CH_A);
// Set incremented duty
}
else
if (PINC.B1) { // Detect if PORTC pin 1 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty--; // Decrement duty ratio
PWM16bit_Change_Duty(current_duty, _TIMER1_CH_A);
// Set decremented duty ratio
}
else
if (PINC.B2) { // Detect if PORTC pin 2 is pressed
Delay_ms(40); // Small delay to avoid deboucing effect
current_duty1++; // Increment duty ratio
PWM16bit_Change_Duty(current_duty1, _TIMER1_CH_B);
// Set incremented duty
}
388
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroC PRO for AVR
CHAPTER 6