Mikroelektronika MIKROE-350 Fiche De Données

Page de 526
Library Example
The example changes PWM duty ratio on pin PB3 continually. If LED is connected
to PB3, you can observe the gradual change of emitted light.
program PWM_Test
dim current_duty as byte
current_duty1 
as byte
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  = 127             ' initial value for current_duty
current_duty1 = 127             ' 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 PWM1 (according to datasheet)
PWM_Init(_PWM_PHASE_CORRECT_MODE, _PWM_PRESCALER_8,
_PWM_NON_INVERTED, 127)
PWM1_Init(_PWM1_PHASE_CORRECT_MODE, _PWM1_PRESCALER_8,
_PWM1_NON_INVERTED, 127)
while TRUE                          ' Endless loop
if (PINB0_bit <> 0) then ' Detect if PORTB pin 0 is pressed
Delay_ms(40)      ' Small delay to avoid deboucing effect
Inc(current_duty)               ' Increment duty ratio
PWM_Set_Duty(current_duty)      ' Set incremented duty
end if
if (PINB1_bit <> 0) then
' Detect if PORTB pin 1 is pressed
Delay_ms(40)     ' Small delay to avoid deboucing effect
Dec(current_duty)               ' Decrement duty ratio
PWM_Set_Duty(current_duty)      ' Set decremented duty ratio
end if
if (PINC0_bit <> 0) then
' Detect if PORTC pin 0 is pressed
Delay_ms(40)       ' Small delay to avoid deboucing effect
Inc(current_duty1)              ' Increment duty ratio
PWM1_Set_Duty(current_duty1)    ' Set incremented duty
end if
315
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroBasic PRO for AVR
CHAPTER 6