devo generare un PWM con frequenza a 1Mhz...
utilizzo un micro ATmega32, con il codice seguente sono riuscito a generare un pwm con duty cycle 50%
Non ho margine per variare il duty cycle, come posso risolvere?
- Code: Select all
$regfile = "m32def.dat"
$crystal = 16000000
$baud = 115200
Config Portd.2 = Output 'BUZZER
Config Portd.5 = Output
Buzzer Alias Portd.2
Declare Sub Bip
'2. Set Timer1 registers to Fast-PWM Mode with ICR1 as TOP Value (Mode 14),Compare A = Clear down:
Tccr1a = &B10000010
'Bit 7:6 – COM1A1:0: Compare Output Mode for channel A set to 10
' = Clear OC1A on Compare Match, set OC1A at TOP
'Bit 5:4 – COM1B1:0: Compare Output Mode for channel B set to 00
' = Normal port operation, OC1B disconnected.
'Bit 3 – FOC1A: Force Output Compare for channel A set to 0 - not used in PWM
'Bit 2 – FOC1B: Force Output Compare for channel B set to 0 - not used in PWM
'Bit 1:0 – WGM11:0: Waveform Generation Mode set to ..10
' = lower bits of Mode 14: fast PWM; TOP = ICR1
Tccr1b = &B00011010
'Bit 7 – ICNC1: Input Capture Noise Canceler set to 0 - not used in PWM
'Bit 6 – ICES1: Input Capture Edge Select set to 0 - not used in PWM
'Bit 5 – Reserved Bit set to 0
'Bit 4:3 – WGM13:2: Waveform Generation Mode set to 11..
' = higher bits of Mode 14: fast PWM; TOP = ICR1
'Bit 2:0 – CS12:0: Clock Select set to
' = higher bits of Mode 14: fast PWM; TOP = ICR1
'Bit 2:0 – CS12:0: Clock Select set to 010 = clkI/O / 8 (From prescaler)
'Icr1 = (fclk_I/O / (fOCnxPWM * N)) - 1
Icr1 = 1 '(16000000 / (8 * 1000000)) - 1
Pwm1a = 0 '50%
Waitms 1
Bip
Do
Loop
Sub Bip
Buzzer = 0
Waitms 20
Buzzer = 1
End Sub